Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Summary Write a program using thread functions to create, synchronize and terminate threads. What To Do Assume that you have the following functions: initque (

Summary
Write a program using thread functions to create, synchronize and terminate threads.
What To Do
Assume that you have the following functions:
initque();
int enque(data_t * dt);
double proc(char * ar);
where data_t is a structure:
struct data_t
{
double val;
pthread_t id;
int index;
};
Function val = proc(char * ar) receives an array of 128 bytes and returns one
double value val. This function may be used by several threads at the same time.
Function enque(struct data_t * 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 C code for the main thread function main() and the C 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().
int count;
int main (int argc, char ** argv)
{
}
void *work (void * arg)
{
}
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 0 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 128 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 data_t 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_t 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 128 bytes or it encounters end of file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago