Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

using C++ implementing Semaphores by using the sem_t class. This semaphore must be initialized as a counting semaphore through the sem_init function. The third parameter

using C++ implementing Semaphores by using the sem_t class. This semaphore must be initialized as a counting semaphore through the sem_init function. The third parameter in the sem_init function allows the programmer to specify the amount of processes the semaphore can be used by at once.

In main, create an output file output.txt to which to write to. Then create 3 processes that will each execute the same function.

In this function:

1) Implement a critical section through the use of semaphores that only allows one process to enter at once. This is done through c functions sem_wait() and sem_post() and sem_init()

2) Inside the critical section: Open the existing output file. Output to the file and console the process ID of the process that is in control of the semaphore. Sleep for 1 second.

Output to the output file and console that the process is giving up control of the semaphore. Close the output file.

3) meanwhile, in the main function, wait for each process to complete and print out that the process has completed.

Output on console sample:

Process 2001 has taken control of the critical section

writing to out file...

closing output file...

process 2001 is giving up control of the critical section.

--process P1 completed--

Process 2002 has taken control of the critical section

writing to out file...

closing output file...

process 2002 is giving up control of the critical section.

--process 2 completed--

Process 2003 has taken control of the critical section

writing to out file...

closing output file...

process 2003 is giving up control of the critical section.

--process 3 completed--

Output file:

Process 2001 has taken control of the critical section

Process 2001 is giving up control of the critical section.

Process 2002 has taken control of the critical section

Process 2002 is giving up control of the critical section.

Process 2003 has taken control of the critical section

Process 2003 is giving up control of the critical section.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions