Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    child processes exit codes

    Hello everyone,

    I have to write in c a program which return an exit codes of three child processes. I know that wait() function can do that (writes to the ver so i wrote something like this:
    Code:
    #include <stdio.h>
    #include <sys/wait.h>
    
    int main(void)
    {
    int pid1,pid2,pid_from_wait,status,status2;
    pid1=fork();
    	if( pid1 != 0 ) 
    	{
    		pid_from_wait = wait(&status);
    		printf("Child1 %d ended up with code %d \n",
    pid_from_wait, status);
    		pid2=fork();
    			if( pid2 != 0 ); 
    			{
    				wait(&status2);
    		printf("Child2 %d ended up with code %d \n", getpid(),
    status2);
    			}
    			else 
    			printf("I'm child 2");
    			
    	}
    	else 
    		printf("I'm child0");
    return 0;
    }
    It doesn't work, could someone help me with that ?




  2. #2
    Join Date
    Oct 2012
    Location
    Minsk, Belarus
    Posts
    15
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    0
    man wait
    Code:
    #include <stdio.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    int main(void){
       pid_t chid, pid_from_wait;
       int status=0, cnt=0;
    
       chid=fork();
       if (chid== -1){ /* fork failed */
          perror("fork");
          exit(EXIT_FAILURE);
       }
       if (chid == 0) { /* child */
          printf("First child PID is %d\n", getpid());
          _exit(41);
       } 
       /* parent */
       chid=fork();
       if (chid == -1){ /* second fork failed */
          perror("second fork");
          exit(EXIT_FAILURE);
       }
       if (chid == 0) { /* second child */
          printf("Second child PID is %d\n", getpid());
          _exit(42);
       }
       /* parent */
       chid = fork();
       if (chid == -1){ /* third fork failed */
          perror("third fork");
          exit(EXIT_FAILURE);
       }
       if (chid == 0) { /* third child */
          printf("Third child PID is %d\n", getpid());
          _exit(43);
       }
       /* parent */
       do {
          pid_from_wait = wait(&status);
          if (pid_from_wait == -1) {
    	 perror("wait");
    	 exit(EXIT_FAILURE);
          }
          if (WIFEXITED(status)) {
    	 cnt++;
    	 printf("[%d] Child with pid %d ended up with code %d \n", cnt,
    		pid_from_wait,  WEXITSTATUS(status));
          }
       } while (cnt < 3);
       exit(EXIT_SUCCESS);
    }
    http://c.pastebay.net/1161693

 

 

Similar Threads

  1. How to list processes?
    By carbon333 in forum Command Line
    Replies: 0
    Last Post: 02-16-2012, 04:16 PM
  2. Replies: 0
    Last Post: 01-08-2012, 11:03 AM
  3. NetWare-Linux love child turned up to 11
    By Fred in forum Linux News
    Replies: 0
    Last Post: 12-13-2011, 04:45 PM
  4. UK.gov to build child army of software coders
    By Fred in forum Linux News
    Replies: 0
    Last Post: 11-28-2011, 05:20 PM
  5. Testing the Exit Status of a Command
    By Fred in forum Linux News
    Replies: 0
    Last Post: 11-03-2011, 01:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,559
Threads: 3,916
Posts: 9,431
Top Poster: Fred (1,486)
Welcome to our newest member, ssanthshtech

» Links



Powered by vBadvanced CMPS