Sponsored Content
Top Forums Programming Sinal-processing alarm()-function Post 302268762 by xcoder44@gmx.de on Tuesday 16th of December 2008 08:54:15 AM
Old 12-16-2008
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);
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Alarm signal

Hi, when I execute a script on unix AIX, I've got an error message: "Execution: 85328 Signal d'alarme". If I edit this file with "vi", I ve got the same error after a while (about 1 minute). If I try with another user I still have the problem. But if I rename this file, no problem. My... (5 Replies)
Discussion started by: cgsteph
5 Replies

2. UNIX for Dummies Questions & Answers

alarm

Hello I have a server HP ES40 with unix 5.1B, and if i open from Start-Programs-IN Tools-GUI/pfmalarm/Alarm-start monitoring , I receive this error message "IOR : STRING IS TOO LONG ! MAXIMUM SIZE = 1024" Anybody heard about this error? Thanks Alin (0 Replies)
Discussion started by: tomaalin
0 Replies

3. AIX

File Accessed Alarm ??

Hey, I want to ask a simple Question.... How would I be able to come to know that files/directoires in a Parent directory has been accessed (means contents of the file has been just viewed) by the user(s) in a group ? and mail the name(s) of those files/directories which has been accessed... (1 Reply)
Discussion started by: varungupta
1 Replies

4. Shell Programming and Scripting

File Accessed Alarm ??

Hey, I want to ask a simple Question.... How would I be able to come to know that files/directoires in a Parent directory has been accessed (means contents of the file has been just viewed) by the user(s) in a group ? and mail the name(s) of those files/directories which has been accessed... (16 Replies)
Discussion started by: varungupta
16 Replies

5. Shell Programming and Scripting

raise an alarm in Unix

Hi members, I am working in WebSphere in Unix environment. we are working with 500 odd servers and most of the times processes got down. Can i have any shell script through whih some popup with alarm get raised whenever some server get down. kindly help.. Thanks Rishi (1 Reply)
Discussion started by: rishi.madan
1 Replies

6. Programming

alarm signal processing

I'm writing a function right now, and I want to set an alarm to avoid a timeout, here's the general idea of my code: int amt = -2; alarm(10); amt = read(fd, &t->buf, TASKBUFSIZ - tailpos); //do a read when the alarm goes off, i want to check the value of "amt" ... (1 Reply)
Discussion started by: liaobert
1 Replies

7. Shell Programming and Scripting

Scripting an alarm

Hi All, I am monitoring batch Processes running in UNIX environment. I use PuTTy to monitor the process running. I have to continuously monitor and look on the screen if some error has come or not. If an error comes FAILURE word is displayed instead of SUCCESS as shown below on the... (2 Replies)
Discussion started by: sampandey31
2 Replies

8. What is on Your Mind?

Alarm interrupt and multithreading

Hi Friends any know how became a friend in this Android Programming Language (0 Replies)
Discussion started by: ljarun
0 Replies

9. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

10. Shell Programming and Scripting

Bash to goto specific line/function and start processing if user response is yes

In the bash below I am trying to run the script entire script including the ....(which is a bunch of code) and then in the run function if the user response is y (line in bold). then start processing from execute function. Basically, goto the # extract folder for variable filename line and start... (4 Replies)
Discussion started by: cmccabe
4 Replies
All times are GMT -4. The time now is 11:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy