| | |
waitpid w/ multiple processes
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
Hi!
I'm trying to find out what is wrong with the next code which I'm trying to run in Linux:
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!
I'm trying to find out what is wrong with the next code which I'm trying to run in Linux:
C Syntax (Toggle Plain Text)
int e1; int e2; pid_t pid1; // pid of child pid_t pid2; pid_t w1; // return code of wait() pid_t w2; // child process 1 pid1 = fork(); if (pid1 == 0) { // some code... exit(e1); } // child process 2 pid2 = fork(); if (pid2 == 0) { // some code exit(e2); } // wait for children to terminate w1 = waitpid(pid1, &e1, WEXITED); if (w1 == -1) perror(""); w2 = waitpid(pid2, &e2, WEXITED); 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!
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
WEXITED can be only used with waitid(), not waitpid(). Hum... Ok, it works better when I put
Just as it says in manual. :I
Or wait a minute.
It waits only process number 1, and not number 2. Why?
C Syntax (Toggle Plain Text)
waitpid(pid, &status, 0);
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.
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
This way you can wait for specific pid's one at a time, as is done in this example.
Thanks to my professor.
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:
C Syntax (Toggle Plain Text)
close(pipefd[0]); close(pipefd[1]); // wait for children to terminate w1 = waitpid(pid1, &status1, 0); if (w1 == -1) perror(""); printf("%d terminated: %d\n", pid1, w1); w2 = waitpid(pid2, &status2, 0); if (w2 == -1) perror(""); printf("%d terminated: %d\n", pid2, w2);
Thanks to my professor.
Last edited by rhp405; Jun 20th, 2006 at 6:19 am.
![]() |
Similar Threads
- threading in linux (C)
- Ideas for a new XT compatible OS... (IT Professionals' Lounge)
- message passing interfaces (C)
- Set Performance Options in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C Forum
- Previous Thread: ostream - how to insert a line of text
- Next Thread: Base64 Decoder
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






