You are here:

C++/Unix like programming help needed

Advertisement


Question
Dear Vijayan,
I need help in the following please: I know that in unix like systems there is only one ALARM signal. Knowing that an ALARM creates a child that pauses for n seconds then returns the SIGALARM to the parent process before ending.  Also having in mind that when the life of a child finishes, he sends SIGCHLD to his parent and becomes zombie; we need to write a code in C that creates 256 different ALARMS in the same process.
I appreciate your help on this.
I am assuming you know C since you are in the C++ channel?
Excuse me if I sent you the wrong question.
It is my first experience with the site and I do not get any replies from others! It may be my last experience as well.

Answer
Signals are notifications sent to a process in order to notify it of various events. they interrupt whatever the process is doing at this minute, and force it to handle them immediately. each signal has an integer number that represents it (1, 2 and so on), as well as a symbolic name like SIGALARM (usually defined in the file /usr/include/signal.h).

Each signal may have a signal handler, which is a function that gets called when the process receives that signal. The function is called in "asynchronous mode", meaning that no where in your program you have code that calls this function directly. Instead, when the signal is sent to the process, the operating system stops the execution of the process, and "forces" it to call the signal handler function. When that signal handler function returns, the process continues execution from wherever it happened to be before the signal was received, as if this interruption never occurred.

If you are familiar with interrupts (you are, right?), signals are very similar in their behavior. The difference is that while interrupts are sent to the operating system by the hardware, signals are sent to the process by software.

the way of sending signals to processes is by using the kill system call. This system call is also used by the 'kill' command or by the 'fg' command. Here is an example code that causes a process to send to itself the ALARM signal:


#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

/* first, find my own process ID */
pid_t my_pid = getpid();

/* now that i got my PID, send myself the ALARM signal. */
kill(my_pid, SIGALARM);


There are several ways to install signal handlers. The most basic form is the signal() system call which is used to set a signal handler for a single signal type. as an example, here is a code snippet that causes the program to print the string "Alarm!" when an ALARM signal is received:


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

/* first, the signal handler */
void catch_alarm( int sig_num )
{
   /* re-set the signal handler again to catch_alarm, for next time */
   signal( SIGALARM, catch_alarm );
   /* and print the message */
   printf( "Alarm!\n");
   fflush(stdout);
}

.
.
.
/* and somewhere later in the code.... */
.
.

/* set the ALARM signal handler to 'catch_alarm' */
signal( SIGALARM, catch_alarm );

here are a couple of liks for you to peruse:
http://www.cis.temple.edu/~ingargio/cis307/readings/signals.html
http://en.wikipedia.org/wiki/Signal_(computing)  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.