Thread: Forks and Processes

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    Forks and Processes

    Ok im trying to make this simple program in UNIX using forks. What it does is it creates two child processes, and they both should print out a string, and then when the program finishes it prints out the getpid() function. Only three lines should appear, however, my code just jumps to the second child first, then prints that, then prints the first child stuff and prints the second child stuff again. Here's my code, I have no idea what is going on.

    http://sourcepost.sytes.net/sourcevi...ource_id=16947

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's a lot of code in there that I can't quite understand the logic of. Like:
    Code:
                if(pid != 0)
                {
                    while(pid ==0)
                    {
                          //do nothing
                    }
    If pid isn't 0 then how will the while() loop ever do anything? I'd set it up something like this:
    Code:
    pid1 = fork();
    if pid1 is not 0
      pid2 = fork();
    else
      print a line from child1
    if pid2 is 0
      print a line from child2
    if pid1 is not 0 and pid2 is not 0
      wait for children to exit and then print parent's pid
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    Ok I did what you said but it first prints getpid, then it prints the pid1 and pid2, and they are said to both be 0. heres the code.

    http://sourcepost.sytes.net/sourcevi...ource_id=16951

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's one of the side effects. Each process is scheduled independently by the kernel so you really have no way of knowing which message is going to show up first without waiting for children or something like that. If you run the program repeatedly you should see the messages come up in different orders.

    The reason it's showing the pid as 0 inside the child process is because that's what it's supposed to do. This is from 'man fork':
    Code:
    RETURN VALUE
           On  success,  the  PID of the child process is returned in
           the parent's thread of execution, and a 0 is  returned  in
           the child's thread of execution.  On failure, a -1 will be
           returned in the parent's context, no child process will be
           created, and errno will be set appropriately.
    If you want to print the pids of the child processes you'll have to call getpid() from inside those processes.
    Last edited by itsme86; 09-26-2004 at 05:27 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's the simplest implementation I can think of; each process printing its own pid:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int main(void)
    {
      if(fork())
        fork();
    
      printf("I am pid %d!\n", getpid());
    
      return 0;
    }
    Should give you something like this (assuming fork() doesn't fail).
    Code:
    I am pid 2913!
    I am pid 2914!
    I am pid 2912!
    Last edited by itsme86; 09-26-2004 at 05:49 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. forks and child processes
    By kpax in forum C Programming
    Replies: 1
    Last Post: 05-28-2008, 04:47 AM
  2. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  3. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 06:29 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Unix processes
    By J-Dogg in forum Linux Programming
    Replies: 1
    Last Post: 03-24-2003, 05:42 PM