I have a question about the waitpid parameters. I'm supposed to start p[1] (process 1) when p[0] is done.
This is what starts p0:
if(p[0] == 0){
process(0,1); //(process, duration(time))
return 0;
}
Now I want p1 to start as soon as p0 ends (after 1 sec)
if(p[1] == 0){
process(1,2);
return 0;
}
waitpid(p[0], NULL, 0);
Here's my question: what do the parameters in waitpid mean? should the last parameter be set to 1, since p[0] ends after 1 sec and this is when I want p[1] to start?
what does the parameters in waitpid means?
You can look up the manual of waitpid(3)
for the meanings of its arguments.
In your case,
waitpid(p[0], NULL, 0);
means
p[0]
: wait for the pid hold on p[0]
NULL
: don't care about status0
: no flagsshould the last parameter be set to 1, since p[0] ends after 1 sec and this is when I want p[1] to start?
To achieve your goal, starting process 1 as soon as process 0 ends, you should put the waitpid(...);
statement before the fork()
used to create process 1, and use right parameters in your call to waitpid()
.
Full documentation is on the manpage or here: http://linux.die.net/man/2/waitpid
Basically you have 3 parameters:
pid_t waitpid(pid_t pid, int *status, int options);
pid
is the process you are waiting for. Unless you are waiting for multiple children (in which case specify 0, -1, or a number less than -1 - details in the manpage), specify the pid
of your process here.
status
is a pointer to an integer which will be filled in with the exit status. This is a combination of the process's exit status and a description of how it has (or has not) exited. The manpage gives you macros you can use to understand this.
options
can be filled in with a number of flags, or
ed together. The most useful of these is the somewhat oddly named W_NOHANG
, which makes waitpid
simply tell you whether the process has finished (and if so what its exit status was) rather than wait for it to finish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With