.
exec
exec command replaces the shell program with itself.This is very unlike fork() which spawns a new process called the child
process.exec spawns a new process which replaces the original program.By fork system call,shell executes its duplicate copy
in main memory.Whereas by exec system call,shell overwrites the duplicate copy with the code corresponding to the given command.Now
the command program is in the main memory.
Syntax: exec [command] [args..]
Here the shell will exit and the command will execute.

exec family has many functions.execvp execv are amongst them.
Suntax:-
int execv( const char *path, char *const argv[]);
int execvp( const char *file, char *const argv[]);
In case of execvp,the first argument has to be a string the command name.But in case of execv,the first argument has to
be a string denoting the full path name.
The second argument of both has to be an <u> array of pointer to strings</u>.While calling the functions,the
base address of the array of pointers to strings has to be given.
An example of execv
#include<stdio.h>
int main()
{
int i;
char *arg_list[] = {
"ls", /* argv[0], the name of the program. */
"/",
NULL /* The argument list must end with a NULL. */
};
printf("This wont get printed on screen unless something wrong");
fprintf(stderr,"This will get printed at first");
execvp("ls",arg_list);
return 0;
}
example of using execv.Same as the above program except instead of execvp use:-
execv("/bin/mkdir",arg_list2);
where
char *arg_list2[]={"mkdir","made",NULL};
A directory called made will be formed at the location where the program is executed.
exec can also be used to feed a file into a program.eg
echo enter file name
read fname
exec <$fname
while read l
do echo $l
done
Here exec is used to change the standard imput from keyboard to the given file.The program reads contents of the given file
instead of the keyboard inout until no lines exists on the keyboard.
When exec is used alone, it kills the shell and other commands doenst get executed.When used with fork, exec overloads the
forked(dupliacte) process retaining the original process.
An example of fork and exec used together:-
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
char *arglist[]={"ls","-l","/",NULL};
char *arglist2[]={"ls","-l","/home",NULL};
pid_t ireturn;
ireturn= fork();
if (ireturn==0)
{
printf("i am child");/*wont get printed as execvp gonna ill this shell */k
execvp("ls",arglist);
printf("This wont printed");/*wont get printed as execv/execvp gonna kill this shell */
abort();
}
else
{
printf("I am parent");/*wont get printed as execv gonna kill this shell */
execv("/bin/ls",arglist2);}
}
return 0;
}
Zombie Process
Zombie process is a process is a process which is still waiting to read its child's exit status,But whose child has exited
altogether.Zombie process is a process that has terminated but still has an entry in the process table.This entry is needed
to allow the process which started the zombie process ,read the exit status.The parent can read the exit status of a child
by executing the wait system call.
Here is an example of a zombie process.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
/* Create a child process. */
child_pid = fork ();
if (child_pid > 0) {
/* This is the parent process. Sleep for a minute. */
sleep (60);
}
else {
/* This is the child process. Exit immediately. */
exit (0);
}
return 0;
}
Feedback, query, ideas? Email crenshaw_jo@yahoo.com.
|