Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

semThreadNamed.c #include #include #include #include #include /* For O_* constants */ #include /* For mode constants */ sem_t *s; //a semaphore s //function prototype void*

image text in transcribed

semThreadNamed.c

#include #include #include #include #include /* For O_* constants */ #include /* For mode constants */

sem_t *s; //a semaphore s

//function prototype void* threadFunction(void* param);

int main() { char *name = "my_semaphore"; int VALUE = 1; //initialize semaphore to 1 s = sem_open(name, O_CREAT, 0666, VALUE); //if the semaphore has not been already created, create pthread_t t0,t1; int tnumber = 0; pthread_create(&t0,NULL,threadFunction,&tnumber); sleep(2); tnumber++; pthread_create(&t1,NULL,threadFunction,&tnumber); pthread_join(t0,NULL); pthread_join(t1,NULL); sem_close(s); sem_unlink(name); return 0; }

void* threadFunction(void* param) { int tnum; tnum = *((int *)param); sem_wait(s); //can I access my critical section? (semWait()) //critical section printf(" Thread %d has enetered its CS.. ", tnum); printf(" Thread %d is executing in its CS.. ", tnum); sleep(4); printf(" Thread %d is leaving its CS.. ", tnum); sem_post(s); //signal that the thread is leaving and the CS is free (semSignal()) pthread_exit(0); }

Examine the attached code in semThreadNamed.c. This code protects the critical section using an named semaphore. The critical section in this example is simply a message and is named semaphore version of the code from Task 1. Compile the code using gcc semThreadNamed.c -o sem2 -1pthread -lrt The - Ipthread and -1rt links the pthreads and real-time modules. Execute with . /sem2 and observe the output. Submit a screenshot of the output

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 Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions