Thread (5 messages) 5 messages, 4 authors, 2012-02-08

fork() and exec()

From: Vijay Chauhan <hidden>
Date: 2012-02-08 09:31:32

Thank you all for your help.

Vijay.

On Tue, Feb 7, 2012 at 9:10 PM, Bernd Petrovitsch
[off-list ref] wrote:
On Die, 2012-02-07 at 00:38 +0530, Vijay Chauhan wrote:
quoted
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.
quoted
? ? ? ? ? ? ? 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.
quoted
? ? ? ? ? ? ? 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.
quoted
? ? ? ? ? ? ? wait(NULL);
You should check the return value here to know why "wait()" returns.
quoted
? ? ? ? ? ? ? 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help