kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: kernel.vijay@gmail.com (Vijay Chauhan)
To: kernelnewbies@lists.kernelnewbies.org
Subject: fork() and exec()
Date: Tue, 7 Feb 2012 00:38:19 +0530	[thread overview]
Message-ID: <CAJ61zBDgtV+2RahdE2sF9bjkOWvYC4ABRpbEdWRo0GuZS8f7+Q@mail.gmail.com> (raw)

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

             reply	other threads:[~2012-02-06 19:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-06 19:08 Vijay Chauhan [this message]
2012-02-06 19:27 ` fork() and exec() Jeff Haran
2012-02-07  5:48 ` Srikrishan Malik(gmail)
2012-02-07 15:40 ` Bernd Petrovitsch
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=CAJ61zBDgtV+2RahdE2sF9bjkOWvYC4ABRpbEdWRo0GuZS8f7+Q@mail.gmail.com \
    --to=kernel.vijay@gmail.com \
    --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).