Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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到Python管道-如何检测读卡器访问_Python_C_Named Pipes - Fatal编程技术网

C到Python管道-如何检测读卡器访问

C到Python管道-如何检测读卡器访问,python,c,named-pipes,Python,C,Named Pipes,我正在用C程序编写一个命名管道,并用Python程序读取它 如果我停止Python程序(reader),那么编写器将自行停止,尽管这是一个while(1)循环。为什么会发生这种情况?是无声的撞击吗 第二个问题如果我想检测读卡器何时断开连接,我应该怎么做。我的理想方案是检测到断开连接,然后继续空闲(即停止发送任何内容),并在读卡器返回后恢复 玩具代码如下 作者(C): 当您停止读取程序时,写入程序在尝试写入管道时将收到一个SIGPIPE信号。此信号的默认配置是终止进程 如果要检测此情况,请使用si

我正在用C程序编写一个命名管道,并用Python程序读取它

如果我停止Python程序(reader),那么编写器将自行停止,尽管这是一个while(1)循环。为什么会发生这种情况?是无声的撞击吗

第二个问题如果我想检测读卡器何时断开连接,我应该怎么做。我的理想方案是检测到断开连接,然后继续空闲(即停止发送任何内容),并在读卡器返回后恢复

玩具代码如下

作者(C):


当您停止读取程序时,写入程序在尝试写入管道时将收到一个
SIGPIPE
信号。此信号的默认配置是终止进程

如果要检测此情况,请使用
signal()
sigaction()
将配置更改为
SIG\u IGN
。然后写入管道将报告
EPIPE
错误

此外,不应每次通过循环时都关闭和重新打开管道。开始时打开一次,结束时关闭。关闭管道会导致读卡器获得EOF,之后它将无法读取任何内容

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
  
int main()
{
    int fd;
  
    // FIFO file path
    char * myfifo = "/tmp/myfifo";
    
    // Creating the named file(FIFO)
    // mkfifo(<pathname>, <permission>)
    mkfifo(myfifo, 0666);
    // Open FIFO for write only
    fd = open(myfifo, O_WRONLY);
  
    signal(SIGPIPE, SIG_IGN);

    char arr1[80];
    while (1)
    {
  
        // Take an input from user.
        fgets(arr1, 80, stdin);
  
        // Write the input on FIFO
        // and close it
        int res = write(fd, arr1, strlen(arr1)+1);
        if (res < 0) {
            perror("write");
            break;
        }
    }
    close(fd);
    return 0;
}

然后,循环将在管道关闭时终止。

当您停止读取程序时,写入程序将在尝试写入管道时收到一个
SIGPIPE
信号。此信号的默认配置是终止进程

如果要检测此情况,请使用
signal()
sigaction()
将配置更改为
SIG\u IGN
。然后写入管道将报告
EPIPE
错误

此外,不应每次通过循环时都关闭和重新打开管道。开始时打开一次,结束时关闭。关闭管道会导致读卡器获得EOF,之后它将无法读取任何内容

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
  
int main()
{
    int fd;
  
    // FIFO file path
    char * myfifo = "/tmp/myfifo";
    
    // Creating the named file(FIFO)
    // mkfifo(<pathname>, <permission>)
    mkfifo(myfifo, 0666);
    // Open FIFO for write only
    fd = open(myfifo, O_WRONLY);
  
    signal(SIGPIPE, SIG_IGN);

    char arr1[80];
    while (1)
    {
  
        // Take an input from user.
        fgets(arr1, 80, stdin);
  
        // Write the input on FIFO
        // and close it
        int res = write(fd, arr1, strlen(arr1)+1);
        if (res < 0) {
            perror("write");
            break;
        }
    }
    close(fd);
    return 0;
}
当管道闭合时,回路将终止

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
  
int main()
{
    int fd;
  
    // FIFO file path
    char * myfifo = "/tmp/myfifo";
    
    // Creating the named file(FIFO)
    // mkfifo(<pathname>, <permission>)
    mkfifo(myfifo, 0666);
    // Open FIFO for write only
    fd = open(myfifo, O_WRONLY);
  
    signal(SIGPIPE, SIG_IGN);

    char arr1[80];
    while (1)
    {
  
        // Take an input from user.
        fgets(arr1, 80, stdin);
  
        // Write the input on FIFO
        // and close it
        int res = write(fd, arr1, strlen(arr1)+1);
        if (res < 0) {
            perror("write");
            break;
        }
    }
    close(fd);
    return 0;
}
with open("/tmp/myfifo") as f:
    while True:
        line = f.readline()
        if not line:
            break
        print(line, end = "")