question about fork


 
Thread Tools Search this Thread
Top Forums Programming question about fork
# 1  
Old 09-06-2011
question about fork

i'm experimenting fork function and i found this code
Code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wait.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
	int fd[2];
	pid_t p;

	p = fork();
	fork();
	if (p>0) { fork();}
	fork();
	fork();
	sleep(30);
	exit(0);
}

I thought that there were 2^5 processes after running this code , but there were less processes.
Why?

Last edited by blob84; 09-06-2011 at 01:23 PM..
# 2  
Old 09-06-2011
Because when fork() returns, 2 processes exist: one parent, and one child.
  1. So after the first fork(), you have 2 processes.
  2. This 2 processes calls fork() again; you have now 4 processes.
  3. The third fork() is only performed by the children, resulting 4 more processes (now 6=4+2 in total).
  4. These 6 processes calls fork() twice, resulting 2*2*6 = 24 processes in total.
From this, it should be clear that several fork() one after the other causes an exponential growth of the number of processes.

Cheers,
/Lew
# 3  
Old 09-06-2011
The relationship between fork and the number of processes created isnt 2^n but 2n...and in this program it will be 2n-2 due to...if (p>0) { fork();}
This User Gave Thanks to shamrock For This Post:
# 4  
Old 09-06-2011
Quote:
The relationship between fork and the number of processes created isnt 2^n but 2n...and in this program it will be 2n-1 due to...if (p>0) { fork();}
May I suggest to check this assumption against reality by writing a small program?

Thanks,
/Lew
This User Gave Thanks to NH2 For This Post:
# 5  
Old 09-06-2011
Quote:
Originally Posted by NH2
[*]The third fork() is only performed by the children, resulting 4 more processes (now 6=4+2 in total). [/LIST]
Non zero return value goes to parent not child...and after double cheking my calculations yes it will be 24 procs.
# 6  
Old 09-06-2011
Quote:
Originally Posted by shamrock
Non zero return value goes to parent not child...and after double cheking my calculations yes it will be 24 procs.
Thanks shamrock for double checking.

Cheers,
/Lew
# 7  
Old 09-10-2011
thanks NH2 for explanation!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies

2. Programming

Linux fork, execv, wait question

Hi All, I have a program for class that needs to do the following: 1. Print the directory entries from the current directory using ncurses 2. Provide a prompt next to each directory entry and allow the user to enter commands that may or may not be about the file 3. Execute those commands in... (1 Reply)
Discussion started by: afulldevnull
1 Replies

3. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

4. Programming

multiple fork() question

I writing a program that forks three times but only on the parent process. The three children processes then produces output in order. 1, 2, 3. I am confused on how to do this. I have tried multiple if and else if statements but the output does not come out right. How should I go about doing this? (1 Reply)
Discussion started by: TWhitt24
1 Replies

5. Programming

Beginners question about fork

Hi everyone: I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an... (4 Replies)
Discussion started by: edgarvm
4 Replies

6. Programming

Question About Multi-Processed Applications... fork()

Assume we have an application built on *nix that uses fork()...then the processes procedure is going to act as follow: X is considered a parent process (first click on application) Y is considered a child process of X (second click on application) Z is considered a child process of Y (third... (6 Replies)
Discussion started by: f.ben.isaac
6 Replies

7. Programming

A small question about fork()

Hello experts, I am using fork() in my code but I am confused which output comes first child or parent? I did the following code .My book shows parent first but my linux shows child first.Can anyone tell me why? #include <stdio.h> int main(){ int pid; printf("I am original process with pid... (5 Replies)
Discussion started by: mlhazan
5 Replies

8. Programming

fork() help

Hi everybody, I wanna write a code to understand how fork works. my target -------------- -Parent creates a file(called temp) and writes into this file "1".Then it closes the file. -Then parent creates a child and wait until execution of this child ends. -Then child opens the same... (3 Replies)
Discussion started by: alexicopax
3 Replies

9. Programming

Fork or what?

Hello all. I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
Discussion started by: crippe
0 Replies

10. Programming

simple fork question

When executing this simple program: #include <unistd.h> void main() { int f; printf("\n Parent procces ID=%d\n",getpid()); f=fork(); if(f==0) { printf("\n Child process ID=%d father=%d\n",getpid(),getppid()); } ... (2 Replies)
Discussion started by: bb666
2 Replies
Login or Register to Ask a Question