Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to create the user level thread library called threadlib . The library should create user level threads, map the user level threads to

I need to create the user level thread library called threadlib. The library should create user level threads, map the user level threads to a kernel thread following the many to one mapping model, schedule the mapping based on the user-level thread priority numbers (i.e., when the kernel thread becomes available, the user thread with highest priority should be mapped to it; when there are multiple highest priority user threads, the thread at the head of the priority ready queue should be selected for mapping), and support inter thread message passing. Please run the test code below before sumbit your answer

/************** Here are the function requirment for this threadlib ********************/

thread_set_init()

/* functions can be called. It initializes the uthread system. */

int thread_create(void (* func)( ), int priority)

/* The calling thread requests the thread library to create a new user level thread that runs the function func(), which is specified as the first argument of this function. A context of this new user thread should be properly created and stored on the priority ready queue, based on the thread priority number, which is specified as the second argument of this function.The ready queue should be a priority queue that sorts all ready threads based on their priorities. The smaller is a priority number, the higher is the priority. */

void thread_yield()

/* The calling thread requests to yield the kernel thread to another user level thread with the same or higher priority. If each ready thread has lower priority than this calling thread, the calling thread will continue itsrunning; otherwise, the kernel thread is yielded to a ready thread with the highest priority. */

void uthread_exit()

/* This function is called when the calling user level thread terminates its execution. In respond to this call, if no ready user thread in the system, the whole process terminates; otherwise, a ready user thread with the highest priority should be mapped to the kernel thread to run. */

int thread_send(int tid, void *content, int size)

/* The calling thread requests to send a message pointed to by argument content to the thread with ID tid. The third argument, size, specifies the size of the message. If there is no user thread with ID tid, the function returns 1; otherwise 0 is returned. Note that, this sending operation is non-blocking. */

int thread_recv(int tid, void **content)

/* The calling thread requests to retrieve a message sent by a thread of IDtid. If there is no message from tid available, the calling thread is blocked (i.e., it should be put into a waiting queue to wait to be moved back to the ready queue until the thread with ID tid sends a message to it) When returning from this function, the starting address of the retrieved message should be saved in *content, which is a variable of type void*,and the function returns an integer which is the size of the retrieved message. In the case of failure, the function returns 1. Note that, once a message has been retrieved, it is consumed and cannot be retrieved again. */

/*********************** Here are the Test Code ***********************************/

#include

void th0 (){

int i;

for(i=0;i<3;i++){

printf("This is thread 0. ");

sleep(1);

uthread_yield();

}

printf("Thread 0 exit. ");

uthread_exit();

}

void th1 (){

int i;

for(i=0;i<3;i++){

printf("This is thread 1. ");

sleep(1);

uthread_yield();

}

printf("Thread 1 exit. ");

uthread_exit();

}

void th2 (){

int i;

for(i=0;i<3;i++){

printf("This is thread 2. ");

sleep(1);

uthread_yield();

}

printf("Thread 2 exit. ");

uthread_exit();

}

int main(){

uthread_init();

uthread_create(th0,1);

uthread_create(th1,2);

uthread_create(th2,1);

uthread_exit(); }

/*********************** Here is what the output should look like ***********************************/

This is thread 0

This is thread 2

This is thread 0

This is thread 2

This is thread 0

This is thread 2

Thread 0 exits

Thread 2 exits

This is thread 1

This is thread 1

This is thread 1

Thread 1 exits

/*********************** Here are the send and recv message test code ***********************************/

#include

void th0 () {

int i;

for(i=0;i<3;i++){

if(i==0) continue;

printf("This is thread 0. ");

char msg[256];

sprintf(msg, "greeting from %d to %d", 0, i);

uthread_send(i,msg,256);

printf("Thread %d has sent to Thread %d: %s ", 0, i, msg);

char *rcv_msg;

uthread_recv(i,(void **)&rcv_msg);

printf("Thread %d receives from Thread %d: %s ", 0, i, rcv_msg);

}

printf("Thread 0 exit. ");

uthread_exit();

}

void th1 () {

int i;

for(i=0;i<3;i++){

if(i==0) continue;

printf("This is thread 1. ");

char msg[256];

sprintf(msg, "greeting from %d to %d", 1, i);

uthread_send(i,msg,256);

printf("Thread %d has sent to Thread %d: %s ", 1, i, msg);

char *rcv_msg;

uthread_recv(i,(void **)&rcv_msg);

printf("Thread %d receives from Thread %d: %s ", 1, i, rcv_msg);

}

printf("Thread 1 exit. ");

uthread_exit();

}

void th2 () {

int i;

for(i=0;i<3;i++){

if(i==0) continue;

printf("This is thread 2. ");

char msg[256];

sprintf(msg, "greeting from %d to %d", 2, i);

uthread_send(i,msg,256);

printf("Thread %d has sent to Thread %d: %s ", 2, i, msg);

char *rcv_msg;

uthread_recv(i,(void **)&rcv_msg);

printf("Thread %d receives from Thread %d: %s ", 2, i, rcv_msg);

}

printf("Thread 2 exit. ");

uthread_exit();

}

int main(){

uthread_init();

uthread_create(th0,1);

uthread_create(th1,1);

uthread_create(th2,1);

uthread_exit();

}

/*********************** Here is what the output should look like ***********************************/

This is thread 0.

Thread 0 has sent to Thread 1: greeting from 0 to 1

This is thread1.

Thread 1 has sent to Thread 0: greeting from 1 to 0

Thread 1 receives from Thread 0: greeting from 0 to 1

This is thread 1.

Thread 1 has sent to Thread 2: greeting from 1 to 2

This is thread 2.

Thread 2 has sent to Thread 0: greeting from 2 to 0

Thread 0 receives from Thread 1: greeting from 1 to 0

This is thread 0.

Thread 0 has sent to Thread 2: greeting from 0 to 2

Thread 0 receives from Thread 2: greeting from 2 to 0

Thread 0 exit.

Thread 2 receives from Thread 0: greeting from 0 to 2

This is thread 2.

Thread 2 has sent to Thread 1: greeting from 2 to 1

Thread 2 receives from Thread 1: greeting from 1 to 2

Thread 2 exit.

Thread 1 receives from Thread 2: greeting from 2 to 1

Thread 1 exit.

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 Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

Derive expressions for the rates of forward and reverse reactions?

Answered: 1 week ago

Question

Write an expression for half-life and explain it with a diagram.

Answered: 1 week ago

Question

What do you mean by underwriting of shares ?

Answered: 1 week ago

Question

Define "Rights Issue".

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago