Semaphores are very useful in process synchronization and multithreading. But how to use one in real life, for example say in C Language?
Well, we have the POSIX semaphore library in Linux systems. Let’s learn how to use it.
The basic code of a semaphore is simple as presented here. But this code cannot be written directly, as the functions require to be atomic and writing code directly would lead to a context switch without function completion and would result in a mess.
The POSIX system in Linux presents its own built-in semaphore library. To use it, we have to :
- Include semaphore.h
- Compile the code by linking with -lpthread -lrt
To lock a semaphore or wait we can use the sem_wait function:
int sem_wait(sem_t *sem);
To release or signal a semaphore, we use the sem_post function:
int sem_post(sem_t *sem);
A semaphore is initialised by using sem_init(for processes or threads) or sem_open (for IPC).
sem_init(sem_t *sem, int pshared, unsigned int value);
- sem : Specifies the semaphore to be initialized.
- pshared : This argument specifies whether or not the newly initialized semaphore is shared between processes or between threads. A non-zero value means the semaphore is shared between processes and a value of zero means it is shared between threads.
- value : Specifies the value to assign to the newly initialized semaphore.
To destroy a semaphore, we can use sem_destroy.
To declare a semaphore, the data type is sem_t.
Compilation should be done with gcc a.c -lpthread -lrt
2 threads are being created, one 2 seconds after the first one.
But the first thread will sleep for 4 seconds after acquiring the lock.
Thus the second thread will not enter immediately after it is called, it will enter 4 – 2 = 2 secs after it is called.
So the output is:
Entered.. Just Exiting... Entered.. Just Exiting...
Entered.. Entered.. Just Exiting... Just Exiting...
This article is contributed by Suprotik Dey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference Between C Language and LISP Language
- C Programming Language Standard
- A C Programming Language Puzzle
- C Language Introduction
- Arrays in C Language | Set 2 (Properties)
- Convert C/C++ code to assembly language
- Signals in C language
- Constants vs Variables in C language
- Difference between %d and %i format specifier in C language
- lvalue and rvalue in C language
- kbhit in C language
- Benefits of C language over other programming languages
- fgets() and gets() in C language
- isxdigit() function in C Language
- isupper() function in C Language
- isalnum() function in C Language
- Stopwatch using C language
- trunc() , truncf() , truncl() in C language
- Assigning multiple characters in an int in C language
- chdir() in C language with Examples
Improved By : ParulShandilya