Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COMPLETE THE FOLLOWING PROGRAM IN C: disk1, disk2, disk3 are all modified - disk.c files! disk.c: #include #include #include #include #include #include #define ITERATIONS 10

COMPLETE THE FOLLOWING PROGRAM IN C:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

disk1, disk2, disk3 are all modified - disk.c files!

disk.c:

#include

#include

#include

#include

#include

#include

#define ITERATIONS 10

#define DEFAULT_NWRITEJOBS 10

#define DEFAULT_NREADJOBS 10

#define work() do {random_r(&buf, &random); usleep(random % 50000);} while (0)

#define usedisk() do {random_r(&buf, &random); usleep(random % 10000);} while (0)

typedef struct disk {

int numwritejobs; // number of writejobs in the disk

int numreadjobs; // number of readjobs in the disk

pthread_mutex_t mutex; // mutex to protect shared info

pthread_cond_t available; // condition variable

} disk_t;

typedef struct thread_data {

int id;

disk_t* shared_info;

} thr_data;

void * writejob(void * arg_orig)

{

thr_data *arg = arg_orig;

int i, id = arg->id;

disk_t* shared_info = arg->shared_info;

int32_t random;

struct random_data buf;

char state[64];

initstate_r(id, state, sizeof(state), &buf);

for(i=0; i

work();

pthread_mutex_lock(&shared_info->mutex);

while(shared_info->numreadjobs > 0) { // must wait

printf("writejob #%2d waits ", id);

pthread_cond_wait(&shared_info->available, &shared_info->mutex);

}

shared_info->numwritejobs++;

printf("writejob #%2d enters ", id);

pthread_mutex_unlock(&shared_info->mutex);

usedisk();

pthread_mutex_lock(&shared_info->mutex);

shared_info->numwritejobs--;

if(shared_info->numwritejobs==0)

pthread_cond_broadcast(&shared_info->available);

printf("writejob #%2d exits ", id);

pthread_mutex_unlock(&shared_info->mutex);

}

return NULL;

}

void* readjob(void * arg_orig)

{

thr_data *arg = arg_orig;

int i, id = arg->id;

disk_t* shared_info = arg->shared_info;

int32_t random;

struct random_data buf;

char state[64];

initstate_r(id, state, sizeof(state), &buf);

for(i=0; i

work();

pthread_mutex_lock(&shared_info->mutex);

while(shared_info->numwritejobs > 0) { // must wait

printf("readjob #%2d waits ", id);

pthread_cond_wait(&shared_info->available, &shared_info->mutex);

}

shared_info->numreadjobs++;

printf("readjob #%2d enters ", id);

pthread_mutex_unlock(&shared_info->mutex);

usedisk();

pthread_mutex_lock(&shared_info->mutex);

shared_info->numreadjobs--;

if(shared_info->numreadjobs==0)

pthread_cond_broadcast(&shared_info->available);

printf("readjob #%2d exits ", id);

pthread_mutex_unlock(&shared_info->mutex);

}

return NULL;

}

int main(int argc, char *argv[])

{

int i, n, nwritejobs = DEFAULT_NWRITEJOBS, nreadjobs = DEFAULT_NREADJOBS;

for(i = 1; i

if(strncmp(argv[i], "-w", strlen("-w")) == 0) {

nwritejobs = atoi(argv[++i]);

}

else if(strncmp(argv[i], "-r", strlen("-r")) == 0) {

nreadjobs = atoi(argv[++i]);

}

else {

fprintf(stderr, "Usage: %s [-w N|-writejobs N] [-r N|-readjobs N] ", argv[0]);

return 1;

}

}

disk_t shared_info;

shared_info.numwritejobs = shared_info.numreadjobs = 0;

pthread_mutex_init(&shared_info.mutex, NULL);

pthread_cond_init(&shared_info.available, NULL);

n = nwritejobs + nreadjobs; // number of threads to create

pthread_t tid[n];

thr_data data[n];

for(i = 0; i

data[i] = (thr_data){i,&shared_info};

pthread_create(&tid[i], NULL, writejob, &data[i]);

}

for(i = nwritejobs; i

data[i] = (thr_data){i-nwritejobs,&shared_info};

pthread_create(&tid[i], NULL, readjob, &data[i]);

}

for(i = 0; i

pthread_join(tid[i], NULL);

}

return 0;

}

Problem Statement In a computing system, there is often multiple jobs running concurrently. Let us assume that each of these jobs accesses the disk multiple times either to read data or write data. To keep it simple, we assume that each job accesses disks only to read or write, but not both. Also, they access disk only a fixed number of times (which is hard coded in the given program). In this homework, we provided you disk.c program that simulates a single disk and uses a mutex and a condition variable to solve the problem of disk access under the constraint that read and write requests cannot use the disk at the same time. Specifically, the disk.c program takes two optional command line parameters (the number of read jobs and write jobs, respectively), then creates a thread for each job and simulates disk use under some highly contentious conditions. For the complete command line options, build the executables using make and run $./disk -h Use mutexes and condition variables only in this assignment. Other mechanisms do not necessarily help. In the descriptions, several terms have similar meaning. When a read (or write) job is dispatched, it starts to access disks and becomes active. Problem Statement In a computing system, there is often multiple jobs running concurrently. Let us assume that each of these jobs accesses the disk multiple times either to read data or write data. To keep it simple, we assume that each job accesses disks only to read or write, but not both. Also, they access disk only a fixed number of times (which is hard coded in the given program). In this homework, we provided you disk.c program that simulates a single disk and uses a mutex and a condition variable to solve the problem of disk access under the constraint that read and write requests cannot use the disk at the same time. Specifically, the disk.c program takes two optional command line parameters (the number of read jobs and write jobs, respectively), then creates a thread for each job and simulates disk use under some highly contentious conditions. For the complete command line options, build the executables using make and run $./disk -h Use mutexes and condition variables only in this assignment. Other mechanisms do not necessarily help. In the descriptions, several terms have similar meaning. When a read (or write) job is dispatched, it starts to access disks and becomes active

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions