kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: bernd@petrovitsch.priv.at (Bernd Petrovitsch)
To: kernelnewbies@lists.kernelnewbies.org
Subject: fork() and exec()
Date: Tue, 07 Feb 2012 16:40:13 +0100	[thread overview]
Message-ID: <1328629213.25984.123.camel@thorin> (raw)
In-Reply-To: <CAJ61zBDgtV+2RahdE2sF9bjkOWvYC4ABRpbEdWRo0GuZS8f7+Q@mail.gmail.com>

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

  parent reply	other threads:[~2012-02-07 15:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2012-02-08  9:31   ` Vijay Chauhan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1328629213.25984.123.camel@thorin \
    --to=bernd@petrovitsch.priv.at \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).