C Unix 管道示例
尝试实现一个shell,主要是管道。我已经编写了这个测试用例,我希望将 ls 简单地通过管道传输到 wc ...它肯定不会按预期工作。它将 ls 打印到终端,然后打印内存耗尽。 我非常不知道如何解决这个问题并让它发挥作用。 find_path 在我的所有测试中都有效。
编辑-我必须在项目中使用 execv ,它是一个类的东西,但我已经用 execvp 尝试过以防万一,它做了完全相同的事情。另外,这只是一个例子,一个测试来看看为什么它不起作用,我为两个命令和 waitpid 调用 fork 两次,因为我没有其他事情可做。
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int find_path(char* execname, char** dst)
{
char *path = getenv("PATH");
path = strdup(path);
char *pos;
path = strtok_r(path, ":", &pos);
char *originalpath = path;
do
{
char* test = (char*)calloc(strlen(path) + strlen(execname) + 2, sizeof(char));
test = strcpy(test, path);
int testlen = strlen(test);
(*(test+testlen)) = '/';
strcpy(test + testlen + 1,execname);
struct stat buf;
int result = stat(test, &buf);
if (result == 0)
{
*dst = test;
free (originalpath);
return 1;
}
else
{
free(test);
}
} while ((path = strtok_r(NULL, ":", &pos)) != NULL);
free(originalpath);
return 0;
}
int main()
{
char *cmd1 = "ls";
char *cmd2 = "wc";
int filedes[2];
pipe(filedes);
char** argv = (char**)calloc(1, sizeof(char*));
argv[0] = (char*)malloc(sizeof(char*));
argv[0] = NULL;
pid_t pid = fork();
if (pid == 0)
{
char *path;
find_path(cmd1, &path);
dup2(filedes[1],stdout);
execv(path,argv);
}
pid = fork();
if (pid == 0)
{
dup2(filedes[0], stdin);
char *path;
find_path(cmd2, &path);
execv(path, argv);
}
else
waitpid(pid);
}
Trying to implement a shell, mainly piping. I've written this test case which I expect to simply pipe ls to wc...it definitely doesn't work as expected. It prints ls to the terminal then prints memory exhausted.
I'm very lost in how to fix this and get it to work. find_path works in all of my tests.
Edit - I have to use execv for the project, its a class thing, but I've tried it with execvp just in case and it does the exact same thing. Also this is just an example, a test to see why it does not work, I call fork twice once for both commands and waitpid because I have nothing else to do.
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int find_path(char* execname, char** dst)
{
char *path = getenv("PATH");
path = strdup(path);
char *pos;
path = strtok_r(path, ":", &pos);
char *originalpath = path;
do
{
char* test = (char*)calloc(strlen(path) + strlen(execname) + 2, sizeof(char));
test = strcpy(test, path);
int testlen = strlen(test);
(*(test+testlen)) = '/';
strcpy(test + testlen + 1,execname);
struct stat buf;
int result = stat(test, &buf);
if (result == 0)
{
*dst = test;
free (originalpath);
return 1;
}
else
{
free(test);
}
} while ((path = strtok_r(NULL, ":", &pos)) != NULL);
free(originalpath);
return 0;
}
int main()
{
char *cmd1 = "ls";
char *cmd2 = "wc";
int filedes[2];
pipe(filedes);
char** argv = (char**)calloc(1, sizeof(char*));
argv[0] = (char*)malloc(sizeof(char*));
argv[0] = NULL;
pid_t pid = fork();
if (pid == 0)
{
char *path;
find_path(cmd1, &path);
dup2(filedes[1],stdout);
execv(path,argv);
}
pid = fork();
if (pid == 0)
{
dup2(filedes[0], stdin);
char *path;
find_path(cmd2, &path);
execv(path, argv);
}
else
waitpid(pid);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,当程序难以调试时,最好稍微简化它以消除错误源。这是您的程序,经过简化以删除
find_path
作为错误源:这应该按照您的预期运行。
在简化过程中,出现了一些错误:
argv[]
没有正确设置,特别是 argv[0] 被设置为 NULL;ls
的管道的输入端。当 ls 完成时,管道并未关闭(因为 wc 进程仍将其打开),从而阻止了 wc 完成。stdout
和stdin
(其类型为FILE*
)与文件描述符编号0
混淆> 和1
(由dup
、pipe
等使用)Often when it is hard to debug a program, it is best to simplify it a little to eliminate sources of error. Here is your program, simplified to remove
find_path
as a source of errors:This should behave as you expect.
During simplification, a few errors came out:
argv[]
wasn't being setup correctly, in particular, argv[0] was being set to NULL;ls
. Whenls
finished, the pipe wasn't being closed (because thewc
process still had it open), preventingwc
from ever finishing.stdout
andstdin
(which are of typeFILE*
) with the file descriptor numbers0
and1
(used bydup
,pipe
, etc.)您可以做很多事情来改进此代码(例如,将其分解为较小的函数将是一个开始),但我怀疑您的内存不足问题来自 find_path() 中的代码,您可以通过使用 execvp 它将使用标准 PATH 机制为您找到可执行文件。使用 sigaction 安装信号处理程序来处理可能是个好主意SIGCHLD 并从信号处理程序调用 waitpid,而不是仅仅调用 waitpid( )就像你正在做的那样。您的分叉次数似乎超出了您的预期,而且您没有检查错误。希望这些建议有所帮助。
There is a lot you can do to improve this code (e.g. breaking this into smaller functions would be a start), but I suspect your out of memory issue is from the code in find_path(), which you could avoid entirely by using execvp which will locate the executable using the standard PATH mechanism for you. It is probably a good idea to install a signal handler using sigaction to handle SIGCHLD and invoke waitpid from the signal handler, instead of just invoking waitpid() ad-hoc like you are doing. You appear to be forking more times than you want, and you aren't checking for errors. Hope these suggestions help.