I am implementing semaphore in c language.I have a POSIX counting semaphore. I want to assign a value to it. and i don't want to use Wait or Post. Can i do that? Is there any function like "setValue" for POSIX Semaphore ?
How to set POSIX semaphore value to 1?
6.7k Views Asked by KrunalParmar At
2
There are 2 best solutions below
0
too honest for this site
On
If you could directly change its value during normal operation (i.e. except for initialization), it would not be a semaphore anymore. So you might be looking for something different, maybe a thread-safe counter/shared variable? Such more complex shared objects are normaly implemented with the basic synchronization primitives, like locks/mutex/semaphore/etc. Which to use depends on what you want to implement.
OTOH, you are possibly presenting an XY-problem. Perhaps if you state what you actually want to achieve, we can point you to a better/easier/ solution.
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in POSIX
- How to write data to stdin of the first process in a Python shell pipeline?
- C - does read() add a '\0'?
- What does a POSIX interface refer to in terms of microkernels?
- mq_timedsend() returns error 14 "bad address"
- Posix message queues and the command line?
- Copying existing function into memory buffer
- Ghost variable "idle" in terminal/cygwin?
- converting "1984-03-25 02:00:00" to POSIX gives NA
- Issue with pthread_setschedparam, system hangs
- tee stdout and stderr independently, without adding jitter
- POSIX-compliant file locking (within a single process)?
- Linux : pthread_cond_signal() is not working inside a Signal Handler()
- Compiling GRUB on Cygwin (64 bit)
- Counting bytes received by posix read()
- Bash: "invalid number" error when passing array elements value to variable
Related Questions in SEMAPHORE
- Implement wait-notify on database level to handle deadlocks
- How to synchronise two processes using a semaphore
- Synchronizing processes with semaphores and signals in C
- "S->value <= 0" signal() implementation in semaphore with no busy waiting
- Create Semaphore in PHP linux (Centos)
- How can I use semaphore to do a correct android ble communication?
- Synchronize a program with semaphores
- Named Semaphore just not working
- Semaphores and Web Sockets
- deadlock using Semaphore
- C Semaphore strange precedence behavior
- Trying to get a Boost named_semaphore to use the Windows semaphore API
- How can I control thread count when I use "Task.WhenAll"
- How can I serialize access to a directory in Linux?
- Blocked Streaming Class with Semaphore in delphi
Related Questions in BINARY-SEMAPHORE
- How is semaphore queue protected with multiple threads
- Error while compiling the code 'double free or corruption (out)' using threads in C?
- Synchronization between Semaphore/Mutex and Printf
- What happens without a binary semaphore
- Single use condition_variable
- Multithread Binary Semaphore's: Alternating Outputs
- Synchronize parent and forked process using POSIX named semaphores to print in text file
- Usage of spinlocks and semaphore in linux in process and interupt context
- Making binary semaphore shared between multiple processes(not threads , Process Only) using POSIX in C language
- How to set POSIX semaphore value to 1?
- Avoid taking a long time to finish the 'too much milk' scenario
- Actual difference between Mutex and Binary semaphore
- Java: stuck in an infinite loop
- How does binary semaphore guarantee mutual exclusion?
- How do binary semaphores achieve equal execution and starvation?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Wait and Post are the only operations supported by a classic semaphore. POSIX semaphores can be initialized with a count using sem_init(). Any kind of 'setValue' function would destroy the functionality of the semaphore by allowing units to be 'lost', eg. by being posted by one thread just before 'setValue' was called by another.
It's a really bad idea, which is why it is not implemented.