How can I monitor a FIFO?

為{幸葍}努か 提交于 2021-01-28 09:28:25

问题


I want to debug an issue between two processes ideally by setting up a read-only terminal window of that traffic. Is this something I can simply use existing, standard linux utilities for?

The FIFO lives at /run/myfifo and is created in one of the processes with:

/* Create a FIFO if one doesn't already exist */
int createFifo(char *filepath) {
  if (access(path, F_OK) == -1) {
    return mkfifo(filepath, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  }

  return 0;
}

tail -F /run/myfifo?


回答1:


There are various options how to monitor that. I expect you have two processes. One process is writing to the fifo and the other is reading.

If you need to debug reader and writer separately than you can use a simple program like cat.

writer-process

# and in another terminal
cat /run/myfifo

or

reader-process &

# and in another terminal
cat > /run/myfifo

When you need debug writer and reader together you can use strace that Daniel Schepler recommended. The strace can be run together with your program and the logging output is redirected to another terminal /dev/pts/4 in this case.

strace -e read -s 999 reader-process 2> /dev/pts/4

The command logs all read calls from all file descriptors. If you want to filter only read from pipe you must identify the fifo file descriptor and grep the output.

If the strace not an option you can parhaps force the reader and writer to use a different fifo names and then connect these two fifos in a program that logs the transferred data. The simplest variant of such a connector can be a script like

 cat < /run/mywritefifo | tee /dev/tty > /run/myreadfifo


来源:https://stackoverflow.com/questions/46498213/how-can-i-monitor-a-fifo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!