gpt4 book ai didi

C编程管道只工作了一半

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:37 26 4
gpt4 key购买 nike

我正在为一项大学作业开发一个迷你外壳。我们必须读入命令,从路径 var 中找到要执行的二进制文件,然后执行命令,无论是否使用管道。除了管道外,我的一切正常(我认为)。通过网络搜索,我已经能够构建一个测试程序,该程序使用两个硬编码命令并将一个命令传递给另一个命令,并获得预期结果。现在,当我将该代码复制并粘贴到我的实际程序中时,第一个命令输出正常(实际上输出命令就像没有管道一样),而第二个我认为实际上没有做任何事情(第一个的输出不是通过管道传递到第二个)。

完整代码如下:

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

#define BUFFSIZE 1024
#define MAXWORDS 17
#define MAXCHAR 64

static char *path;
extern char **environ;

//split cmd "string" on pipe (|) symbol
void split(char **pipe, char **left, char **right, int n)
{
int i, x;

for(i = 0; i < n; i++)
{
if (strchr(&pipe[i][0], '|') != 0)
{
for(x = 0; x < i; x++)
strcpy(left[x], pipe[x]);
left[x++] = 0;

break;
}
}

i++;
for(x = 0; i < n; x++)
strcpy(right[x], pipe[i++]);
right[x++] = 0;
}

//Find directory where cmd can be executed from (PATH or direct access)
char *finddir(char *s)
{
char *pp;
char *pf;
int ok;
strcpy(path, getenv("PATH"));
pp = strtok(path, ":");
while (pp != NULL)
{
pf = (char *)malloc(strlen(pp) + strlen(s) + 2);
if (pf == NULL)
{
fprintf(stderr, "Out of memory in finddir\n");
return NULL;
}

strcpy(pf,pp);
strcat(pf,"/");
strcat(pf,s);
ok = !access(pf, X_OK);
free(pf);
if (ok)
return pp;

pp = strtok(NULL, ":");
}
return NULL;
}

int cmdcheck(char *cmd, char *p)
{
char *dir;

if (strchr(p, '/') != NULL)
sprintf(cmd, "%s\0", p);
else
{
dir = finddir(p);
if (dir == NULL)
return 1;
else
sprintf(cmd, "%s/%s\0", dir, p);
}

return 0;
}

void runpipe(int pfd[], char *cmd1, char *p1[], char *cmd2, char *p2[])
{
int pid;
int status;

switch (pid = fork())
{
case 0: //Child
dup(pfd[0]);
close(pfd[1]); //the child does not need this end of the pipe
execve(cmd2, p2, environ);
perror(cmd2);

default: //Parent
dup(pfd[1]);
close(pfd[0]); //the parent does not need this end of the pipe
execve(cmd1, p1, environ);
perror(cmd1);

case -1: //ERROR
perror("fork-RP");
exit(1);
}
}

int main(void)
{
int status; //read status when reading cmd in
char ch; //character currently reading
int n, i, x; //(n) count of chars read; (i) cmd args iter; (x) cmd arg iter in cmd array
char buffer[BUFFSIZE]; //read buffer
char *token; //token var when splitting buffer
int pid0, pid1, pid2; //return ID from fork call
int which; //return value from wait (child pID that just ended)
char msg[100]; //messages to print out
char *cmd1, *cmd2; //cmds when piping
char *params[MAXWORDS]; //cmd parameters to send to execve
int fd[2]; //pipe file descriptors
char *pparam1[MAXWORDS]; //cmd "string" on left side of pipe
char *pparam2[MAXWORDS]; //cmd on right side of pipe

for(;;)
{
for (i = 0; i < MAXWORDS; i++)
params[i] = malloc(MAXCHAR);

n = 0;
write(1, "# ", 2);

for(;;)
{
status = read(0, &ch, 1);
if (status == 0)
return 0; //End of file
if (status == -1)
return 1; //Error

if(n == BUFFSIZE)
{
write(1, "Line too long\n", 14);
return 1;
}

buffer[n++] = ch;

if(ch == '\n')
break;
}

buffer[n] = '\0';

x = 0;
token = strtok(buffer, " \t\n\0");
while(token != NULL)
{
strcpy(params[x++], token);
token = strtok(NULL, " \t\n\0");
}
params[x] = 0;

path = getenv("PATH");
if (path == NULL)
{
fprintf(stderr, "PATH environment variable not found.\n");
return 1;
}

n = strlen(path);
path = (char *)malloc(n+1);
if (path == NULL)
{
fprintf(stderr, "Unable to allocate space for copy of PATH.\n");
return 1;
}

cmd1 = malloc(MAXCHAR);
cmd2 = malloc(MAXCHAR);
for (i = 0; i < MAXWORDS; i++)
pparam1[i] = malloc(MAXCHAR);
for (i = 0; i < MAXWORDS; i++)
pparam2[i] = malloc(MAXCHAR);

split(params, pparam1, pparam2, x);

//Check first cmd
if(cmdcheck(cmd1, pparam1[0]))
{
sprintf(msg, "cmd '%s' is not executable\n", pparam1[0]);
write(1, msg, strlen(msg));
break;
}

//Check second cmd
if(cmdcheck(cmd2, pparam2[0]))
{
sprintf(msg, "cmd '%s' is not executable\n", pparam2[0]);
write(1, msg, strlen(msg));
break;
}

pipe(fd);

switch (pid0 = fork())
{
case 0: //Child
switch (pid1 = fork())
{
case 0: //Child
runpipe(fd, cmd1, pparam1, cmd2, pparam2);
exit(0);

default:
exit(0);
//break;

case -1: //ERROR
perror("fork-2");
exit(1);
}

default: //Parent
which = wait(&status);
if (which == -1)
{
write(1, "wait failed\n", 12);
exit(1);
}

if (status & 0xff)
sprintf(msg, "process %d terminated abnormally for reason %d\n", which, status & 0xff);
else
sprintf(msg, "process %d terminated normally with status %d\n", which, (status >> 8) & 0xff);

write(1, msg, strlen(msg));
break;

case -1: //ERROR
perror("fork-1");
exit(1);
}

free(cmd1);
free(cmd2);
for (i = 0; i < MAXWORDS; i++)
free(pparam1[i]);
for (i = 0; i < MAXWORDS; i++)
free(pparam2[i]);

free(path);
for (i = 0; i < MAXWORDS; i++)
free(params[i]);
}

return 0;
}

