Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run a process and controll it within C or C++

Status
Not open for further replies.

scecilia

Programmer
Jul 8, 2002
35
US
How can I run an external processes under control of a C program. That is, if I have a .exe, and I want the C (or C++) program to execute it, but then, I do not want the .exe to hog resources, and I do not want to wait long for it to finish. I need to make sure it is running, and I want to have the control to kill it if I need to, from within the C program.

In Windows there are API functions that can be used to launch an external process, you can use these from within a VB app, for example. The API functions are: CreateProcess, WaitForSingleObject, and TerminateProcess to kill it if necessary. You can launch such a VB program with IDLE_PRIORITY_CLASS so not to dominate the computer's resources and be able to do desktop work while the VB is running.

So, we would like to do that in Unix, and I think there should be something out there to do that. Can you help?
 
The UNIX fork() function is should do the trick. Basically it duplicates the current process, returning 0 to the child process and the child's process ID to the parent. You can then run another process from the child if you wish to using one of the variety of exec() functions (man -s 2 exec). If the processes need to communicate with each other you could use IPC (Inter-Process Communication). Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top