You are on page 1of 2

FACULTY OF INFORMATION TECHNOLOGY

>>> COURSE MATERIAL <<<

INTRODUCTION TO OPERATING SYSTEM


Course ID 502047

Lab 5 – Threads
- Read “Lab5-Lecture.pptx”.
- Finish below programs, compile and execute them.
- Submit to SAKAI in the end of lab hours.

1. Write a program that creates three threads that perform the following tasks:
- The first thread takes arguments from the environment (argv [1]), checks if the number is 0,
calculates the factorial of this number (can use struct or global variables).
- The second thread waiting for the first thread to complete, perform the calculation of the
number of words smaller than the result of the first factorial factor calculated.
- Third thread waiting for the second thread to complete, write the result into the file is the
second argument from the environment variable (argv [2]). The contents of the record include:
the first line stores the value of argv [1]; The second line stores the results of the factorial; The
third line stores the sum of smaller numbers than the factorial.

For example: run ./bai1 4 res1


Output to res1 file is as follows:
N=4
4! = 24
Sum = 276

2. For a file with the following structure:


- The first line contains the number of array elements
- The other lines contain integer elements

Write a program of threads that perform the following tasks:


- The first read the input file is the first argument from the environment variable
- The second thread counts the sum of primes in the array
- Third thread sorts array increase
- The fourth thread executes the writing of the result file.

The input and output file contents are as follows:


For example, the input file is of the following format
ten
4
5
7
8
11
9
20
13
2
3

The result in the result file looks like this:


Size of array: 10
Value of elements: 4 5 7 8 11 9 20 13 2 3
Primes: 5 7 11 13 2 3
Sum of Primes: 41
Primes sorted from smallest to highest: 2 3 5 7 11 13

You might also like