输入echo one | wc -l 在提示符下只会输出 one 以及后面的相应等待打印语句。我已经有几年没有使用 C 语言了,我走在正确的轨道上吗?

谢谢。

编辑:这是现在的 runpipe 函数。但唯一打印的是等待语句。

void runpipe(int pfd[], char *cmd1, char *p1[], char *cmd2, char *p2[])
{
const int READ = 0;
const int WRITE = 1;
int pid;
int status;

switch (pid = fork())
{
case 0: //Child
close(pfd[WRITE]);
dup2(pfd[READ], STDIN_FILENO);
close(pfd[READ]);
execve(cmd2, p2, environ);
perror(cmd2);

default: //Parent
close(pfd[READ]);
dup2(pfd[WRITE], STDOUT_FILENO);
close(pfd[WRITE]);
execve(cmd1, p1, environ);
perror(cmd1);

case -1: //ERROR
perror("fork-RP");
exit(1);
}
}

最佳答案

有几件事导致了意外行为。

首先是你 fork 的太多了。如果将 runpipe() 函数调用展开到 main() 中的 switch 语句中,您将看到您达到了曾孙级别:

switch (pid0 = fork())
{
case 0: // Child
switch (pid1 = fork())
{
case 0: // GRAND-Child
// function call to runpipe()
switch (pid = fork())
{
case 0: // GREAT-GRAND-Child
close(pfd[WRITE]);
dup2(pfd[READ], STDIN_FILENO);
close(pfd[READ]);
execve(cmd2, p2, environ);
perror(cmd2);

default: // GRAND-Child
close(pfd[READ]);
dup2(pfd[WRITE], STDOUT_FILENO);
close(pfd[WRITE]);
execve(cmd1, p1, environ);
perror(cmd1);

这不是必需的。在 main() 中 fork 一次,然后调用您的 runpipe() 函数。

与此问题相关的是您创建管道的位置。当您 fork 时,新创建的子进程将继承父进程的所有打开文件(以及许多其他内容)。这包括默认描述符 0、1 和 2(stdin、stdout 和 stderr),以及任何其他打开的文件,包括您创建的名为 fd 的管道。这意味着父、子、孙和曾孙都继承了管道两端的副本。您正确地关闭了 runpipe() 函数中未使用的末端(孙子和曾孙的副本),但是您的 main() 函数中的父项和子项也有副本!

由于使用管道的唯一一对进程是在 runpipe() 中创建的进程,您可以移动 fd 的声明和对 pipe( 2) 进入那个函数。

这两个修改将解决您的问题。

一个与您的 shell 流程完全无关的问题是您的 main() 最终在“父”进程上执行其 wait(2) runpipe() 函数。由于该父级是运行 cmd1 的,您的 shell 将在 cmd1 完成后立即返回其提示,而不是在最后一个命令 (cmd2 在这种情况下)在管道中完成。您可以通过运行 echo | 之类的命令来查看行为差异。睡 10 进入你的 shell 和一个真正的 shell。

关于C编程管道只工作了一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190871/

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