- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Timeout getchar() in C
Operating System - HP-UX
1819814
Members
4155
Online
109607
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-12-2008 12:17 PM
тАО11-12-2008 12:17 PM
Hi,
I have a program receiving one character at a time. If nothing is entered for 5 min, I'd like to break out of the loop.
something like
while (time(&curtime) - lastsendtime) < 300 )
getchar();
unfortunately, this doesn't get to my time check until a char is entered
Any ideas?
tia
I have a program receiving one character at a time. If nothing is entered for 5 min, I'd like to break out of the loop.
something like
while (time(&curtime) - lastsendtime) < 300 )
getchar();
unfortunately, this doesn't get to my time check until a char is entered
Any ideas?
tia
Solved! Go to Solution.
- Tags:
- getchar
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-12-2008 03:32 PM
тАО11-12-2008 03:32 PM
Solution
Perhaps you should just use alarm(2)?
Otherwise you would have to use select(2) and system calls.
#include
#include
#include
void got_alarm(int sig) {
fprintf(stderr, "Got signal %d\n", sig);
}
int main() {
alarm(5*60);
signal(SIGALRM, got_alarm);
int c = getchar();
printf("getchar returned %x\n", c);
return 0;
}
Before you get each char, you would have to reenable the alarm. While not reading, you would have to disable it.
Otherwise you would have to use select(2) and system calls.
#include
#include
#include
void got_alarm(int sig) {
fprintf(stderr, "Got signal %d\n", sig);
}
int main() {
alarm(5*60);
signal(SIGALRM, got_alarm);
int c = getchar();
printf("getchar returned %x\n", c);
return 0;
}
Before you get each char, you would have to reenable the alarm. While not reading, you would have to disable it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-12-2008 04:55 PM
тАО11-12-2008 04:55 PM
Re: Timeout getchar() in C
Hi Renda:
Sending 'alarm()' to SIGALRM works nicely in Perl too when using 'getc()':
Consider this script where I'va added the appropriate signal handler (SIGALRM) and 'alarm()' call to timeout a character read (getc()) after 5-seconds.
The Term::ReadKey Perl module is a compiled C module that offers control over various terminal modes.
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{ALRM} = sub { ReadMode 'restore'; print STDERR "\nTIMEOUT!\n"; exit 0 };
$SIG{TERM} = sub { ReadMode 'restore'; exit 0 };
my $char;
my @password;
my $tmout = 0; #...ARBITRARY...set to 0 to disable...
print "Enter password: ";
ReadMode 'noecho';
ReadMode 'raw';
alarm $tmout;
while ( $char = ReadKey 0 ) {
last if $char eq "\n";
next if ( ord($char) < 040 or ord($char) > 0176 );
push( @password, $char );
print '*';
alarm $tmout;
}
ReadMode 'restore';
print "\n>", @password, "<\n";
1;
#_jrf.
Regards!
...JRF...
Sending 'alarm()' to SIGALRM works nicely in Perl too when using 'getc()':
Consider this script where I'va added the appropriate signal handler (SIGALRM) and 'alarm()' call to timeout a character read (getc()) after 5-seconds.
The Term::ReadKey Perl module is a compiled C module that offers control over various terminal modes.
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{ALRM} = sub { ReadMode 'restore'; print STDERR "\nTIMEOUT!\n"; exit 0 };
$SIG{TERM} = sub { ReadMode 'restore'; exit 0 };
my $char;
my @password;
my $tmout = 0; #...ARBITRARY...set to 0 to disable...
print "Enter password: ";
ReadMode 'noecho';
ReadMode 'raw';
alarm $tmout;
while ( $char = ReadKey 0 ) {
last if $char eq "\n";
next if ( ord($char) < 040 or ord($char) > 0176 );
push( @password, $char );
print '*';
alarm $tmout;
}
ReadMode 'restore';
print "\n>", @password, "<\n";
1;
#_jrf.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-13-2008 04:19 AM
тАО11-13-2008 04:19 AM
Re: Timeout getchar() in C
Excellent... works great!
thanks for your help
thanks for your help
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP