waitpid w/ multiple processes

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 3
Reputation: rhp405 is an unknown quantity at this point 
Solved Threads: 0
rhp405 rhp405 is offline Offline
Newbie Poster

waitpid w/ multiple processes

 
0
  #1
Jun 8th, 2006
Hi!
I'm trying to find out what is wrong with the next code which I'm trying to run in Linux:

  1. int e1;
  2. int e2;
  3. pid_t pid1; // pid of child
  4. pid_t pid2;
  5. pid_t w1; // return code of wait()
  6. pid_t w2;
  7.  
  8. // child process 1
  9. pid1 = fork();
  10. if (pid1 == 0) {
  11. // some code...
  12. exit(e1);
  13. }
  14.  
  15. // child process 2
  16. pid2 = fork();
  17. if (pid2 == 0) {
  18. // some code
  19. exit(e2);
  20. }
  21.  
  22. // wait for children to terminate
  23. w1 = waitpid(pid1, &e1, WEXITED);
  24. if (w1 == -1) perror("");
  25. w2 = waitpid(pid2, &e2, WEXITED);
  26. if (w2 == -1) perror("");

The problem here is that even if the program compiles all right and runs all the way to end and does what supposed in the child processes, I get "invalid argument" error message from waitpid's and the flow of program just happily passes those waits. What's that? What I don't do right? I have also tried _exit() but with no different results.

I have printed out pids to see what they are and they seem to be ok.

All help appreciated!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: rhp405 is an unknown quantity at this point 
Solved Threads: 0
rhp405 rhp405 is offline Offline
Newbie Poster

Re: waitpid w/ multiple processes

 
0
  #2
Jun 9th, 2006
WEXITED can be only used with waitid(), not waitpid(). Hum... Ok, it works better when I put
  1. waitpid(pid, &status, 0);
Just as it says in manual. :I

Or wait a minute.

It waits only process number 1, and not number 2. Why?
Last edited by rhp405; Jun 9th, 2006 at 3:34 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: waitpid w/ multiple processes

 
0
  #3
Jun 9th, 2006
> It waits only process number 1, and not number 2. Why?
Because you asked it to.

If you use -1 as the pid, then it will wait for any child, and return with the pid of the child which ended.
Put that into a loop until all your children have ended, then carry on as normal.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: rhp405 is an unknown quantity at this point 
Solved Threads: 0
rhp405 rhp405 is offline Offline
Newbie Poster

Re: waitpid w/ multiple processes

 
0
  #4
Jun 20th, 2006
Ok, here's the solution.
When you have a pipe open in parent process, you have to close the pipe before waiting. It has to do with the file descriptor counters of the pipe ends, they have to be zeroes to not cause a child to block on them (in particular, the read end). The counters are the key if you want to look for more information.

The code which works is here:
  1. close(pipefd[0]);
  2. close(pipefd[1]);
  3.  
  4. // wait for children to terminate
  5.  
  6. w1 = waitpid(pid1, &status1, 0);
  7. if (w1 == -1) perror("");
  8. printf("%d terminated: %d\n", pid1, w1);
  9.  
  10. w2 = waitpid(pid2, &status2, 0);
  11. if (w2 == -1) perror("");
  12. printf("%d terminated: %d\n", pid2, w2);
This way you can wait for specific pid's one at a time, as is done in this example.

Thanks to my professor.
Last edited by rhp405; Jun 20th, 2006 at 6:19 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC