Solution for a Interrupted System Call error of EINTR while semop is waiting for a signal under Linux

  • 2020-04-02 00:56:44
  • OfStack

Error :(semop function call, strerror(errno) output)
Interrupted the system call
Platform: RedHat Linux

The LINUX documentation describes EINTR like this:
  While blocked in this system call, the process caught a signal.
The UNIX documentation [IEEE Std 1003.1-2008] describes EINTR like this:
  The semop() function was interrupted by a signal.

These two sentences, if taken literally, are INTR signals while semop is waiting.
However, errors need to be fixed, and they are usually caused by the code written by the programmer.
After debugging the output to locate the cause of the problem, the problem was finally found:
While semop is waiting for a resource, if at this time, a thread in the process calls the SHELL function using system, semop returns immediately with the error number EINTR, the error message is as above. Despite such a minor problem, in my system, due to the use of various means to achieve IPC (in-process communication), the reason to call because of a system call is not so simple.

[because there is no solution to this problem on the Internet, I hope I can help others.]

I googled some posts about the error, and one of them said it was caused by a deadlock
Because the semaphore itself prevents deadlocks. I deliberately experimented with using a mutually exclusive variable, a semaphore, and two semaphores, in different order, to achieve deadlock, but the system did not appear as I had expected "Interrupted system call", but simply waited.

Today, when I was reading the UNIX network programming volume 1 interface API, I saw a sentence like this, which made me understand why this error occurred. The text reads:
"The basic rule for slow system calls is that when a process blocking a slow system call captures a signal and the corresponding signal handler returns, the system call may return an EINTR error. Some kernels automatically restart certain interrupted system calls.
Here, slow the system call (missile system call) in the book refers to cause the function of blocking, like the accept and the semop functions discussed above, I think I should also is such, so when now EINTR signal, the system call is interrupted, and returns an error, the error number is: EINTR, we can from this mistake to restart our system call.

Related articles: