RE: how is "exec" executed by a shell?

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

 



Hi,

The exec shell built-in is like the exec group of system calls
(generally referred to as the exec system call). Before we use the exec
system call, we do a fork. The code is generally like this:

int main()
{
	int pid=fork();

	if (0 == pid)
	{
		// Child process
		// exec system call can be used here
	}
	else if (-1 == pid)
	{
		// Error condition
		// Exit from here
	}
	else
	{
		// Parent process
		// exec not called here
		// wait for pid
	}
	exit(0);
}

Note that the child process is getting created by fork. The exec call on
the other hand replaces the current process with the process that has
been passed as argument to it. Thus the "parent" process (if we can use
that term) does not exist anymore. 

The exec shell built-in also works in the same way. It replaces the
currently executing process with it's argument. So when we exit from the
new process, there is no caller we can return to and so the shell
closes. To escape from this, open a new sheel in which you execute the
command.

A similar effect can be seen when we source a script that uses the
"exit" shell built-in.

Cheers,
Karthick S.


-----Original Message-----
From: kernelnewbies-bounce@xxxxxxxxxxxx
[mailto:kernelnewbies-bounce@xxxxxxxxxxxx] On Behalf Of Mandeep Sandhu
Sent: Monday, January 10, 2005 13:10
To: kernelnewbies
Subject: Re: how is "exec" executed by a shell?


On Sun, 2005-01-09 at 07:47, Jim Bauer wrote:
> On Saturday 08 January 2005 16:10, Jaydeep Chokshi wrote:
> >
> > Now My confusion is as follows,
> >
> > $exit
> >
> > doin this shell exits right, How come shell shold exit because 1st 
> > shell forked and had child process to execute the 'exit' call and 
> > thats why exit terminates but how this affect the shell to 
> > terminate, I mean it is child process who executed the 'exit' call 
> > so why then shell exits.
> 
> There is no fork() call.  exit is a shell builtin.  Running it, causes
> the shell to terminate.
is exec also a shell built-in?
> 
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[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