Exercises On Processes
Exercises On Processes
Exercises On Processes
2. Including the initial parent process, how many processes are created by the fol-
lowing program? Construct a tree that shows the parent-child relationship among
processes.
#include <stdio.h>
#include <unistd.h>
int main()
{
/* fork a child process */
fork();
/* fork another child process */
fork();
/* and fork another */
fork();
return 0;
}
3. Including the initial parent process, how many processes are created by the program
below?
int main()
int i;
pid t pid;
for (i=0;i<5;i++)
pid = fork();
return 0;
1
int i;
for (i = 0; fork(); i++) {
if (i == 4) { break; }
printf{("PID: %d, i = %d \n", getpid(), i);
}
(a) Excluding the calling process, how many new processes are created?
(b) Assume that initially the process ID executing this code is 3376, and the PID
always increases sequentially by 1. For example, the first time this process calls
fork() the PID of the child is 3377. You can also assume no other processes in
the system are calling fork() after this program starts to execute. What will
be printed by this program?
int i;
for (i = 0; i < 4; i++) {
if (fork()) { break; }
printf{("PID: %d, i = %d \n", getpid(), i);
}
Assume that initially the process ID executing this code is 3376, and the PID always
increases sequentially by 1. For example, the first time this process calls fork() the
PID of the child is 3377. You can also assume no other processes in the system
are calling fork() after this program starts to execute. What will be printed by this
program?
Write a C or C++ program using the fork() system call that generates this sequence
in the child process. The starting number will be provided from the standard input.
Have the parent invoke the wait() call to wait for the child process to complete
before exiting the program.
7. Give three conditions that cause switching from user mode to kernel mode.
8. Comparing system calls vs. procedure calls. The simple test program below com-
pares the cost of a simple procedure call to a simple system call. Compile and run
this program.
2
(a) Run your experiment on two different hardware architectures. Report your
results for each architecture.
(b) Explain the difference (if any) between the time required by your simple pro-
cedure call and simple system call by discussing what work each call must
do.
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
int foo(){
return(10);
}
main(){
int i,j,res;
long N_iterations=1000000; /* A million iterations */
float avgTimeSysCall, avgTimeFuncCall;
struct timeval t1, t2;
9. Using the program below and the command “execl”, make the child process to
execute the command ps -f which is in the directory /bin
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
3
#include <sys/wait.h>
int main(void) {
pid_t childpid;
pid_t mypid = getpid();
printf("I am parent with PID = %ld\n", (long)mypid);
childpid = fork();
if (childpid == 0) { /* child code */
printf("I am child with PID = %ld\n", (long)getpid());
execl();
return 1;
}
if (childpid != wait(NULL)) { /* parent code */
perror("Parent failed to wait due to signal or error");
return 1;
}
return 0;
}
10. Here are two versions of the code that generates a chain of processes. Run these two
versions, observe the outputs and explain why the two outputs are not the same.
4
int i, n;
n = atoi(argv[1]);
for (i = 0; i < n; i++)
if (childpid = fork()) break;
wait(NULL);
fprintf(stderr, "i:%d process ID:%ld child ID:%ld, parent ID:%ld\n",
i, (long)getpid(), (long)childpid,(long)getppid());
return 0;
}
11. Here there is again a new version of the program that generates a chain of processes.
There is two possible interpretations about what this program is doing:
Does the program below will work? If yes, which of the above two interpretations
is correct? You may observe zombies, can you explain why this happens? Notice
the last process to be created does something different from the others, could you
explain why?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main (int argc, char *argv[]) {
pid_t childpid = 0;
int i, n;
n = atoi(argv[1]);
for (i = 0; i < n; i++)
if (childpid = fork()){
execl("/bin/ps", "ps", "-f", NULL);
break;
}
wait(NULL);
fprintf(stderr, "i:%d process ID:%ld child ID:%ld, parent ID:%ld\n",
i, (long)getpid(), (long)childpid,(long)getppid());
return 0;
}
12. Using the execl() command, change the program below such that the child processes
will execute the command “date”. How many processes will execute the fprintf()
command, which one execute it and why?
5
int main (int argc, char *argv[]) {
pid_t childpid = 0;
int i, n;
n = atoi(argv[1]);
for (i = 1; i < n; i++)
if ((childpid = fork()) <= 0) break;
fprintf(stderr, "i:%d process ID:%ld child ID:%ld, parent ID:%ld\n",
i, (long)getpid(), (long)childpid,(long)getppid());
wait(NULL);
return 0;
}
13. The program below generates a tree of processes. Do you think it is possible for one
of these processes to print the total number of processes in the tree?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Create a multilevel tree of processes*/
int main (int argc, char *argv[]) {
pid_t childpid = 0;
int i,j, n;
n = atoi(argv[1]);
for (i = 1; i < n; i++){
childpid = fork();
if (childpid != 0)fprintf(stderr, "tree: %d i:%d process ID:%ld parent ID:%ld chil
tree,i, (long)getpid(), (long)getppid(), (long)childpid);
}
sleep(10);
return 0;
}