The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 12-16-2008
xcoder44@gmx.de xcoder44@gmx.de is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 1
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
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0