Sponsored Content
Full Discussion: FORK/EXEC technique
Top Forums UNIX for Dummies Questions & Answers FORK/EXEC technique Post 302103476 by Perderabo on Thursday 18th of January 2007 09:12:00 PM
Old 01-18-2007
First, as I said the reason for the fork/exec in the shell is that there is no other way. Now I think I see your question... why doesn't the kernel has a spawn_process() system call to provide an alternative to fork/exec. Since I did not design the kernel, I don't know the definitive answer. But I can speculate. First both fork() and exec() are needed. For example an ftp server must fork a copy of itself to handle each new connection. Threads now provide an alternate, but threads are new and an OS without a fork() would be missing an important capability. exec is even more fundamental and almost all OS's will have an exec capability. For example, a login program will exec a shell. Another separate spawn_process() system call would expand the kernel. Originally unix was run on systems with only a few kilobytes of memory. The added cost of a separate system call could not be tolerated. It does seem like a waste to copy an entire process and then a few instructions later invoke exec to replace it. This bothered everyone a little bit and as unix evolved, the addition of shell scripts hightened the concern since unix was forking more often. Meanwhile memory sizes were increasing. Also a version of unix was being developed at Berkeley and the Berkeley guys loved shoving stuff into the kernel. They added vfork(). With vfork, a process pretends to copy itself. The parent hangs and the child runs. When the child execs, the parent is free to run. No more copying those large data regions. vfork() tends to still be available, but fork() was souped up. These days a fork() does not copy the process. Both processes simply more forward using the same image. If either process wants to change something, that page is copied. This is called copy-on-write. Some architectures cannot handle this so they use copy-on-access instead which is almost as good. And neither process stalls as with vfork(). So fork() can now out-perform vfork() in many cases. This eliminates most of the overhead you may be perceiving with fork/exec.

Again these days, most stuff will be shared by the two processes so very little new stuff pops into existence at fork time. exec overlays these structures that define the process. But you may be surprised here. Suppose that we have a case of a "login" doing a fork/exec for ksh. There probably is already a ksh running somewhere on the system. We simply point to its text segment. If this is the first ksh, the structures that define the process are empty. As it runs, the pages it needs will not be there. So a page fault will occur and the page will be loaded. So ksh will page fault its way into core and only the parts of the program actually used will come in. In each case, ksh will probably be using shared libraries which are already in core. So not that much happens during an exec() either. Entire processes are never copied nor loaded. Both fork and exec are very fast now.

To be complete, Linux has clone() and clone2() system calls which are a bit like vfork() was. But these clone calls are intended for internal use only and then only to implement threads.
 

10 More Discussions You Might Find Interesting

1. Programming

Fork and exec

Hello! I am working on a server where I should have 4 (resident)processes, one of them being "the father" of the others, so I do 3 forks. The problem that I have is that I do an accept (for sockets) in the "father" process and I want to transmit the job to one of the processes "child" with... (3 Replies)
Discussion started by: driki
3 Replies

2. Programming

fork/exec clobbers write. I need ideas why...

On my *nix box, I have a telegram program. When I go like tel person "la\nla\nla\n" the person sees "la\nla\nla\n" However, when I have a program that forks and execs tel like: pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } if (pid == 0) {... (7 Replies)
Discussion started by: frequency8
7 Replies

3. Solaris

fork and exec ftp

Hi, I need to find/implement an application that FTPs (puts) all new files in a certain directory to an external storage unit. This application should check for new files every 10 seconds (leaving the FTP connection open in between the 10 seconds). The easiest way would be if there are... (2 Replies)
Discussion started by: KittyJ
2 Replies

4. Shell Programming and Scripting

fork and exec

I need to ssh to a remote server and run my script there. This is my script. $ssh = "ssh username@host"; $cmd = "$ssh 'cd <my dir> && < sudo Run_exe>'"; my $pid = fork; if ($pid == 0){ exec $cmd; } When I run this I get: pccons_getchar: got r == 0 (1 Reply)
Discussion started by: looza
1 Replies

5. Programming

How forbid use fork() in exec() program.

Hello World! I am writing code in C++ which have to launch another application X using exec(). I would like to set some limits on it using setrlimit etc... My problem is that i don't know how to forbid using fork() and strlimit by application X. How can i do it? (3 Replies)
Discussion started by: kzi
3 Replies

6. Programming

Fork and then exec problem with signals

Hi All, In my program i am handling SIGHUP signal. In the handler i fork and then exec on child process same binary file which is running. Parent process will die after 10 mins. Now my child process which was exec with same binary file is not receiving SIGHUP signal. Below is the progran code:... (6 Replies)
Discussion started by: sushil_shalin
6 Replies

7. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

8. UNIX for Dummies Questions & Answers

fork with exec

What is is difference between 'fork with exec' and 'fork without exec'? How both are related? (1 Reply)
Discussion started by: kkalyan
1 Replies

9. Linux

Best Compression technique ?

Hi all, I am working on a sample backup code, where i read the files per 7200 bytes and send it to server. Before sending to server, i compress each 7200 bytes using zlib compression algorithm using dictionary max length of 1.5 MB . I find zlib is slow. Can anyone recommend me a... (3 Replies)
Discussion started by: selvarajvss
3 Replies

10. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies
FORK(2) 							System Calls Manual							   FORK(2)

NAME
fork - create a new process SYNOPSIS
pid = fork() int pid; DESCRIPTION
Fork causes creation of a new process. The new process (child process) is an exact copy of the calling process except for the following: The child process has a unique process ID. The child process has a different parent process ID (i.e., the process ID of the parent process). The child process has its own copy of the parent's descriptors. These descriptors reference the same underlying objects, so that, for instance, file pointers in file objects are shared between the child and the parent, so that an lseek(2) on a descriptor in the child process can affect a subsequent read or write by the parent. This descriptor copying is also used by the shell to establish standard input and output for newly created processes as well as to set up pipes. The child processes resource utilizations are set to 0; see setrlimit(2). RETURN VALUE
Upon successful completion, fork returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set to indicate the error. ERRORS
Fork will fail and no child process will be created if one or more of the following are true: [EAGAIN] The system-imposed limit on the total number of processes under execution would be exceeded. This limit is configuration- dependent. [EAGAIN] The system-imposed limit MAXUPRC (<sys/param.h>) on the total number of processes under execution by a single user would be exceeded. [ENOMEM] There is insufficient swap space for the new process. SEE ALSO
execve(2), wait(2) 3rd Berkeley Distribution May 22, 1986 FORK(2)
All times are GMT -4. The time now is 08:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy