Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming in C, producer consumer, using semaphore Producer- consumer problem Write a program to synchronize processes with semaphores: a parent process fills a shared buffer

Programming in C, producer consumer, using semaphore

image text in transcribed

Producer- consumer problem Write a program to synchronize processes with semaphores: a parent process fills a shared buffer and sleeps for n seconds, and seconds. You will need the buffer, counter and semaphore, all stored in the shared memory (Hint: use a sem_t semaphore to your shared memory area. Initialize the semaphore with sem init. Perform the operation on the buffer and the counter in the critical section guarded by the sem wait and sem_post operations.) a child process prints the contents of the buffer and sleeps for m Experiment with the program by changing the (relative) production rate and the consumption rate (change sleep times n and m) to achieve a processes alternation. Explain the results Once you complete a task, please submit your solution via D2L. POSIX semaphore functions and types are defined in semaphore.h To define a semaphore object, use sem_t sem_name, To initialize a semaphore, use sem init(0 int seminit(sem_t *sem, int pshared, unsigned int value); .sem points to a semaphore object to initialize pshared is a flag indicating whether or not the semaphore should be shared with forkOed processes. LinuxThreads does not currently support shared semaphores value is an initial value to set the semaphore to Example of use: sem init(&sem name, 0, 10); To wait on a semaphore, use sem_wait: int sem_wait(semt sem) If the value of the semaphore is negative, the calling process blocks; one of the blocked processes wakes up when another process calls sem_post. Example of use: sem_wait(&sem_name); To increment the value of a semaphore, use sem_post: int sem_post(sem t *sem); It increments the value of the semaphore and wakes up a blocked process waiting on the semaphore, if any Example of use: sem post(&sem_name)

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 Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

Advance warning and an explanation for the layoff.

Answered: 1 week ago