Thread: Executing system commands cross platform

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Executing system commands cross platform

    Hi,

    I am writing a CppUnit test framework, and I need to execute system commands. Each of the commands will take about 5 minutes to complete, and I need to wait for the results. I was going to use a fork(), and then execute the command and wait for this process to complete. The problem is that this will only work on linux, and I need to be able to run the tests under windows. It would be helpful if I didnt have to change the tests for windows and linux. Does anyone know a way to execute system commands and wait for the command to complete on both windows and linux?

    Thanks in advance
    Mark

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you will have to change the test anyway because the two operating systems have different system commands -- by that I assume you mean shell functions such as system("ls") in using and system("dir") in MS-DOS/MS-Windows.
    Last edited by Ancient Dragon; 11-04-2005 at 06:27 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    Well the command I am executing will be the same, as it is a program that works on both platforms. But I need to be able to call it from the linux shell and from the dos shell. This program will produce an output after a few minutes. So I need to wait for the program to finish. It will produce an out put file and this is what I need to check. So is there some way to execute a program in linux and windows and wait for it to finish. For example, is there some way for the system() function in c++ to tell me if the program has finished?

    Thanks

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use one of the spawn family of functions (pass _P_WAIT as the mode parameter)-- I think they are available on most compilers in both *nix and MS-Windows. there is no need to create another thread just to spawn an external program and wait for it to finish.
    Last edited by Ancient Dragon; 11-04-2005 at 08:05 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    Ok, But will the system() function wait until the program has finished before returning control to the program?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    yes, on MS-Windows system() waits for the program to terminate. I assume *nix has the same behavior.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    yeah, that works.
    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. System(); commands
    By Ryan in forum C++ Programming
    Replies: 15
    Last Post: 03-15-2002, 07:05 PM
  3. system commands
    By mervin in forum C Programming
    Replies: 3
    Last Post: 02-03-2002, 09:01 AM
  4. string variables and system commands part 2
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-25-2001, 12:57 AM
  5. string variables in system commands
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-24-2001, 12:36 PM