Lab - II


01 August 2019

In this lab, you will write programs that use system calls for creating and managing processes.

  1. A program takes two command-line arguments. The first argument is the name of a valid Linux command such as ls. The second argument, if it exists, is a single argument to the Linux command given as argument 1. Your program should create a child process and run the given command with its argument as the child. The parent process initially prints a message listing the two command-line arguments; then runs the child process; and after the child returns, prints another message stating that the job is done. Then the program exits. Ask the parent as well as the child print their PIDs and PPIDs.
  2. Type ls -l /etc | more in a terminal. While the output is not complete, open another terminal and type the ps command with appropriate arguments. Trace the process tree (i.e., child-parent relationships) all the way up to your login shell.
  3. Take the first program and modify it so that the parent prints the time taken for creating the child process. You will need to use the clock() function.
  4. Given the following program, explain the process tree that results from it.

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
       fork();
       fork();
       fork();
       exit(0);
    }