fork function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers fork function
# 1  
Old 01-27-2004
Error fork function

hi everyone,
have this little piece of code in order to help you understand my question
Code:
#include<stdio.h>
#include<unistd.h>

int main()
{
	int i, pid;
	pid = fork();

	if(pid<0)
	{
		fprintf(stderr, "fork failed\n");
		_exit(1);
	}
	if(pid==0)
	{
		printf("in Child\n");
		_exit(0);
	}
	printf("in parent\n");
}

/** my question is: according to the definition of fork, it creates an
exact copy of original process. what is the original process here?
is it the whole code i.e. the program where this code is written
i named it as one.c, so is one.c the original process? orrrr
when i do pid = fork(), thats the original process created in one.c?
also does the child process exists only in the if loops if child
succeeds?
about fork() function, how does it generates a number? is it like
generating a random number? i just can't figure out, whats my parent process here and whats my child process here, i m confused with the two.
if fork is created when i do pid = fork(), isn't that the
parent process created? and in order to create a child process
am i not supposed to do pid = fork() again!? and what do the parent and child process share in common and what are their differences. i m aware that fork is done for synchronization but m having trouble with the basic */
appreciate your comments.

code tags added for readability --oombera

Last edited by oombera; 04-12-2004 at 01:18 AM..
# 2  
Old 01-27-2004
Since your code is running, it is already a process. Think about this program:

int main()
{
exit(0);
}

This is perfectly valid program. Note that it did not need to fork. But the shell had to fork to run it.

When you execute a line of code like:

pid=fork();

A complete copy of the process springs into existence. Both copies store something in pid. And then both copies continue with the next statement.

But they store a different value in pid. That's how the programmer can make parent and child do something different.

The child will store 0 in pid.

The parent will store the pid of the child in pid.

fork() is a system call. This is, it asks the kernel to do something. The kernel must pick a pid for the child. The algorithm is a little complex, but you don't need to know the gory details at this point. But you can be sure that it is not exactly random and it will pick a pid not currently in use.
# 3  
Old 01-27-2004
hi there,
for instance you had it like this

int main()
{int pid = fork();
exit(0);
}

so the above code is parent process? lets assume that the fork succeeds.
u said, a complete copy of the process springs into existence. does
that mean that the code
int main() { int pid = fork(); exit(0); } will be regenerated ? if
so, then this will be always recurring or recrusive. this child process
will keep generating different child processes! n this will never
come to an end? will it!?

then u said "both copies store somethin in pid". we just did
fork once, why are u saying both copies? i dont' quite get that!

then u said
The child will store 0 in pid.

The parent will store the pid of the child in pid.

but the code that i had has got just one pid! hence the call
pid = fork() would store the pid of child. and child would store
0 in pid, but wouldn't the child have the same code as parent since
the child process is a copy of a process, so the child would store
another childs pid! when would the child store 0 in pid!

what i m tryin to say is if an exact copy of the process is created,
how would that come to an end, since the child will do fork as well
as it has the same code involved as in its parents!

i know i m not right, but i don't quite get this here. i may sound
stupid but i am probably missing something.
Perderabo, can you give me more insight on that!
Thanks
# 4  
Old 01-27-2004
Let's take:

int main()
{int pid = fork();
exit(0);
}

as an example. Let's say that it is compiled and is sitting on disk as a file called forktest.

And let's say that forktest starts to run. This means that it must have a pid, let's say that it is pid 25021.

Well, pid 25021 will quickly invoke fork(). This means that the kernel must create a second process. The second process will be the child of process 25021. Let's say that the second process gets a pid of 25079. So now we have two processes: 25021 which is the original and 25079 which is the child.

Now both processes can run. If the system has two cpu's, they can literally run at the same time. But neither process restarts from the beginning. They both continue from where process 25021 was.

Both processes seem to be returning from fork. And both must store a value in pid. Now they do not share a common variable called pid. Each process has a private copy. The variable called pid in process 25021 gets a value of 25079. The variable called pid in process 25079 gets a zero.

Next both processes continue with the next statement. So both processes now exit.

The end.

Now the situation that you are worrying about can occur. And it is a nightmare. But it requires a bug like:

while(1) fork();

Here both processes are in an infinite loop. So both will fork again resulting in 4 processes. Then all 4 processes will fork again. Then all 8 processes will fork again. And so it goes until fork fails. So please don't do that.
# 5  
Old 02-01-2004
Computer

thanks perderabo
i got it Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. AIX

Fork Function Failed on 4GB ?

Hello, I am running Oracle Database and after a while I keep getting this message whenever I execute any command. I cannot execute any command even shutdown, whenever I execute any command , I get this message /usr/bin/ksh: 0403-031 The fork function failed. There is not enough memory... (7 Replies)
Discussion started by: filosophizer
7 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies

10. Programming

how to use function fork() in Windows NT

Hello, I need to make a gateway from Ethernet to RS-485. I am using stream socket, and I am programming in windows nt. I would like to know how could I use some functions from unix in windows nt. I would like to use the function fork(). Which library it uses and how can I get it? Can I... (1 Reply)
Discussion started by: danieljorge
1 Replies
Login or Register to Ask a Question