Thread: Vim help!

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    2

    Vim help!

    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:

    Code:
    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?
    Last edited by LiamSalt; 08-08-2013 at 12:15 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should think about what will happen in the child process when it gets to the second iteration of the loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    2
    Quote Originally Posted by vart View Post
    you should think about what will happen in the child process when it gets to the second iteration of the loop
    I think I have it working now, I was missing a return statement within that if statement.

    Is that correct?

    Well regardless its outputting the requested amount of prints now with the exception of this:

    k110-1:~ s029119a$ ./working.exe 10
    Child printingMy ID is: 10715
    Child printingMy ID is: 10716
    Child printingMy ID is: 10717
    Child printingMy ID is: 10719
    k110-1:~ s029119a$ Child printingMy ID is: 10721
    Child printingMy ID is: 10720
    Child printingMy ID is: 10722
    Child printingMy ID is: 10718
    Child printingMy ID is: 10723
    Child printingMy ID is: 10724
    So it prints 10 as requesting in the command line but I don't know how to get rid of the k110 thing that pops up half way, but that might just be the size of the window

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you probably want to wait for the child to exit
    Code:
    #include <unistd.h>     /* Symbolic Constants */
    #include <sys/types.h>  /* Primitive System Data Types */ 
    #include <errno.h>      /* Errors */
    #include <stdio.h>      /* Input/Output */
    #include <sys/wait.h>   /* Wait for Process Termination */
    #include <stdlib.h>     /* General Utilities */
    #include <time.h>
    
    static void Usage(const char* prog)
    {
        printf("Not enough arguments\n");
        printf("Usage:\n");
        printf("%s <Number>\n", prog);
        printf("\t where <Number> is number of child processes to start\n");
        return;
    }
    int main(int argc, char* argv[])
    {
        pid_t childpid; /* variable to store the child's pid */
        int retval;     /* child process: return code */
        int status;     /* parent process: child's exit status */
        int count;
        int childs;
        
        srand(time(NULL));
        if(argc < 2)
        {
            Usage(argv[0]);
            exit(0);
        }
        childs = (int)strtol(argv[1],NULL,10);
    
        for(count=0; count <  childs;count++)
        {
            /* now create new process */
            childpid = fork();
    
            if (childpid >= 0) /* fork succeeded */
            {
                if (childpid == 0) /* fork() returns 0 to the child process */
                {
                    printf("CHILD: I am the child process!\n");
                    printf("CHILD: Here's my PID: %d\n", getpid());
                    printf("CHILD: My parent's PID is: %d\n", getppid());
                    printf("CHILD: The value of my copy of childpid is: %d\n", childpid);
                    printf("CHILD: Sleeping for 1 second...\n");
                    sleep(1); /* sleep for 1 second */
                    retval = getpid()%255;
                    printf("CHILD: Goodbye! retval=%d\n",retval);    
                    exit(retval); /* child exits with random return code */
                }
                else /* fork() returns new pid to the parent process */
                {
                    if(count == 0)
                    {
                        printf("PARENT: I am the parent process!\n");
                        printf("PARENT: Here's my PID: %d\n", getpid());
                    }
                    printf("PARENT: The value of my copy of childpid is %d\n", childpid);
                        
                }
            }
            else /* fork returns -1 on failure */
            {
                perror("fork"); /* display error message */
                exit(0); 
            }
        }
        for(count=0; count <  childs;count++)
        {
            printf("PARENT: I will now wait for my child %d to exit.\n",count);
            wait(&status); /* wait for child to exit, and store its status */
            printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
        }
        printf("PARENT: Goodbye!\n");             
        exit(0);  /* parent exits */   
    }
    Last edited by vart; 08-08-2013 at 01:43 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Tags for this Thread