Table of Contents

Return to Operationg Systems home


Operating Systems Course: Lab04

Laboratory number 04

Exercise 01: Shell commands for processes and redirection

A.

Write a C program that reads integer numbers and prints in standard output (stdout)

fprintf (stdout, ...);

the even numbers and in standard error (stderr)

fprintf (stderr, ...);

the odd numbers. The program must terminate after the introduction of the value 0.

Make use of redirection for:

B.

Use pipe and redirection on shell command to perform the following operations:

Exercise 02: Concurrent exchange-sort on files

Premise number 1.

The sorting algorithms by exchange (exchange-sort or bubble-sort) sort a vector comparing adjacent elements. The following is a possible implementation (with sorting of the vector v of n elements):

for (i=0; i<n-1; i++) {
  for (j=0; j<n-1-i; j++) {
    if (v[j] > v[j+1]) {
       val = v[j];
       v[j] = v[j+1];
       v[j+1] = val;
    }
  }
}

Premise number 2.

A binary file can be write (UNIX function write) or read (UNIX function read) using a random access by means of the function lseek.

For example, the program lab04ex02.c, after receiving the name of a file from command line,

Exercise specifications

Implement in a concurrent and multi-process way the bubble-sort algorithm.

The algorithm, by using the system calls fork and wait, and sharing data in the file (i.e., by reading and writing data in the same file), must order the file itself.

The program (i.e., the parent):

Each of its children:

Observations

  1. Working on file is indispensable since a parent process and a child do not share the address space (in fact, ordering a vector would actually order different vectors).
  2. The program can be generalized by considering the first integer number stored in the file as indicator of the actual number of the integer numbers saved in the file.
  3. Managing a binary file simplifies the positioning on a specific data i.
    In fact, in the case of a ASCII file, the number of character occupied is not constant, as in the case of binary file, but it depends on the value of the number:
    • number 5
      • ASCII coding = 1 character
      • binary coding = sizeof(int) bytes,
    • number 12345
      • ASCII coding = 5 characters
      • binary coding = sizeof(int) bytes,
    • In addition, in the first case of ASCII coding, it would also be necessary to count “additional” characters, such as the \n character of end of line.

Exercise 03: Use of signals

Write a program that, using signals, synchronize two processes (a parent and it child) in a way that they print alternatively a specific message.

The following is an example of output:

Child Woke-up
Father Woke-up
Child Woke-up
Father Woke-up
Child Woke-up
Father Woke-up
Child Woke-up
Father Woke-up
...

Insert appropriate sleep to avoid race conditions.

Exercise 04: Use of signals and communication through files

Implement a C program that generates two children, a producer and a consumer. The producer child reads from the keyboard some strings, and it transfers them to the consumer. The consumer child converts the strings into uppercase characters, and it visualizes the strings in standard output.

The introduction of the string “end” terminates both children's processes and then, in sequence, it terminates the parent process. The transfer of the strings has to take place through a file.

Suggestions:

  1. Derive the solution from that of the previous exercise
  2. Communicating processes are not a parent and a child, but two “siblings”; as a consequence, you have to make sure that each child knows the PID of the “brother”