Results 1 to 6 of 6
Hello All,
Let me preface this thread by stating that I am a new user to Linux and to programming. I am having significant difficulties understanding the fork call (in ...
- 02-28-2009 #1Just Joined!
- Join Date
- Feb 2009
- Posts
- 5
Newbie: fork() function giving me difficulties
Hello All,
Let me preface this thread by stating that I am a new user to Linux and to programming. I am having significant difficulties understanding the fork call (in C)and what it does.
What I understand is that when a fork is called, the remaining code of a program is executed twice, in two separate memory spaces.
What is throwing me for a loop is how to utilize this knowledge to my advantage. How can forks be used practically? Since I am beginning, I am working with basic arithematic functions and the manipulation of data. What possible use can forks be? Of course, I probably could better understand their application if I understood how to go about using them effectively in the first place.
My eventual goal is to learn how to get child processes to communicate with parents. If anyone could direct me to a website or a book wherein these things are clearly explained it would be much appreciated.
- 02-28-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 5
Here is an example of what I have been messing around with. It has not been very elucidating as of yet, but perhaps I could add a line or two or pithy code to enhance my own understanding:
#include <stdio.h>
#include <unistd.h>
main ()
{
int pchild, i;
pchild =fork();
sleep(15);
pchild =fork();
sleep(15);
}
- 02-28-2009 #3Just Joined!
- Join Date
- Feb 2009
- Posts
- 1
use pid
fork() generates a child process that is exactly same with the parent process except the pid returned from fork().
To make the child or the parent process to do diferent job, the pid can be used as a distinguisher.
- 02-28-2009 #4Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
Answer:
«fork()»s are the only mechanism in Linux, that can be used to create real processes («clone()» is the thread-equivalent). So “init” (PID 1) is the parent, grandparent, great grandparent, etc. … of each process in a Linux system. The only process creation facility in Linux is, so to speak, replication.
Originally Posted by msbachman
An illustrative example is, when you run a command on the console, by typing its name and hitting «Return». Besides other, in this context irrelevant tasks, what basically happens is:
- The shell «fork()»s itself
- Some stuff in regard to fore- and background-process handling happens (not of interest here)
- The child process «exec()»s¹ and thus becomes the process whose name you typed on the console. The shell process still runs.
Other examples are:
- A webserver creates a child process for every incoming connection. This way socket-multiplexing with «select()» or «poll()» is avoided.
- Rights management. When a process, that has to run with root privileges, needs to run with lower rights. This can be done by creating a child and letting it permanently lose its root privileges (loosing privileges is always possible). An example of this is the «ssh»-daemon. It permanently runs with run privileges, but on logon spawns a child that runs with the rights of the user that logged in. This can illustratively be seen (on a systems that harbors ssh connections) by inspecting the output of
Code:$> ps auxf
- Utilising multi CPU computers.
Good keywords are: Linux IPC child parent
Originally Posted by msbachman
Footnotes:
- In case this was not known: the «exec()» system call letʼs a process “forget” itʼs old memory and a memory image of the new binary is loaded instead (i.e. from harddisk), thus effectively “transforming” a process into another one.
- 02-28-2009 #5Just Joined!
- Join Date
- Feb 2009
- Posts
- 5
Sincere thanks to both of the above posters for their help. I googled this using the keyword interprocess communication, which for some reason eluded me previously, and almost immediately found something that clarified this for me.
It's still going to be a lot of work to master this stuff, but thanks to the two above posters I've got a better grasp on it than I did not four hours ago.
Thanks again.
- 03-05-2009 #6Just Joined!
- Join Date
- Oct 2008
- Posts
- 1
I suggest you to read the APUE2.You can get benefit from it.


Reply With Quote