You may want to communicate messages between processes in the following circumstances: When a task is finished, the child process that was started by the parent process is terminated. The child process may have been running an infinite loop. When a failure or other event like deadlock occurs, a process that is part of a group must either stop or signal the other processes within the group. While coordinating, two processes must synchronize using this function. For instance, one thread could utilize this to influence how a signal is broadcast to a group of other threads. Following shown below is the syntax of the pthead_kill() function of C. Signal.h header file is used because all these functions are defined inside this header file.
int pthread_kill(pthread_t thread_id, int signal);
The pthread kill() method would do error checking if the signal value was set to 0, but in reality, no signal has been supplied to this function for the thread. The function returns zero when it has completed its job. If not, a numerical error is returned by the function. No signal is delivered if the pthread kill() method fails. If either of the following occurs: No thread could be discovered matching the one indicated by the supplied thread ID; An invalid or unauthorized signal number is the value of the signal argument.
Let’s create a C programming file to understand the pthread_kill() function. By using the VIM command, we have to create a code file and save the file as pthread_Kill.c. To obtain the desired outcome, type the subsequent command on the screen.
Here is the code snippet; in the first few lines of code, we included the header files. We just print the message from the thread that is now running in the display_Output method. We have declared an integer type variable in the main function to detect the thread status. The num variable has been initialized, and the thread creation method has been passed. Pthread t is used to distinctively identify the thread. Then, using the pthread create method, we generated a thread. In the event of failure, see if this method returns a value that is less than 0, which would indicate that the thread creation process failed.
After that, the thread is put to sleep for two seconds by calling the sleep method. The pthread kill procedure has then been invoked, and the thread id and signal SIGUSR1 have been passed to it. The use of SIGUSR1 and SIGUSR2 signals depends on the requirements of the user. If you implement a signal handler for them in the application that receives the signal, they are particularly helpful during inter-process communication. The next line makes a conditional check that will confirm whether the thread killing process failed and whether an error message was printed using the perror statement. In case of success, we have called the pthread_join method, which offers a straightforward mechanism that enables an application to wait for a thread to finish. Later we checked whether this process failed to print an error message.
Close the VIM Editor after entering the code by hitting the escape key and the: x command. Compile the code after that using a C compiler of your choice, making sure it produces the desired output file. To build the code, type the commands shown below. Any errors will be shown on the terminal screen if they occur. Nothing will appear on the screen after a successful compilation.
Run the output file to see the message that the thread is executing, indicating that there were no errors after calling the thread to kill function and that everything went smoothly from thread creation through thread kill to thread join functions. To obtain the desired outcome, type the subsequent command on the screen.
Let’s make some changes in the code, so type the vim command along with the C file name. To obtain the desired outcome, type the subsequent command on the screen.
The conditional statement following the modification to the thread joins method, where we checked to see if the thread join method had set the thread Status variable to greater than or equal to 1, indicating a successful operation, is absent from the lines of code below. If this is successful, we will display a message on the screen that says, “Thread Joining Successful.”. Another minor change in the code is that the thread_kill function is now placed after the thread_join function’s conditional statement. This means that inter-process communication will now take place after the thread_join function.
To update the output file, recompile the code. This command will compile the C file that contains the thread code. To obtain the desired outcome, type the subsequent command on the screen.
Run the code now, and you’ll see that it successfully printed the thread joining method on the screen. The thread execution is displayed before the call to the display output function. To obtain the desired outcome, type the subsequent command on the screen.
Conclusion
This is about the demonstration of a pthread_kill() function of C to request for a signal to be directed to the chosen thread. We have discussed a detailed example of C using the pthread_kill function within it to send a signal to a particular method when it comes to the termination of a calling function. This is all about pthread_kill. Hope you like it.