Open the console application from the search area. A quick alternative way to open the console is via “Ctrl+Alt+T”.
Create a new C file in the home folder of the Linux system with the utilization of the “touch” command. We have named this C file as “semctl.c”. You can name it as you want. The query is as follows:
The GNU nano editor will be used to edit and open the file. So, the stated below query has been used to open the “semctl.c” file.
As shown in the image beneath that the file has already been opened in the editor. Write out the same code in your file to see the working of the “semctl()” system call. The header file for input-output and semaphores have been included at the start of the C file. The main method has been declared along with another function, “get_val()”. Starting from the main method, we have defined a variable “v” getting its value from the get_val function. This method has been called while passed with two arguments in the parameters. Now the control has been given to the function get_val(). This function contains two arguments, e.g. sid as semaphore id and “semnum” as semaphore number. The passed values 1 and 4 will be saved to these arguments.
The semctl command has been utilized within the function to get the Current semaphore set using semaphore ID and semaphore number. The GETVAL function has been used here to get the current semaphore value. After the collection of a semaphore value, it has been returned to the main method. The value has been stored in the variable “v”. This variable value will be displayed using the printf statement shown in the C code. You can now save the completed code by “CTRL+S” and leave it back to the console by utilizing “Ctrl+X” shortcut commands.
Let’s compile the “semctl.c” file in the console. Use the recommended “gcc” compiler for such types of complicated C codes. The compilation returns the warning, but there is no need for panic. Execute your file with the “a.out” command. The output shows that the current semaphore value is “-1”.
Let’s open the file once again to make it different a little bit. Open it using the “nano” editor and define the variable “MAX_COMPS” with some value, e.g. 10. This time update the code with the shown below script. Start from the initialization of the main method. It contains the function call of the method comp_use(). Now the control has been given to the method comp_use.
The function comp_use contains the “for” loop to use the variable “MAX_COMPS” value as a max value to the “for” loop. Until the loop ends, the print statement will show the computer number. It also gets and prints the current semaphore value from the method “get_val” by calling it here. The get_val method got executed and returned its value to the print statement of function comp_use() to display it on the screen. Save the code file once more and quit it to come back.
Now we have to compile the updated code with the “GCC” command for compilation. After the successful compilation, we have executed the file and got the semaphore value for each computer.
The semctl system call and its working has been demonstrated clearly in this guide with C language using Ubuntu 20.04 Linux system. We hope you will find this article helpful while understanding and learning the concepts of semaphores in the Linux system.