How to call the interrupted system in Linux

How to call the interrupted system in Linux

Preface

Slow system calls refer to system calls that may never return, thus blocking the process forever. For example, accept when there is no client connection and read when there is no input are both slow system calls.

In Linux, when a process blocked in a slow system call captures a signal, the system call will be interrupted and the signal processing function will be executed instead. This is the interrupted system call.

However, when the signal processing function returns, the following situations may occur:

  • If the signal processing function is registered with signal, the system call will be automatically restarted and the function will not return.
  • If the signal processing function is registered with sigaction
    • By default, the system call will not be automatically restarted, the function will return failure, and errno will be set to EINTR
    • The system call will automatically restart only when the SA_RESTART flag of the interrupt signal is valid.

Next, we write code to verify the above situations respectively, where the system call selects read, the interrupt signal selects SIGALRM, and the interrupt signal is generated by alarm.

Using signal

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

void handler(int s)
{
  printf("read is interrupted by signal handler\n");
  return;
}

int main()
{
  char buf[10];
  int nread = 0;

  signal(SIGALRM, handler);
  alarm(2);

  printf("read start\n");
  nread = read(STDIN_FILENO, buf, sizeof(buf));
  printf("read return\n");

  if ((nread < 0) && (errno == EINTR))
  {
    printf("read return failed, errno is EINTR\n");
  }

  return 0;
} 

Use sigaction + default

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

void handler(int s)
{
  printf("read is interrupted by signal handler\n");
  return;
}

int main()
{
  char buf[10];
  int nread = 0;
  struct sigaction act;

  sigemptyset(&act.sa_mask);
  act.sa_handler = handler;
  act.sa_flags = 0; //Do not set the SA_RESTART flag for the SIGALRM signal, and use the default processing method of sigaction //act.sa_flag |= SA_INTERRUPT; //SA_INTERRUPT is the default processing method of sigaction, that is, it does not automatically restart the interrupted system call. //In fact, no matter what the value of act.sa_flags is, as long as SA_RESTART is not set, sigaction is processed according to SA_INTERRUPT sigaction(SIGALRM, &act, NULL);
  alarm(2);

  printf("read start\n");
  nread = read(STDIN_FILENO, buf, sizeof(buf));
  printf("read return\n");

  if ((nread < 0) && (errno == EINTR))
  {
    printf("read return failed, errno is EINTR\n");
  }

  return 0;
} 


Use sigaction + specify the SA_RESTART flag

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

void handler(int s)
{
  printf("read is interrupted by signal handler\n");
  return;
}

int main()
{
  char buf[10];
  int nread = 0;
  struct sigaction act;

  sigemptyset(&act.sa_mask);
  act.sa_handler = handler;
  act.sa_flags = 0;
  act.sa_flags |= SA_RESTART; //Set the SA_RESTART flag for the SIGALRM signal sigaction(SIGALRM, &act, NULL);
  alarm(2);

  printf("read start\n");
  nread = read(STDIN_FILENO, buf, sizeof(buf));
  printf("read return\n");

  if ((nread < 0) && (errno == EINTR))
  {
    printf("read return failed, errno is EINTR\n");
  }

  return 0;
} 


Due to the differences in how interrupted system calls are handled, the issues related to interrupted system calls for applications are:

  • The application cannot always know how the signal handler was registered and whether the SA_RESTART flag is set.
  • Portable code must explicitly handle error returns from key functions. When a function fails and errno equals EINTR, you can handle it accordingly, such as restarting the function.
int nread = read(fd, buf, 1024);

if (nread < 0)
{
  if (errno == EINTR)
  {
    //The read is interrupted, which should not be considered a failure. You can handle it according to actual needs, such as rewriting the call to read, or ignoring it.}
  else
  {
    //read the real read error}
}

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Get the system call and parameters of a process in Linux (troubleshooting case)
  • Detailed analysis of the difference between library functions and system calls in Linux C
  • Detailed analysis of the difference between Linux system calls and standard library calls
  • Three ways to implement Linux system calls
  • Method based on Linux system call--getrlimit() and setrlimit() functions
  • Detailed explanation of the Linux system call principle
  • Linux kernel device driver system call notes

<<:  MySQL configuration SSL master-slave replication

>>:  React antd realizes dynamic increase and decrease of form

Recommend

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons an...

In-depth understanding of javascript class array

js array is probably familiar to everyone, becaus...

How to export mysql table structure to excel

The requirements are as follows Export the table ...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

Detailed explanation of jquery tag selector application example

This article example shares the specific code of ...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

Detailed explanation of Vue's custom event content distribution

1. This is a bit complicated to understand, I hop...

React encapsulates the global bullet box method

This article example shares the specific code of ...

CentOS IP connection network implementation process diagram

1. Log in to the system and enter the directory: ...

An in-depth introduction to React refs

1. What is Refs is called Resilient File System (...

The submit event of the form does not respond

1. Problem description <br />When JS is use...

25 div+css programming tips and tricks

1. The ul tag has a padding value by default in M...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...