| advertise add site services publishers database health videos | ![]() | about toolbar stats live show health store more stuff JOIN/LOGIN |
Operating Scissors, Operating Scissors Manufacturer, Operating Scissors... themedica.com | Operating Table|China Operating Table|Operating Table Products... industry-medical.com | Operating Room Instruments - Operating Room Equipment - Operating Room... allegromedical.com |
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. 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.
[edit] Importance of forking 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 (usually 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 [edit] Process Address SpaceWhenever 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.
The [edit] Fork and page sharingWhen a In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork occurs, 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. [edit] Vfork and page sharing
On some systems, The The use of
The The If signal handlers are invoked in the child process after [edit] Application usage[edit] Example in C#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; /* Output from both the child and the parent process * will be written to the standard output, * as they both run at the same time. */ 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); } } [edit] Example in Perl#!/usr/bin/perl $pid = fork(); #Declare fork if ($pid == 0) { #Jump into the Child process for ($j = 0; $j < 10; $j++) { print "child: $j\n"; sleep 1; } exit(0); #Exit the fork [child process] } elsif ($pid > 0) { for ($i = 0; $i < 10; $i++) { print "parent: $i\n"; sleep 1; } exit(0); #Exit parent } [edit] Example in Python#!/usr/bin/python # -*- coding: utf-8 -*- import os, time def createDaemon(): """ This function create a service/Daemon that will execute a det. task """ try: # Store the Fork PID pid = os.fork() if pid > 0: print 'PID: %d' % pid os._exit(0) except OSError, error: print 'A criacao do fork falhou. Erro: %d (%s)' % (error.errno, error.strerror) os._exit(1) doTask() def doTask(): """ This function create a task the will be a daemon """ # Open the file in write mode arquivo = open('/tmp/tarefa.log', 'w') # Start the write while True: print >> arquivo, time.ctime() arquivo.flush() time.sleep(2) # Close the file arquivo.close() if __name__ == '__main__': # Create the Daemon createDaemon() [edit] See also[edit] References
[edit] External links
|
| ↑ top of page ↑ | about thumbshots |