The prctl system call has been used in the C language to manipulate diverse characteristics of the calling function or process activities. The first parameter of the “prctl” system call defines what has to be done with the initialised values in header. All the other arguments or parameters would be used as per the first argument and its worth. Let’s take a deep glance at the “prctl” system call in C while we have been working on the Ubuntu 20.04 at the time of implementing this article.

Example 01:

Open and log in from Ubuntu 20.04 and launch the application named “terminal” from the activity area. This could be done by utilizing a simple key shortcut “Ctrl Alt T” on your desktop. Create a C-type file to implement the prctl() system call, perform the command shown in the snap underneath.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-51.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”323″>

After creation, let’s open the file with a GNU Nano editor as per the shown instruction.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-48.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”355″>

Add the code shown in the snap image underneath within the GNU file. The code contains necessary header files for the working of a prctl() code. Then we have created and defined 4 threads named process1, process2, process3, and process4. All 4 processes or functions contain the void as a general or signature parameter, but it could be something else. As we have elaborated before, the first parameter of the “prctl()” system call will show what we have to do with the calling function. So, We have called prctl() in all 4 methods to set the name of a process by using the “PR_SET_NAME” argument. After the 2 second sleep, the puts function will be executed to set the name of a process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-47.jpg" data-lazy- height="453" src="data:image/svg xml,” width=”571″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-43.jpg" data-lazy- height="453" src="data:image/svg xml,” width=”571″>

Then we have declared an array type pointer named “fp” and its elements contain the names of 4 methods or processes. The main method declared a variable “id” here indicates processes. The “for” loop has been used here to create a child process for every parent process using the “fork()” method and save that to variable “int”. The “if” statement has been used to check if the “id” is 0. If the condition meets, it will print the child process number, and the “fp” array will be used as a method to fetch the first element, process 1, and so on until the loop ends. The calling of methods in this way would make it execute all the methods defined above.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/5-38.jpg" data-lazy- height="311" src="data:image/svg xml,” width=”689″>

Compile the file first.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/6-33.jpg" data-lazy- height="20" src="data:image/svg xml,” width=”410″>

The execution of the file shows the below output. The name has been set for each process.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7-29.jpg" data-lazy- height="150" src="data:image/svg xml,” width=”424″>

Example 02:

Let’s have another illustration of prctl. Let’s open the prctl.c file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/8-30.jpg" data-lazy- height="23" src="data:image/svg xml,” width=”338″>

After the headers have been included, the method “cap_1” has been initialized. The file descriptor “f” has been defined, and a variable “res” has been initialized with a value “-1”. Now the file descriptor will be used to get the maximum capability from the kernel. The file descriptor will open the file as read-only from the kernel folder. If the file descriptor gets more than 0 characters, the “buf” array will be defined with size 32. Two integers have been defined, and the read method has been used to get the data from the buffer using file descriptor and saved to the variable “num”. If the variable “num” value is greater than 0, the index-matched value of variable “num” will be initialized as Null. The “sscanf” method will bind the “res” pointer with the “buf” array and store it within variable “r”. That’s how maximum capability could be got from the kernel. If the value for variable “r” does not equal 1, it will update the value of “res” with “-1” again. In the end, the descript has been closed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/9-25.jpg" data-lazy- height="480" src="data:image/svg xml,” width=”678″>

The second method, “cap_2” has been used to initialize the capability variable equals 0. The prctl() method use “PR_CAPBSET_READ” to read the maximum capability. If the capability’s value is greater than 0, it will be incremented. When the capability gets to 0, it will stop incrementing and return the “cp” value with a decrement of 1.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/10-23.jpg" data-lazy- height="173" src="data:image/svg xml,” width=”698″>

The main method is getting the capability from the “cap_1” and cap_2 and print it upon the condition is met.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/11-21.jpg" data-lazy- height="259" src="data:image/svg xml,” width=”714″>

The compilation and running of this file show that the maximum capacity value is 40.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/12-18.jpg" data-lazy- height="72" src="data:image/svg xml,” width=”625″>

Conclusion:

In this guide, we have discussed two examples to elaborate on the prctl() system call in C. It will help you a lot as we have demonstrated it with two different arguments.

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