C Programming

Kill System Call in C

The kill () system call in C language is used by the operating system to send a discontinuation signal to a process, urging it to exit. On the other hand, a kill system call does not always imply that the process is being terminated; it might have a variety of meanings. Using the “kill ()” system function, a process can signal to a new process. The kill () system call is widely used to transmit signals to multithreaded programs or traditional processes. Based on its numeric values, the integer PID parameter has multiple implications. If the PID value is more than zero, the sig signal is issued to the process’s thread group whose PID matches PID. If the PID value equals zero, the sig signal is transmitted to all thread groups in the same process group as the caller process. If PID equals -1, Except for swapper (PID 0), init (PID 1), and current, the signal is given to all processes.

In this guide, we will explain the implementation of the kill () system call in C language.

Pre-requisites:

  • Installation and configuration of any Operating system.

Note: We are using Ubuntu 20.04 Linux system.

  • Installation of GCC compiler for code compilation and execution
  • Sudo rights

Example for kill () system call in C

Let’s start with a brief and simple example to clarify our kill () system call concept. For this, we need to have a .c file in our system. As it is already stated that we are utilizing Ubuntu 20.04 Linux system, so we will create a .c file using the shell. Firstly, launch the shell by searching it in the application of the Ubuntu 20.04 Linux system or utilizing the “Ctrl+Alt+T” key. Once you get access to the shell, execute the below-stated command in the console.

$ nano kill.c

The file name in our case is “kill.c”. However, it’s up to you to name your file as per your choice. Upon the implementation of the above-listed command, the GNU nano editor will pop up on your screen. You have to write a similar code as displayed in the below-attached screenshot. However, depending on your work requirements, the code can easily be modified.

The int argc (Argument Count) holds the amount of command-line arguments given by the user, such as the program’s name. If we give the program a value, the value of argc will be two; one for the argument and one for the program’s name. The argc value should not be negative. argv(Argument Vector) is a character pointer array that contains all of the arguments. In the Printf function, we have used getpid().

The function Getpid() is used to obtain the process ID of the process that calls it. The PID for the initial process is 1, and each succeeding process is given a different Id. It is a straightforward method for obtaining the PID. This function solely assists you in obtaining unique process identifiers. The unistd.h library, a shortcut of the Unix standard library, provides the sleep () function. We want to sleep for 5 seconds in this scenario. The parameter will be 5, which is an integer. The abbreviation SIGSEGV stands for “Segmentation Violation.” When a program tries to write/read outside of the memory allocated to it, or when writing memory that can only be read, SIGSEGV occurs. The abbreviation SIGSEGV stands for “Segmentation Violation”. After writing the code, it’s time for the execution and compilation.

Execute the stated below command in the console of the Ubuntu 20.04 Linux system.

$ gcc kill.c

There will be no output of this command. Now, to get the output of the above-written command, execute the stated below command in the console of the Ubuntu 20.04 Linux system.

$ ./a.out

You can check that the output has been displayed on the shell.

Conclusion

This article has provided you with a brief description of the kill() system call in C. The usage of the kill() system call in C has been provided in detail. We have executed one of the examples to clarify the concept. I hope this program will benefit you in executing your code effectively.

About the author

Kalsoom Akhtar

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