编写AC程序以实现以下UINIX | Linux命令(使用叉,管道和Exec系统调用)LS -L | WC -L
我在作业中有疑问 编写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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论