×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

forking a grandchild process in C

forking a grandchild process in C

forking a grandchild process in C

(OP)

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);
}

RE: forking a grandchild process in C

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.

RE: forking a grandchild process in C

(OP)
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.

RE: forking a grandchild process in C

(OP)
*in both of my code examples

RE: forking a grandchild process in C

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close