gpt4 book ai didi

c - 使用管道时出现错误,我的管道未声明

转载 作者:太空宇宙 更新时间:2023-11-04 10:56:51 25 4
gpt4 key购买 nike

我正在制作一个基本上会模仿的程序

    find $1 -name '*'.[ch] | xargs grep -c $2 | sort -t : +1.0 -2.0 --numeric --reverse | head --lines=$3

程序将 fork 成 4 个子进程,第一个的输出是下一个的输入。现在它说我的 pipe12 和 pipe23 没有声明。他们显然是,但我不确定他们是否在错误的位置或发生了其他事情。任何帮助是极大的赞赏。抱歉所有奇怪的注释 block 。

    [cs@cycle2 ipc]$ make clean
rm -f finder pipe tmp1 tmp2
[cs@cycle2 ipc]$ make build
gcc -Wall -g finder.c -o finder
finder.c: In function ‘main’:
finder.c:45:15: error: ‘pipe12’ undeclared (first use in this function)
if ((dup2(pipe12[1], 1)) < 0){
^
finder.c:45:15: note: each undeclared identifier is reported only once for each function it appears in
finder.c:95:15: error: ‘pipe23’ undeclared (first use in this function)
if ((dup2(pipe23[1], 1)) < 0){
^
finder.c:73:11: warning: unused variable ‘count’ [-Wunused-variable]
ssize_t count;
^
make: *** [build] Error 1
[cs@cycle2 ipc]$ vim finder.c


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <sys/wait.h>

#define BSIZE 256

#define BASH_EXEC "/bin/bash"
#define FIND_EXEC "/bin/find"
#define XARGS_EXEC "/usr/bin/xargs"
#define GREP_EXEC "/bin/grep"
#define SORT_EXEC "/bin/sort"
#define HEAD_EXEC "/usr/bin/head"

int main(int argc, char *argv[]){
int status;
pid_t pid_1, pid_2, pid_3, pid_4;



// int pipe(int pipe12[2]),pipe(int pipe23[2]),pipe(int pipe34[2]);
int pipe(int pipe12[2]);
int pipe(int pipe23[2]);


if (argc != 4) {
printf("usage: finder DIR STR NUM_FILES\n");
exit(0);
}



pid_1 = fork();
if (pid_1 == 0) {
/* First Child */


char cmdbuf[BSIZE];
bzero(cmdbuf, BSIZE);
sprintf(cmdbuf, "%s %s -name \'*\'.[ch]",FIND_EXEC, argv[1]);

if ((dup2(pipe12[1], 1)) < 0){
perror("pipe12 broke in process 1");
exit(-1);
}
close(pipe12[1]);
close(pipe12[0]);
if ( (execl(BASH_EXEC, BASH_EXEC, "-c", cmdbuf, (char *) 0)) < 0) {
fprintf(stderr, "\nError execing find. ERROR#%d\n", errno);
return EXIT_FAILURE;
}


/*
if((execl("/usr/bin/find","find","-name","*.[ch]", (char *)NULL)) < 0){
perror("execl 1 didn't work. child 1");
exit(-1);
}
*/
exit(0);
}



//----------------------------------------------------------------------


char buffer;
ssize_t count;

pid_2 = fork();
if (pid_2 == 0) {
/* Second Child */

if ( (read(pipe12[0], &buffer, 1))< 0){
perror("read broke in pipe12 process 2");
exit(-1);
}
if ((dup2(pipe12[0],0)) < 0){
perror("");
exit(-1);
}
close(pipe12[1]);
close(pipe12[0]);


char cmdbuf[BSIZE];
bzero(cmdbuf, BSIZE);
sprintf(cmdbuf, "%s %s -c %s ",XARGS_EXEC, GREP_EXEC, argv[1]);

if ((dup2(pipe23[1], 1)) < 0){
perror("pipe23 broke in process 2");
exit(-1);
}
close(pipe23[1]);
close(pipe23[0]);


if((execl(BASH_EXEC, BASH_EXEC, "-c", cmdbuf, (char *) 0)) < 0){
fprintf(stderr, "\nError execing find. ERROR#%d\n", errno);
return EXIT_FAILURE;
}

/*
if((execl("/usr/bin/xargs","grep","-c",argv[1], (char *)NULL))< 0){
perror("execl 2 didn't work. child 2");
exit(-1);
}

*/
exit(0);
}

//----------------------------------------------------------------------


pid_3 = fork();
if (pid_3 == 0) {
/* Third Child */
exit(0);
}

//----------------------------------------------------------------------


pid_4 = fork();
if (pid_4 == 0) {
/* Fourth Child */
exit(0);
}

//----------------------------------------------------------------------


if ((waitpid(pid_1, &status, 0)) == -1) {
fprintf(stderr, "Process 1 encountered an error. ERROR%d", errno);
return EXIT_FAILURE;
}
if ((waitpid(pid_2, &status, 0)) == -1) {
fprintf(stderr, "Process 2 encountered an error. ERROR%d", errno);
return EXIT_FAILURE;
}
if ((waitpid(pid_3, &status, 0)) == -1) {
fprintf(stderr, "Process 3 encountered an error. ERROR%d", errno);
return EXIT_FAILURE;
}
if ((waitpid(pid_4, &status, 0)) == -1) {
fprintf(stderr, "Process 4 encountered an error. ERROR%d", errno);
return EXIT_FAILURE;
}

return 0;
}

最佳答案

这里有点:

int pipe(int pipe12[2]);
int pipe(int pipe23[2]);

两次声明函数 pipe 接受两个整数的数组。1 相反,我假设您想声明两个包含两个 int 的数组并将它们作为参数调用 pipe。如果将上面的替换为

// declare two arrays of two ints each called pipe12 and pipe23
int pipe12[2];
int pipe23[2];

// call pipe with them.
pipe(pipe12);
pipe(pipe23);

它编译,程序的这一部分将工作。我没有检查程序的其余部分。

1实际上是一个 pipe 函数,它接受一个指向 int 的指针,原因对此并不十分重要。如果您有兴趣:数组不能作为函数参数,但指向其元素的指针可以,因此标准中有一条特殊规则规定函数参数列表中的数组声明符具有指向声明的数组类型的元素。长话短说:int pipe(int foo[2]); 等同于 int pipe(int *foo);

关于c - 使用管道时出现错误,我的管道未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com