These are the first two results when I search "fork vs exec" in google.<br><br><br>
<a href="http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html" target="_blank">http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html</a><br>
<a href="http://en.wikipedia.org/wiki/Fork-exec" target="_blank">http://en.wikipedia.org/wiki/Fork-exec</a><br><br>I found very good explanations in both the links(yolinux is a bit better)<br><br>-Sri<br><br><div class="gmail_quote">
On Tue, Feb 7, 2012 at 12:38 AM, Vijay Chauhan <span dir="ltr"><<a href="mailto:kernel.vijay@gmail.com">kernel.vijay@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi List,<br>
<br>
I am learning Linux and trying to understand exec and fork function.<br>
execl says that it overlays the running address space. What does it mean?<br>
<br>
I created the following program and used top command with<br>
intentionally wrong arguments:<br>
<br>
#include<stdio.h><br>
#include<unistd.h><br>
#include<sys/types.h><br>
#include<stdlib.h><br>
<br>
int main(){<br>
int a = -1;<br>
if(fork()==0){<br>
printf("Inside child\n");<br>
printf("child pid=%d, parentid=%d\n", getpid(), getppid());<br>
execl("/usr/bin/top", "/usr/bin/top", ">/dev/null" ,(char*)0 );<br>
scanf("inside child provide a %d", &a);<br>
printf("Inside child a=%d\n", a);<br>
exit(1);<br>
} else {<br>
printf("Inside parent, going to wait\n");<br>
printf("my pid=%d, parentid=%d\n", getpid(), getppid());<br>
scanf("input parent %d\n", &a);<br>
wait(NULL);<br>
printf("Wait over\n");<br>
printf("Inside parent a=%d\n", a);<br>
}<br>
return 0;<br>
}<br>
<br>
When i run this program, it gives following output:<br>
[vijay@localhost]$ ./a.out<br>
Inside parent, going to wait<br>
Inside child<br>
child pid=2775, parentid=2774<br>
my pid=2774, parentid=2681<br>
top: unknown argument '>'<br>
usage: top -hv | -bcisSHM -d delay -n iterations [-u user | -U user]<br>
-p pid [,pid ...]<br>
<br>
10<br>
Wait over<br>
Inside parent a=-1<br>
[vijay@localhost]$<br>
<br>
Why the child scanf and printf not executed?<br>
In the parent program, i expected input parent should be printed. But<br>
it doesnt and just wait for some input. When i entered 10 it resumes<br>
but printing -1 as a value.<br>
<br>
Could anyone please help me to understand this behavior? or any good<br>
tutorial or book<br>
<br>
Thanks.<br>
Vijay<br>
<br>
_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>SK Malik<br>