You started with one process, then two processes that forked into 4 processes, three total fork calls. For the second fork, two were parents and two were children. Welcome to multiprocessing. Each process has its own copies of the variables. For instance if the first process was 1000, it forked off 1001 and then 1002, and 1001 forked off 1003. Actually, as I recall, the child tends to run before the parent of a fork, so 1001 might have forked 1002 before 1000 could, so it forks off 1003, but you can tell from the order of the printouts.
Last edited by DGPickett; 07-10-2013 at 05:15 PM..
You started with one process, then two processes that forked into 4 processes, three total fork calls. For the second fork, two were parents and two were children. Welcome to multiprocessing. Each process has its own copies of the variables. For instance if the first process was 1000, it forked off 1001 and then 1002, and 1001 forked off 1003.
Ok, this part I got.
But how come ?
One process wasn't supposed to fork another process.
-P0 forks P1 and both have i=2
- then only P1 does i++ because:
P1 is the forked child, who sees pid1==0 and P0 the father, who sees pid1>0
So, it means if(0) to P1 and if(true) to P0.
---
So far we have P0 with i=2 and P1 with i=3
-next step is:
For P1, the result of 3%3 will be 0,
so if ( 0 ) pid2 = fork();
and as far as I understood, P1 WONT FORK P2!!!
Instead, P0 will produce 2%3 == 2, so P0 will fork P2,
And we'll have only 3 processes.
Where is my mistake ?
---------- Post updated at 03:24 PM ---------- Previous update was at 03:19 PM ----------
Maybe because pid2 wasn't initialized ?
So when P0 tests pid2 and its value without being initialized is 0, P0 prints sea and mountain ?
I did not compile and test the code, but I don't think there are 4 processes. Fork is called twice, both times by the original process, creating two siblings.
I too suspect that the problem is the uninitialized pid2 in the first child; since it is an automatic variable, it may contain garbage. A printf statement will confirm.
Yes, remember that you are working with 4 different variables, with values copied at fork. I think for security reasons the unitialized automatics are at least initially zeroes. Later, in subroutines, they might be your old stack frame values. Don't do that. Try putting pid's on the output and fflush( stdout ) to spit out full lines always, since the stdout is being shared. https://www.unix.com/man-page/All/3/fflush/ Man Page for getpid (all Section 3) - The UNIX and Linux Forums
Good evening everyone. I have my finals and I'm facing a problem:
I have a for cycle that is supposed to fork 2 children but somehow it forks only the first one.
What am I doing wrong ?
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>... (1 Reply)
Hello everyone, this is my first post.
I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers.
The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to... (3 Replies)
Hi, can someone please help me with creating mutltiple fork.. I was expecting something like this:
I am a child: 1 PID: 1215
I am a child: 2 PID: 1216
I am a child: 3 PID: 1217
I am a child: 4 PID: 1218
I am a child: 5 PID: 1219
I am a child: 6 PID: 1215
I am a child: 7 PID: 1216
I am a... (4 Replies)
i am a beginner of C, and i tired to fork multiple child and all of them belongs to same parents and each of child responsible for printing individual data.
but i don't have any idea how to do......
Can any body help? thanks a lot really. (7 Replies)
Hi All,
In my program i am handling SIGHUP signal. In the handler i fork and then exec on child process same binary file which is running. Parent process will die after 10 mins.
Now my child process which was exec with same binary file is not receiving SIGHUP signal. Below is the progran code:... (6 Replies)
I have this little program ...
int main(void){
printf("Before");
fork();
printf("After");
}
output is this.....
BeforeAfterBeforeAfter
Shouldnt it be.....BeforeAfterAfter
After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Hi,
I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty file creator program. While creating a large file it must print # for progress bar. But the output shows it happening reverse way. ie. first it copies file and shows the progress... (7 Replies)
Hi,
I have developed a datastage job...which has many process running in parallel..but because of Fork Error my job is not working:(
Can any body help me out to solve this Fork error problem.:rolleyes:
My Os is SUNOS.
IS there any setting in Unix through admin where in if i set some paramter... (8 Replies)
i'm just trying to make 2 process read from the same 1 line a time. For some reason only the child reads.
#include<stdio.h>
#include <sys/types.h>
void getlinefromfilep(void);
void getlinefromfilec(void);
int see=0;
FILE * fileptr1;
//need globe variable to tell pro3 to stop
main()... (3 Replies)
Hi,
Consider the following piece of code:
int main(void) {
int i;
pid_t pidp;
for (i=0;i<4;i++) {
switch (pidp=fork()) {
case -1:
fprintf(stdout, "Error during fork.\n");
exit (1);
case 0:
fprintf(stdout, "From child: I am... (4 Replies)