gpt4 book ai didi

c - Unix 中的非阻塞代码

转载 作者:行者123 更新时间:2023-11-30 21:10:39 24 4
gpt4 key购买 nike

如何在 unix 中为非阻塞代码提供输入?代码如下:

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

void set_fl(int fd, int flags);

char buf[10];

int main()
{
int noread;

set_fl(STDIN_FILENO, O_NONBLOCK); //code for nonblocking
set_fl(0, O_NONBLOCK); //code for nonblocking
noread = read(0, buf, sizeof(buf));

printf("INPUT: %s\n", buf);

return 0;
}

void set_fl(int fd, int flags)
{
int val;

if ((val = fcntl(fd, F_GETFL, 0)) < 0)
printf("ERROR: %s\n", "fcntl F_GETFL error");

val |= flags; //changes the bit

if (fcntl(fd, F_SETFL, val) < 0)
printf("ERROR: %s\n", "fcntl F_SETFL error");
}

最佳答案

您可以使用 select() 来确定 STDIN_FILENO 何时有更多字节可供读取,如下所示:

#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>

void set_fl(int fd, int flags);

char fullBuf[1024] = ""; // initialized to an empty string, so we can strncat() to it below

int main()
{
set_fl(STDIN_FILENO, O_NONBLOCK); //code for nonblocking

while(1)
{
fd_set inFDs;
FD_SET(STDIN_FILENO, &inFDs);

int maxFD = STDIN_FILENO; /* set this to the maximum of all fd's in the inFDs set */
if (select(maxFD+1, &inFDs, NULL, NULL, NULL) >= 0) /* select() won't return the user has entered a line of text */
{
char buf[128];
memset(buf, 0, sizeof(buf));

int noread = read(0, buf, sizeof(buf));
if (noread < 0)
{
printf("Error reading from stdin!\n");
break;
}

printf("read %i bytes\n", noread);

strncat(fullBuf, buf, sizeof(fullBuf)-strlen(fullBuf)-1);

if ((strchr(buf, '\n'))||(strchr(buf, '\r'))) break; /** We'll stop gathering input after one line */
}
}

printf("INPUT: %s\n", fullBuf);

return 0;
}

void set_fl(int fd, int flags)
{
int val;

if ((val = fcntl(fd, F_GETFL, 0)) < 0)
printf("ERROR: %s\n", "fcntl F_GETFL error");

val |= flags; //changes the bit

if (fcntl(fd, F_SETFL, val) < 0)
printf("ERROR: %s\n", "fcntl F_SETFL error");
}

请注意,这比仅使用阻塞读取更复杂,但这样做确实有一个优点,因为您可以将指向非 NULL timeval 结构的指针作为 select() 的最后一个参数传递,以实现超时。

关于c - Unix 中的非阻塞代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com