-
August 8th, 2013, 11:56 AM
#1
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?
-
August 8th, 2013, 12:26 PM
#2
Re: Help!
Help us help you by posting properly indented code between code tags, and without line numbers.
-
August 8th, 2013, 12:33 PM
#3
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?
-
August 8th, 2013, 12:35 PM
#4
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 8th, 2013, 12:37 PM
#5
Re: Help!
Originally Posted by 2kaud
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.
-
August 8th, 2013, 12:40 PM
#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
-
August 8th, 2013, 12:42 PM
#7
Re: Help!
Okay done, do you have any suggestions as for my issue?
-
August 8th, 2013, 01:20 PM
#8
Re: Help!
Originally Posted by LiamSalt
Okay done, do you have any suggestions as for my issue?
please, indent your code properly!
Victor Nijegorodov
-
August 8th, 2013, 01:21 PM
#9
Re: Help!
Originally Posted by LiamSalt
Okay done, do you have any suggestions as for my issue?
Well, one very important suggestion is you must use debugger!
Victor Nijegorodov
-
August 8th, 2013, 01:26 PM
#10
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?
-
August 8th, 2013, 01:29 PM
#11
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
-
August 8th, 2013, 01:34 PM
#12
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|