C Programming

Sem_post 3 C Function

The “sem_post ()” method performs a semaphore unlock operation on the semaphore that is referenced by sem to unlock it. No threads were held up while waiting for the semaphore to unlock. Instead, the semaphore value is simply increased if the semaphore value obtained from this action is positive. The “sem_post ()” function raises (unlocks) the semaphore that sem indicated. A semaphore’s value is increased by one when a post is made using the sem_post () method. We will use this “sem_post ()” function here in our C programming and will show you how “sem_post ()” function will increase the value of the semaphore by one in this guide.

Syntax:

The syntax of this “sem_post ()” function in C programming is given below:

int sem_post(sem_t *sem);

Example # 1:

The examples in this guide are compatible with Ubuntu 20.04. The GCC compiler needs to be installed for Ubuntu 20.04 to execute these examples. When GCC is installed, a tiny portion of C code will be created to show how to utilize the “sem_post ()” function in C programming. A few header files are used at the start of the code. The default input/output header file is “stdio. h” and is available as follows. When writing the C code, we must include this header file. The “semaphore. h” header file is then added. To use the “sem_post ()” function, which is defined in this header file, we have included it.

The “main ()” function is then placed here. With this “main ()” function, the “int” data type is utilized. Additionally, there is “sem_t” with the name “my_sem”. In this case, “my sem” is declared along with the “num” of the “int” data type. We are using the “sem init ()” code below. There are three arguments in this “sem_init.” Here, the “&my_sem” parameter initializes the semaphore as the first parameter. The second option specifies how to share or use this semaphore. The second argument, in this case, is set to “0,” which indicates that this semaphore will only be used by threads that are currently operating.

The starting value of the semaphore is described by the third parameter. Here, “20” is entered as the semaphore’s initial value. Then we put the “sem_post ()” function here and pass “&my_sem” as its parameter. The “sem getvalue ()” method will then be used to retrieve the semaphore’s initial value. Here, the parameters “&my sem” and “&num” are passed. This will return the semaphore’s number after executing the “sem_post ()” function. Its initial value will be increased by one.

Now, by entering the “printf” statement in this location, we are printing this number after the execution of the “sem_post ()” function on the terminal screen. The information entered will appear on the screen.

To get the output of this code, we have two instructions. The first command must contain the “-lpthread” and “-lrt” before we can hit enter. We execute the second command after the successful compilation of this C code. The output is then displayed. You can see from this output that it shows the semaphore’s value after the “sem_post ()” function is incremented by one and displays “21”.

Example # 2:

We start our next example by placing the headers files “stdio. h” and “semaphore. h”. We must add these header files for this code. Now, we use the “int main ()” function. We have a semaphore named “new_sem” and declare the “number” as the “int” data type. We place the “sem_init ()” function and give three parameters that are necessary for this. We put the initial value of this semaphore as “20” and then utilize the “sem_post ()” function which will increment its initial value by one.

Then put the “sem_getvalue ()” function and pass two parameters of this function and utilize the “printf” below this. So, it will display the incremented value on the screen. We again put another “sem_post ()” function and put the same semaphore in this second “sem_post ()” function. Then, again utilize the “sem_getvalue ()” which we already discussed in our previous example. “printf” is utilized for printing the value after this second “sem_post ()” function.

You can see that first, it prints the value of the semaphore as “21” as we give the initial value of the semaphore as “20”. After the “sem_post ()” function, its values increment by one and display “21”. In the second line, the value of the semaphore is “22” because we again put the “sem_post ()” function.

Example # 3:

We slightly update the code mentioned previously. The code is the same as the one we established earlier. We place the two header files “stdio. h” and “semaphore. h” that is necessary for this code. We also utilize the “int main ()” function and we declare the semaphore and the value, an “int” data type, underneath this main function. After using “sem init” to establish the semaphore and passing its arguments, we use the “sem getvalue ()” method to retrieve the semaphore’s initial value. After that, print it as we’ve already explained. The “sem_post ()” function is a new one that we are adding here. This feature is used to increase the value of the semaphore. The result will show how the “sem_post ()” function increases the initial value of this semaphore. We then use the “sem_getvalue ()” function once again and using “printf ()”, we also print that value.

In this output, the initial value of the semaphore is displayed first and then the value after the “sem_post ()” function is displayed. You can notice that it increments the initial value of the semaphore.

Conclusion

We are writing this guide for you to learn more about the “sem_post ()” function. This guide covered the “sem_post ()” function’s codes, and results. The C function “sem_post ()” was covered in this guide. We’ve discussed how the “sem_post ()” function increases the value of the semaphore in C. We’ve given three separate examples here. The C source code necessary to use the “sem_post ()” function has been provided, along with the output for each example. We hope that you will understand this notion clearly after attentively reading this guide.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.