Why is fork used




















In this tutorial, we will talk about the fork function and then implement some examples in the C programming language. According to Wikipedia , a process is the instance of a computer program that is being executed by one or many threads.

It contains the program code and its activity. Depending on the operating system OS , a process may be made up of multiple threads of execution that execute instructions concurrently.

In the computing field, fork is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent. When the parent process closes or crashes for some reason, it also kills the child process. Image Source. The operating system is using a unique id for every process to keep track of all processes. If you are running Windows, then I recommend you to use Cygwin.

Simply, we can tell that the result is 2 power of n, where n is the number of fork system calls. Why do we create a copy of the parent process in the first place and not create a new process directly? The short answer is, fork is in Unix because it was easy to fit into the existing system at the time, and because a predecessor system at Berkeley had used the concept of forks. Process control in its modern form was designed and implemented within a couple of days.

It is astonishing how easily it fitted into the existing system; at the same time it is easy to see how some of the slightly unusual features of the design are present precisely because they represented small, easily-coded changes to what existed. A good example is the separation of the fork and exec functions. The most common model for the creation of new processes involves specifying a program for the process to execute; in Unix, a forked process continues to run the same program as its parent until it performs an explicit exec.

The separation of the functions is certainly not unique to Unix, and in fact it was present in the Berkeley time-sharing system, which was well-known to Thompson. Still, it seems reasonable to suppose that it exists in Unix mainly because of the ease with which fork could be implemented without changing much else. The system already handled multiple i. The initial implementation of fork required only.

In fact, the PDP-7's fork call required precisely 27 lines of assembly code. Of course, other changes in the operating system and user programs were required, and some of them were rather interesting and unexpected. But a combined fork-exec would have been considerably more complicated , if only because exec as such did not exist; its function was already performed, using explicit IO, by the shell.

Since that paper, Unix has evolved. After doing a vfork, the parent and child processes share the same data space, and the parent process is suspended until the child process either execs a program or exits. It takes a bunch of parameters that let you selectively share the caller's open files and copy its signal disposition and other attributes to the new process.

Why not just have a command that creates a new process from scratch? Isn't it absurd and inefficient to copy one that is only going to be replaced right away? The "copy" produced by fork is a bit of an abstraction, since the kernel uses a copy-on-write system ; all that really has to be created is a virtual memory map.

Various significant aspects of the child process e. They're just assumed to be the same as that of the calling process, and this is the fairly intuitive system we are familiar with. To explain 1 a little further, memory which is "copied" but never subsequently accessed is never really copied, at least in most cases. An exception in this context might be if you forked a process, then had the parent process exit before the child replaced itself with exec.

I say might because much of the parent could be cached if there is sufficient free memory, and I am not sure to what extent this would be exploited which would depend on the OS implementation. Of course, that doesn't on the surface make using a copy more efficient than using a blank slate -- except "the blank slate" is not literally nothing, and must involve allocation. So 1 just demonstrates that using a "new" empty process would not be more efficient.

Point 2 does explain why using the fork is likely more efficient. A child's environment is inherited from its parent, even if it is a completely different executable. The one in the child is produced by the original fork. A strategy that may not make much literal sense, but my point is that creating a process involves more than copying it's image into memory from disk.

I think the reason Unix had only the fork function to create new processes is a result of the Unix philosophy. What one does with the new process is then up to the programmer.

There are two philosophies of process creation: fork with inheritance, and create with arguments. Imagine trying to eat soup out of that spoon. The Rise of the Fork. See the rest of the objects in our series on the evolution of design. Once the fork became a daily staple, it, like so many other household objects of the 20th century, was pressed into the service of style.

Early 20th-century designers like Henry van der Velde , Charles Mackintosh , and Josef Hoffman , with the aim of producing a Gesamtkunstwerk total work of art , designed forks—along with windows, chairs, and lamps—for their buildings.

There were slinky Italian forks in the s , colorful Bakelite forks in the s, architect-designed forks with three tines in the s and five tines in the s , neon plastic forks in the s, postmodern forks in the s, and, in the s, sci-fi forks and quirky forks. Even artists like Alexander Calder jumped on the bandwagon. The variety of both shapes and styles leads not only to etiquette confusion, but to other problems as well.

