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:
- read integer numbers from the file “
inFile.txt
”, instead of from standard input - write even numbers to the file “
evenFile.txt
”, instead of standard output - write the odd numbers to the file “
oddFile.txt
”, instead of standard error.
B.
Use pipe and redirection on shell command to perform the following operations:
- count the number of files/directories contained in your “
home
” directory (do not analyze sub-directories) - after downloading the file lab01e01in.txt in a directory, print the total number of strings in the file.
- count the number of active processes in the system who are owned by the user “
root
” (Suggestion: use the commandman ps
to analyze the option-U
of the command).
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,
- it writes in the file the vector
vet
with 12 elements - it reads the file starting from the last 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):
- receives on the command line the name of a file containing a predefined number
n
of integer numbers. - at each iteration of the external cycle, it “simulates” the action of the internal cycle by using
n-1-i
child processes
Each of its children:
- reads two adjacent elements from the file
- swaps them if it is necessary
- writes them in the file
- it synchronizes with the other children processes to generate the overall sorting effect (i.e., at each iteration in the external cycle the highest value among the remaining numbers is moved at the end of the file)
- terminates
Observations
- 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).
- 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.
- 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:
- Derive the solution from that of the previous exercise
- 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”
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/lab04