How to Send Value Between Processes Using Signal

Developed By:

Ajish Alfred

Linux operating systems especially Ubuntu is preferred for all kind of programming and development. In a multi-tasking environment of the Operating System several processes executes at the same time and the Signals provide an Inter-Process Communication (IPC) method. The Operating System sends signals to the process to notify them about the events occurred and to control them. A Parent process can create another process, which are called the Child process and the Parent process can use signals to control the Child process.

How to Send Value Between Processes Using Signal

The Raspberrypi is a board actually designed for helping computer education for remote schools but it is a good platform for programmers especially beginners to explore various coding techniques. The Raspberrypi is a mini-computer board which is powerful enough to run large operating systems like Linux, Mac and Windows. The Linux operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board.

The Multi-tasking Operating Systems can run several processes at a time creating and effect of parallel processing with the help of the high speed ARM111 processor of the Raspberypi. A process can get the attention of other processes by sending signals to them. The signals can also be used to send data or value along with it. This particular article demonstrates how to send a value using signal between two processes.

In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC by attaching the Raspberrypi board through a LAN cable at the Ethernet port of the PC. The coding is done from the command line using the ‘vim’ editor and is compiled using ‘cc’ compiler.

A signal is a software interrupt that can be sent to a process which is currently executing in the Operating System. Most of the time the Operating system send signals to the processes, but process can also send signals to each other. Much like hardware interrupts it is a mechanism to notify a process that an event has occurred. Different signals are available which can be used to notify different events and these signals are differentiated by their signal numbers. The list of all the available signals in the OS and their signal numbers can be obtained using the following command:

kill -l

The following table gives a list of the most common signals that a process might encounter in an Operating System:

NAME NUMBER DESCRIPTION
SIGHUP 1 Linux sends a process this signal when it becomes disconnected from a terminal.
SIGINT 2 Linux sends a process this signal when the user tries to end it by

pressing CTRL+C.

SIGILL 4 Linux sends a process this signal when it attempts to execute an illegal instruction.
SIGABRT 6 Linux sends a process this signal to the process when the process calls the ‘abort ()’ function
SIGFPE 8 Linux sends a process this signal when it has executed an invalid floating-point math instruction
SIGKILL 9 Linux sends a process this signal to end it immediately
SIGUSR1 10 User programs can send this signal to other process
SIGUSR2 12 User programs can send this signal to other process
SIGSEGV 11 Linux sends a process this signal when the program has attempted an invalid memory access
SIGPIPE 13 Linux sends a process this signal when the program has attempted to access a broken data stream, such as a socket connection that has been already closed
SIGALRM 14 A process can receive this signal from the Linux using the function alarm (), after a time period mentioned in its argument.
SIGTERM 15 Linux sends a process this signal requesting it to terminate
SIGCHLD 17 Linux sends a process this signal when a child process exits
SIGXCPU 24 Linux sends a process this signal when it exceeds the limit of

CPU time that it can consume.

SIGVTALRM 26 A process can receive this signal from the Linux using the function setitimer (), after a time period mentioned in its argument.

The user programs should always try to use only the signals which are reserved for them, SIGUSR1 and SIGUSR2 to communicate between the processes. The steps which a process performs corresponding to a received signal are called ‘Signal Handling’. There should be a function called ‘Signal Handler’ inside the process which can perform the necessary things in response to a signal received.

Incase to get values or messages that are send along with the signals the function ‘sigaction ()’ can be used other than signal () defined in the header file <signal.h>. The signal () can pass only the signal number to the Signal Handler function whereas the function ‘sigaction ()’ has a third argument which is a structure holding the details of the received signal that can be passed on to the Signal Handler function.

This advantage of the ‘sigaction ()’ function comes at a cost of increasing code complexity and since the audience of this article are expected to be mostly beginners, a separate function has been written which uses the ‘sigaction ()’ function to set a particular function as Signal Handler. The function that can be set as a Signal Handler should have the same set of arguments and return value as shown in the following function prototype;

void signal_handler ( int sig, siginfo_t *siginfo, void *context );

 

Read more: How to Send Value Between Processes Using Signal

Scroll to Top
Scroll to Top
Read previous post:
Raspberry Pi Tor relay

Tor (short for The Onion Router) is a system intended to enable online anonymity. The Tor client software directs internet...

Close