![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 20
| Processing both I/O and timed events Code: #include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
volatile unsigned int alarmTrig;
static void alrmHandler(int x) //indicates alarm triggered and resets same
{
alarmTrig = 1;
alarm(1);
return;
}
int main(int argc, char *argv[])
{
struct sigaction theHandler; //sigaction struct
char inpChar;
int i, bytesRead = 0;
sigset_t emptyMask;
sigemptyset(&emptyMask);
theHandler.sa_handler = alrmHandler;
theHandler.sa_mask = emptyMask; //nothing special
theHandler.sa_flags = 0; //nothing special here
theHandler.sa_restorer = NULL; //option obsolete
sigaction(SIGALRM, &theHandler, NULL);
alarmTrig=0;
alarm(1);
for(i=0; i<20; i++)
{
while(alarmTrig == 0)
{
bytesRead = fread(&inpChar, 1, 1, stdin);
if(bytesRead == 1)
{
printf("You entered %c.\n",inpChar);
}
}
if (alarmTrig == 1)
{
alarmTrig=0;
printf("%d seconds have gone by.\n",i);
alarm(1);
}
}
return 0;
}
|
| drunken_scot is offline | |
| | #2 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| >> ... given that global variables should generally be avoided. There are still times when they're needed. >> ... is this a reasonable approach, with the alarm handler setting a global variable? Yes - It's an ideal signal handler in general. However, global should be of type "volatile sig_atomic_t". gg |
| Codeplug is offline | |
| | #3 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| The select() system call with a timeout allows you to monitor i/o for activity, and allows you to observe the passage of time.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #4 | |
| Registered User Join Date: Feb 2009
Posts: 20
| Quote:
Thanks. It's nice to know that my microcontroller work is applicable to a computer system. I was looking up sig_atomic_t, and it appears to be typedefed int, with its typedef being performed between Code: __BEGIN_NAMESPACE_STD and __END_NAMESPACE_STD | |
| drunken_scot is offline | |
| | #5 |
| Registered User Join Date: Feb 2009
Posts: 20
| |
| drunken_scot is offline | |
| | #6 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
The point is that sig_atomic_t is GUARANTEED to be of a type that can be accessed as a single operation, which is not guaranteed [but still possible] about other types such as int or long. [It happens that the C runtime for this environment KNOWS that int is OK for this purpose, but it's not necessarily the case for another environment]. The select() solution is definitely one worth looking at. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #7 |
| Registered User Join Date: Feb 2009
Posts: 20
| |
| drunken_scot is offline | |
![]() |
| Tags |
| linux, signals |
| Thread Tools | |
| Display Modes | |
|