简体   繁体   中英

scanf function and EINTR signal

I am working on Linux platform. I have a console based multi-threaded application which loads a multi-threaded shared object library for other functionalities. The shared object library internally opens a serial port for communication. The library uses 'open', 'read' and 'write' Linux system calls for serial communication. Serial communication uses signal-handler to receive data. The main thread in console application waits on 'scanf' statement, to get input from user.

Whenever there is any activity on serial port, signal are generated due to which the 'scanf' call is interrupted with EINTR (interrupted system call).

Is there any way by which 'scanf' would not be interrupted because on read-write operations on serial port ?

If you install the signal handler with the SA_RESTART flag, read() and write() calls will not return EINTR errors in Linux; neither will the scanf() family of standard I/O functions (as they use read() internally). See man 7 signal section "Interruption of system calls and library functions by signal handlers" for details. Although this behaviour is system-specific, all other Unix-like systems I've used behave the same way. Nominal Animal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM