Help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Help!

  1. #1
    Join Date
    Aug 2013
    Posts
    6

    Help!

    Sorry I know this is a C++ forum but I am hoping there is someone that can help me!

    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:
    1 #include <stdio.h>
    2 #include <sys/types.h>
    3 #include <unistd.h>
    4 #include <stdlib.h>
    5
    7 int main(int argc, char *argv[])
    8 {
    9 int prod = atoi(argv[1]);
    10 int count = 0;
    11
    12 if(argc == 2)
    13 {
    14 while(count < prod)
    15 {
    16 printf("success\n\n");
    18 int i;
    19 i=fork();
    20 printf("\n");
    21
    22 if(i==0)
    23 {
    24 printf("Child printing");
    25 printf("getpid: %d, getppid: %d
    \n",getpid(), getppid());
    26 }
    27
    28 count++;
    29 }
    30
    31 }
    32 else
    33 {
    34 printf("Not enough paramterers");
    35 }
    36
    37 return 0;
    38
    39 exit(0);
    40 }

    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. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Help!

    Help us help you by posting properly indented code between code tags, and without line numbers.

  3. #3
    Join Date
    Aug 2013
    Posts
    6

    Re: Help!

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

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


    success



    Child printinggetpid: 9484, getppid: 1
    success



    Child printinggetpid: 9485, getppid: 1
    k110-1:~ s029119a$
    Child printinggetpid: 9486, getppid: 1


    Hows that?

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    Re: Help!

    When posting code, Go advanced, select code and then click '#'

    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;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Help!

    Quote Originally Posted by 2kaud View Post
    When posting code, Go advanced, select code and then click '#'
    Wouldn't it be better to let the poster do it himself that to do it for him? FWIW, I find that formatting style just about as unreadable as the original.

  6. #6
    Join Date
    Aug 2013
    Posts
    6

    Re: Help!

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

  7. #7
    Join Date
    Aug 2013
    Posts
    6

    Re: Help!

    Okay done, do you have any suggestions as for my issue?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Help!

    Quote Originally Posted by LiamSalt View Post
    Okay done, do you have any suggestions as for my issue?
    please, indent your code properly!
    Victor Nijegorodov

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Help!

    Quote Originally Posted by LiamSalt View Post
    Okay done, do you have any suggestions as for my issue?
    Well, one very important suggestion is you must use debugger!
    Victor Nijegorodov

  10. #10
    Join Date
    Aug 2013
    Posts
    6

    Re: Help!

    I'm using VIM, it's a shell, I don't know how to use it.

    I was wondering if it was something to do with the getpid() function as it prints 2 on the second iteration?

  11. #11
    Join Date
    Aug 2013
    Posts
    6

    Re: Help!

    Appreciate the help but I've worked it out, I wasn't returning 0 in the it statement that gets and prints the child pid

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    Re: Help!

    I'm got no experience of fork as Windows doesn't support this. However, you might get some info from here

    http://mij.oltrelinux.com/devel/unixprg/#tasks
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center