![]() |
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 |
| Basic bash 'for loop' usage | Orange Stripes | Shell Programming and Scripting | 2 | 12-18-2007 08:58 PM |
| basic question about disk usage | karthikosu | AIX | 3 | 07-23-2006 10:16 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 04:13 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Basic signal and alarm usage
I am trying to write a program that will;
1) Show the message "Snoozing now...zzzz" on the screen for 5 seconds 2) Then in the same position show the message "The ALARM is going off now!" for 5 seconds 3) Repeat 1) then 2) infinitely until user presses Ctrl C I can't make it work. Any hints appreciated. Here is my code; [me@myplace]$ cat main10.c /* main10.c testing signals and alarms */ #include <curses.h> #include <signal.h> #include <stdlib.h> #include <stdio.h> void handler( void ) { clear(); mvprintw(10, 10, "The ALARM is going off now!\n" ); refresh(); signal( SIGALRM, handler ); alarm( SIGALRM, 5 ); } int main( void ) { clear(); signal( SIGALRM, handler ); alarm( SIGALRM, 5 ); mvprintw( 10, 10, "Snoozing now..zzzzzz\n"); refresh(); endwin(); } [me@myplace]$ gcc main10.c -lcurses -o mainApp10 main10.c: In function `handler': main10.c:17: warning: passing arg 2 of `signal' from incompatible pointer type main10.c: In function `main': main10.c:24: warning: passing arg 2 of `signal' from incompatible pointer type [me@myplace]$ mainApp10 Segmentation fault [me@myplace]$ |
|
||||
|
Modified your code,
alarm prototype is not correct! Check this, Code:
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
void handler(void);
void handler( void )
{
clear();
mvprintw(10, 10, "The ALARM is going off now!\n" );
refresh();
signal( SIGALRM, handler );
alarm( 5 );
}
int main( void )
{
initscr();
clear();
signal( SIGALRM, handler );
alarm( 5 );
mvprintw( 10, 10, "Snoozing now..zzzzzz\n");
getch();
refresh();
endwin();
return 0;
}
|
|
||||
|
Thanks a lot. matrixmadhan your code almost works perfectly. The only thing it doesn't do is loop infinitely between "The ALARM...." and "Snoozing.....".
It starts by displaying "Snoozing....." than after 5 seconds "The ALARM....". This is good. But then "The ALARM...." stays on screen forever. I want it to then revert back to "Snoozing....." and then back to "The ALARM...." etc etc etc in an infinite loop every 5 seconds. I will try to modify. Any more hints appreciated. Last edited by enuenu; 05-28-2007 at 09:41 AM.. |
|
||||
|
Although that may be a jolly program, that is far too much code for a signal handler.
C libraries are not "signal safe", the most you should be doing in a signal handler is setting a flag or a minimal bit of IPC to awaken the main program. If your handler was called in the middle of malloc your heap could get trashed. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|