Lab: 5 Exec System Call
Exec Family Examples:
execl
execl(“/usr/bin/wc”,”wc”,”-w”,(char*)0);
execle
char
*env[] = {“HOME=/usr/home”,”LOGNAME=home”,(char *)0};
execle(“/bin/ls”,”ls”,”-l”,(char*)0,env);
execlp
execlp(“sort”,”sort”,”-k”,”2”,”-n”,”ex4_4.txt”,”-o”,”ex4_4_sorted.text”,0);
execv
char *cmd[] = {“ls”,”-l”,(char*)0};
execv(“/bin/ls”,cmd);
execve
char *cmd[] = {“ls”,”-l”,”-a”,(char *)0};
char *env[] = {“HOME=/usr/home”,”LOGNAME= home”,(char*)0};
execve(“/bin/ls”,cmd,env);
execvp
char *cmd[] ={“ls”,”-l”,(char*)0};
execvp(“ls”,cmd);
Learning goals: in this laboratory activity you will learn how to use
waitpid and exec system calls.
You will understand how to wait for a specific child and how to
excute command in your own programs.
Exercise 1
Using system calls fork and waitpid write a program that receive a number n
from the command line and do the following:
1.creates n processes
2.the first child created sleep n second, the second child sleep n-1
seconds and so on.....(the last child sleep 1 second)
3.the parent wait for “all” the child(it waits sequentially
starting from the first created to the last – you have to use waitpid system call)
Exercise 2
Modify the exercise 1 using wait instead waitpid . How many times the parent
has to call “wait” ?
Does this program behaves as the previous one? Check it out!
Exercise 3
Run, in background mode, the programs you wrote in the exercises 1 and 2
and using ps command try to figure out how many zombie process you have.
Exercise 4
Download ex4_4.txt file then write a C program which will execute the
following series of Linux Commands:
1. cp {path}/ex4_4.txt . #path is the path you downloaded ex4_4.txt file
– it has to be different from “.”
2. sort -k 2 -n ex4_4.txt -o ex4_4_sorted.txt
3. wc -w ex4_4.txt
4. cat ex4_4.txt
Commands 2 and 3 have to be executed concurrently, find below the
corresponding execution
graph:
At the end of the excution of all of its children the parent has to
print a message.
Hint: consider the 4 commands as 4 different processes(which are created by
the parent)
Exercise 5
Try to figure out what is the output of the following program:
#include
<stdio.h>
int
main (int argc, char ** argv){
int
i = 0;
printf
("%d %d\n", getpid(), ++i);
execl
(argv[0], argv[0], 0);
printf
("end\n");
return
(1);
}
Then compile and run it and see what's going on.
Summary
At the end of this laboratory activity you should have understood how to
use waitpid and exec system calls . You should have understood the difference between wait and
waitpid system calls, and the difference between zombie and orphan process too
Comments
Post a Comment