FOrk and processes, how does it work

Hi.

I do know the concept of process, but I do not know how it relates with fork();

Suppose I write a c program with a statement printf.

If I use fork() in this c program

a) what does fork do?
b) Does it call .exe program as process or the printf as process?
c) How does fork know to identify the process as parent and child?
d) Does it make any difference whether you use fork() before or after the printf statement?

Please explain

Thanks
a) man fork
b) no
c) don't understand your question, please rephrase it
d) try it
Fork() creates a copy of the running process. The original process that calls fork() is the parent, and the new process is the child.

At the point where fork() returns, both processes resume running at that point. The prudent thing to do is to look at the return code from fork. If it is zero, that process is the child. If not, the return code is the process id of the child. The parent can use this value to its advantage if it wants to. Here is a link that NE555 perhaps hopes you could have found on your own:

http://en.wikipedia.org/wiki/Fork-exec
What exactly is this process? How fork knows that printf(....) after fork() is a process?

Thanks
The fork() call creates a copy of the invoking process. If fork() returns OK, there are two processes running. As I said, the return code from fork() is zero for the new (child) copy; while the parent (invoking process) is the process id of the new (child) process.

Fork() itself doesn't "know" that printf() is a process, all of the code from after the return of fork() is part of both processes. Both process continue running until they end.

Your operating system ultimately is in charge of creating the new processes -- the fork() call is the programmer's interface into that mechanism.


thanks
Topic archived. No new replies allowed.