Linux select函数

2021/12/15 7:19:25

本文主要是介绍Linux select函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

select会被信号量中断,比如SIGINT

#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>

int s_runflag = 1;

void sig_int(int sig)
{
    printf("signal int[%d]\n", sig);
}

void sig_quit(int sig)
{
    printf("signal quit[%d]\n", sig);
    s_runflag = 0;
}

int main()
{
    int fd = open("./1.txt", O_CREAT);
    signal(SIGINT, sig_int);
    signal(SIGQUIT, sig_quit);
    while(s_runflag)
    {
        printf("before select\n");
        select(fd+1, NULL, NULL, NULL, NULL);
        printf("after select\n");
    }
    return 0;
}

运行结果

before select
^Csignal int[2]
after select
before select
^\signal quit[3]
after select


这篇关于Linux select函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程