Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

How to Send Value Between Processes Using Signal -(Part 24/38)

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.

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.

Fig. 2: Common Signals For Sending Values Between Process In Operating System

 

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 );

In this project the function written to set the required function as Signal Handler using the ‘sigaction ()’ is named as ‘sig_set_handler ()’ and the details of the same are discussed below:

sig_set_handler ()

This function can be used to set a particular function as the Signal Handler for a particular signal number and hence the function has only two arguments, one for the signal number and the other for the function that need to be set as the Signal Handler. The prototype of the function is given below:

void sig_set_handler ( int signo, void *handler );

The first argument is the signal number that needs to be handled and the second argument is the pointer to the function that needs to be set as the Signal Handler. For example to set the following function as the Signal Handler using the ‘sig_set_handler ()’ for a signal ‘SIGUSR1’

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

use the following statement in the code;

sig_set_handler ( SIGUSR1, &signal_handler );

A signal can be send to a process from another process using the using kill () function. But the kill () function is not able to send any values or message along with the signals. A function called ‘sigqueue ()’ is available in the <signal.h> which can be used to send values or messages along with the signals. Again using this function is also a bit complex and hence for the ease of programming a new function is written based on the ‘sigqueue ()’ to send value along with signals to another process. The function is called ‘sig_send_val ()’ and the details are discussed below;

sig_send_val ()

This function can be used to send a particular signal to a particular process along with a value and hence the function has three arguments, one for the process id, one for the signal number and the other for the value to be sent. The prototype of the function is given below;

void sig_send_val ( pid_t id, int signo, int val );

The first argument is the process id of the process to which the signal needs to be send and the second argument is the signal number of the signal to be send and the third argument is the value that needs to be send along with the signal. As an example to send the signal ‘SIGUSR1’ to a process with a process id 2107 along with a value 50, use the following statement;

sig_send_val ( 2107, SIGUSR1, 50 );

To retrieve the value send along with the signal inside the receiving function the structure pointer “siginfo -> si_value” can be used. For example the following statements of the Signal Handler function will print the value that passed on by the received signal.

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

{

            printf ( “nvalue = %dn”, siginfo -> si_value );

}  

 

Code Output:

Code Output of Signal Handler function

Fig. 3: Code Output Of Signal Handler Function

 

Project Source Code

###

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

void signal_handler ( int sig, siginfo_t *siginfo, void *context );
void signal_gen ( void );
void sig_set_handler ( int signo, void *handler );
void sig_send_msg ( pid_t id, int signo, char *msg );
void sig_send_val ( pid_t id, int signo, int val );

int main ( void )
{
sig_set_handler ( SIGUSR1, &signal_handler );

if ( !fork () )
{
signal_gen ();
_exit ( 0 );
}
else;

while ( 1 )
usleep ( 1000 );

return 0;
}

void sig_set_handler ( int signo, void *handler )
{
struct sigaction *act;
act = malloc ( sizeof ( struct sigaction ) );
act -> sa_sigaction = handler;
act -> sa_flags = SA_SIGINFO;

sigaction ( signo, act, NULL );
}

void signal_handler ( int sig, siginfo_t *siginfo, void *context )
{
printf ( "nrec signal with infon" );
printf ( "nvalue = %dn", siginfo -> si_value );
}

void signal_gen ( void )
{
int i = 0;

while ( 1 )
{
sig_send_val ( getppid (), SIGUSR1, i );
sleep ( 2 );
i ++;
}
}

void sig_send_msg ( pid_t id, int signo, char *msg )
{
union sigval *sigdata;

sigdata = malloc ( sizeof ( union sigval ) );
sigdata -> sival_ptr = msg;

sigqueue ( id, signo, *sigdata );

free ( sigdata );
}

void sig_send_val ( pid_t id, int signo, int val )
{
union sigval *sigdata;

sigdata = malloc ( sizeof ( union sigval ) );
sigdata -> sival_int = val;

sigqueue ( id, signo, *sigdata );

free ( sigdata );
}
 

###

 



Filed Under: Raspberry pi
Tagged With: Raspberry Pi
 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • PS2 Keyboard To Store Text In SD Card Using Arduino Circuit Setup On Breadboard
    How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)
  • Wireless Path Tracking System Using Mouse, XBee And Arduino Circuit Setup On Breadboard
    How To Make A Wireless Path Tracking System Using Mouse, XBee And Arduino- (Part 43/49)
  • How to Make a Wireless Keyboard Using Xbee with Arduino- (Part 44/49)
  • Making Phone Call From GSM Module Using Arduino Circuit Setup On Breadboard
    How to Make Phonecall From GSM Module Using Arduino- (Part 45/49)
  • How to Make a Call using Keyboard, GSM Module and Arduino
    How To Make A Call Using Keyboard, GSM Module And Arduino- (Part 46/49)
  • Receiving SMS Using GSM Module With Arduino Prototype
    How to Receive SMS Using GSM Module with Arduino- (Part 47/49)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas delivers intelligent sensor solutions for IoT applications
  • Microchip Technology releases AVR-IoT Cellular Mini Development Board
  • Qualcomm acquires Cellwize to accelerate 5G adoption and spur infrastructure innovation
  • MediaTek’s chipset offers high-performance option for 5G smartphones
  • Nexperia’s new level translators support legacy and future mobile SIM cards

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • SRF04 module measure distance
  • Adaptive filters fundamental
  • Using LTspice to check Current sense transformer reset?
  • Thermal pad construction on pcb
  • lna+mixer noise figure problem

RSS Electro-Tech-Online.com Discussions

  • Are Cross-wind compensation and Road crown compensation functions inputs to LKA function?
  • Interfacing ZMOD4410 with Arduino UNO
  • Help diagnosing a coffee maker PCB
  • Capacitor to eliminate speaker hum
  • Identify a circuit.
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering