测试sigaction重启动标识

Posted Andy.gbhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试sigaction重启动标识相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <iostream>

using namespace std;

bool flag = true;

static void sig_func(int signo){

  flag = false;
  cout<<"signal function called";
}


int main(){

 

  struct sigaction newact, oldact;
  newact.sa_handler = sig_func;
  sigemptyset(&newact.sa_mask);
  newact.sa_flags = 0;
  newact.sa_flags |= SA_RESTART;

  sigaction(SIGINT, &newact, &oldact);


//  signal(SIGINT, sig_func);


  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  int broad = 1;
  setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broad, sizeof(broad) );

  struct sockaddr_in addr;

  struct sockaddr_in addr2;
  socklen_t len = sizeof(addr2);

  memset(&addr, 0x00, sizeof(addr) );
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(3000);

  bind(sock, (struct sockaddr*)&addr, sizeof(addr) );
 
  while( true && flag){

    char msgbuf[1024];
    int nbytes = recvfrom(sock, msgbuf, sizeof(msgbuf), 0, (struct sockaddr*)&addr2, &len);

    if (nbytes < 0 && !flag){
      cerr<<"interrupted by ctrl c"<<endl;

    }

 


  }

 


  return 0;

}

以上是关于测试sigaction重启动标识的主要内容,如果未能解决你的问题,请参考以下文章

10.14 sigaction函数

signal() 和 sigaction()

sigaction函数解析

信号 signal sigaction补充

linux sigaction 函数 用法释义

sigaction函数