Table of Contents

Return to Operationg Systems home


Operating Systems Course: Lab03

Laboratory number 03

Exercise 01: System call fork, exec, system

Report the control flow graph (CFG) and the process generation tree for the following C program.

In addition, indicate what it produces in output on the screen and for which reason.

lab04_e01.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main () {
  char str[100];
  int i;
 
  setbuf(stdout,0);
  fork();
  for (i=0; i<2; i++){
    if (fork()!=0) {
      sprintf (str, "echo system with i=%d", i);
      system (str);
    } else {
      sprintf (str, "exec with i=%d", i);
      execlp ("echo", "myPgrm", str, NULL);
    }
  }
  return (0);
}

Exercise 02: System call fork, exec, system

Report the control flow graph (CFG) and the process generation tree for the following C program.

In addition, indicate what it produces in output on the screen and for which reason.

lab04_e02.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main () {
  int i = 0;
 
  setbuf(stdout,0);
  while (i<=2 && fork()){
    if (!fork()) {
      fprintf (stdout, "Running Exec ...");
      execlp ("echo", "echo", "i*i", (char *) 0);
    }
    i++;
    fprintf (stdout, "Running System ...");
    system ("echo i+i");
  }
 
  return (0);
} 

Exercise 03: System call fork, wait, exec, system

A file contains strings logically grouped into distinct sets. Any set of strings indicates a Unix/Linux command and the related parameters. Each set is terminated with the string “end”. The following is an example of correct file:

commands.txt
touch mioFile.c end
ls -laR end
cat mioFile.c end
mkdir -p tmpDir end
cp -f mioFile.c tmpDir end

Observe that, if you prefer, you can assume that the string “end” is not present in the file, and it can be substituted with the end-of-line character \n to indicate the end of the strings associated with a certain command.

Write a program that, after receiving the name of the file through the command line, executes the Unix/Linux command listed in the file by using: