LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2016, 11:43 PM   #1
BruceV
LQ Newbie
 
Registered: Jan 2016
Posts: 22

Rep: Reputation: Disabled
Using unsafe stdio.h functions in handlers


See update at end...

Hello, I’m creating a program under Debian that uses Posix timers. Here’s the relevant pseudocode:

/* Handler, triggered on posix timer timeout. Has to include a write statement that sends up to 20 bytes to a serial port on every pass. */

static void handler (….)
{
……..
write (fd, buf, nvalues); // UNSAFE!!
}

Int main()
{
/* Set up posix timer & associated signal handling(typically 0.5 seconds repeat rate).
The program closely follows the example in Kerrisk: Linux programming interface”, ch23, pg500. */

// 'Sleep' loop, waiting for timer signals.
for ( ; ; )
{
pause() ;
}
}

The write statement is not handler-safe, neither is printf(), which I’ve tried to use for debugging. What’s happening is that the serial port write, and debug printf, statements, behave erratically, sometimes they work and sometimes they don’t.

I’ve tested the handler code by running it in a simple sleep() loop in main(), without problems. So aside from the unsafe functions, there appear to be no problems with it. I've also replaced the handler code with a single, simple printf statement, and that works OK (the Kerrisk sample code does the same thing).

How does one invoke write and printf safely from handlers?

Incidentally, I’m using posix timers because I need better timing accuracy than ordinary timer objects.

Also, please note that the the website is removing my line indents.

UPDATE (1 hour later)
A very slight recode within the handler, which should have had no effect, has caused the problem to go away. However, I aren't confident that the situation is solved, and would appreciate any advice to make the situation completely sound.

SECOND UPDATE: Found a website that looks promising. Shouldn't need any further attention.

Last edited by BruceV; 10-03-2016 at 12:43 AM. Reason: Solution probably found
 
Old 10-03-2016, 04:15 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by BruceV View Post
Also, please note that the the website is removing my line indents.
Use [code][/code] tags to preserve indentation.
 
Old 10-03-2016, 05:53 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
It depends on what you mean by "sometimes they work and sometimes they don’t".

One thing to note - some system calls can be interrupted (the error return is EINTR). If this happens you are supposed to try the syscall again. For library control, you can always have a configuration option...
 
  


Reply

Tags
function, handler, timer



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
static thread handlers thirumalesh Linux - Networking 1 06-29-2010 02:43 PM
LXer: Unsafe Functions In C And Their Safer Replacements: Strings Part II LXer Syndicated Linux News 0 03-03-2009 04:18 AM
LXer: Alternatives for Unsafe Strings Functions in C LXer Syndicated Linux News 0 02-15-2009 04:41 AM
signal handlers begi Linux - Newbie 1 03-05-2008 08:28 AM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration