我有以下C代码从管道读取然后应该阻止但它永远不会阻止
int pipe_fd;
int res;
int open_mode = O_RDONLY;
char buf[100];
int bytes_read = 0;
memset (buf, '\0', sizeof(buf));
pipe_fd = open(FIFO_NAME, open_mode);
if (access(FIFO_NAME, F_OK) == -1)
{
res = mkfifo(FIFO_NAME, 0777);
if (res != 0)
{
fprintf (stderr, "Could not create fifo %s\n", FIFO_NAME);
exit (EXIT_FAILURE);
}
}
for(;;)
{
do
{
res = read(pipe_fd, buf, sizeof(buf));
bytes_read += res;
}while (res > 0);
// process data then go back and block
............
}
Run Code Online (Sandbox Code Playgroud)
它通过bash脚本中的某些代码发送一个简单的缓冲区,例如'./test 1'
#!/bin/bash
pipe=/tmp/pipe …Run Code Online (Sandbox Code Playgroud)