Starting from:
$30

$24

HW 04 Solution

Write a C/C++ program that uses the PTHREAD library to create 2 threads

and mutexes to synchronize them. The program must to the following:




* MAIN will open a file (hw4.out) for writing, then call thread #1 and

thread #2. MAIN will also close the file when thread #1 and thread

#2 are finished.




* THREAD #1 will open a file (hw4.in) for reading, will read 1 integer

value from the file, and put that value in a global variable. Thread #1

will then wait for thread #2 (use mutexes to control the wait) before

moving on to the next value (in hw4.in).




* THREAD #2 will read the global variable. If the global variable is odd,

thread #2 will simply write the value to hw4.out. If the global variable

is even thread #2 will write the value to hw4.out TWICE.




Requirements:

-------------

1. The hw4.in file will have the format of "integer" "linefeed". If you do not

abide by the input format and my program cannot test your program will my inputs

it will be considered wrong. No exceptions! Thus, I suggest you use the command

"fscanf(file, "%d\n", &value1)" to read your hw4.in file. For example:

1

2

3

3

6




2. Hw4.out must have the exact format of "integer" "linefeed" as the *.in files

will be very long and I will have a program that runs through your output files

checking results. If you do not abide by the output format and my program cannot

test your output it will be considered wrong. No exceptions! Thus, I suggest you

use the command "fprintf(file, "%d\n", value)" to write your hw4.out file. For example:

1

2

2

3

3

6

6




REQUIREMENTS:

-------------

1. Your program must run on Linux Mint (Leonard 110/112).




2. Your full name must appear as a comment at the beginning of your

program.




3. Your source code must be named hw4-yourname.c or hw4-yourname.cpp




4. Your program must use mutex(s).




5. Your program must use pthreads.

More products