Fork() and Exec()
4 posters
Fork() and Exec()
Can anyone explain the proper difference between the fork and exec command?
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: Fork() and Exec()
Fork() creates a new process on execution.
exec() does not create a new process.In fork, the process id of the process remains constant unlike in the case of fork()
exec() does not create a new process.In fork, the process id of the process remains constant unlike in the case of fork()
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: Fork() and Exec()
The exec system calls reinitialize a process from a
designated program the program changes while the process
remains
i found this somewhere.... can anyone explain exec in a better way!!!
designated program the program changes while the process
remains
i found this somewhere.... can anyone explain exec in a better way!!!
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: Fork() and Exec()
In fork fn, a new process is created and it occupies a different memory location whereas in exec, when the new process is created the parent process will not get the ctrl again since its memory space is overwritten by new process...this is the benefit from we get while using exec.
Mekala- Posts : 7
Points : 12
Join date : 2010-03-04
Sample program to demonstrate fork() and exec()
- Code:
#include<stdio.h>
#include<stdlib.h>
main()
{
int id;
printf("Parent process \n");
if(( id = fork())==0)
{
printf("ID of child process - %d\n",getpid());
printf("Statement from child process \n");
execl("/bin/date","date",0);
printf("ID of child process is - %d\n",getpid());
}
system("ps");
printf("Parent process again \n");
}
The output of this program is :
- Code:
[335818@oracleclient ~]$ ./a.out
Parent process
ID of child process - 12312
Statement from child process
Thu Mar 18 10:05:41 IST 2010
PID TTY TIME CMD
11267 pts/114 00:00:00 bash
12311 pts/114 00:00:00 a.out
12312 pts/114 00:00:00 date <defunct>
12313 pts/114 00:00:00 ps
Parent process again
From the output, the child process created on the fork() command is replaced by the date process.The line following the exec function in the child is not executed because, after the exec() is executed, the process is no more the child process, but the date process.However,if we notice the processes in existence, the child process and the date process have the same process id 12312.
Thus, a fork() creates a new process afresh (with a new process id), but exec() replaces the existing process with the new program, therefore the process id remains the same.Hence the program changes but the process remains!
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum
|
|