Sponsored Content
Full Discussion: Basic signal and alarm usage
Top Forums Programming Basic signal and alarm usage Post 302119207 by Perderabo on Monday 28th of May 2007 09:48:31 PM
Old 05-28-2007
porter's objection is a bit frivolous... if we cannot execute two successive system calls during a 5 second period, we do not have the resources available to run a program that that displays alternating messages every 5 seconds. But I will fix that objection anyway since I see a more serious problem. The signal() function may set the SA_RESETHAND flag on some versions of Unix. So to be portable, I would need to move that signal call into the loop and set it every time. Instead of that I would prefer to to switch to sigaction() so I can portably install the handler one time. Once I do that, I may as well, switch pause() to sigsuspend() to fix porter's objection. So...
Code:
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

void handler(int sig) { }

int main( void )
{
        int state;
        sigset_t imask, omask;
        struct sigaction act, oact;
        sigemptyset(&imask);
        sigemptyset(&omask);
        sigaddset(&imask, SIGALRM);

        sigemptyset(&act.sa_mask);
        act.sa_handler = handler;
        act.sa_flags = 0;
        sigaction(SIGALRM, &act, &oact);

        initscr();
        clear();
        state=0;
        while(1) {
                if(state) {
                        mvprintw(10, 10, "The ALARM is going off now!\n" );
                } else {
                        mvprintw( 10, 10, "Snoozing now..zzzzzz\n");
                }
                refresh();
                sigprocmask(SIG_SETMASK, &imask, NULL);
                alarm(5);
                sigsuspend(&omask);
                state=!state;
        }
}

Also, I want to point out that in real life I would call sleep() to get a 5 second delay. I assume that, for purposes of this thread, that would be cheating.
 

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. Shell Programming and Scripting

Perl alarm signal

I am trying to write a signal to exit when a process times out. What I have come up with from poking around the web is this. #!/usr/bin/perl eval { local $SIG{ALRM} = sub { die "alarm clock restart" }; alarm 10; open(DSMADMC, "dsmadmc -se=tsmpc1 -id=XXXXX... (2 Replies)
Discussion started by: reggiej
2 Replies

3. AIX

basic question about disk usage

how to i find out the disk usage on a server. say in windows examples its like C:/ D:/ and checking out the disk space. how can i find in Unix. can i just use df -k (3 Replies)
Discussion started by: karthikosu
3 Replies

4. Shell Programming and Scripting

Basic bash 'for loop' usage

Hi! I have a simple question about using a for loop. I'm trying to open up all the zip files in the currect directory with ark, but I am getting the error "bash: syntax error near unexpected token `for $i ; do ark $i ; done ; I looked in the info pages for bash, but I can't seem to figure... (2 Replies)
Discussion started by: Orange Stripes
2 Replies

5. Programming

Usage of exit() inside a signal handler

Is it ok to use exit() inside a signal handler? I catch SIGUSR1 in a signal handler and I try to close a file and then exit. The result is inconsistent. Sometimes the process exit and sometimes it returns to the original state before the signal handler was invoked. Perhaps exit is not legal in... (8 Replies)
Discussion started by: Tuvia
8 Replies

6. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

7. Solaris

False Memory usage alarm!!

Hi Experts, I am using Solaris-10, Sun-Fire-V445. i got often the below message- "Memory Usage – Critical, Memory usage (RAM) exceeding 90% The memory utilization is exceeding 90%" in a application running on solaris. I checked with Vmstat. Everything seems to be fine. Where i should... (5 Replies)
Discussion started by: thepurple
5 Replies

8. 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

9. Shell Programming and Scripting

basic computer usage report

Our small company, about 5 users, need a basic script that scans mapped network drives (example: drive b,c,d, e, and f) for hard drive usage. This needs to send a report to myself in any type of basic notepad format (easy to read and decipher) for drives that have reached 80% usage... any ideas? ... (1 Reply)
Discussion started by: jessessays
1 Replies

10. UNIX for Dummies Questions & Answers

Basic sed usage in shell script

Hi, A basic sed question. I have a set of files. In each file there is a number that I want replaced. For example, if I run sed I should get the following: % cat test2.txt #goofy//171.00 goofy 171.00 % sed -i 's/171/xxx/g' test2.txt % cat test2.txt #goofy//xxx.00 goofy xxx.00 ... (2 Replies)
Discussion started by: pc2001
2 Replies
SIGACTION(2)							System Calls Manual						      SIGACTION(2)

