![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| File Accessed Alarm ?? | varungupta | Shell Programming and Scripting | 16 | 10-15-2007 04:14 AM |
| File Accessed Alarm ?? | varungupta | AIX | 1 | 09-29-2007 07:31 AM |
| Perl alarm signal | reggiej | Shell Programming and Scripting | 2 | 01-15-2006 04:00 PM |
| alarm | tomaalin | UNIX for Dummies Questions & Answers | 0 | 01-11-2006 08:21 AM |
| Alarm signal | cgsteph | UNIX for Dummies Questions & Answers | 5 | 09-09-2004 03:13 AM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Sinal-processing alarm()-function
hi programmers from all over the world,
i am programming a simple program and want to deal with signals. i want to understand and work with the old signal-concept under unix, but i have a problem, hope you can help or knoew where i can get help. i use just one sig-handler, if a signal is recognized, then the sig-handler should handle this. after i do a sig-longjmp() and finally i got back to the point where sigsetjmp() was saving it. my idea is to put an alarm() everytime, if there's no answer. if a signal is beeing processed and finished, after sigsetjmp() i do an alarm() again, but there's the problem, it's just working one time. if i do nothing on keyboard, then the alarm() does SIGARLM one time. After it's setted again, it's doing nothing. i also looked for the mask, but it's NULL everytime, so the signal SIGARLM can not be ignored, or? thanks for helping, greetings here's the code: Code:
/*
loop.c
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
//#include "../utils.h"
jmp_buf jmpbuf;
int savemask;
static void sig_handler(int signr)
{
// static int counter=0;
printf("CHILD> signal %d erkannt...\n", signr);
signal(signr, sig_handler);
// Ruecksprung und Uebergabe der Signalnummer
siglongjmp(jmpbuf, signr);
}
int main(int argc, char **argv)
{
int setjmp_ret;
unsigned int alarm_counter = 0,
i;
// Zwischenspeichen der PID's
int pid = getpid(), ppid = getppid();
/* Erstellung des Signalhandlers mit Bindung der entsprechenden Signale,
Somit wird immer sig_handler aufgerufen, egal welches Signal eingetreten ist */
signal(SIGABRT, sig_handler);
signal(SIGALRM, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGILL, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGKILL, sig_handler);
signal(SIGPIPE, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
/* Setzen der Sprungmarke: Alle Informationen werden in jmpbuf gespeichert.
und das Signal in Maske hinzufügen, zur Verhinderung eines erneuten Aufrufs.
Gibt als Rückgabewert bei longjmp die sign an setjmp_ret */
setjmp_ret = sigsetjmp(jmpbuf, savemask);
// printf("\nSavemask: %d\n",savemask);
// Setzen des Alarms
int alrm = alarm(5);
printf("\n***alarm: %d gesetzt\n",alrm);
switch(setjmp_ret)
{
// Funktion: Interrupt feststellen
case SIGINT:
puts("CHILD> SIGINT!!!");
signal(SIGINT, sig_handler);
alarm_counter = 0;
break;
// Funktion: Aufforderung zur Eingabe
case SIGALRM:
puts("CHILD> bitte sprecht mit mir!");
signal(SIGALRM, sig_handler);
alarm_counter++;
break;
// Funktion: Ausgabe des PID's
case SIGUSR1:
printf("CHILD> PID:%d\n PPID:%d\n\n", pid, ppid);
alarm_counter = 0;
break;
// Programm beenden
case SIGUSR2:
printf("***Unterprogramm 'loop' von 'com' terminiert!***\n\n");
exit (0);
default:
break;
}
// Wenn 60 Sekunden abgelaufen sind [ 3 x alarm(20) = 60]
if(alarm_counter>2)
{
// Deaktivierung des Alarms
alarm(0);
// 10 Sekunden Deadline [ mit Eingabe ( -> Singalauslösung) unterbrechbar]
for(i=0; i<10; i++)
{
printf("Keine Eingabe erkennbar... Bitte um Wahl\n Sie haben noch %02d Sekunden!\r", i);
// 1 Sekunde warten (systemabhängig)
sleep(1);
}
}
else
{
printf("\n***pause() eingetreten...\n");
// Suspendierung des Prozesses, bis ein Signal eintrifft
pause();
}
fflush(stdout);
exit(EXIT_SUCCESS);
}
|
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|