User Tools

Site Tools


Action disabled: source
os:lab03
Return to Home page

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:

  • the system call system, executing one command after another
  • the system call exec, executing a command every 3 seconds (you can decide with version of the exec to use).

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/os/lab03?do=edit
/web/htdocs/www.skenz.it/home/data/pages/os/lab03.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy