Thread: linux process question

  1. #1
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46

    linux process question

    Hi, I don't know why when i run this code with redirection of output,
    Code:
    a.out > file
    , it seems that the process child is not adopted by init.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    static void ex1(void)
    {
    printf("student\n");
    }
    static void ex2(void)
    {
    printf("prof \n");
    }
    
    int main(void)
    {
    	pid_t pid1,pid2;
    	int s=0, c;
    
    	pid1=fork();
    	if (pid1==0) { 
    		atexit(ex1); 
    		atexit(ex2);
    		while ((c=getppid()) != 1) { 
    			printf("pid=%d\n", c);		
    			s=s+1;
    			printf("userc: %d\n",s);
    			exit(0);
    		}
    	}		
    	else {
    		atexit(ex1); 
    		atexit(ex2);
    		printf("userf: %d\n",s);
    		exit(0);
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because your program is run in a process that is a child of the shell in which you type "a.out > file". That shell process will continue to exist until the child exits. Which means the child never becomes an orphan, and is never adopted by init.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46
    what are you saying is that the parent process doesn't exit?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The parent process waits for the child to terminate before it continues execution (and continuing execution is a precursor to exiting).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 07-19-2009, 05:44 PM
  2. What is maximum process size in linux?
    By darsunt in forum Linux Programming
    Replies: 2
    Last Post: 07-15-2009, 03:55 PM
  3. In Linux, how to know what process is sleeping
    By meili100 in forum Tech Board
    Replies: 2
    Last Post: 07-28-2008, 03:52 PM
  4. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 07:02 PM
  5. ?writing bug in a multi-process Linux server application?
    By Arrummzen in forum C Programming
    Replies: 7
    Last Post: 12-14-2002, 11:53 AM