• 0

Using semaphores


Question

Hello, this is the first time I need to use semaphores, yes it is for an assignment. I have trouble wrapping my head around the concept. Come to think of it, wrapping a solid spheroidal body around an idea isn't trivial by itself, but anyway I disgress.

I have to write a little program that simulates a network. There are several computers in the network. Only one computer can be executing at any given time, and they must execute one after the other. Also, each computer is responsible for telling the "next" computer that it is its time to execute.

I am supposed to use semaphores to synchronize this. Now I don't see really how can semaphores help me do this. My first idea would be to make a "cycle" object, protected with a mutex, so only one computer can own the cycle at any given time. The computer could write in a global variable the id of the next computer and unlock the mutex when it's done. However, we're supposed to use semaphores, not mutexes.

Oh and it's POSIX semaphores and pthreads btw.

Thanks.

Link to comment
https://www.neowin.net/forum/topic/749492-using-semaphores/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

I only covered semaphores on my course a few weeks back but I think the basic principle is the same as the mutex in that it creates a critical region that only one thread/process can access at one.

For example:

You spawn 2 threads to carry out some job, but only one can execute at the one time. Initially you set the semaphore to 1.

The first thread to call wait(sem) decrements the semaphore and is allowed to continue. The slower of the two threads is blocked at the wait statement because sem cannot be < 0. When the first thread is finished the work it is to carry out, it performs a signal(sem) which increments the semaphore, allowing the second thread to decrement it, and continue. This could also be looped so they can pass it back and forth between them. You will probably need a small wait after the signal though however to ensure that the other thread has enough time to perform the wait.

Think that's right. Hope it helps.

  • 0

Hmm, so you know how to do this using a mutex but not a semaphore? A mutex is basically just a binary semaphore. Of course since you said you need to control the order, right? As in there's a specific ordering to the "computers" and you want to loop through each, activating them in sequence? So couldn't you just have each node in the network provide its own semaphore that it blocks on until the preceding node signals it?

Or are you supposed to using a counting semaphore somehow, or only signal computers that have requested to be called when its their turn?

  • 0
  Quote
Hmm, so you know how to do this using a mutex but not a semaphore? A mutex is basically just a binary semaphore.
Well I know how pthread_mutex works but I never used semaphore.h. Also, I understand the concept of a mutex (locking/unlocking a resource), but I don't get the concept of a "non-binary" one. What, so you can lock the resource a certain number of times before it's, uh, really locked? :unsure:
  Brandon Live said:
Of course since you said you need to control the order, right? As in there's a specific ordering to the "computers" and you want to loop through each, activating them in sequence?
Yes exactly. It should go like this :

Computer 1 does cycle #1

Computer 2 does cycle #1

Computer 3 does cycle #1

Main thread does some checking routine

Computer 1 does cycle #2

Copmuter 2 does cycle #2

etc.

Each computer is its own thread.

  Quote
So couldn't you just have each node in the network provide its own semaphore that it blocks on until the preceding node signals it?
Can you explain your idea a bit further please ?
  Quote
Or are you supposed to using a counting semaphore somehow, or only signal computers that have requested to be called when its their turn?
It just says "use semaphors to order the computers". Edited by Dr_Asik
  • 0
  Dr_Asik said:
  Quote
So couldn't you just have each node in the network provide its own semaphore that it blocks on until the preceding node signals it?

Can you explain your idea a bit further please ?

I think he means that the first task signals the semaphore for task 2 so it can begin.

Maybe something like this?

Thread 1

task1 = 1;

wait(task1);

signal(task2);

Thread 2

The task2 semapohre is initially set to 0 until it is signalled from thread 1, so thread 2 blocks at the wait statement.

wait(task2);

.

.

.

.

signal(task3);

etc.

  • 0

Relinquishing the semaphore key is not a good idea, whos to say computer 3 wont get the key when its computer 2 that wants it.

C subroutines to checkout

sem_post(), sem_wait()

signals.h

in this u can do something like kill(PID, SIGINT); which will send the pid(computer#) a signal interrupt.

..
	signal(SIGINT, s_handler);
...

void s_handler(int sig)
{
	   switch(sig)
	   {
		case SIGINT:
			 //grab key and begin execution
		 }
}

once u set up the signals to recieve sigint, you can communicate with processes to grab the semaphore key instead of all processes waiting for it.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Finished Subnaitica 1 last year. In my 38 years of playing video games, it was such an awesome experience. Never have I felt a game that has left a mark. How did I manage that? I didn't read any guides or saw any videos until 15 hours in and also only looked up for very specific area I was stuck. Took me 50 -60 hours to finish.
    • Finished it recently. No weapons was just a great feature. You aren't military nor have military experience.
    • Excel, Word, and PowerPoint on the web grab welcome new feature by Usama Jawad The Microsoft 365 suite of software is a vital part of any organization that has decided to go with Microsoft's tech stack to meet its needs. As such, it's critical for the Redmond firm to continue upgrading the capabilities of its tools to retain its existing customers and attract new ones. And now, Microsoft has done just that by adding a very welcome new feature to some of its applications. Microsoft has announced that viewers of an Excel, PowerPoint, or Word file on the web can now directly request edit access to it. Previously, customers were forced to resort to workarounds to achieve the same purpose. One of these was to download a view-only file and make edits in a copy and reshare, while the other was to manually reach out to the owner of the file and ask them to give you edit access. Microsoft rightly acknowledged that these methods are too cumbersome and slow, so now it has integrated the option to directly request edit access in its web applications. In order to leverage this capability, customers need to do the following: Open the Excel, Word, or PowerPoint file on the web Click on the Viewing icon on the top-right Select Request more access Select the Ask to edit or Ask to review option, depending upon what you need You can optionally write a note to the file owner too; this can be used to provide more context about why you need additional rights Click send File owners will then receive an email with the request change in access along with the optional note, and they will be able to accept or deny the request directly from within the email. If they do the former, the requestor can refresh the file in order to get take advantage of increased access. That said, Microsoft has cautioned that the propagation of access elevation may take some time to reflect in large or complex documents, or those with many co-authors. Additionally, the capability is not supported in Classic View in Word for the web. However, it is available to all enterprise customers with Microsoft 365 and a stable internet connection as long as the file they are requesting elevated privilege for is available on OneDrive or SharePoint.
    • Things like these I would hope wasn't on the internet months prior launch. Having a WOW moment when it's presented to the audience for the first time would had been jaw dropping. (this amoung other tech accouncements)
  • Recent Achievements

    • One Year In
      K.I.S.S. earned a badge
      One Year In
    • Week One Done
      solidox earned a badge
      Week One Done
    • Dedicated
      solidox earned a badge
      Dedicated
    • Week One Done
      Devesh Beri earned a badge
      Week One Done
    • Week One Done
      956400 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      452
    2. 2
      ATLien_0
      166
    3. 3
      +FloatingFatMan
      150
    4. 4
      Nick H.
      67
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!