0

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 ?

  • EINTR is a errno not a signal – Bhuvanesh Mar 25 '15 at 10:29
  • You can try `NONBLOCK` for getting the input. It will not help you to avoid that. – Karthikeyan.R.S Mar 25 '15 at 10:31
  • yes, EINTR is a errno. – Pratham Mar 25 '15 at 10:35
  • below is my code snippet – Pratham Mar 25 '15 at 10:36
  • possible duplicate of [Program terminates if I use scanf](http://stackoverflow.com/questions/10086760/program-terminates-if-i-use-scanf) – Anto Jurković Mar 25 '15 at 10:37
  • g_SerialFileDescriptor = open(portname, O_RDWR | O_NOCTTY | O_NDELAY, 0); if (g_SerialFileDescriptor < 0) { //error } else { /* install the signal handler before making the device asynchronous */ saio.sa_handler = OSAL_Serial_Receive; saio.sa_flags = 0; saio.sa_restorer = NULL; if(sigaction(SIGIO, &saio, NULL) < 0) { // error } } – Pratham Mar 25 '15 at 10:38
  • is there any other way by which scanf would not be interrupted due to signals ? – Pratham Mar 25 '15 at 10:52
  • @Pratham: If you install the signal handler with the [`SA_RESTART`](http://man7.org/linux/man-pages/man2/sigaction.2.html) 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](http://man7.org/linux/man-pages/man7/signal.7.html) 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 Mar 25 '15 at 15:39
  • SA_RESTART works, thanks for the information. – Pratham Apr 06 '15 at 18:10

1 Answers1

0

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

Community
  • 1
  • 1
Armali
  • 18,255
  • 14
  • 57
  • 171