User Tools

Site Tools

Return to Home page

Return to Operationg Systems home

Operating Systems Course: Lab09

Laboratory number 09

Exercise 01: concurrent programs with semaphores

A file contains a text in the ASCII format with indefinite length, but its lines have a maximum length of 100 characters.

Write a multi-threads concurrent program that:

  • receives in the command line two strings. The first specifies the name of an input file. The second specifies the name of an output file. The input file contains the text previously specified.
  • executes 3 threads:
    • the first thread reads lines from the input file (one at a time)
    • the second thread inverts the content of a line (the last character becomes the first and vice versa), and transforms all the literal characters in uppercase
    • the third thread saves each line transformed by the second thread in the output file (sequentially)

All three threads are cyclic and they must be created only one time. When the first thread identifies the end of the file, it must allow the second and third thread an ordered termination, and it terminates itself.
To synchronize threads use the semaphoric primitives sem_*.

Exercise 02: concurrent programs with semaphores

A binary files contains triplets of numbers
c x n
that individuate the monomial c*x^n, where c and x are real values (float) and n is an integer value (int).

Fon instance, the triplet
1.0 2.0 3 indicates the monomial 1.0*2.0^3, which is equal to the value 8.0.

Triplets have to be considered in groups of 3. Each triplet belongs to the same monimial.

Write a multi-thread program that:

  • receives from the command line a string. This string specifies the name of an input file containing the previous specified records, but in binary format.
  • executes 4 cyclic threads:
    • the first thread computes the first monomial of each polynomial.
    • the second thread computes the second monomial of each polynomial.
    • the third thread computes the third monomial of each polynomial.
    • the fourth thread computes the sum of the 3 monomials, and prints (in the screen) the value of the polynomial.

All the threads are cyclic, and read from the file only the information (monomials) of their competence (i.e., you must avoid that the first 3 threads read the whole input file).

Assume that does not exist mathematical operation beyond the 4 main ones. In other words, all the threads compute the monomials:

c * x^n

using the multiplication

c * x * x * x * x ... n times.

The 4 threads terminate in an ordered way, when the end of the file is reached.
The main thread waits for the 4 executed threads, and then it ends itself.

The input file can be assumed correct, but if the number of monomials stored in the file it is not multiple of 3, the value of the missing monomials is equal to 0.


  • Write a program/function to convert between the ASCII format and the binary format, and if it is necessary (for verification) a program/function that performs the conversion in the opposite direction.
  • The conversion is done with serialization.
  • Use the fscanf/fprintf primitives to read/write (on the binary file).
  • Each thread can make use of the lseek system call to perform the read of only the data related to the correct monomial.

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

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url:
/web/htdocs/ · Last modified: 2024/04/08 22:35 by

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