RE: fork() and exec()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: kernelnewbies-bounces@xxxxxxxxxxxxxxxxx [mailto:kernelnewbies-
> bounces@xxxxxxxxxxxxxxxxx] On Behalf Of Vijay Chauhan
> Sent: Monday, February 06, 2012 11:08 AM
> To: kernelnewbies@xxxxxxxxxxxxxxxxx
> 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@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@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@xxxxxxxxxxxxxxxxx
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux