日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 管道非阻塞,linux – 管道上的非阻塞读取

發(fā)布時(shí)間:2024/7/23 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 管道非阻塞,linux – 管道上的非阻塞读取 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

可以在管道上進(jìn)行非阻塞I / O嗎? fcntl無法設(shè)置O_NONBLOCK. Linux編程接口的頁面918包括一個(gè)表’從管道讀取n個(gè)字節(jié)或FIFO(p)’的語義.此表列出了管道和FIFO的行為,其中一列標(biāo)題為O_NONBLOCK已啟用?這意味著您可以在管道上設(shè)置O_NONBLOCK標(biāo)志.它是否正確?以下代碼無法設(shè)置標(biāo)志,但fcntl(2)不報(bào)告錯(cuò)誤.

#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沒有什么特別之處.以下示例按預(yù)期工作.我沒有檢查每次調(diào)用的每個(gè)retval,以使示例更具可讀性.真實(shí)世界的應(yīng)用程序必須進(jìn)行檢查.

#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);

}

}

}

}

寫完我的例子后,我檢查你的代碼,發(fā)現(xiàn):

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

flags |= O_NONBLOCK;

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

請(qǐng)將F_SETFD更改為F_SETFL以及get操作.您不會(huì)更改文件描述符標(biāo)志,但文件描述符狀態(tài)標(biāo)志:-)

來自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.

標(biāo)簽:linux,pipe,glibc

來源: https://codeday.me/bug/20190727/1555320.html

總結(jié)

以上是生活随笔為你收集整理的linux 管道非阻塞,linux – 管道上的非阻塞读取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。