NAME
sigaction, signal - manage signal state and handlers SYNOPSIS
#include <signal.h> int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) void (*signal(int sig, void (*handler)(int)))(int); DESCRIPTION
Sigaction() is used to examine, set, or modify the attributes of a signal. The argument sig is the signal in question. The act argument points to a structure containing the new attributes of the signal, the structure pointed to by oact will receive the old attributes that were in effect before the call. The act and oact arguments may be NULL to indicate that either no new attributes are to be set, or that the old attributes are not of interest. The structure containing the signal attributes is defined in <signal.h> and looks like this: struct sigaction { void (*sa_handler)(int sig); sigset_t sa_mask; int sa_flags; }; The sa_handler field contains the address of a signal handler, a function that is called when the process is signalled, or one of these special constants: SIG_DFL Default signal handling is to be performed. This usually means that the process is killed, but some signals may be ignored by default. SIG_IGN Ignore the signal. The sa_mask field indicates a set of signals that must be blocked when the signal is being handled. Whether the signal sig itself is blocked when being handled is not controlled by this mask. The mask is of a "signal set" type that is to be manipulated by the sigset(3) functions. How the signal is handled precisely is specified by bits in sa_flags. If none of the flags is set then the handler is called when the sig- nal arrives. The signal is blocked during the call to the handler, and unblocked when the handler returns. A system call that is inter- rupted returns -1 with errno set to EINTR. The following bit flags can be set to modify this behaviour: SA_RESETHAND Reset the signal handler to SIG_DFL when the signal is caught. SA_NODEFER Do not block the signal on entry to the handler. SA_COMPAT Handle the signal in a way that is compatible with the the old signal() call. The old signal() signal system call sets a signal handler for a given signal and returns the old signal handler. No signals are blocked, the flags are SA_RESETHAND | SA_NODEFER | SA_COMPAT. New code should not use signal(). Note that signal() and all of the SA_* flags are Minix extensions. Signal handlers are reset to SIG_DFL on an execve(2). Signals that are ignored stay ignored. Signals Minix knows about the following signals: signal num notes description SIGHUP 1 k Hangup SIGINT 2 k Interrupt (usually DEL or CTRL-C) SIGQUIT 3 kc Quit (usually CTRL-) SIGILL 4 kc Illegal instruction SIGTRAP 5 xkc Trace trap SIGABRT 6 kc Abort program SIGFPE 8 k Floating point exception SIGKILL 9 k Kill SIGUSR1 10 k User defined signal #1 SIGSEGV 11 kc Segmentation fault SIGUSR2 12 k User defined signal #2 SIGPIPE 13 k Write to a pipe with no reader SIGALRM 14 k Alarm clock SIGTERM 15 k Terminate (default for kill(1)) SIGCHLD 17 pvi Child process terminated SIGCONT 18 p Continue if stopped SIGSTOP 19 ps Stop signal SIGTSTP 20 ps Interactive stop signal SIGTTIN 21 ps Background read SIGTTOU 22 ps Background write SIGWINCH 23 xvi Window size change The letters in the notes column indicate: k The process is killed if the signal is not caught. c The signal causes a core dump. i The signal is ignored if not caught. v Only Minix-vmd implements this signal. x Minix extension, not defined by POSIX. p These signals are not implemented, but POSIX requires that they are defined. s The process should be stopped, but is killed instead. The SIGKILL signal cannot be caught or ignored. The SIGILL and SIGTRAP signals cannot be automatically reset. The system silently enforces these restrictions. This may or may not be reflected by the attributes of these signals and the signal masks. Types POSIX prescribes that <sys/types.h> has the following definition: typedef int (*sighandler_t)(int) With this type the following declarations can be made: sighandler_t sa_handler; sighandler_t signal(int sig, sighandler_t handler); This may help you to understand the earlier declarations better. The sighandler_t type is also very useful in old style C code that is compiled by a compiler for standard C. SEE ALSO
kill(1), kill(2), pause(2), sigprocmask(2), sigsuspend(2), sigpending(2), sigset(3). DIAGNOSTICS
Sigaction() returns 0 on success or -1 on error. Signal() returns the old handler on success or SIG_ERR on error. The error code may be: EINVAL Bad signal number. EFAULT Bad act or oact addresses. AUTHOR
Kees J. Bot (kjb@cs.vu.nl) SIGACTION(2)
All times are GMT -4. The time now is 06:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy