Basic signal and alarm usage


 
Thread Tools Search this Thread
Top Forums Programming Basic signal and alarm usage
# 8  
Old 05-28-2007
Quote:
Originally Posted by Perderabo
Code:
void handler(int sig) { }

Your signal handler has the ideal amount of code!

Now time to start looking at how to block and unblock the signals so they only occur during the pause().

Eg, what happens if you are on a really slow or heavily loaded box and the following sequence occurs...

alarm(5)

<----- signal fires here ----->

pause()

Then pause() would never see the signal and would hang.
# 9  
Old 05-28-2007
OK thanks, I will look into this issue.
# 10  
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.
# 11  
Old 05-28-2007
Thanks again. I will sift through your code. There is much for me to learn in there.
# 12  
Old 05-29-2007
Could you please explain the purpose of the following statement;
Quote:
Originally Posted by Perderabo
Code:
void handler(int sig) { }

Why does the program contain no prototype of this function?
Further, I do not understand what it does or how. Thanks.
# 13  
Old 05-29-2007
prototypes are a good idea but are never required in c. Even in c++ a prototype would not be required because the entire function appears in the source file before it is used. The function does nothing at all... an empty function is legal and it behaves like it just had a a single return statement in it. But we need some function to install if the program is going to respond to signals. Without a function our choices are to ignore signals or die upon their receipt.
# 14  
Old 05-29-2007
Quote:
Originally Posted by Perderabo
prototypes are a good idea but are never required in c.
Not true, they *are* required if the function has not already been seen and returns anything other than an int. This is true even in K&R C.

Two examples why....

1. if the function returns a double some architectures require an FPU pop even if the value is not used.

2. if the function returns a struct it requires a certain amount of extra stackspace to be allocated in addition to an extra hidden pointer.

Finally,

If using GNU C use the options -Wall and -Werror, they actually help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question