Need urgent help with C VIM program

Discussion in 'C' started by LiamSalt, Aug 8, 2013.

  1. LiamSalt

    LiamSalt New Member

    Joined:
    Aug 8, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am new to coding in C and even newer to using VIM

    I am currently trying to write a code that will store PID numbers of a child after a fork, I have to be able to enter an amount that will be created, so far I have managed to be able to get them to print (which puts me on the right path as far as I am concerned) but I am having issues.

    Using the following code:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
       int prod = atoi(argv[1]);
       int count = 0;
     
       if(argc == 2)
       {
          while(count < prod)
       {
          printf("success\n\n");
          int i;
          i=fork();
          printf("\n");
     
          if(i==0)
          {
             printf("Child printing");
             printf("getpid: %d, getppid: %d \n",getpid(), getppid());
          }
    
          count++;
          }
    
       }
       else
       {
          printf("Not enough paramterers");
       }
     
       return 0;
    
    exit(0);
    }
    while this has some success, it is for some reason creating a 3rd iteration and I can't work out why

    The "success" print is more of a marker, but every time I run the program I get this result:

    k110-1:~ s029119a$ ./try3.exe 2
    success


    success



    Child printinggetpid: 9335, getppid: 1
    success


    k110-1:~ s029119a$
    Child printinggetpid: 9336, getppid: 1

    Child printinggetpid: 9337, getppid: 1​

    Can anyone offer any advice or tell me what I'm doing wrong, why is it doing this?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    At count=0, try3.exe calls fork() and creates a new process. That new process displays 9335. Remember that you have two identical processes immediately after a fork. After the if, both processes increment count and go to the top of the while. Both processes then have count=1, which is less than prod=2, so display "success", and both then fork again, creating two processes 9336 and 9337. All four processes then increment count, find it to be equal to prod and exit.

    Basically what you're doing wrong here is allowing the child processes to continue executing the parent code. Typically the "if(i==0)" block would contain the child code, and that block would end with a call to exit() or some other mechanism to prevent it continuing into the wrong code.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Before you fix this bug, have a bit of fun by trying a larger parameter, maybe up to 5, but not much bigger unless it's a computer you can freely perform a DoS attack on without implications. See if you can work out why you get the number of processes you do. If you don't see why you shouldn't try a number bigger than 5, first read up on "exponential growth".
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice