execl()似乎没有从stdin中读取

编程入门 行业动态 更新时间:2024-10-24 00:20:12
本文介绍了execl()似乎没有从stdin中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试用C语言重现此命令:

I'm trying to reproduce this command in c language:

ls | wc > output.txt

为此,我编写了以下程序:

So, to do that, I wrote the following program:

#include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> int main() { pid_t lsFork, wcFork; int tube[2]; pipe(tube); lsFork = fork(); if(lsFork == 0) // ls command { close(tube[0]); dup2(tube[1], STDOUT_FILENO); close(tube[1]); if(execl("/usr/bin/ls", "ls", NULL) == -1) perror("Cannot execute ls"); } else { wcFork = fork(); if(wcFork == 0) // wc command { sleep(1); int file = open("output.txt", O_WRONLY | O_CREAT); if(file == -1) perror("Cannot open output.txt"); close(tube[1]); dup2(tube[0], STDIN_FILENO); close(tube[0]); dup2(file, STDOUT_FILENO); close(file); /*char buffer[BUFSIZ]; read(STDIN_FILENO, buffer, BUFSIZ); write(STDOUT_FILENO, buffer, BUFSIZ);*/ if(execl("/usr/bin/wc", "wc", NULL) == -1) perror("Cannot execute wc"); close(STDOUT_FILENO); } else // parent { int status; waitpid(lsFork, &status, 0); waitpid(wcFork, &status, 0); } } return EXIT_SUCCESS; }

但是,该程序不会退出.根据htop的说法,wc命令正在阻止该程序.为了理解这种行为,我编写了一段代码(在execl()之前的行注释了),但我不理解它的工作原理,而不是execl().调用此函数时我会忘记一些东西吗?

But, the program does not exit. According to htop, the wc command is blocking the program. To understand this behaviour, I wrote a piece of code (the lines commented before execl()) and I don't understand what this works and not execl(). Am I forgetting something when calling this function?

推荐答案

在您可以轻松完成时不要使事情复杂化.请尝试以下& ;;中的简单代码看看您是否能听懂任何内容.

Don't complicate things when you can do it easily.. Try the simpler code below & see if you can understand anything or not.

int main(){ int tube[2]; int fID; pipe(tube); if (fork() == 0){ // this is the child process close(tube[0]); // reading end of the pipe dup2(tube[1], 1); // stdout ---> pipe writing end execlp("ls", "ls", NULL); }else{ if (fork() == 0){ //umask(0022); fID = open("sample.txt", O_WRONLY | O_CREAT, 0644); close(tube[1]); // writing end of the pipe dup2(tube[0], 0); // stdin ----> pipe reading end dup2(fID, 1); execlp("wc", "wc", NULL); } } return 0; }

注意如果代码的目的是仅实现上述管道,那么您无需实现任何等待机制.操作系统将自动杀死所有僵尸孩子(如果有).此外, execlp("wc","wc",NULL); 将自动阻止程序结束.因此它不会提早退出

Note If the purpose of the code is to solely implement the above mentioned piping, then you don't need to implement any waiting mechanisms. The OS will auto-kill all the zombie child, if any. Moreover execlp("wc", "wc", NULL); will auto block the program to end. Hence it will not exit early

更多推荐

execl()似乎没有从stdin中读取

本文发布于:2023-07-06 07:56:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:execl   stdin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!