linux 管道非阻塞,在Linux中管道上的非阻塞读取
发布日期:2022-02-18 13:08:15 浏览次数:5 分类:技术文章

本文共 2821 字,大约阅读时间需要 9 分钟。

可以在管道上进行非阻塞I / O吗? fcntl无法设置O_NONBLOCK.

Linux编程接口的页面918包括一个表’从管道读取n个字节或FIFO(p)’的语义.此表列出了管道和FIFO的行为,其中一列标题为O_NONBLOCK已启用?这意味着您可以在管道上设置O_NONBLOCK标志.它是否正确?以下代码无法设置标志,但fcntl(2)不报告错误.

#include

#include

#include

#include

#include

#define SLEEP 1

int

main(int argc, char *argv[]) {

pid_t childPid;

int pfd[2];

int nread, flags;

int c = 'a';

setbuf(stdout, NULL);

if (pipe(pfd) == -1) {

printf("error: pipe");

exit(EXIT_FAILURE);

}

switch (childPid = fork()) {

case -1:

printf("error: fork");

exit(EXIT_FAILURE);

case 0: /* child */

if (close(pfd[0]) == -1) {

printf("child: close pfd read");

exit(EXIT_FAILURE);

}

sleep(SLEEP);

_exit(EXIT_SUCCESS);

default:

break;

/* parent falls through */

}

if (close(pfd[1]) == -1) {

printf("parent: close pipe write");

exit(EXIT_FAILURE);

}

flags = fcntl(pfd[0], F_GETFD);

flags |= O_NONBLOCK;

if (fcntl(pfd[0], F_SETFD, flags))

perror("fcntl");

/* verify flags set correctly */

flags = fcntl(pfd[0], F_GETFD);

if (!(flags & O_NONBLOCK)) {

printf("failed to set O_NONBLOCK\n");

exit(EXIT_FAILURE);

}

wait(NULL);

exit(EXIT_SUCCESS);

}

管道和O_NONBLOCK没有什么特别之处.以下示例按预期工作.我没有检查每次调用的每个retval,以使示例更具可读性.真实世界的应用程序必须进行检查.

#include

#include

#include

#include

int main()

{

int fds[2];

pid_t pid;

char buf[100];

pipe(fds);

pid = fork();

if ( pid )

{

while (1 )

{

memcpy( buf, "abcdefghi\0",10);

write( fds[1], buf, 10);

sleep(2);

}

}

else

{

int retval = fcntl( fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK);

printf("Ret from fcntl: %d\n", retval);

while (1)

{

ssize_t r=read( fds[0], buf, 10 );

printf("read: %d\n", r);

if ( r > 0 )

{

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

}

else

{

printf("Read nothing\n");

perror("Error was");

sleep(1);

}

}

}

}

写完我的例子后,我检查你的代码,发现:

flags = fcntl(pfd[0], F_GETFD);

flags |= O_NONBLOCK;

if (fcntl(pfd[0], F_SETFD, flags))

请将F_SETFD更改为F_SETFL以及get操作.您不会更改文件描述符标志,但文件描述符状态标志:-)

来自man 3 fcntl:

File descriptor flags

The following commands manipulate the flags associated with a file

descriptor. Currently, only one such flag is defined: FD_CLOEXEC, the

close-on-exec flag. If the FD_CLOEXEC bit is 0, the file descriptor

will remain open across an execve(2), otherwise it will be closed.

File status flags

Each open file description has certain associated status flags, ini‐

tialized by open(2) and possibly modified by fcntl(). Duplicated file

descriptors (made with dup(2), fcntl(F_DUPFD), fork(2), etc.) refer to

the same open file description, and thus share the same file status

flags.

F_SETFL (int)

Set the file status flags to the value specified by arg. File

access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags

(i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.

On Linux this command can change only the O_APPEND, O_ASYNC,

O_DIRECT, O_NOATIME, and O_NONBLOCK flags. It is not possible to change the O_DSYNC and O_SYNC flags; see BUGS, below.

转载地址:https://blog.csdn.net/weixin_28871097/article/details/116922508 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:从qspi启动linux时间,Zynq-Linux移植学习笔记(二十三)——QSPI速度配置
下一篇:C语言随机字母生成,C++ 随机数字以及随机数字加字母生成的案例

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月11日 00时13分17秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【软件开发底层知识修炼】六 Binutils辅助工具之- addr2line与strip工具 2019-04-28
【软件开发底层知识修炼】七 Binutils辅助工具之- ar工具与nm工具 2019-04-28
【软件开发底层知识修炼】八 Binutils辅助工具之- objdump工具 与 size,strings工具 2019-04-28
【C++深度剖析教程39】实现C++数组类模板 2019-04-28
【C++深度剖析教程40】使用数值型模板技术计算1+2+3+...+N的值 2019-04-28
【软件开发底层知识修炼】九 链接器-可重定位文件与可执行文件 2019-04-28
【软件开发底层知识修炼】十 链接器-main函数不是第一个被执行的函数 2019-04-28
【OS学习笔记】十二 现代处理器的结构和特点 2019-04-28
【OS学习笔记】十三 保护模式一:全局描述符表(GDT) 2019-04-28
【OS学习笔记】十四 保护模式二:段描述符 2019-04-28
【OS学习笔记】十五 保护模式三:保护模式下的内存访问机制 2019-04-28
【OS学习笔记】十六 保护模式四:进入保护模式与在保护模式下访问内存的汇编代码 2019-04-28
【OS学习笔记】十七 保护模式五:保护模式下如何进行内存保护 与 别名段的意义与作用 2019-04-28
【软件开发底层知识修炼】十一 链接器-链接脚本 2019-04-28
【OS学习笔记】十八 保护模式五:保户模式下如何进行内存保护 与 别名段的意义与作用 对应汇编代码 2019-04-28
【软件开发底层知识修炼】十二 C/C++语言中内嵌汇编语言(asm) 2019-04-28
【OS学习笔记】十九 保护模式六:保户模式下操作系统内核如何加载用户程序并运行 2019-04-28
【OS学习笔记】二十 保护模式六:保户模式下操作系统内核如何加载用户程序并运行 对应的汇编代码之主引导扇区程序 2019-04-28
【OS学习笔记】二十一 保护模式六:保户模式下操作系统内核如何加载用户程序并运行 对应的汇编代码之内核代码 2019-04-28
【OS学习笔记】二十二 保护模式六:保户模式下操作系统内核如何加载用户程序并运行 对应的汇编代码之用户程序 2019-04-28