0% found this document useful (0 votes)
18 views44 pages

UNIX Multiprocess Fork Exec

programs of unix

Uploaded by

rushi45976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views44 pages

UNIX Multiprocess Fork Exec

programs of unix

Uploaded by

rushi45976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

UNIX multi-

multi-process programming
using fork and execs

By
CHITRAKANT BANCHHOR
IT, SCOE, Pune
References
1. W. Richard Stevens , “Advanced Programming in the UNIX® Environment”, Addison
Wesley Professional .

2. W. Richard Stevens , UNIX Network Programming, Volume– II, PHI

3. Compiled from various Internet sites.


PID related Functions

 getpid();

 getppid();

 Use pid_t for variable declarations.

# include <sys / types.h>


Example

#include<stdio.h>
#include<unistd.h>
int main()
{
printf(“The process id =%ld\n”, (long) getpid() );
printf(“The parent id =%ld\n”, (long) getppid() );
return 0;
}
fork(): creating new process

#include<sys/ types.h>
#include<unistd.h>

pid_t fork(void);
fork returns

 In parent : pid of the child

 In child : 0

 -1 : on errors
Main Memory
if (pid == 0) { Secondary Memory
printf ( “Child Process\n”);
}
Parent Process
Address else {
Space printf ( “Parent Process\n”);
}
pid = fork ();
if (pid == 0) {
printf ( “Child Process\n”);
}
if (pid == 0) {
else {
printf ( “Child Process\n”);
printf ( “Parent Process\n”);
}
}
Child Process else {
Address
Space printf ( “Parent Process\n”);
}
wait() function

#include<sys/ types.h>
#include<sys/ wait.h>

pid_t wait ( int *status );

 Status receive the child termination status.

 Return value from wait is the pid that matches the return termination
status.

 - 1 on error.
Assignment--1
Assignment

• Problem Statement – I : Program where parent process sorts array


elements in descending order and child process sorts array elements in
ascending order.
Using fork and exec

• New process created by fork executes another program by


calling one of the exec function.
Six types of exec functions

1 int execle (
const char* pathname,
const char* arg0,
…………..
(char*)0,
char *const envp[ ]
);

2 int execl (
const char* pathname,
const char* arg0,
…………..
(char*)0,
);
3
int execv (
const char* pathname,
char* const argv[ ]
);

4
int execlve(
const char* pathname,
char* const argv[ ],
char * const envp[ ]
);
5 int execlp (
const char* filename,
const char* arg0,
…………………….,
(char*)0
);

6
int execlvp (
const char* filename,
char* const argv[ ],
);
1. execl():execute and leave

#include<unistd.h>
execl(path,arg0,arg1,…,argn,0);

path: points to the name of the file holding a command to be executed.


arg0: name of the command.
arg1,…,argn: pointers to the arguments for the command.
0: end of the variable list of arguments.
Example1 execl ( path, arg0, arg1 ,…, argn
argn,, 0 );

main()
{
printf(“The output of date command\n”);
execl(“/bin/date”, ”date”, (char*)0 );
printf(“It was the date\n”);
}
Example: Executing new program

int main()
{
int pid;
/* fork another process */
pid = fork();
if (pid == 0) {
execl(“/bin/ls”,”ls”,”-l”,0);
}
else {
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
exit(0);
}
}
execv.c int execv ( const char* pathname, char* const argv[ ] ) ;

int main(int argc, char *argv[ ] )


{
pid_t pid;
char *args[3] = { “/bin/ls”, “-l”, (char*) 0 };

pid = fork ( ) ;
if ( pid == 0 ) {
execv ( “/bin/ls” , args );
}
else {
wait ( NULL );
printf ( “Parent process\n” );
}
}
execv--1
execv
execv1.c

int execv ( const char* pathname, char* const argv[ ] ) ;


oneIntArg.c
excev--2
excev
exec2.c

int execv ( const char* pathname, char* const argv[ ] ) ;


twoIntArg.c
array--1
array
array1.c

int execv ( const char* pathname, char* const argv[ ] ) ;


arrayDisp.c
Assignment – 2
array.c
asc.c
dsc.c
CHITRAKANT BANCHHOR

Thank You!

You might also like