如何解决如何使用管道和 fork() 在 C 中执行命令 ls|sort -r?
我试图为学校项目解决这个问题,但我无法理解如何打开和关闭管道内的读写: 问题是:创建一个执行以下命令 ls| 的 C 程序sort -r 使用管道并创建 fork() 和 dup()
解决方法
我通常不会容忍发布家庭作业问题的解决方案,但互联网上有足够多的糟糕代码,我想我会发布我认为并不糟糕的代码。也许我这么想是冒昧的,但是:
/* Execute ls | sort -r */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
static void xpipe(int *fd) { if( pipe(fd) == -1 ){ err(1,"pipe"); } }
static void xdup2(int a,int b) { if( dup2(a,b) == -1 ){ err(1,"dup2"); } }
static void xclose(int fd) { if( close(fd) == -1 ){ err(1,"close"); } }
static void
execute(int *fd,char **cmd,int w)
{
switch( fork() ){
case -1:
err(EXIT_FAILURE,"fork");
case 0:
xdup2(fd[w],w);
xclose(fd[0]);
xclose(fd[1]);
execvp(cmd[0],cmd);
perror("execvp");
exit(EXIT_FAILURE);
}
}
int
main(void)
{
int rv = EXIT_SUCCESS;
char *ls[] = { "ls",NULL };
char *sort[] = { "sort","-r",NULL };
int fd[2];
xpipe(fd);
execute(fd,ls,1);
execute(fd,sort,0);
xclose(fd[0]);
xclose(fd[1]);
for( int i = 0; i < 2; i++ ){
int status;
wait(&status);
if( ! WIFEXITED(status) || WEXITSTATUS(status) ){
rv = EXIT_FAILURE;
}
}
return rv;
}
,
int main(int argc,char*argv[])
{
int fd[2];
pid_t p1,p2;
pipe(fd);
if (pipe(fd)==-1){
perror("Erreur pipe");
exit();
}
p1=fork();
if (p1==-1){
perror("Erreur fork");
exit();
}
else if (p1==0) {
close(fd[0]);
dup2(fd[1],1);
execlp("ls","ls",argv[1],0);
}
p2=fork();
if (p2==-1){
perror("Erreur fork");
exit();
}
else if (p2==0){
close(fd[1]);
dup2(fd[0],0);
execlp("sort",NULL);
}
waitpid(p1,nullptr,0);
close(fd[1]);
waitpid(p2,0);
return 0;
}
// can you have a look at this and tell me if it would work?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。