使用命名管道将用户选择的值发送到其他进程
Send the value selected by user to an other process with named pipe
我写了两个程序,第一个有一个 switch case 并创建一个命名管道 "pipeselect" 来读取用户切换的值,第二个用命名管道读回值。但是无论我输入 1 还是 2,第二个程序也显示 "Option 1 is selected"。我的脚本有什么问题?
计划 1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char pipeselect[] = "/tmp/pipeselect";
int bufs[2];
int fds;
int select1;
/* Pipe Creation */
if (access(pipeselect, F_OK) == -1) {
fds = mkfifo(pipeselect, 0700);
if (fds != 0) {
printf("Pipe creation error\n");
exit(1);
}
}
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("Please select an option: ");
scanf("%d", &select1);
if (select1 == 1 || select1 == 2)
{
if ((fds = open(pipeselect, O_WRONLY)) < 0) {
printf("Pipe open error\n");
exit(1);
}
bufs[0] = select1; // put value entered by user into buffer
write(fds, bufs, 1); // write 1 byte from the buffer
close(fds);
printf("Option %d is selected\n", select1);
}
else {
printf("Wrong Input!\n");
}
unlink(pipeselect);
exit(0);
}
计划 2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char pipeselect[] = "/tmp/pipeselect";
int bufs[2];
int fds;
int select1;
if ((fds = open(pipeselect, O_RDONLY)) < 0) {
printf("Pipe open error\n");
exit(1);
}
select1 = read(fds, bufs, 1); // write 1 byte from the buffer
printf("Option %d is selected\n", select1);
close(fds);
exit(0);
}
这是因为 read()
return 成功读取的字节数,在您的情况下 1
因为您正在读取 1
。猜测事情应该如何工作不是一个好主意,有一半的时间你会错的。
你必须做的是阅读库函数文档以了解它们应该如何使用,例如你忽略 scanf()
的 return 值。
你应该试试这个1,而不是
write(fds, bufs, 1);
选择
if (write(fds, &select1, sizeof(select1)) != sizeof(select1))
fprintf("error writing to pipe: %d:%s\n", errno, strerror(errno));
/* Inlucde <errno.h> and <string.h> for this error message to work */
而在另一个程序中,更改
select1 = read(fds, bufs, 1);
至
if (read(fds, &select1, sizeof(select1)) == sizeof(select1))
printf("Option %d is selected\n", select1);
此外,检查 scanf()
returned 而不是假设 select1
已初始化并使用它,以防它不是您的程序调用未定义的行为。
1注意,这样发送数据时,如果字节序不一样,将无法直接工作。在这种情况下它显然没问题,但如果它是网络代码,这不是一个好的方法,因为你应该考虑字节顺序。除此之外,这个还可以。
我写了两个程序,第一个有一个 switch case 并创建一个命名管道 "pipeselect" 来读取用户切换的值,第二个用命名管道读回值。但是无论我输入 1 还是 2,第二个程序也显示 "Option 1 is selected"。我的脚本有什么问题?
计划 1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char pipeselect[] = "/tmp/pipeselect";
int bufs[2];
int fds;
int select1;
/* Pipe Creation */
if (access(pipeselect, F_OK) == -1) {
fds = mkfifo(pipeselect, 0700);
if (fds != 0) {
printf("Pipe creation error\n");
exit(1);
}
}
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("Please select an option: ");
scanf("%d", &select1);
if (select1 == 1 || select1 == 2)
{
if ((fds = open(pipeselect, O_WRONLY)) < 0) {
printf("Pipe open error\n");
exit(1);
}
bufs[0] = select1; // put value entered by user into buffer
write(fds, bufs, 1); // write 1 byte from the buffer
close(fds);
printf("Option %d is selected\n", select1);
}
else {
printf("Wrong Input!\n");
}
unlink(pipeselect);
exit(0);
}
计划 2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
char pipeselect[] = "/tmp/pipeselect";
int bufs[2];
int fds;
int select1;
if ((fds = open(pipeselect, O_RDONLY)) < 0) {
printf("Pipe open error\n");
exit(1);
}
select1 = read(fds, bufs, 1); // write 1 byte from the buffer
printf("Option %d is selected\n", select1);
close(fds);
exit(0);
}
这是因为 read()
return 成功读取的字节数,在您的情况下 1
因为您正在读取 1
。猜测事情应该如何工作不是一个好主意,有一半的时间你会错的。
你必须做的是阅读库函数文档以了解它们应该如何使用,例如你忽略 scanf()
的 return 值。
你应该试试这个1,而不是
write(fds, bufs, 1);
选择
if (write(fds, &select1, sizeof(select1)) != sizeof(select1))
fprintf("error writing to pipe: %d:%s\n", errno, strerror(errno));
/* Inlucde <errno.h> and <string.h> for this error message to work */
而在另一个程序中,更改
select1 = read(fds, bufs, 1);
至
if (read(fds, &select1, sizeof(select1)) == sizeof(select1))
printf("Option %d is selected\n", select1);
此外,检查 scanf()
returned 而不是假设 select1
已初始化并使用它,以防它不是您的程序调用未定义的行为。
1注意,这样发送数据时,如果字节序不一样,将无法直接工作。在这种情况下它显然没问题,但如果它是网络代码,这不是一个好的方法,因为你应该考虑字节顺序。除此之外,这个还可以。