Codingdumb
Codingdumb

Reputation: 11

How can I use Semaphore in this C code about multithreading

I am solving the assignment problem about thread. It's about sorting random lowercase characters. when it's sorted well, I print out O mark and or not, Print out X. using 2 threads.

I need to write code using semaphore, but I do not know how to and where to write sempahore.

So once I wrote the code without semaphore. I should add the Sempahore now. but i don't know what part to fix.

Could you help me? Do i have to make one more function for semaphore?

It's my code in C.

Upvotes: 0

Views: 126

Answers (1)

HowellJenkins
HowellJenkins

Reputation: 28

As ThingyWotsit said, do go back to your professor.

For the semaphore, think of its concept like a gate.

  • The aforementioned sem_post() will increase the semaphore's counter by 1.
  • sem_wait() will permit a thread passage and decrement the counter by 1;
  • however, if after a sem_wait() the counter reaches 0, any other sem_wait() will halt the thread, close the gate until sem_post is > 1.

I hope that aids you in your endeavor.

Upvotes: 1

Related Questions