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

Closed Thread
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
  #1 (permalink)  
Old 05-28-2007
enuenu enuenu is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 39
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]$
  #2 (permalink)  
Old 05-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
Where is the curses mode initialized,

Code:
initscr();
  #3 (permalink)  
Old 05-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
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;
}
  #4 (permalink)  
Old 05-28-2007
enuenu enuenu is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 39
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..
  #5 (permalink)  
Old 05-28-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
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.
  #6 (permalink)  
Old 05-28-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,123
One way:
Code:
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

void handler(int sig) { }

int main( void )
{
        int state;
        signal(SIGALRM, handler);
        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();
                alarm(5);
                pause();
                state=!state;
        }
}
  #7 (permalink)  
Old 05-28-2007
enuenu enuenu is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 39
Thanks very much to all. I have a project in mind and this forms a piece of it. I will forge ahead now. You have helped me understand how to implement alarms and signals.
Closed Thread

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 07:20 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