0
mkfifo fifo1
mkfifo fifo2
mkfifo fifo3
xterm -e bash -c "cat <fifo1 & tee fifo2 fifo3" &
xterm -e bash -c "cat <fifo2 & tee fifo1 fifo3" &
xterm -e bash -c "cat <fifo3 & tee fifo1 fifo2" &

知道如何在 c 编程中执行上述 unix 命令。我尝试使用 execl,但似乎不起作用。提前致谢。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(){
int pid;
char parmList[100];
int i=1;

sprintf(parmList,"-e bash -c {cat <fifo%d & tee fifo%d fifo%d}",i,i+1,i+2);


if ((pid = fork()) == -1)
    perror("fork error");
else if (pid == 0)
{
     execl("/usr/bin/xterm","xterm",parmList,NULL);
}

return 0;

}
4

1 回答 1

1

使用execv系统调用(execv 手册页

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(){
    int pid;
    int i=1;
    char command[100];
    char * args[] = {
        "-e",
        "bash",
        "-c",
        NULL,
        NULL
    };
    sprintf(command, "{cat <fifo%d & tee fifo%d fifo%d}", i, i+1, i+2);
    args[3] = command;

    if ((pid = fork()) == -1)
        perror("fork error");
    else if (pid == 0)
        execv("/usr/bin/xterm", args);
    return 0;
}
于 2013-04-03T07:23:35.750 回答