欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Linux/apache

當(dāng)前位置:主頁 > 服務(wù)器 > Linux/apache >

Linux被中斷的系統(tǒng)如何調(diào)用詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Linux/apache|點(diǎn)擊: 次

前言

慢系統(tǒng)調(diào)用,指的是可能永遠(yuǎn)無法返回,從而使進(jìn)程永遠(yuǎn)阻塞的系統(tǒng)調(diào)用,比如無客戶連接時(shí)的accept、無輸入時(shí)的read都屬于慢速系統(tǒng)調(diào)用。

在Linux中,當(dāng)阻塞于某個(gè)慢系統(tǒng)調(diào)用的進(jìn)程捕獲一個(gè)信號(hào),則該系統(tǒng)調(diào)用就會(huì)被中斷,轉(zhuǎn)而執(zhí)行信號(hào)處理函數(shù),這就是被中斷的系統(tǒng)調(diào)用。

然而,當(dāng)信號(hào)處理函數(shù)返回時(shí),有可能發(fā)生以下的情況:

  • 如果信號(hào)處理函數(shù)是用signal注冊(cè)的,系統(tǒng)調(diào)用會(huì)自動(dòng)重啟,函數(shù)不會(huì)返回
  • 如果信號(hào)處理函數(shù)是用sigaction注冊(cè)的
    • 默認(rèn)情況下,系統(tǒng)調(diào)用不會(huì)自動(dòng)重啟,函數(shù)將返回失敗,同時(shí)errno被置為EINTR
    • 只有中斷信號(hào)的SA_RESTART標(biāo)志有效時(shí),系統(tǒng)調(diào)用才會(huì)自動(dòng)重啟

下面我們編寫代碼,分別驗(yàn)證上述幾種情形,其中系統(tǒng)調(diào)用選擇read,中斷信號(hào)選擇SIGALRM,中斷信號(hào)由alarm產(chǎn)生。

使用signal

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

void handler(int s)
{
  printf("read is interrupt 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;
}

使用sigaction + 默認(rèn)情況

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

void handler(int s)
{
  printf("read is interrupt 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; //不給SIGALRM信號(hào)設(shè)置SA_RESTART標(biāo)志,使用sigaction的默認(rèn)處理方式
  //act.sa_flag |= SA_INTERRUPT; //SA_INTERRUPT是sigaction的默認(rèn)處理方式,即不自動(dòng)重啟被中斷的系統(tǒng)調(diào)用
  //實(shí)際上,不管act.sa_flags值為多少,只要不設(shè)置SA_RESTART,sigaction都是按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;
}


使用sigaction + 指定SA_RESTART標(biāo)志

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

void handler(int s)
{
  printf("read is interrupt 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; //給SIGALRM信號(hào)設(shè)置SA_RESTART標(biāo)志

  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;
}


由于對(duì)被中斷系統(tǒng)調(diào)用處理方式的差異性,因此對(duì)應(yīng)用程序來說,與被中斷的系統(tǒng)調(diào)用相關(guān)的問題是:

  • 應(yīng)用程序無法保證總是知道信號(hào)處理函數(shù)的注冊(cè)方式,以及是否設(shè)置了SA_RESTART標(biāo)志
  • 可移植的代碼必須顯式處理關(guān)鍵函數(shù)的出錯(cuò)返回,當(dāng)函數(shù)出錯(cuò)且errno等于EINTR時(shí),可以根據(jù)實(shí)際需求進(jìn)行相應(yīng)處理,比如重啟該函數(shù)
int nread = read(fd, buf, 1024);

if (nread < 0)
{
  if (errno == EINTR)
  {
    //read被中斷,其實(shí)不應(yīng)該算作失敗,可以根據(jù)實(shí)際需求進(jìn)行處理,比如重寫調(diào)用read,也可以忽略它
  }
  else
  {
    //read真正的讀錯(cuò)誤
  }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。

上一篇:CentOS8 yum/dnf 配置國(guó)內(nèi)源的方法

欄    目:Linux/apache

下一篇:Linux中g(shù)rep和egrep命令詳解

本文標(biāo)題:Linux被中斷的系統(tǒng)如何調(diào)用詳解

本文地址:http://mengdiqiu.com.cn/a1/Linux_apache/10787.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有