我的under-linux c prog出了什么问题:“ls -al | tr a-z A-Z>
发布时间:2020-12-14 00:52:37 所属栏目:Linux 来源:网络整理
导读:我是 linux的新手,所以.我不能让我的脚本工作.我只是猜测,程序在执行tr函数时被暂停. #include fcntl.h#include stdio.h#include unistd.hint main(){int pdesc[2];pipe(pdesc);int a = fork();if (a == 0) // child { dup2(pdesc[1],1); // chaning std_out
我是
linux的新手,所以.我不能让我的脚本工作.我只是猜测,程序在执行tr函数时被暂停.
#include <fcntl.h> #include <stdio.h> #include <unistd.h> int main() { int pdesc[2]; pipe(pdesc); int a = fork(); if (a == 0) // child { dup2(pdesc[1],1); // chaning std_out to pipes_out execlp("ls","ls","-l","-a",NULL); } else //parent { wait(); int file1 = open("file.txt",O_WRONLY|O_CREAT|O_TRUNC,0777); dup2(pdesc[0],0); // chaning std_in to pipes_in dup2(file1,1); // chaning std_out to file's stream execlp("tr","tr","a-z","A-Z",NULL); } return 0; } 解决方法
经典错误,所以,好问题.
您需要关闭父级和子级中未使用的管道文件描述符. 从管道读取的过程具有(本身)开放管道写入端,因此管道永远不会完全关闭,因此它永远不会提供EOF. 此外,wait(2)导致死锁,程序不包括< sys / wait.h>,并且wait(2)的调用缺少必需的参数.因为shell会等待父进程而不是子进程,所以实际上在这里等一下wait(2)调用会很好.但是在当前的双进程设计中你无处可去,因为在父进程的execlp(2)之后你无法控制.解决这个问题的一种方法是再次使用父fork(),并让原始PID在循环中等待(2)除外,直到所有子节点都完成为止. 这是一个工作版本,请注意输出文件模式的更改. #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main() { int pdesc[2]; pipe(pdesc); int a = fork(); if (a == 0) { // child dup2(pdesc[1],1); // chaining std_out to pipes_out close(pdesc[1]); close(pdesc[0]); execlp("ls",NULL); } else { //parent int file1 = open("file.txt",0644); dup2(pdesc[0],0); // chaning std_in to pipes_in dup2(file1,1); // chaning std_out to file's stream close(pdesc[0]); close(pdesc[1]); close(file1); execlp("tr",NULL); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |