Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help in Operating System : have a working code just want you to answer the question: Question: How long do you think the following

Need help in Operating System: have a working code just want you to answer the question:

Question:How long do you think the following execution, with one producer, three consumers, a single-entry shared buffer, and each consumer pausing at point c3 for a second, will take?(This is the code 2 producers and 2 consumers)

prompt> ./main-two-cvs-while -p 1 -c 3 -m 1 -C

0,0,0,1,0,0,0:0,0,0,1,0,0,0:0,0,0,1,0,0,0 -l 10 -v -t

Code:

#include

#include

#include

#include

#include

#include

#include

#include

#include "mythreads.h"

#include "pc-header.h"

// used in producer/consumer signaling protocol

pthread_cond_t empty = PTHREAD_COND_INITIALIZER;

pthread_cond_t fill = PTHREAD_COND_INITIALIZER;

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

#include "main-header.h"

void do_fill(int value) {

// ensure empty before usage

ensure(buffer[fill_ptr] == EMPTY, "error: tried to fill a non-empty buffer");

buffer[fill_ptr] = value;

fill_ptr = (fill_ptr + 1) % max;

num_full++;

}

int do_get() {

int tmp = buffer[use_ptr];

ensure(tmp != EMPTY, "error: tried to get an empty buffer");

buffer[use_ptr] = EMPTY;

use_ptr = (use_ptr + 1) % max;

num_full--;

return tmp;

}

void *producer(void *arg) {

int id = (int) arg;

// make sure each producer produces unique values

int base = id * loops;

int i;

for (i = 0; i < loops; i++) { p0;

Mutex_lock(&m); p1;

while (num_full == max) { p2;

Cond_wait(&empty, &m); p3;

}

do_fill(base + i); p4;

Cond_signal(&fill); p5;

Mutex_unlock(&m); p6;

}

return NULL;

}

void *consumer(void *arg) {

int id = (int) arg;

int tmp = 0;

int consumed_count = 0;

while (tmp != END_OF_STREAM) { c0;

Mutex_lock(&m); c1;

while (num_full == 0) { c2;

Cond_wait(&fill, &m); c3;

}

tmp = do_get(); c4;

Cond_signal(&empty); c5;

Mutex_unlock(&m); c6;

consumed_count++;

}

// return consumer_count-1 because END_OF_STREAM does not count

return (void *) (long long) (consumed_count - 1);

}

// must set these appropriately to use "main-common.c"

pthread_cond_t *fill_cv = &fill;

pthread_cond_t *empty_cv = ∅

// all codes use this common base to start producers/consumers

// and all the other related stuff

#include "main-common.c"

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_2

Step: 3

blur-text-image_3

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

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago

Question

What is the relationship between humans?

Answered: 1 week ago