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
VFORK(3P)                                                    POSIX Programmer's Manual                                                   VFORK(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
vfork - create a new process; share virtual memory SYNOPSIS
#include <unistd.h> pid_t vfork(void); DESCRIPTION
The vfork() function shall be equivalent to fork(), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions. RETURN VALUE
Upon successful completion, vfork() shall return 0 to the child process and return the process ID of the child process to the parent process. Otherwise, -1 shall be returned to the parent, no child process shall be created, and errno shall be set to indicate the error. ERRORS
The vfork() function shall fail if: EAGAIN The system-wide limit on the total number of processes under execution would be exceeded, or the system-imposed limit 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. The following sections are informative. EXAMPLES
None. APPLICATION USAGE
Conforming applications are recommended not to depend on vfork(), but to use fork() instead. The vfork() function may be withdrawn in a future version. On some implementations, vfork() is equivalent to fork(). The vfork() function differs from fork() only in that the child process can share code and data with the calling process (parent process). This speeds cloning activity significantly at a risk to the integrity of the parent process if vfork() is misused. The use of vfork() for any purpose except as a prelude to an immediate call to a function from the exec family, or to _exit(), is not advised. The vfork() function can be used to create new processes without fully copying the address space of the old process. If a forked process is simply going to call exec, the data space copied from the parent to the child by fork() is not used. This is particularly inefficient in a paged environment, making vfork() particularly useful. Depending upon the size of the parent's data space, vfork() can give a significant performance improvement over fork(). The vfork() function can normally be used just like fork(). It does not work, however, to return while running in the child's context from the caller of vfork() since the eventual return from vfork() would then return to a no longer existent stack frame. Care should be taken, also, to call _exit() rather than exit() if exec cannot be used, since exit() flushes and closes standard I/O channels, thereby damaging the parent process' standard I/O data structures. (Even with fork(), it is wrong to call exit(), since buffered data would then be flushed twice.) If signal handlers are invoked in the child process after vfork(), they must follow the same rules as other code in the child process. RATIONALE
None. FUTURE DIRECTIONS
This function may be withdrawn in a future version. SEE ALSO
exec(), exit(), fork(), wait(), the Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 VFORK(3P)
All times are GMT -4. The time now is 02:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy