|
This article is about "forking" a process in a multitasking or multithreading operating system. For other uses, see Fork (disambiguation).
In computing, when a process forks, it creates a copy of itself, which is called a "child process." The original process is then called the "parent process". More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread. Under Unix and Unix-like operating systems, the parent and the child processes can tell each other apart by examining the return value of the fork() system call. In the child process, the return value of fork() is 0, whereas the return value in the parent process is the PID of the newly-created child process. The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process, though if copy-on-write semantics are implemented actual physical memory may not be assigned (i.e., both processes may share the same physical memory segments for a while). Both the parent and child processes possess the same code segments, but execute independently of each other.
Importance of Fork In UnixForking is an important part of Unix, critical to the support of its design philosophy, which encourages the development of filters. In Unix, a filter is a small program that reads its input from stdin, and writes its output to stdout. A pipeline of these commands can be strung together by a shell to create new commands. For example, one can string together the output of the
$ find . -name "*.cpp" -print | wc -l
In order to accomplish this, the shell forks itself, and uses pipes, a form of interprocess communication, to tie the output of the More generally, forking is also performed by the shell each time a user issues a command. A child process is created by forking the shell, and the child process is overlaid, once again by Address Space CopyWhat does the "copy of parent process's address space" mean? Let us try to examine the internals of a process. Whenever an executable file is executed, it becomes a process. An executable file contains binary code grouped into a number of blocks called segments. Each segment is used for storing a particular type of data. A few segment names of a typical ELF executable file are listed below.
If you want further analysis of an ELF file you can use the "readelf" command. When such a file is loaded in the memory for execution, the segments are loaded in memory. It is not necessary for the entire executable to be loaded in contiguous memory locations. Memory is divided into equal sized partitions called pages (typically 4KB). Hence when the executable is loaded in the memory, different parts of the executable are placed in different pages (which might not be contiguous). Consider an ELF executable file of size 10K. If the page size supported by the OS is 4K, then the file will be split into three pieces (also called frames) of size 4K, 4K, and 2K respectively. These three frames will be accommodated in any three free pages in memory. Fork and page sharingWhen a In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork is done, the parent process's pages are not copied for the child process. Instead, the pages are shared between the child and the parent process. Whenever a process (parent or child) modifies a page, a separate copy of that particular page alone is made for that process (parent or child) which performed the modification. This process will then use the newly copied page rather than the shared one in all future references. The other process (the one which did not modify the shared page) continues to use the shared version of the page. This technique is called copy-on-write since the page is copied when some process writes to it. Vfork and page sharing
Application usageOn some systems, One example of the unique use of The The use of
The The If signal handlers are invoked in the child process after ExampleBelow is some sample C code to illustrate the idea of forking. The code that is in the "Child process" and "Parent process" sections is executed simultaneously in two different processes. #include <stdio.h> /* printf, stderr, fprintf */ #include <unistd.h> /* _exit, fork */ #include <stdlib.h> /* exit */ #include <errno.h> /* errno */ int main(void) { pid_t pid = fork(); if (pid == 0) { /* Child process: * When fork() returns 0, we are in * the child process. * Here we count up to ten, one each second. */ int j; for (j = 0; j < 10; j++) { printf("child: %d\n", j); sleep(1); } _exit(0); /* Note that we do not use exit() */ } else if (pid > 0) { /* Parent process: * When fork() returns a positive number, we are in the parent process * (the fork return value is the PID of the newly-created child process). * Again we count up to ten. */ int i; for (i = 0; i < 10; i++) { printf("parent: %d\n", i); sleep(1); } exit(0); } else { /* Error: * When fork() returns a negative number, an error happened * (for example, number of processes reached the limit). */ fprintf(stderr, "can't fork, error %d\n", errno); exit(1); } } This code will print out something similar to the following: parent: 0 child: 0 child: 1 parent: 1 parent: 2 child: 2 child: 3 parent: 3 parent: 4 child: 4 child: 5 parent: 5 parent: 6 child: 6 child: 7 parent: 7 parent: 8 child: 8 child: 9 parent: 9 While the printed count from each process steadily increases from 0 to 9, the output of the two processes are intertwined according to the order in which the kernel schedules them to execute. See alsoReferences
External links
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net