The C language comes up with the semctl system call in the Linux system. This system call has been widely used in the Linux system to carry out the control processes specified by the console shell on some particular semaphore set. The semaphores are known IPs widely used to give access to processes for communication with one another. Semaphore can be a variable having abstract data type for the control access of 1 resource for many procedures. Hence, the semctl system call has been used here to control the semaphore set. Let’s start by signing up from the Linux system.

Open the console application from the search area. A quick alternative way to open the console is via “Ctrl Alt T”.

Example:

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-1.png" data-lazy- height="22" src="data:image/svg xml,” width=”331″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-2.png" data-lazy- height="19" src="data:image/svg xml,” width=”334″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-3.png" data-lazy- height="215" src="data:image/svg xml,” width=”518″>

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”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-4-1.png" data-lazy- height="119" src="data:image/svg xml,” width=”595″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-5.png" data-lazy- height="289" src="data:image/svg xml,” width=”480″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Semctl-System-Call-in-C-6.png" data-lazy- height="288" src="data:image/svg xml,” width=”538″>

Conclusion:

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.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content