我发现GNU库中记录了SIGIO信号。据说系统有可能在我输入时发送信号(特别是在插座中)。 根据创建此类信号的文档,我应将O_ASYNC标志设置为相应的filedescritor。 我的问题是我的GNU版本(gcc 6.3.0)没有识别出这样的关键字:
错误:'O_ASYNC'未声明(首次使用此功能)
我使用了以下块来设置标志:
/* Set socket status async */
int status = fcntl(sockfd, F_SETFL, O_ASYNC);;
if (status < 0) error("Can't set async mode");
else printf("Async is set for signal %d\n", SIGIO);
我使用Cigwin GCC 6.3.0
代码如下:
static void OnTimer(int sig)
{
/* write request to socket BIO */
}
static void OnTick(int sig)
{
char read[BUFSIZE] = {};
int received;
received = SSL_read(ssl, read, sizeof(read));
/* Handle errors here */
/* print the server's reply */
printf("%s\n\n", read);
}
void connectSSL()
{
/* do all socket set-up and connection */
/* Establish handler for I/O signal */
saction.sa_flags = 0;
saction.sa_handler = OnTick;
sigemptyset(&saction.sa_mask);
sigaddset(&saction.sa_mask, SIGALRM);
if (sigaction(SIGIO, &saction, NULL) == -1) error("sigaction");
else printf("OnTick handler created\n");
/* Set socket status async */
int status = fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | FASYNC);
if (status < 0) error("Can't set async mode");
else printf("Async is set for signal %d\n", SIGIO);
/* Select the process to receive the signal */
int process = fcntl(sockfd, F_SETOWN, getpid());
if (process < 0) error("Can't set address process");
else printf("Process %d is set\n", getpid());
/* do the rest SSL staff here */
}
int main(int argc, char argv[])
{
sigset_t mask, oldmask;
/* Wait for OnTimer event*/
sigemptyset(&oldmask);
sigaddset(&oldmask, SIGALRM);
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
sigaddset(&mask, SIGIO);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
connectSSL();
createTimer(500000000);
while(1)
{
printf("\nWaiting OnTimer %d\n", count + 1);
sigsuspend(&oldmask);
}
return 0;
}
答案 0 :(得分:0)
很可能您忘记间接或直接包含sys/fcntl.h
。这是定义标志的地方。
如果未定义O_ASYNC
,您可以尝试使用该标志的功能等效前导FASYNC
。
同时确保您已执行
int flags;
/* Install signal handler here... */
/* Set flags to receive SIGIO (and SIGURG) signals */
flags = fcntl (F_GETFL, 0);
flags = fcntl (F_SETFL, flags | FASYNC);
/* Bind our process as receiver of SIGIO signals */
flags = fcntl (F_SETOWN, getpid ());