# 1
09-06-2011

i'm experimenting fork function and i found this code
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..
 blob84 View Public Profile for blob84 Find all posts by blob84
# 2
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
 NH2 View Public Profile for NH2 Find all posts by NH2
# 3
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:
 shamrock View Public Profile for shamrock Find all posts by shamrock
# 4
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:
 NH2 View Public Profile for NH2 Find all posts by NH2
# 5
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.
 shamrock View Public Profile for shamrock Find all posts by shamrock
# 6
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
 NH2 View Public Profile for NH2 Find all posts by NH2
# 7
09-10-2011
thanks NH2 for explanation!
 blob84 View Public Profile for blob84 Find all posts by blob84

## 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()...

## 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...

## 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...

## 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?

## 5. Programming

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...

## 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...

## 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...

## 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...

## 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...

## 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()); } ...