ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Help! I need to learn to use fork, and can't find any good documentation on it. Where can I go? I've googled, and havn't come up with any good documentation.
Fork is an alternative to threading. One program can fork off into two programs and use some sort of IPC to handle data exchange so that two tasks can be done concurrently (or on SMP, simultaneously)
FORK(2) Linux Programmer’s Manual FORK(2)
NAME
fork - create a child process
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
DESCRIPTION
fork creates a child process that differs from the parent process only
in its PID and PPID, and in the fact that resource utilizations are set
to 0. File locks and pending signals are not inherited.
Under Linux, fork is implemented using copy-on-write pages, so the only
penalty incurred by fork is the time and memory required to duplicate
the parent’s page tables, and to create a unique task structure for the
child.
RETURN VALUE
On success, the PID of the child process is returned in the parent’s
thread of execution, and a 0 is returned in the child’s thread of exe-
cution. On failure, a -1 will be returned in the parent’s context, no
child process will be created, and errno will be set appropriately.
ERRORS
EAGAIN fork cannot allocate sufficient memory to copy the parent’s page
tables and allocate a task structure for the child.
ENOMEM fork failed to allocate the necessary kernel structures because
memory is tight.
CONFORMING TO
The fork call conforms to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.
SEE ALSO
clone(2), execve(2), vfork(2), wait(2)
Linux 1.2.9 1995-06-10 FORK(2)
Typical 30 second example:
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main ()
{
int pid;
printf ("Forking!\n");
if ((pid = fork()) == 0) {
printf ("I am the child process!\n");
} else {
printf ("I am the parent process!\n");
printf ("My child is %d\n",pid);
}
printf ("I am process %d\n",getpid());
return 0;
}
Example output:
Code:
j_shaw@jtshaw:~$ ./fork
Forking!
I am the child process!
I am process 9319
I am the parent process!
My child is 9319
I am process 9318
As you can see, one process says its the child, the other says is the parent, they both print the last printf...
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
Fork is an alternative to threading.
What is threading? I don't know much about those things. I know about the scripting and some people on this forum taught me a bit about 'sed' and 'awk'.
What is threading?
-------------------------------------------------------
Fork, together with exec, is the Unix way of executing another program.
What is ' exec' ?
I know UNIX fairly well.
----------------------------------
It seems fork is for limited Linux versions. I am running Mandriva 2006 64 bit version.
[nissanka@c83-250-107-194 ~]$ man fork
No manual entry for fork
[nissanka@c83-250-107-194 ~]$
Fork should be in all versions of unix/linux. It is a basic POSIX function. I'm guessing you just don't have the developers man pages installed on your system.
Exec is a system call for executing a file. If you are writing a program that needs to launch another porgram you will typically see something like this:
Code:
if (fork() == 0) {
execv("/bin/myprogram",NULL);
}
This would exec the program /bin/myprogram with no command line arguements.
A thread can pretty much be thought of as a "light-weight" process that shares the same memory space as the process that creates it (and all other threads that process created). This allows you to easily share information across your threads while having each do its own thing with it.
Threads enable you to have 'lightweight process' within a parent process. On 2.6 kernel you only see 1 process, on 2.4 you see a process for each thr.
Here's the Perl tutorial: http://72.14.203.104/search?q=cache:...en&lr=&strip=1
fork() and exec() are linux system calls... The basic purpose of these calls are to create a new process... Like if we have a program which is executing and we want to create run a child that will run in parallel with this, what we will do ... We will make a fork() call and this system call will make a copy of parent process... Now we have 2 process with same binary, now we call the exec() that will overwrite the parents binary with your requested stuff....
It seems fork is for limited Linux versions. I am running Mandriva 2006 64 bit version.
[nissanka@c83-250-107-194 ~]$ man fork
No manual entry for fork
[nissanka@c83-250-107-194 ~]$
To GINs:
First try:
$ info libc
Then chose: 'Processes' from the menu
Then chose: 'Create a Process' from the menu.
This is a great way to see info the C concepts and API of fork. You need the libc manual installed, and info. Fork has been around for a long time, it is just part of the C libraries the concept is repeated in many languages. I am fairly certain that all Linux versions that have c libraries WILL make references to fork in MANY utilities, but you may never notice them if you do not look into the source or program.
Quote:
A thread can pretty much be thought of as a "light-weight" process that shares the same memory space as the process that creates it (and all other threads that process created). This allows you to easily share information across your threads while having each do its own thing with it.
Yep, exactly true. Also, from my understanding, fork is similar except it copies the name space/activation records/memory, as if the child is a separate running program.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.