Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Fork() and Exec()

4 posters

Go down

Fork() and Exec() Empty Fork() and Exec()

Post by Vineet_More Mon Mar 15, 2010 1:08 pm

Can anyone explain the proper difference between the fork and exec command?

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

Back to top Go down

Fork() and Exec() Empty Re: Fork() and Exec()

Post by Maithreyi Mon Mar 15, 2010 2:24 pm

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()

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Fork() and Exec() Empty Re: Fork() and Exec()

Post by Vineet_More Mon Mar 15, 2010 3:25 pm

Thank you !!

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

Back to top Go down

Fork() and Exec() Empty Re: Fork() and Exec()

Post by jeeva Wed Mar 17, 2010 12:06 pm

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

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

Back to top Go down

Fork() and Exec() Empty Re: Fork() and Exec()

Post by Mekala Wed Mar 17, 2010 12:46 pm

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

Back to top Go down

Fork() and Exec() Empty Sample program to demonstrate fork() and exec()

Post by Maithreyi Thu Mar 18, 2010 11:38 am

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

Back to top Go down

Fork() and Exec() Empty Re: Fork() and Exec()

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum