Very basic problem with fork() using c


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Very basic problem with fork() using c
# 1  
Old 07-10-2013
Very basic problem with fork() using c

Hi guys,
I have the following code:

Code:
int main(int argc, char *argv[])  
{     
int pid1,pid2,i=0;
       pid1=fork();
      i+=2;
          if(!pid1) i++;
     if(i%3) pid2=fork();     
if (pid2==0)
     {
         printf("sea \n ");    
         i-=1;
     }
if(i>=2) printf("Mountain\n"); 

}

Please, can someone enlight me and explain WHY the output is
Code:
sea
mountain
mountain
sea ??

Why a 4 child is forked ? From where ?!
the if(i%3)

if the result is "ZERO", it should be read as FALSE by the program, right ? But even if the result is == 0 its like if the condition was true.

Can someone explain it to me?

Last edited by Scott; 07-10-2013 at 05:04 PM.. Reason: Fixed code tags; removed Urgent title
# 2  
Old 07-10-2013
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..
# 3  
Old 07-10-2013
Quote:
Originally Posted by DGPickett
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:

Code:
if(!pid1) i++

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:
Code:
if(i%3) pid2=fork();

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 ?
# 4  
Old 07-10-2013
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.

Regards,
Alister
# 5  
Old 07-16-2013
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem with fork() while reading files

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)
Discussion started by: pfpietro
1 Replies

2. Programming

Problem with fork() and execlp process

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)
Discussion started by: Johnathan_1017
3 Replies

3. Programming

problem with mutltiple fork()

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)
Discussion started by: Placenzo
4 Replies

4. Programming

help in C of fork() problem

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)
Discussion started by: wendy1089
7 Replies

5. Programming

Fork and then exec problem with signals

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)
Discussion started by: sushil_shalin
6 Replies

6. UNIX for Dummies Questions & Answers

simple fork() problem

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)
Discussion started by: joker40
3 Replies

7. Programming

problem implementing fork

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)
Discussion started by: dheerajsuthar
7 Replies

8. UNIX for Advanced & Expert Users

Problem due to Fork Error

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)
Discussion started by: Amey Joshi
8 Replies

9. Programming

fork() problem

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)
Discussion started by: ddx08
3 Replies

10. Programming

fork problem

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)
Discussion started by: qntmteleporter
4 Replies
Login or Register to Ask a Question