C Programming

prctl in C example Usage

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.

$ touch prtcl.c

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

$ nano prtcl.c

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.

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.

Compile the file first.

$ gcc prctl.c

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

$ ./a.out

Example 02:

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

$ nano prctl.c

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.

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.

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

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

$ gcc prctl.c

$ ./a.out

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

Kalsoom Akhtar

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