FIFO 管道 returns 0 而不是正确的整数?
FIFO pipe returns 0 instead of correct integer?
我正在尝试实现两个进程之间的基本通信。我打算让每个进程接收一条信息,然后传回一条信息。我是管道的新手,所以尝试使用此代码示例进行此操作:
How to send a simple string between two programs using pipes?
我设置了代码并且它工作正常,然后我复制了第二个管道的代码以接收另一个整数。但是我的第二个管道不传输整数,程序接收到一个 0。
program1.c:
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd; // file descriptor
int fd_b;
int data = 5;
int buf; // buffer from fifo
char * fifo_one = "/tmp/fifo_one";
char * fifo_two = "/tmp/fifo_two";
// create fifo
mkfifo(fifo_one, 0666);
// write to FIFO
fd = open(fifo_one, O_WRONLY);
write(fd, &data, sizeof(&data));
close(fd);
// remove FIFO
unlink(fifo_one);
// receive from FIFO
fd_b = open(fifo_two, O_RDONLY);
read(fd_b, &buf, MAX_BUF);
printf("Received: %d\n", buf);
close(fd_b);
return 0;
}
program2.c:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd; // file descriptor
int fd_b;
char * fifo_one = "/tmp/fifo_one";
char * fifo_two = "/tmp/fifo_two";
int buf; // buffer from fifo
int ret_dat; // return data
// receive data from fifo
fd = open(fifo_one, O_RDONLY);
read(fd, &buf, MAX_BUF);
printf("Received: %d\n", buf);
close(fd);
// decide return
if (buf == 5) {
ret_dat = 10;
}
// send data back
// create fifo
mkfifo(fifo_two, 0666);
// write to FIFO
fd_b = open(fifo_two, O_WRONLY);
write(fd_b, &ret_dat, sizeof(&ret_dat));
close(fd_b);
// remove FIFO
unlink(fifo_sendBal);
return 0;
}
第二个程序收到了5,但是没有发回10成功,
我知道计时会影响 IPC,所以我尝试在某些事件后使用睡眠,但我无法让它工作。
第二个程序收到了5,但是没有发回10成功? FIFO
调用[=创建的进程间通信的属性 11=] 是 两个进程都应该活着以相互通信 。来自 mkfifo
的手册页
Once you have created a FIFO
special file in this way, any process
can open it for reading or writing, in the same way as an ordinary
file. However, it has to be open at both ends simultaneously before
you can proceed to do any input or output operations on it. Opening
a FIFO for reading normally blocks until some other process opens the
same FIFO for writing, and vice versa. See fifo(7)
for nonblocking
handling of FIFO special files.
我正在尝试实现两个进程之间的基本通信。我打算让每个进程接收一条信息,然后传回一条信息。我是管道的新手,所以尝试使用此代码示例进行此操作: How to send a simple string between two programs using pipes?
我设置了代码并且它工作正常,然后我复制了第二个管道的代码以接收另一个整数。但是我的第二个管道不传输整数,程序接收到一个 0。
program1.c:
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd; // file descriptor
int fd_b;
int data = 5;
int buf; // buffer from fifo
char * fifo_one = "/tmp/fifo_one";
char * fifo_two = "/tmp/fifo_two";
// create fifo
mkfifo(fifo_one, 0666);
// write to FIFO
fd = open(fifo_one, O_WRONLY);
write(fd, &data, sizeof(&data));
close(fd);
// remove FIFO
unlink(fifo_one);
// receive from FIFO
fd_b = open(fifo_two, O_RDONLY);
read(fd_b, &buf, MAX_BUF);
printf("Received: %d\n", buf);
close(fd_b);
return 0;
}
program2.c:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd; // file descriptor
int fd_b;
char * fifo_one = "/tmp/fifo_one";
char * fifo_two = "/tmp/fifo_two";
int buf; // buffer from fifo
int ret_dat; // return data
// receive data from fifo
fd = open(fifo_one, O_RDONLY);
read(fd, &buf, MAX_BUF);
printf("Received: %d\n", buf);
close(fd);
// decide return
if (buf == 5) {
ret_dat = 10;
}
// send data back
// create fifo
mkfifo(fifo_two, 0666);
// write to FIFO
fd_b = open(fifo_two, O_WRONLY);
write(fd_b, &ret_dat, sizeof(&ret_dat));
close(fd_b);
// remove FIFO
unlink(fifo_sendBal);
return 0;
}
第二个程序收到了5,但是没有发回10成功,
我知道计时会影响 IPC,所以我尝试在某些事件后使用睡眠,但我无法让它工作。
第二个程序收到了5,但是没有发回10成功? FIFO
调用[=创建的进程间通信的属性 11=] 是 两个进程都应该活着以相互通信 。来自 mkfifo
Once you have created a
FIFO
special file in this way, any process can open it for reading or writing, in the same way as an ordinary file. However, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa. Seefifo(7)
for nonblocking handling of FIFO special files.