How to use Alarm Signal in Raspberry Pi

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 automatically and sometimes the user can initiate a signal sending. A process can also send signals to each other by calling some specific functions. A signal is sent for the purpose of notifying the process about something that required immediate attention. Different signals are used to notify different events and the 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;

How to use Alarm Signal in Raspberry Pi Schematic

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 steps that the process do corresponding to a received signal is called ‘Signal Handling’. It is very much similar to the ‘Interrupt Servicing’ and just like the ‘Interrupt Service Routine’, there should be a function called ‘Signal Handler’ which can perform the necessary things in response to a signal received. A function can be made the Signal Handler for a particular signal using the function called ‘signal ()’. This particular project is based on receiving and handling the signal number 14, the SIGALRM.

In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.  To access the pins that coming out of the Broadcom controller of the Raspberrypi board using C language, a C library is available called “bcm2835” which has been downloaded and installed.

A process can receive the SIGALRM signal from the OS by calling a function named ‘alarm ()’. The OS will send the SIGALRM to the process after a time period mentioned in the parameter passed to the function during the function call.

alarm ()

The call made to the function alarm () will make the OS to send a SIGALRM to the process after a time period mentioned in the parameters passed to the function. The prototype of the function is defined in the header file <signal.h> as given below;

unsigned int alarm ( unsigned int seconds );

The only argument is the number of seconds that should be elapsed before receiving the SIGALRM after a call has been made to this function. If the process wants to cancel any existing alarm, it can be done by calling alarm with a seconds argument of zero.The return value indicates how many seconds remain before the previous alarm would have been sent. If there is no previous alarm, alarm returns zero.

#include <bcm2835.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#define PIN1 RPI_GPIO_P1_11
#define PIN2 RPI_GPIO_P1_12
#define PIN3 RPI_GPIO_P1_24
#define PIN4 RPI_GPIO_P1_15
#define PIN5 RPI_GPIO_P1_16
#define PIN6 RPI_GPIO_P1_18
#define PIN7 RPI_GPIO_P1_22
#define PIN8 RPI_GPIO_P1_07
void sig_handler ( int signo );
void set_pins_output ( void );
void set_all_pin_low ( void );
int main(){if (!bcm2835_init())
return 1;set_pins_output ();
set_all_pin_low ();
 signal ( SIGALRM, sig_handler );
alarm ( 2 );while ( 1 );  bcm2835_close();return 0;}
void set_all_pin_low ( void )
{bcm2835_gpio_write(PIN1, LOW);
bcm2835_gpio_write(PIN2, LOW);
bcm2835_gpio_write(PIN3, LOW);
bcm2835_gpio_write(PIN4, LOW);
bcm2835_gpio_write(PIN5, LOW);
bcm2835_gpio_write(PIN6, LOW);
bcm2835_gpio_write(PIN7, LOW);
bcm2835_gpio_write(PIN8, LOW);}
 void set_pins_output ( void )
{bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN2, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN3, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN4, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN5, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN6, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN7, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(PIN8, BCM2835_GPIO_FSEL_OUTP);}

For more detail: How to use Alarm Signal in Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top