Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forking a grandchild process in C

Status
Not open for further replies.

bterribi

Programmer
Sep 27, 2006
8
US
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int
main (int argvc, char *argv[])
{           
    pid_t pid;
    int arg1 = atoi(argv[1]);
    pid = fork();

    if (pid == 0) {
        printf("I am child\n");
        printf("my pid is: %d\n", getpid());
        printf("my parent pid is: %d\n", getppid());
        printf("I am changing argument from %d ", arg1);
        arg1 *= 2;
        printf(" to %d\n", arg1);
    }
    else {
        printf("I am a parent\n");
        printf("my pid is: %d\n", getpid());
        printf("my child pid is: %d\n", pid);    
        printf("The argument passed to me is %d\n", arg1);
    }

    /* This code executes in both processes */
    printf("exiting ... %d\n", getpid());
    exit(0);
}
I’m trying to modify the above code so that three generations of processes are created instead of 2. The original process prints its pid, its child pid, and the command line argument passed to it. The child process prints its pid, its parent pid, its child pid, the original command line argument and the modified argument. The grandchild prints its pid, its parent id, the command line argument that was there when its parent called it, and the modified
argument. (So if the program was called as “./a.out 10” the parent prints 10 for argument, child prints 10 and 20; and the grandchild prints 20 and 40 as the argument value.) I can't quite figure out what I'm doing wrong though. Any help would be greatly appreciated:) btw...I'm using gcc to compile this code in cygwin.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

int
main (int argvc, char *argv[])
{      
    /* parent process created */  
    pid_t pid;
    
    int arg1 = atoi(argv[1]);

    /* child process created */
    if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }
    
    if (pid == 0) {
        
        /* grandchild process created */        
        if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
        }
        
        printf("I am child\n");
        printf("my pid is: %d\n", getpid());
        printf("my parent pid is: %d\n", getppid());
        printf("I am changing argument from %d ", arg1);
        arg1 *= 2;
        printf(" to %d\n", arg1); 
                
        printf("I am grandchild\n");
        printf("my pid is: %d\n", getpid());
        printf("my parent pid is: %d\n", getppid());
        printf("I am changing argument from %d ", arg1);
        arg1 *= 2;
        printf(" to %d\n", arg1);

        exit(1);
        wait(1);     
    }

    else {
        printf("I am a parent\n");
        printf("my pid is: %d\n", getpid());
        printf("my child pid is: %d\n", pid);    
        printf("The argument passed to me is %d\n", arg1);
    }
    
        while (pid != wait (0));
       	printf ("Parent: Child %ld terminated\n", pid);
	    close (*argv[1]);
        printf ("Parent: Killing Grand Child %ld\n", pid);
	    kill (pid, SIGTERM);
	    
    /* This code executes in both processes */
    printf("exiting ... %d\n", getpid());
    exit(0);
}
 
Another if/else construct to separate the child and grandchild code perhaps?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
What would the conditional expression be for another if statement? From my understanding, if pid > 0, then pid is considered a parent process and will always execute the else statement both my code expamples.
 
By calling fork() in the child, you create another parent.
Code:
if ( pid == 0 ) {
  // child
  pid = fork();
  if ( pid == 0 ) {
    // grandchild (of top-level parent)
  } else {
    // parent of the grandchild
  }
} else {
  // parent
}

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top