我的目标是通过FIFO在孩子和父母之间建立IPC。孩子应该跑
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
将其输出重定向到父节点的输入,父节点应该运行这个命令:
cut -l : -f 1
并将其输出到命令行。
现在,我已经成功地链接了我的FIFO,并将子进程的输出重定向到父进程的输入。我做了多次测试,连接正常。问题出在执行文件中,它应该看起来像这样:
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
但我很确定它不是。
int cut(){
//
int myfifo;
char buf[MAX_BUF];
printf("nCut opening FIFO");
if((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC))<0){
perror("open FIFO at cut");
quit(EXIT_FAILURE);}
else{printf("nCut has FIFO opened and is readingn");}
//read(myfifo, buf, MAX_BUF); outputting buf goes as supposed to
if( dup2(myfifo, 0) < 0 ){
perror("dup2 at cut");
quit(EXIT_FAILURE);}
//read(STDIN_FILENO, buf, MAX_BUF);
close(myfifo);
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
//this is for testing buf, but i guess the program shouldn't even get here
printf("nCut has received: %snAnd is closing FIFO", buf);
return -1;
}
int cat(){
int myfifo;
//OPEN FIFO
printf("nCat opening FIFO");
if( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) )<0){
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
else{
printf("nCat has opened FIFO");
//WRITE OUTPUT OF "cat etcpasswd" TO FIFO
dup2(myfifo, 1);
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
}
close(myfifo);
return 0;
}
main当前只创建fifo (mkfifo), fork()并调用函数。我的问题可能与父(运行切割)的标准输入有关,但我不这样认为,或者我假设execl()直接从标准输入读取,而它没有。我认为这真的是因为我没有正确地通过execl()编写"cut"。
对代码的任何更正,甚至我表达一些想法的方式都可以表明我没有正确理解某些东西,将非常感激。谢谢你的帮助
在注释中指出,GNU和BSD(和POSIX)下的cut
命令不支持-l
选项;您需要的选项是-d
分隔符。
这段代码适合我。在我的机器上,/bin/cat
是正确的,但/usr/bin/cut
是正确的(不是/bin/cut
),所以我必须用:
$ rmk cutcat UFLAGS=-DCUT_CMD=/usr/bin/cut && ./cutcat
gcc -O3 -g -std=c11 -Wall -Wextra -Werror -DCUT_CMD=/usr/bin/cut cutcat.c -o cutcat
$
使用make
的自定义变体(称为rmk
)和自定义makefile
,但是cut
命令的位置是通过UFLAGS
(用户标志)宏在命令行上指定的。C代码中的STRING
和EXPAND
宏令人讨厌,但不如试图通过调用make
(或rmk
)的shell获得双引号,然后调用make
来运行编译器的shell那么令人讨厌。
代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifndef CUT_CMD
#define CUT_CMD /bin/cut
#endif
#ifndef CAT_CMD
#define CAT_CMD /bin/cat
#endif
#define EXPAND(x) #x
#define STRING(x) EXPAND(x)
static const char cut_cmd[] = STRING(CUT_CMD);
static const char cat_cmd[] = STRING(CAT_CMD);
static inline void quit(int status) { exit(status); }
enum { MAX_BUF = 4096 };
static void cut(void)
{
int myfifo;
printf("nCut opening FIFO");
if ((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC)) < 0)
{
perror("open FIFO at cut");
quit(EXIT_FAILURE);
}
printf("nCut has FIFO opened and is readingn");
if (dup2(myfifo, 0) < 0)
{
perror("dup2 at cut");
quit(EXIT_FAILURE);
}
close(myfifo);
execlp(cut_cmd, "cut", "-d:", "-f", "1", NULL);
fprintf(stderr, "Failed to execute %sn", cut_cmd);
exit(1);
}
static
void cat(void)
{
int myfifo;
printf("nCat opening FIFO");
if ( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) ) < 0)
{
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
printf("nCat has opened FIFO");
dup2(myfifo, 1);
close(myfifo);
execl(cat_cmd, "cat", "/etc/passwd", (char *)0);
fprintf(stderr, "Failed to execute %sn", cat_cmd);
exit(1);
}
int main(void)
{
mkfifo("/tmp/myfifo", 0600);
if (fork() == 0)
cat();
else
cut();
/*NOTREACHED*/
fprintf(stderr, "You should not see this messagen");
return 0;
}
样本输出:多截断
…
nobody
root
daemon
_uucp
_taskgated
_networkd
…
代码实际上与您所拥有的代码没有太大不同。我清理了一些杂物。我确实确保close(myfifo)
被cat()
执行;原版中没有。