The sem_getvalue() function gets the value of the semaphore. It gets the value either it is a named semaphore or unnamed semaphore. In the case of the zero value of the semaphore, it returns a negative value. When some threads are waiting on the semaphore, it returns a negative value. The absolute value of that kind of negative integer represents the number of threads awaiting on the semaphore. The “sem_getvalue()” function is defined in the “semaphore. h” header file. We will go through this “sem_getvalue()” function in this guide and will demonstrate the examples where we utilize this “sem_getvalue()” function to provide you with a good understanding of this concept.
Syntax:
Example #1:
This guide’s examples will work with Ubuntu 20.04. To run these examples on Ubuntu 20.04, the GCC compiler must be installed. A small bit of the C code is generated when GCC is installed to demonstrate how to use the “sem_getvalue()” function in the C programming. A few header files are used at the commencement of the code. We have the “stdio. h” header file as follows which is the default input/output header file. We must add this header file when we are writing the C code.
After this, we put the “semaphore. h” header file. We include this header file so that we can use the “sem_getvalue()” function as it is defined in this header file. Then, we put the “main()” function here. We use the “int” data type with this “main()” function. We also have the “sem_t” with the name “my_sem”. Here, “my_sem” is declared and the “num” of the “int” data type is also declared. Below this, we utilize the “sem_init()” function. This “sem_init” contains three parameters. The first parameter is the “&my_sem” which initializes the semaphore. The second parameter indicates how this semaphore is used or shared.
Here, we put “0” as the second parameter which means that it is the only thread running in the existing process that is going to use this semaphore. The third parameter describes the preliminary value of the semaphore. Here, we put the initial value of this semaphore as “10”. After this, we have the “sem_getvalue()” function which gets the preliminary value of the semaphore. We pass the two parameters as “&my_sem” and “&num”. The semaphore’s number is obtained in this way. Now, we print this number on the terminal screen by typing the “printf” statement. The data written here is rendered on the screen.
We have two commands to obtain the output of this code. We must put the “-lpthread” and “-lrt” with the first instruction and press enter. After a successful compilation, we run the second command and render the output. You can see in this output that it displays the initial value of the semaphore.
Example #2:
We have two header files here, “stdio. h” and “semaphore. h”, which we have already discussed in our previous code. We have the “int main()”. Below this, there is the “sem t” named “my_sem” and “new_sem”. In this case, “my_sem” and “new_sem” are declared and the “num” and “value” are also declared as the “int” data type. We also have the “sem_init()” function and pass the three parameters.
Then, we utilize the “sem_getvalue()” function to get the initial value which we passed as a third parameter of the “sem_init” function and print this value on the screen with the help of the “printf” fucntion. Below this, we again utilize the “sem_init()” function in the same way as we utilized the previous function. Then, we get the initial value with the help of the “sem_getvalue()” function and print it.
Here, we have two different lines on the output. First, we get the value of the first semaphore and display it and then get the initial value of the second semaphore and print it on the output screen.
Example #3:
We update the previous code a little bit here. The code is identical to the one we developed earlier. We place the two header files – “stdio. h”, and “semaphore. h” – which are essential for this code and utilize the “int main()” function. Below this main function, we declare the semaphore and the value which is the “int” data type. Then, we initialize the semaphore using the “sem_init” and pass the parameters of this function. Then, we get the initial value of this semaphore using the “sem_getvalue()” function. Then print it in the same manner as we described previously.
Here, we add a new function which is the “sem_wait()” function. We utilize this function to lock the semaphore. Then, we again utilize this “sem_getvalue()” function. You will see in the output how it gets the initial value after the “sem_wait()” function. We also print that value using the “printf()” function.
In this screenshot, you can see that the first initial value of the semaphore is “10” but after the “sem_wait()” function, it gets the value which is “9”. It prints the “9” value after the “sem_wait()” function.
Conclusion
We provided this guide for you to explain the “sem_getvalue()” function in detail. The purpose, codes, and outcomes of the “sem_getvalue()” function were addressed in this guide. This guide already discussed the C function “sem_getvalue()”. We discussed that the “sem_getvalue()” function in C is used to get the value of the semaphore. We have gone through the three different examples. Each example’s output as well as the C source code are required to utilize the “sem_getvalue()” function are provided. After carefully reading this guide and running those codes on your own, we are certain that you will grasp this concept easily.