PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] How do I implement my own semaphore in C?

 September 20, 2022     c, consumer, function, producer, semaphore     No comments   

Issue

I'm working on solving the Producer/Consumer problem through different methods. The one I'm currently working on involves my own implementation of a semaphore, paired with Peterson's solution to protect the semaphore's down() and up() function calls.

The problem is, I'm working in C, and the only way I can think of creating a semaphore would be to use a struct with function pointers for down() and up(). Is this the right idea?


Solution

You will likely want to use some OS-provided or hardware-provided synchronization primitive. Examples include system calls (e.g. Linux's futex call, Windows' EnterCriticalSection), hardware swap calls (e.g. cmpxchg on x86, ldrex/strex on ARM), or library functions which use one of these methods (e.g. pthread_mutex).

Trying to do synchronization without any help from the OS or hardware is likely to be quite difficult.



Answered By - nneonneo
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing