Question
operating system engineering , please read the question and solve on the given skeleton code .
Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem:
Function 4. void* access_buffer(void* arg) - arg should be the ID of the thread, remember odd ID is producer and even ID is c
1 2 3 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <semaphore> #define BUFF SIZE 2 4 5 6 7 int next_i
19 20 // check for producer/consumer thread and acquire required semaphores and mutex // update/read the buffer based on the
31 ال) a thread attribute value that allow the main thread to join with the threads it creates. Note also that we free up han
0 0
Add a comment Improve this question Transcribed image text
Answer #1

semaphores.c

#include "semaphores.h"

#ifdef WIN32

#include <Windows.h>
#include <stdlib.h>

struct _SemaphoreData
{
HANDLE counter;
size_t count, maxCount;
};

Semaphore Sem_Create( size_t startCount, size_t maxCount )
{
Semaphore sem = (Semaphore) malloc( sizeof(SemaphoreData) );
sem->counter = CreateSemaphore( NULL, startCount, maxCount, NULL );
sem->count = startCount;
sem->maxCount = maxCount;
  
return sem;
}

void Sem_Discard( Semaphore sem )
{
CloseHandle( sem->counter );
free( sem );
}

void Sem_Increment( Semaphore sem )
{
ReleaseSemaphore( sem->counter, 1, &(sem->count) );
sem->count++;
}

void Sem_Decrement( Semaphore sem )
{
WaitForSingleObject( sem->counter, INFINITE );
sem->count--;
}

size_t Sem_GetCount( Semaphore sem )
{
if( sem == NULL ) return 0;
  
return sem->count;
}

void Sem_SetCount( Semaphore sem, size_t count )
{
if( sem == NULL ) return;
  
if( count > sem->maxCount ) count = sem->maxCount;

size_t countDelta = (size_t) abs( (int) count - (int) sem->count );
if( count > sem->count )
{
for( size_t i = 0; i < countDelta; i++ )
Sem_Increment( sem );
}
else if( count < sem->count )
{
for( size_t i = 0; i < countDelta; i++ )
Sem_Decrement( sem );
}
}

#else // Unix

#include <semaphore.h>
#include <stdlib.h>
#include <malloc.h>

struct _SemaphoreData
{
sem_t upCounter;
sem_t downCounter;
size_t maxCount;
};

Semaphore Sem_Create( size_t startCount, size_t maxCount )
{
Semaphore sem = (Semaphore) malloc( sizeof(SemaphoreData) );
sem_init( &(sem->upCounter), 0, maxCount - startCount );
sem_init( &(sem->downCounter), 0, startCount );
  
sem->maxCount = maxCount;
  
return sem;
}

void Sem_Discard( Semaphore sem )
{
sem_destroy( &(sem->upCounter) );
sem_destroy( &(sem->downCounter) );
free( sem );
}

void Sem_Increment( Semaphore sem )
{
sem_wait( &(sem->upCounter) );
sem_post( &(sem->downCounter) );
}

void Sem_Decrement( Semaphore sem )
{
sem_wait( &(sem->downCounter) );
sem_post( &(sem->upCounter) );
}

size_t Sem_GetCount( Semaphore sem )
{
int countValue;
sem_getvalue( &(sem->downCounter), &countValue );
  
return (size_t) countValue;
}

void Sem_SetCount( Semaphore sem, size_t count )
{
if( count <= sem->maxCount )
{
size_t currentCount = Sem_GetCount( sem );
  
if( currentCount > count )
{
for( size_t i = count; i < currentCount; i++ )
Sem_Decrement( sem );
}
else if( currentCount < count )
{
for( size_t i = currentCount; i < count; i++ )
Sem_Increment( sem );
}
}
}

i hope this is helpful to you!

Thank you!

Add a comment
Know the answer?
Add Answer to:
operating system engineering , please read the question and solve on the given skeleton code ....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...

    Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...

  • I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so...

    I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so use int buffer[10]; // buffer is global variable and buffersize is 10 and there is only one producer and one consumer. 1) Producer() - At every 200msec, generates random positive integer and add to the buffer. - Then print current state of buffer (numbers and in/out indices) -Also print current state of two semaphore which are full...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...

    Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining space procedure producer() {    While (true) {    item = produceItem(); down(emptyCount); // emptyCount is decremented putItemIntoBuffer(item); up(fillCount); // fillcount is incremented } } procedure consumer() { While (true) {    down(fillCount); item = removeItem(); up(emptyCount); // emptyCount is incremented consumeItem(item); } } Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I...

    I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I need a source comment next to the line that gives the delay so i can compare please. (Concurrency – Semaphores) 1.- In this assignment you will implement a deadlock free variant of the bounded-buffer producer/consumer using jBACI (C - -). C- - is a subset of C + + that allows you to declare semaphores and apply the operations P and V. In the...

  • I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...

    I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer, int size, int *vec, int n) {    int i, j;    long long unsigned int sum = 0;    for(i=0;i<n;i++) {        if(i % 2 == 0) {   // PRODUCER            for(j=0;j<size;j++) {                buffer[j] = vec[i] + j*vec[i+1];            }        }        else {   // CONSUMER            for(j=0;j<size;j++) {...

  • Read given code and write all possible outputs of the program. Assume there will be no...

    Read given code and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there i only one possible output, you just need to write that output. Code: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c[2] = {0,1}; void *UpdateC1(void *arg) { int i; for(i=0;i<1000000;i++) { sem_wait(&s1); c[0]=(c[0]+1)%2; c[1]=(c[1]+1)%2; sem_post(&s1); } return NULL; } void *UpdateC2(void *arg) { int i; for(i=0;i<2000000;i++)...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT