Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please meet all requirments :Write a program using thread functions to create, synchronize and terminate threads. What To Do Assume that you have the following
please meet all requirments :Write a program using thread functions to create, synchronize and terminate threads.
What To Do
Assume that you have the following functions:
Function val proc char ar receives an array of bytes and returns one
double value val. This function may be used by several threads at the same time.
Function enque struct datat dt creates a new element, copies into it the data
from the structure pointed by dt and enques it into a queue which has been created by a
call to initque Function enque may only be used by one thread at a time.
Write the code for the main thread function main and the code for two identical
worker threads called work You do not have to write the code for initque
enque or proc
The program has a global variable count and two functions main and work The main thread should work as follows:
it is called with two arguments like
test abc xyz
the two arguments are names of files in the current directory
it initializes count to and initializes the queue
it opens the two files for reading
it creates two threads, both running function work and gives to each thread as
an argument the file descriptor of one of the two open files
the first thread gets the file descriptor of the first file and the second thread
gets the file descriptor of the second file
it waits for the two threads to terminate, prints the value of count and then exits
it closes the corresponding file when a thread terminates
Each of the threads should work as follows:
it reads bytes from the open file
it calls function proc which returns a value in some variable
it increments global variable count
it stores in a variable of type struct datat the value returned from the call to
proc its own thread id and the value of global variable count obtained from the
increment just performed
it calls function enque to enque the new information
notice that enque copies the information from the variable of type struct
data to a new element in the queue, so that you can reuse the variable
it repeats the above steps until it encounters end of file
it terminates if it reads fewer than bytes or it encounters end of file
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started