Return to Operationg Systems home
The program named lab06e01recursive.c receives from the command line an integer value n
and, using recursion (see funcition binary
), it generates and visualizes all the binary numbers of n
bits.
Transform the program from recursive to concurrent, i.e., substitute the recursive implementation with the generation (by means of a fork()
) of an adequate number of processes capable of printing the binary numbers (the order is not important).
Implement a concurrent program with n
threads that sorts an input file. The program must proceed in parallel on different files, according to the following specifications.
The program (which name pgrm
) receives 3 parameters from command line
pgrm n strA strB
where:
n
is an integer valuestrA
is a string that identifies the name of n
input files of name 'strA1.txt, strA2.txt, …, strAn.txt'strB
is a string that identifies the name of n
output files of name 'strB1.txt, strB2.txt, …, strBn.txt'
The input files strA
contain:
The following is an example of correct file:
5 102 99 34 234 25
The program, after the generation of the n
names of the two input and output files, activates n
threads.
Each thread:
Note that the program implements the following precedence graph:
Mi--------- /\ | / \ | R1 R2 ... | | O1 O2 ... | | W1 W2 ... \ / \/ | Mf---------
where:
Modify the previous program in a way that the n
sorted sequences (read from file) are merged to generate a single ordered sequence.
More in detail:
main()
, after executing the threads, waits for their termination, and after the termination of one of them, it starts the merging of the data just ordered with those merged previouslyThe third command line parameter of the program indicates the name of the output file. For simplicity (to allocate the data structure) it is possible to suppose that all the files store the same number of values and, eventually, this number is known a priori.
Use a matrix to store the data read by the input files, and dedicate a row (or a column) to each file (or, alternatively, a vector of structures with a vector field).
Each thread manipulates exclusively its own row (or column) of the matrix (or element of the vector).
The main()
perform the merge of the sequences sorted in pair:
0
(zero) data with n
datan
old data with n
new data2*n
old data with n
new data
The main()
can make use of one or more support vector for the merging of the matrix data.