User Signalled Process System in Raspberry Pi


The Raspberrypi board is powerful enough to run large operating systems like Linux, Mac and Windows. 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 processor. The Linux Operating Systems provides Multi-User-Multitasking. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board.

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 Raspberry pi board runs on the Broadcom controller chip which is a SoC (System on Chip). This SoC has the powerful ARM11 processor which runs on 700 MHz at its core. This powerful processor and the advanced controller peripherals make it a mini-computer.

In this project a Parent process creates so many Child process and controls them using the signals. The Parent process creates the Child processes using the fork () function. When the Parent calls the fork (), it always returns a positive value on success. The positive value which the Parent receives is the process-id of the newly created Child process. The Child process however get a value ‘0’ from the same fork () function call.Thus the Parent can store the process-id of each and every Child process that has been created. Using this process-id the Parent process can send a signal to a particular Child process with the help of the function ‘kill ()’. The kill () is a function defined in the header file <sys/signal.h> which can be used to send a specified signal to a specified process.

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;

 NAMENUMBERDESCRIPTION
SIGHUP1Linux sends a process this signal when it becomes disconnected from a terminal.
SIGINT2Linux sends a process this signal when the user tries to end it bypressing CTRL+C.
SIGILL4Linux sends a process this signal when it attempts to execute an illegal instruction.
SIGABRT6Linux sends a process this signal to the process when the process calls the ‘abort ()’ function
SIGFPE8Linux sends a process this signal when it has executed an invalid floating-point math instruction
SIGKILL9Linux sends a process this signal to end it immediately
SIGUSR110User programs can send this signal to other process
SIGUSR212User programs can send this signal to other process
SIGSEGV11Linux sends a process this signal when the program has attempted an invalid memory access
SIGPIPE13Linux 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
SIGALRM14A process can receive this signal from the Linux using the function alarm(), after a time period mentioned in its argument.
SIGTERM15Linux sends a process this signal requesting it to terminate
SIGCHLD17Linux sends a process this signal when a child process exits
SIGXCPU24Linux sends a process this signal when it exceeds the limit ofCPU time that it can consume.
SIGVTALRM26A process can receive this signal from the Linux using the function setitimer (), after a time period mentioned in its argument.

User Signalled Process System in Raspberry Pi SchematicFrom the above table the Parent process creates so many Child process and controls them using the signal 12 which is the ‘SIGUSR2’. The user can control the Parent process by sending the signal ‘SIGUSR1’ to the Parent process from the command line. The two signals ‘SIGUSR1’ and the ‘SIGUSR2’ are reserved for the user applications and hence they can be used in this project also.

As soon as the Parent receives a ‘SIGUSR1’ signal from the user, it sends a ‘SIGUSR2’ signal to one of its Child process. The Child processes then changes the glowing state of the LED associated with it. This forms a Process System made up of several Child process and a Parent process and the entire system is controlled by the ‘SIGUSR1’ signal received by the Parent process.

The user can send two signals from the command line using the Keyboard shortcuts and they are SIGINT and the SIGQUIT. The SIGINT can be send by pressing the key CTRL+C and the SIGQUIT can be send by pressing the key CTRL+Y or CTRL + \. Any signals other than these require the ‘kill’ command to be used with the command line.

#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 set_pins_output ( void );
void set_all_pin_low ( void );
void toggle1_on_signal_rx ( void );
void toggle2_on_signal_rx ( void );
void toggle3_on_signal_rx ( void );
void toggle4_on_signal_rx ( void );
void toggle5_on_signal_rx ( void );
void toggle6_on_signal_rx ( void );
void toggle7_on_signal_rx ( void );
void toggle8_on_signal_rx ( void );
void toggle1_sig_handler ( int signo );
void toggle2_sig_handler ( int signo );
void toggle3_sig_handler ( int signo );
void toggle4_sig_handler ( int signo );
void toggle5_sig_handler ( int signo );
void toggle6_sig_handler ( int signo );
void toggle7_sig_handler ( int signo );
void toggle8_sig_handler ( int signo );
void parent_signal_handler ( int signo );
pid_t child_id [ 8 ];
int main ( void )
{
int i;
if (!bcm2835_init())
return 1;
set_pins_output ();
set_all_pin_low ();
signal ( SIGUSR1, parent_signal_handler );
if ( ! ( child_id [ 0 ] = fork () ) )
{
toggle1_on_signal_rx ();
_exit ( 0 );
}
else;
if ( ! ( child_id [ 1 ] = fork () ) ){toggle2_on_signal_rx ();
_exit ( 0 );}else; if  ! ( child_id [ 2 ] = fork () ) ){toggle3_on_signal_rx ();_exit ( 0 );}else;if ( ! ( child_id [ 3 ] = fork () ) ){toggle4_on_signal_rx ();_exit ( 0 );}else;
if ( ! ( child_id [ 4 ] = fork () ) ){
toggle5_on_signal_rx ();_exit ( 0 );}
else;if ( ! ( child_id [ 5 ] = fork () ) ){toggle6_on_signal_rx ();
_exit ( 0 );}else;if ( ! ( child_id [ 6 ] = fork () ) ){toggle7_on_signal_rx ();_exit ( 0 );}
else;if ( ! ( child_id [ 7 ] = fork () ) )
{toggle8_on_signal_rx ();_exit ( 0 );}
else;while ( 1 );bcm2835_close();return 0;}
void parent_signal_handler ( int signo ){
static int i = 0;
kill ( child_id [ i ], SIGUSR2 );i ++;if ( i > 7 )
i = 0;else;}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: User Signalled Process System in Raspberry Pi




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top