I mean, of course, a complete service, so as not to cut a sorry figure when the duchess comes to dinner. They are made of natural wood, like two giant toothpicks ten inches long, and in Japan you can buy them in packets of a hundred in any big store.

They are easy to use, and the food is cut up beforehand into mouthful-sized pieces. Millions of people have been using them for thousands of years! But not us! Far too simple! This vein of fork criticism—comparing them unfavorably with the chopstick—is a long-standing one. De Tott used his description of table manners to distinguish himself from the Turkish, to point out that even when they tried to use a fork, they couldn't get it right: They didn't belong to his group.

Munari and Barthes, on the other hand, use their descriptions of table manners to distinguish themselves from their own cultures, praising the chopstick as a way of pointing out—whether humorously or not—their own individual superiority from the frivolous, fork-using culture they happened to be born into.

Manners are always a way to negotiate social groups. Learning their nuances is a way to ingratiate yourself with the group, not knowing them is frequently a path to anxiety, and refusing to employ them is a way of insisting on one's own individuality. And this is particularly true with forks, which are old enough that we who use them accept them totally, but which are also new enough that it is easy to get them wrong.

Perhaps, then, it is interesting to consider the future of utensils. At his restaurant Alinea, in Chicago, chef Grant Achatz has worked closely with designer Martin Kastner to devise new utensils: the antenna , which supports a single morsel of mackerel; the bow , which allows a transparent slice of bacon to hang freely; and more.

Does this seem a bit silly? Is it genuinely geared to making the food more delicious, and more interesting, to eat? Also yes. And before you imagine yourself incapable, or at least undesiring, of leaning forward, hands-free, to snatch a morsel of food off a thin, wavering stalk of metal with your mouth, just remember that the humble fork used to appear every bit as silly.

Correction, June 21, The final photo caption on this article misidentified a utensil design as the Squid. The photo depicts a Peacock design. While buffering the output for the parent process, the child may also use printf to print out some information, which will also be buffered. As a result, since the output will not be send to screen immediately, you may not get the right order of the expected result.

Worse, the output from the two processes may be mixed in strange ways. To overcome this problem, you may consider to use the "unbuffered" write. Process ID may be the one assigned to the parent or the child. Due to the fact that these processes are run concurrently, their output lines are intermixed in a rather unpredictable way.

Moreover, the order of these lines are determined by the CPU scheduler. Hence, if you run this program again, you may get a totally different result.

Even if you do want your program to start another program to do some task, there are other simpler interfaces which use fork behind the scenes, such as "system" in C and perl.

For example, if you wanted your application to launch another program such as bc to do some calculation for you, you might use 'system' to run it. System does a 'fork' to create a new process, then an 'exec' to turn that process into bc. Once bc completes, system returns control to your program.

If you are writing servers, shells, viruses or operating systems, you are more likely to want to use fork.

Multiprocessing is central to computing. For example, your IE or Firefox can create a process to download a file for you while you are still browsing the internet. Or, while you are printing out a document in a word processor, you can still look at different pages and still do some editing with it. Here is my code that creates processes in the form of binary tree It will ask to scan the number of levels upto which you want to create processes in binary tree.

Fork system call copies the memory on the copy-on-write basis, means child makes in virtual memory page when there is requirement of copying. Typically it's used in similar sorts of situations as threading, but there are differences. Unlike threads, fork creates whole seperate processes, which means that the child and the parent while they are direct copies of each other at the point that fork is called, they are completely seperate, neither can access the other's memory space without going to the normal troubles you go to access another program's memory.

There are some other usecases still, but mostly people have moved to multithreading now. The rationale behind fork versus just having an exec function to initiate a new process is explained in an answer to a similar question on the unix stack exchange. Essentially, since fork copies the current process, all of the various possible options for a process are established by default, so the programmer does not have supply them.

In the Windows operating system, by contrast, programmers have to use the CreateProcess function which is MUCH more complicated and requires populating a multifarious structure to define the parameters of the new process. Fork system call use to create a child process. It is exact duplicate of parent process. Fork copies stack section, heap section, data section, environment variable, command line arguments from parent. The fork function is used to create a new process by duplicating the existing process from which it is called.

The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the purpose of fork? Ask Question. Asked 12 years, 5 months ago.

Active 2 years, 5 months ago. Viewed 64k times. Improve this question.



0コメント

  • 1000 / 1000