编写AC程序以实现以下UINIX | Linux命令(使用叉,管道和Exec系统调用)LS -L | WC -L

发布于 2025-02-02 18:16:22 字数 970 浏览 2 评论 0原文

我在作业中有疑问 编写AC程序以实现以下Uinix | Linux 命令(使用叉,管道和Exec系统调用) LS -L | WC -L

以下解决方案是正确的吗?

#include<stdio.h>
#include<unistd.h>
int main()
{
    int fd[2],dupFd;
    char *filename1 ="ls";
    char *filename2 ="wc";
    char *arg1 = "-l";
    pipe(fd);
    if(!fork())// return 0 for child process and 1 for parent process
    {
        close(1); // 1 for closing stdout
        dup(fd[1]);
        close(fd[0]);
        execlp(filename1,filename1,arg1,NULL);
    }else
    {
        close(0);
        dup(fd[0]);
        close(fd[1]);
        execlp(filename2,filename2,arg1,NULL);
    } 
}

输出是

(base) amol@amol-Ideapad-320:~/AOS$ ./a.out
total 140
drwxrwxr-x 2 amol amol  4096 May 28 16:19 adir
-rw-rw-r-- 1 amol amol 11962 May 29 13:59 AOSSolution2022.txt
-rwxrwxr-x 1 amol amol 16912 May 29 14:00 a.out
drwxrwxr-x 3 amol amol  4096 May 28 17:34 kdir

我需要了解上面的程序实际上在做什么。

I have question in my assignment
Write a c program to implement the following Uinix|Linux
command (use fork,pipe and exec system call)
ls -l|wc -l

The following solution is correct?

#include<stdio.h>
#include<unistd.h>
int main()
{
    int fd[2],dupFd;
    char *filename1 ="ls";
    char *filename2 ="wc";
    char *arg1 = "-l";
    pipe(fd);
    if(!fork())// return 0 for child process and 1 for parent process
    {
        close(1); // 1 for closing stdout
        dup(fd[1]);
        close(fd[0]);
        execlp(filename1,filename1,arg1,NULL);
    }else
    {
        close(0);
        dup(fd[0]);
        close(fd[1]);
        execlp(filename2,filename2,arg1,NULL);
    } 
}

Output is

(base) amol@amol-Ideapad-320:~/AOS$ ./a.out
total 140
drwxrwxr-x 2 amol amol  4096 May 28 16:19 adir
-rw-rw-r-- 1 amol amol 11962 May 29 13:59 AOSSolution2022.txt
-rwxrwxr-x 1 amol amol 16912 May 29 14:00 a.out
drwxrwxr-x 3 amol amol  4096 May 28 17:34 kdir

I need to understand what is above program is actually doing....!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文