views:

180

answers:

3

Is it possible for a parent process to start and stop a child (forked) process in Unix?

I want to implement a task scheduler (see here) which is able to run multiple processes at the same time which I believe requires either separate processes or threads.

How can I stop the execution of a child process and resume it after a given amount of time?

(If this is only possible with threads, how are threads implemented?)

+2  A: 

You could write a simple scheduler imitation using signals. If you have the permissions, then stop signal (SIGSTOP) stops the execution of a process, and continue signal (SIGCONT) continues it.

With signals you would not have any fine grained control on the "scheduling", but I guess OS grade scheduler is not the purpose of this execersice any way.

Check kill (2) and signal (7) manual pages. There are also many guides to using Unix signals in the web.

+1  A: 

You can use signals, but in the usual UNIX world it's probably easier to use semaphores. Once you set the semaphore to not let the other process proceed, the scheduler will swap it out in the normal course of things; when you clear the semaphore, it will become ready to run again.

You can do the exact same thing with threads of course; the only dramatic difference is you save a heavyweight context switch.

Charlie Martin
A: 

Just a side note: If you are using signal(), the behavior may be different on different unixes. If you are using Linux, check the "Portability" section of the signal manpage, and the sigaction manpage, which is preferred.

PolyThinker