JPHHLASH

fork and exec explained

Home
header files
Linux Commands
access windows partition from linux
startup
compiling from source code
how to run C/C++ programs in linux
some useful shell scripts
environment variables
fork and exec
Using apache
php in linux
read/write in linux

When fork system call is executed,a duplicate copy of the original process is loaded into the main memory.ie there runs 2 processes
1)The parent process or the original process.
2)The child process

#include<stdio.h>
int main()
{
fork();
{
printf("\n Hi");
}
return 0;
}

OUTPUT:

Hi
Hi


fork.jpg


1)Child process always RETURNS a 0(zero) value.
2)Parent process always RETURNS a non zero value.

1)Child process's pid(process id) is what the parent process returns.
2)Parent process's id is the pid of the original process.

#include<stdio.h>
int main()
{
int ireturn;
ireturn=fork();
if (ireturn==0)
{ printf("I am child coz ireturn is 0 \n");
}
else printf("I am parent coz ireturn is not 0 \n");
return 0;
}
OUTPUT:
I am child coz ireturn is 0
I am parent coz ireturn is not 0

Notice that the child process executed before the parent.

Note that the program will execute from start to end 2 times,not ONLY the main fucntion.


#include<stdio.h>
int main()
{
int ireturn;
ireturn=fork();
printf("\n ireturn=%d \n",ireturn);
printf("\n ,my pid= %d",getpid());
}

OUTPUT:
ireturn=0
my pid= 5238
iretutn=5238
my pid= 5237

.

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.jpg

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.