Question: write C code --- description -- In this problem, you are going to write a multithreaded program using pthread. Let us assume that there are

write C code

--- description --

In this problem, you are going to write a multithreaded program using pthread. Let us assume that there are N user level threads that need to send information over network. Each thread is labeled from 1 to N, and they must send data in that order. For example, Thread 3 cannot send data until Thread 1 is done sending data. To send data, a thread first needs to copy the data to a designated 1-item buffer (i.e., packet), which is then read by a kernel level thread. Let us assume that there are X number of packets available, and M number of kernel level threads available that are dedicated to read these packets and send the data over the network. Once a user level thread copies data to a packet, the number of available packets decreases by one. If there are no empty packet, the user thread must go to sleep. Also, a thread has to go to sleep if it is not its turn to send data yet, even if there are empty packets. Once the data is written to the packet, the user thread terminates. Once the kernel thread read data from the packet, the number of available packets is increased by one, and kernel thread will wake up user threads who might be sleeping as there were no packets available. A kernel thread will go to sleep if all packets are empty and there is no data to send. A kernel level thread terminates once N user level threads are served. Think carefully how you can terminate the program. Each time a user level thread needs to sleep due to unavailable buffer or not being its turn, it needs to print printf("No packet available or not my turn to produce, user level thread %d going to sleep ", my_id); Each time a user thread gets to put data in a buffer, it should print - printf("User level thread %d is going to put data in a packet ", my_id); Each time a kernel level thread goes to sleep, it needs to print: printf("No data available, Going to sleep kernel thread %d ", my_id); Each time kernel thread serves a user thread, it should print - printf("user thread %d getting served ", servedId);

--- some things to note ---

1. Note that you are simulating user level threads (you can think of them as producer threads) and kernel level threads (you can think of them as consumer trheads). 2. You should not use busy waiting, and should use condition variable. 3. To add randomness within each thread, please use something similar to this to add random delay: usleep(10000 + 10000*(my_id*1000 % 5)); 4. Your program should take X, N, M as input from the command line, in this order.

-- desired output --

>./data_to_kernel 2 6 2

User level thread 1 is going to put data in a packet User level thread 2 is going to put data in a packet No packet available or not my turn to produce, user level thread 3 going to sleep No packet available or not my turn to produce, user level thread 4 going to sleep No packet available or not my turn to produce, user level thread 6 going to sleep No packet available or not my turn to produce, user level thread 5 going to sleep user thread 1 getting served user thread 2 getting served User level thread 3 is going to put data in a packet No packet available or not my turn to produce, user level thread 5 going to sleep No packet available or not my turn to produce, user level thread 6 going to sleep User level thread 4 is going to put data in a packet user thread 3 getting served User level thread 5 is going to put data in a packet No packet available or not my turn to produce, user level thread 6 going to sleep user thread 4 getting served User level thread 6 is going to put data in a packet user thread 5 getting served user thread 6 getting served

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!