FORK/EXEC technique


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers FORK/EXEC technique
# 1  
Old 01-18-2007
FORK/EXEC technique

Hi!

Can someone explain me exactly this technique? Why a process (PARENT) creates a copy of itself with FORK (CHILD)? What's the reason of this behaviour? Sorry, but I cannot understand the logic behind it.

Thanks.
# 2  
Old 01-18-2007
I don't really understand this question. But let's say you have a process running that is a shell. A user has entered the command "date". So no wthe shell would like to run the separate program called "date" and display the results to the user. So the shell will fork and exec the date program. Why? Because that is the only game in town. There is no other way for it to run another program. Either it can fork/exec and thus run the program or it can tell the user "sorry I don't know how to run programs".
# 3  
Old 01-18-2007
thank you for your answer but i'm sorry, i still dont understand this concept.
if I call on the shell the program 'date', you say that the shell, parent process (right?) will fork itself (?) creating a child shell that will execute the date command...now what I don't understand clearly is why not executing directly without forking, so without creating a copy (child) of itself...
hope i made it clearer now and sorry...it might seem a stupid question or not very clearly formulated, but i am new to unix.
thanks.
# 4  
Old 01-18-2007
Shell is a different process,
and the new process that the shell should run is an entirely different one.

As process entities the shell and the new process that should run are different entities.

So to run a process, we need process space and the process binary for it to go.
Hence, the parent process shell creates the process space (fork) and in the new process space, the requested process which should run which is done by exec - overlaying the image of the binary that is to be run.


Any mistakes/modifications please correct them ! Smilie
# 5  
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question