* fork() and exec()
@ 2012-02-06 19:08 Vijay Chauhan
2012-02-06 19:27 ` Jeff Haran
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Vijay Chauhan @ 2012-02-06 19:08 UTC (permalink / raw)
To: kernelnewbies
Hi List,
I am learning Linux and trying to understand exec and fork function.
execl says that it overlays the running address space. What does it mean?
I created the following program and used top command with
intentionally wrong arguments:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int main(){
int a = -1;
if(fork()==0){
printf("Inside child\n");
printf("child pid=%d, parentid=%d\n", getpid(), getppid());
execl("/usr/bin/top", "/usr/bin/top", ">/dev/null" ,(char*)0 );
scanf("inside child provide a %d", &a);
printf("Inside child a=%d\n", a);
exit(1);
} else {
printf("Inside parent, going to wait\n");
printf("my pid=%d, parentid=%d\n", getpid(), getppid());
scanf("input parent %d\n", &a);
wait(NULL);
printf("Wait over\n");
printf("Inside parent a=%d\n", a);
}
return 0;
}
When i run this program, it gives following output:
[vijay at localhost]$ ./a.out
Inside parent, going to wait
Inside child
child pid=2775, parentid=2774
my pid=2774, parentid=2681
top: unknown argument '>'
usage: top -hv | -bcisSHM -d delay -n iterations [-u user | -U user]
-p pid [,pid ...]
10
Wait over
Inside parent a=-1
[vijay at localhost]$
Why the child scanf and printf not executed?
In the parent program, i expected input parent should be printed. But
it doesnt and just wait for some input. When i entered 10 it resumes
but printing -1 as a value.
Could anyone please help me to understand this behavior? or any good
tutorial or book
Thanks.
Vijay
^ permalink raw reply [flat|nested] 5+ messages in thread
* fork() and exec()
2012-02-06 19:08 fork() and exec() Vijay Chauhan
@ 2012-02-06 19:27 ` Jeff Haran
2012-02-07 5:48 ` Srikrishan Malik(gmail)
2012-02-07 15:40 ` Bernd Petrovitsch
2 siblings, 0 replies; 5+ messages in thread
From: Jeff Haran @ 2012-02-06 19:27 UTC (permalink / raw)
To: kernelnewbies
> -----Original Message-----
> From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-
> bounces at kernelnewbies.org] On Behalf Of Vijay Chauhan
> Sent: Monday, February 06, 2012 11:08 AM
> To: kernelnewbies at kernelnewbies.org
> Subject: fork() and exec()
>
> Hi List,
>
> I am learning Linux and trying to understand exec and fork function.
> execl says that it overlays the running address space. What does it
mean?
>
> I created the following program and used top command with
> intentionally wrong arguments:
>
> #include<stdio.h>
> #include<unistd.h>
> #include<sys/types.h>
> #include<stdlib.h>
>
> int main(){
> int a = -1;
> if(fork()==0){
> printf("Inside child\n");
> printf("child pid=%d, parentid=%d\n", getpid(),
getppid());
> execl("/usr/bin/top", "/usr/bin/top", ">/dev/null"
,(char*)0
> );
> scanf("inside child provide a %d", &a);
> printf("Inside child a=%d\n", a);
> exit(1);
> } else {
> printf("Inside parent, going to wait\n");
> printf("my pid=%d, parentid=%d\n", getpid(), getppid());
> scanf("input parent %d\n", &a);
> wait(NULL);
> printf("Wait over\n");
> printf("Inside parent a=%d\n", a);
> }
> return 0;
> }
>
> When i run this program, it gives following output:
> [vijay at localhost]$ ./a.out
> Inside parent, going to wait
> Inside child
> child pid=2775, parentid=2774
> my pid=2774, parentid=2681
> top: unknown argument '>'
> usage: top -hv | -bcisSHM -d delay -n iterations [-u user | -U user]
> -p pid [,pid ...]
>
> 10
> Wait over
> Inside parent a=-1
> [vijay at localhost]$
>
> Why the child scanf and printf not executed?
Because when you called execl(), the code in the child was replaced by
the top executable. execl() replaces the code calling it. The only way
code following a call to execl() or its siblings will ever execute is if
the call to execl() is unsuccessful, for instance, fails to find the top
program in the current path.
> In the parent program, i expected input parent should be printed. But
> it doesnt and just wait for some input. When i entered 10 it resumes
> but printing -1 as a value.
Your scanf() is expecting input that looks something like this:
input parent 10
You just entered 10, so the format string didn't match input and the
variable "a" was never assigned.
Jeff Haran
> Could anyone please help me to understand this behavior? or any good
> tutorial or book
>
> Thanks.
> Vijay
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
^ permalink raw reply [flat|nested] 5+ messages in thread
* fork() and exec()
2012-02-06 19:08 fork() and exec() Vijay Chauhan
2012-02-06 19:27 ` Jeff Haran
@ 2012-02-07 5:48 ` Srikrishan Malik(gmail)
2012-02-07 15:40 ` Bernd Petrovitsch
2 siblings, 0 replies; 5+ messages in thread
From: Srikrishan Malik(gmail) @ 2012-02-07 5:48 UTC (permalink / raw)
To: kernelnewbies
These are the first two results when I search "fork vs exec" in google.
http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
http://en.wikipedia.org/wiki/Fork-exec
I found very good explanations in both the links(yolinux is a bit better)
-Sri
On Tue, Feb 7, 2012 at 12:38 AM, Vijay Chauhan <kernel.vijay@gmail.com>wrote:
> Hi List,
>
> I am learning Linux and trying to understand exec and fork function.
> execl says that it overlays the running address space. What does it mean?
>
> I created the following program and used top command with
> intentionally wrong arguments:
>
> #include<stdio.h>
> #include<unistd.h>
> #include<sys/types.h>
> #include<stdlib.h>
>
> int main(){
> int a = -1;
> if(fork()==0){
> printf("Inside child\n");
> printf("child pid=%d, parentid=%d\n", getpid(), getppid());
> execl("/usr/bin/top", "/usr/bin/top", ">/dev/null"
> ,(char*)0 );
> scanf("inside child provide a %d", &a);
> printf("Inside child a=%d\n", a);
> exit(1);
> } else {
> printf("Inside parent, going to wait\n");
> printf("my pid=%d, parentid=%d\n", getpid(), getppid());
> scanf("input parent %d\n", &a);
> wait(NULL);
> printf("Wait over\n");
> printf("Inside parent a=%d\n", a);
> }
> return 0;
> }
>
> When i run this program, it gives following output:
> [vijay at localhost]$ ./a.out
> Inside parent, going to wait
> Inside child
> child pid=2775, parentid=2774
> my pid=2774, parentid=2681
> top: unknown argument '>'
> usage: top -hv | -bcisSHM -d delay -n iterations [-u user | -U user]
> -p pid [,pid ...]
>
> 10
> Wait over
> Inside parent a=-1
> [vijay at localhost]$
>
> Why the child scanf and printf not executed?
> In the parent program, i expected input parent should be printed. But
> it doesnt and just wait for some input. When i entered 10 it resumes
> but printing -1 as a value.
>
> Could anyone please help me to understand this behavior? or any good
> tutorial or book
>
> Thanks.
> Vijay
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
--
SK Malik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120207/9df33b28/attachment-0001.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* fork() and exec()
2012-02-06 19:08 fork() and exec() Vijay Chauhan
2012-02-06 19:27 ` Jeff Haran
2012-02-07 5:48 ` Srikrishan Malik(gmail)
@ 2012-02-07 15:40 ` Bernd Petrovitsch
2012-02-08 9:31 ` Vijay Chauhan
2 siblings, 1 reply; 5+ messages in thread
From: Bernd Petrovitsch @ 2012-02-07 15:40 UTC (permalink / raw)
To: kernelnewbies
On Die, 2012-02-07 at 00:38 +0530, Vijay Chauhan wrote:
> Hi List,
>
> I am learning Linux and trying to understand exec and fork function.
> execl says that it overlays the running address space. What does it mean?
>
> I created the following program and used top command with
> intentionally wrong arguments:
>
> #include<stdio.h>
> #include<unistd.h>
> #include<sys/types.h>
> #include<stdlib.h>
>
> int main(){
> int a = -1;
> if(fork()==0){
> printf("Inside child\n");
> printf("child pid=%d, parentid=%d\n", getpid(), getppid());
> execl("/usr/bin/top", "/usr/bin/top", ">/dev/null" ,(char*)0 );
You get here only if the execl() as such fails.
> scanf("inside child provide a %d", &a);
You should check the return value here if you actually got a matching
parameter.
scanf() is actually a function to be avoided.
> printf("Inside child a=%d\n", a);
> exit(1);
> } else {
> printf("Inside parent, going to wait\n");
> printf("my pid=%d, parentid=%d\n", getpid(), getppid());
> scanf("input parent %d\n", &a);
You should check the return value here if you actually got a matching
parameter.
scanf() is actually a function to be avoided.
> wait(NULL);
You should check the return value here to know why "wait()" returns.
> printf("Wait over\n");
> printf("Inside parent a=%d\n", a);
> }
> return 0;
> }
Bernd
--
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at
^ permalink raw reply [flat|nested] 5+ messages in thread
* fork() and exec()
2012-02-07 15:40 ` Bernd Petrovitsch
@ 2012-02-08 9:31 ` Vijay Chauhan
0 siblings, 0 replies; 5+ messages in thread
From: Vijay Chauhan @ 2012-02-08 9:31 UTC (permalink / raw)
To: kernelnewbies
Thank you all for your help.
Vijay.
On Tue, Feb 7, 2012 at 9:10 PM, Bernd Petrovitsch
<bernd@petrovitsch.priv.at> wrote:
> On Die, 2012-02-07 at 00:38 +0530, Vijay Chauhan wrote:
>> Hi List,
>>
>> I am learning Linux and trying to understand exec and fork function.
>> execl says that it overlays the running address space. What does it mean?
>>
>> I created the following program and used top command with
>> intentionally wrong arguments:
>>
>> #include<stdio.h>
>> #include<unistd.h>
>> #include<sys/types.h>
>> #include<stdlib.h>
>>
>> int main(){
>> ? ? ? int a = -1;
>> ? ? ? if(fork()==0){
>> ? ? ? ? ? ? ? printf("Inside child\n");
>> ? ? ? ? ? ? ? printf("child pid=%d, parentid=%d\n", getpid(), getppid());
>> ? ? ? ? ? ? ? execl("/usr/bin/top", "/usr/bin/top", ">/dev/null" ,(char*)0 );
>
> You get here only if the execl() as such fails.
>
>> ? ? ? ? ? ? ? scanf("inside child provide a %d", &a);
>
> You should check the return value here if you actually got a matching
> parameter.
> scanf() is actually a function to be avoided.
>
>> ? ? ? ? ? ? ? printf("Inside child a=%d\n", a);
>> ? ? ? ? ? ? ? exit(1);
>> ? ? ? } else {
>> ? ? ? ? ? ? ? printf("Inside parent, going to wait\n");
>> ? ? ? ? ? ? ? printf("my pid=%d, parentid=%d\n", getpid(), getppid());
>> ? ? ? ? ? ? ? scanf("input parent %d\n", &a);
>
> You should check the return value here if you actually got a matching
> parameter.
> scanf() is actually a function to be avoided.
>
>> ? ? ? ? ? ? ? wait(NULL);
>
> You should check the return value here to know why "wait()" returns.
>
>> ? ? ? ? ? ? ? printf("Wait over\n");
>> ? ? ? ? ? ? ? printf("Inside parent a=%d\n", a);
>> ? ? ? }
>> ? ? ? return 0;
>> }
>
> ? ? ? ?Bernd
> --
> Bernd Petrovitsch ? ? ? ? ? ? ? ? ?Email : bernd at petrovitsch.priv.at
> ? ? ? ? ? ? ? ? ? ? LUGA : http://www.luga.at
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-02-08 9:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-06 19:08 fork() and exec() Vijay Chauhan
2012-02-06 19:27 ` Jeff Haran
2012-02-07 5:48 ` Srikrishan Malik(gmail)
2012-02-07 15:40 ` Bernd Petrovitsch
2012-02-08 9:31 ` Vijay Chauhan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).