LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2011, 04:38 AM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Rep: Reputation: 16
waiting for multiple childs - C - waitpid


Hi gurus, I would like to fork more children and then write their return values: so far I tried:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;
        int rv=0, i;
        pid_t child_pids[5];


        for (i=1; i<=5; i++){

                /* Multiple child forking */
                switch(pid = fork()){
                        case -1:
                                /* something went wrong */
                                /* parent exits */
                                perror("fork");
                                exit(1);

                        case 0:
                                /*Children process*/
                                child_pids[i] = getpid();
                                printf(" CHILD: number (and return value): %d PID: %d PPID: %d \n", i, getpid(), getppid());
                                exit(i);
                                break;

                                /*Missing code for parent process - will be executed out of loop*/
                }
        }
        /*Parent process*/
        if (pid!=0 && pid!=-1) {
                printf("PARENT: my PID is %d\n", getpid());

                for (i=1; i<=5; i++){
                        waitpid(child_pids[i], NULL, 0);
                        printf("PARENT: Child: %d returned value is: %d\n", i, WEXITSTATUS(child_pids[i]));
                }
        }
}
But returns any random variables for child process:

...
PARENT: Child: 3 returned value is: 13
PARENT: Child: 3 returned value is: 127
...


also tried following
Code:
int main(void)
{
        pid_t pid;
        int rv=0, i;
        pid_t child_pids[5];
        int child_state = 9;
        pid_t rc_pid;


        for (i=1; i<=5; i++){

                /* Multiple child forking */
                switch(pid = child_pids[i] = fork()){
                        case -1:
                                /* something went wrong */
                                /* parent exits */
                                perror("fork");
                                exit(1);

                        case 0:
                                /*Children process*/
                                //child_pids[i] = getpid();
                                printf(" CHILD: number (and return value): %d PID: %d PPID: %d \n", i, getpid(), getppid());
                                exit(i);
                                break;

                                /*Missing code for parent process*/
                }
        }
        /*Parent process*/
        if (pid!=0 && pid!=-1) {
                printf("PARENT: my PID is %d\n", getpid());

                for (i=1; i<=5; i++){
                        rc_pid = waitpid(child_pids[i], NULL, 0);
                        printf("PARENT: Child: %d returned value is: %d\n", i, WEXITSTATUS(child_pids[i]));
                }
        }
}
but returns always number 19 as return code of children
...
PARENT: Child: 4 returned value is: 19
...

Probably something something is wrong with waitpid() function
Thanks a lot.

Last edited by wakatana; 02-28-2011 at 04:41 AM.
 
Old 02-28-2011, 05:53 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You should focus your eyes on the following:
Code:
pid_t child_pids[5];


        for (i=1; i<=5; i++){
Indices for arrays are zero-based, not one-based.
 
Old 02-28-2011, 06:56 AM   #3
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
Thanks for reply - I repair this mistake but still did not working as expected - seems that waitpid did not catch the correct return value
 
Old 02-28-2011, 07:48 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You are not obtaining the exit status that the child process posts when it exits. Call waitpid() like this:
Code:
int status;

waitpid(child_pids[i], &status, 0);

printf("PARENT: Child: %d returned value is: %d\n", child_pids[i], WEXITSTATUS(status));
 
Old 02-28-2011, 06:26 PM   #5
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by wakatana View Post
But returns any random variables for child process:
Maybe that's because you are feeding it random data?

Your parent process never fills it's child_pids[] array -- you do that inside the child processes instead of the parent.

So the parent's child_pids[] is uninitialized and hence filled with random data. (The children's child_pids[] each have one value set)
 
Old 03-01-2011, 02:45 PM   #6
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by dwhitney67 View Post
You are not obtaining the exit status that the child process posts when it exits. Call waitpid() like this:
Code:
int status;

waitpid(child_pids[i], &status, 0);

printf("PARENT: Child: %d returned value is: %d\n", child_pids[i], WEXITSTATUS(status));
Thanks a lot that worked as expected just one question is this right way to create children ?
 
Old 03-01-2011, 03:08 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by wakatana View Post
... just one question is this right way to create children ?
I'm not sure there's any other way other than using fork(). You can create a separate process using one of the "exec" calls (e.g. execl(), execv(), etc), however these don't share resources in the manner that fork() affords us.
 
Old 03-01-2011, 03:38 PM   #8
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
Thank you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pls tell difference between waitpid(pid,&status,0) and waitpid(pid,&status,WNOHANG) nagendrar Linux - Newbie 3 07-08-2009 08:51 PM
Run multiple apps at once without waiting..? jchambers Programming 4 04-23-2009 06:25 AM
My browser, all day today: 'waiting for linuxquestions.org...' ..waiting.. waiting .. GrapefruiTgirl LQ Suggestions & Feedback 18 05-25-2007 05:35 AM
why can not fork new childs? nenejung Programming 2 08-05-2003 12:58 PM
why can not fork new childs? nenejung Programming 2 07-30-2003 05:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration