Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 有没有办法知道为写而打开的文件描述符是否有人在读端侦听?_C_Linux_Fifo - Fatal编程技术网

C 有没有办法知道为写而打开的文件描述符是否有人在读端侦听?

C 有没有办法知道为写而打开的文件描述符是否有人在读端侦听?,c,linux,fifo,C,Linux,Fifo,我在一边打开fifo进行读取,在另一边进行写入,读取端关闭他打开的fd。有没有办法知道读卡器是否在写卡器端关闭了此fd? 我希望编写器能够收到任何关于阅读器是否已准备好阅读的通知,因为如果没有,我的编写器将在写入时被阻止。 作者c: #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string.h>

我在一边打开fifo进行读取,在另一边进行写入,读取端关闭他打开的fd。有没有办法知道读卡器是否在写卡器端关闭了此fd? 我希望编写器能够收到任何关于阅读器是否已准备好阅读的通知,因为如果没有,我的编写器将在写入时被阻止。 作者c:

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int fd;
     char * myfifo = "/tmp/fifo_pipe";
    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);    

    write(fd, "Hey", sizeof("Hey"));

    /*here is there a posibilty of know that the read side hase close hi's side of the pipe before write? */

    write(fd, "test\n", strlen("test\n"));


    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
int-fd;
char*myfifo=“/tmp/fifo_pipe”;
/*创建FIFO(命名管道)*/
mkfifo(myfifo,0666);
/*向FIFO写入“Hi”*/
fd=打开(仅限myfifo、O_);
写(fd,“嘿”,sizeof(“嘿”);
/*有没有可能知道读端在写之前会关闭管道的hi端*/
写入(fd,“test\n”,strlen(“test\n”);
关闭(fd);
/*卸下FIFO*/
取消链接(myfifo);
返回0;
}
读者c:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/fifo_pipe";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}
#包括
#包括
#包括
#包括
#定义最大值为1024
int main()
{
int-fd;
char*myfifo=“/tmp/fifo_pipe”;
char buf[MAX_buf];
/*打开、读取并显示来自FIFO的信息*/
fd=打开(仅限myfifo、Ordu);
读取(fd、buf、MAX_buf);
printf(“收到:%s\n”,buf);
关闭(fd);
返回0;
}

在没有读卡器的FIFO中写入将引发SIGPIPE并最终返回-1,errno设置为
EPIPE
您可以使用lsof系统命令检查进程打开的文件的信息。提取lsof命令输出的FD字段并继续。 有关更多详细信息和示例,请参阅此

在您的情况下,要获取侦听fifo的进程数,请尝试执行以下系统命令

lsof /tmp/fifo_pipe | grep [0-9]r | wc -l
在C中,您可以实现如下内容:

int i = 0;
FILE *fp;
char *command = "lsof /tmp/rjfifo | grep [0-9]r | wc -l";

fp = popen(command,"r");
if (fp != NULL){
    fscanf(fp,"%d",&i);
}

fclose(fp);
printf("Number of processes reading /tmp/fifo_pipe = %d \n",i);

打开时可以使用
O_NONBLOCK
标志。在这种情况下,
write()
将永远不会阻塞,而是返回
-1
并将
errno
设置为
ewoodblock
。只有在至少一个读卡器打开然后关闭FIFO时才会发生这种情况。如果写入程序试图打开FIFO时,至少有一个读卡器没有打开FIFO,则打开的FIFO将被阻塞。@zwol当然可以,但这不是OP要求的。