Thread: Trouble understanding parent and child processes

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Trouble understanding parent and child processes

    I need help understanding how to get the following answers. Please note that these are review questions, not something I am trying to have done. I want to understand the concepts behind how the answers were achieved. The instructor has told us that questions similiar to these will be on our next exam.

    1) Including the initial parent process, how many processes are created in the below program?

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    
    //fork a child process
    fork();
    
    //fork another child process
    fork();
    
    //and fork another
    fork();
    
    return 0;
    }
    2) Using the below program, identify the values A, B, C, and D. (assume the actual pids of the parent and child are 2600 and 2603, respectively)

    Code:
    #include <sys/types.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    pid_t pid, pid1;
    
    //fork a child process
    pid = fork();
    
    if (pid< 0) //error occurred
    {
    fprintf(stderr, "Fork Failed");
    return 1;
    }
    else if (pid == 0) //child process
    {
    pid1 = getpid();
    printf("child: pid = %d", pid); //line A
    printf("child: pid1 = %d", pid1); //line B
    }
    else //parent process
    {
    pid1 = getpid();
    printf("child: pid = %d", pid); //line C
    printf("child: pid1 = %d", pid1); //line D
    wait(NULL);
    }
    
    return 0;
    }
    3) Using the below program, explain what the output will be at line A

    Code:
    #include <sys/types.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int value = 5;
    
    int main()
    {
    pid_t pid;
    pid = fork()
    
    if (pid == 0) //child process
    {
    value += 15;
    return 0;
    }
    else (id pid > 0) //parent process
    {
    wait(NULL);
    printf("Parent: value = %d", value); //Line A
    return 0;
    }
    }
    Please help if you can...I may be way off base here, but here is what I think:

    One the first question, I am thinking either 7 or 8. Doesn't the fork() copy both a child and a parent process, thus giving 8?

    On the second I believe it is:
    A 0
    B 2603
    C 2600
    D 2600

    On the third, would in not simply be 5?

    Thanks for any advice...I am trying hard, but I am at my wits end..

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For #1, this is what happens.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Perhaps this layout is a little better:
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why not compile and run these example programs yourself to assess your answers?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    So, it would be 7? I guess I was thinking the main had both processes and would spawn six more giving eight.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Count them.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Thanks! Now I finally get it. Your last post was extremely helpful.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Mind you, the shell (or whatever invokes the program first) spawns A, so your program only spawns seven more.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Do I have the other two right?

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You need to run the programs and learn.
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    I tried yesterday and my compiler gave me the error that the wait function was not declared in this scope. Any ideas?

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Are you on a unix machine? It seems you might need to spend some time, face to face, with a patient tutor.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM