Sie sind auf Seite 1von 2

Operating Systems (04JEZOQ)

Laboratory #8 – Threads – May 28th 2018

Exercise 1- Write a multi-thread program that creates two threads. When a thread is created the
program prints the message “first/second thread created”. When the thread execution starts the
thread prints the message “first/second thread processing”, then the thread waits for 5 seconds
and finally it prints the message “first/second thread terminates”.

Exercise 2- Write a multi-thread program that defines a string of 20 characters, instantiates a


thread and then starts a loop in which the user is required to enter a string.

The thread scans every second the content of the string. Whenever the string is equal to “house”
or “casa” the thread prints the message “house detected”. If the string contains the string “exit”
the program must terminate.

Exercise 3– Write a multi-thread program to search for DNA sequence matching. The program
reads the full genome of an organism from a text file. The genome is a string with 4 valid
characters: C, G, A, T. The maximum number of characters in a genome is 50000. A second
sequence of DNA bases comes from a second file and the program must search if this latter
sequence is present in any position of the genome. To increase the computation, this search
procedure must be implemented using several parallel threads. Let the user specify the number P
of threads.
At the end of the execution the program must print the start and the end positions of the genome
subsequences that match with the searched sequence.

As example, look at following files:

Full Genome Sequence File


CACCATAAAACCGTGTGGGACCTACAGGCAGCAACCTCTGGAGC
TACATCACGGATTTTACCCATCCATCCAGGGGGGCGGCGGATTT TACCCAT
TTTTTTTTTTTAACGGTATAAAACGGAGAGGGAGACACGTACCC
ATAACTCACCACACCCTCTGGAGCTAGAGCAGATTTACGATCGG

The program must report:

1. 59 – 66
2. 182 – 189

Hints: resort to common fopen and fprintf to deal with files: indeed, both files contains a “string”.
Operating Systems (04JEZOQ)

Exercise 4– Write a multi-thread program to search if a number N is a prime number by


checking if it is divisible by any number in the range [2, N/2]. The program receives in input the
number N and the number P of threads to use. The program must create P threads and assign to
each of them a subset of possible divisors to test. Each thread checks if N is divisible by any of
the assigned divisors. The main program continuously checks the result of the generated threads.
As soon as a thread detects that the number is not prime, the main program must stop the
executions of all generated threads and exit.
Once the program is ready, try its execution with big numbers and different values of P to
understand how the execution time changes. Printing the start and the end time is a good idea to
trace the whole execution time.

Hint: before writing the program down, think carefully about the way your algorithm is going to split the
data among threads. Then, after the first implementation, ask yourself if the program really stops as
soon as one of the threads “detects” that the number is not prime. In order to check it, try with a huge
non-prime number and notice how fast it is to end.

Das könnte Ihnen auch gefallen