Question: Please fix this C code and run in Linux ubuntu And plese put the sanpshot of working execution if you can. #include #include #include #include

Please fix this C code and run in Linux ubuntu

And plese put the sanpshot of working execution if you can.

#include

#include

#include

#include

#include

%Defining a semaphore that will be used in /dev/shm/

#define SEM_NAME "/name.mutex"

int main(int argc, char *argv[])

{

sem_t *sem;

//initialize to 1

sem = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);

if (sem==SEM_FAILED) {

printf("%s sem_open failed!", SEM_NAME);

return (-1);

}

//wait()

sem_wait(sem);

printf("critical section! ");

//signal()

sem_post(sem);

return 0;

}

The compilation or execution code is

gcc o test test.c -lpthread

A few things of special note:

You must name your semaphore with a unique name, such as "name.mutex. Otherwise your program would be working with someones semaphore or not being able to open it. This might be not a problem in the Ubuntu running in VirtualBox, but imagine a server with all different users!

There is a location in the system where you can find the actual semaphore. Its at /dev/shm/. Look for your own semaphore there. And make sure that it is removed before you run new programs. This is critical during debugging phase.

There is one step missing in the above code. It is related to a function called sem_unlink(). You should find out what it does and how to use it (try man sem_unlink in Ubuntu). There might be other function calls that will do the same job, but please focus on sem_unlink.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!