文章目录
完成下面任务(14分)
1 在 Ubuntu 或 openEuler 中完成任务(推荐openEuler)
2 提交运行 ls | sort -r的结果,总结管道的功能 (2分)
root@LAPTOP-PRC71A0C:~# ls | sort -r
wzy
testgit
test
shiyan2
practice
ostest
exp4
exp2
bestidiocs2024
WPtest
SoftSDF-main
GmSSL
管道的功能
是Unix/Linux系统中的一种进程间通信机制,用于将一个命令的输出作为另一个命令的输入。
组合命令:通过管道,可以将多个命令组合起来,形成一个复杂的命令序列,实现更强大的功能。
数据流动:前一个命令的输出被直接作为后一个命令的输入,中间无需手动干预,提高了效率和自动化程度。
3 使用Linux系统调用编写实现管道(pipe)功能时,需要什么系统调用?提交man -k 相关截图。(2分)
在Linux系统中,实现管道(pipe)功能主要依赖于几个关键的系统调用。这些系统调用包括:
pipe:创建管道。
fork:创建子进程。
dup2:重定向文件描述符。
exec:在子进程中执行新的程序。
close:关闭不再需要的文件描述符。
root@LAPTOP-PRC71A0C:~/bestidiocs2024# man -k pipe
devlink-dpipe (8) - devlink dataplane pipeline visualization
fifo (7) - first-in first-out special file, named pipe
funzip (1) - filter for extracting from a ZIP archive in a pipe
lesspipe (1) - "input preprocessor" for less.
mdig (1) - DNS pipelined lookup utility
mkfifo (1) - make FIFOs (named pipes)
mkfifo (3) - make a FIFO special file (a named pipe)
mkfifoat (3) - make a FIFO special file (a named pipe)
pclose (3) - pipe stream to or from a process
pipe (2) - create pipe
pipe (7) - overview of pipes and FIFOs
pipe2 (2) - create pipe
popen (3) - pipe stream to or from a process
splice (2) - splice data to/from a pipe
systemd-cat (1) - Connect a pipeline or program's output with the journal
tee (2) - duplicating pipe content
vmsplice (2) - splice user pages to/from a pipe
root@LAPTOP-PRC71A0C:~/bestidiocs2024# man -k pipe |grep create
pipe (2) - create pipe
pipe2 (2) - create pipe
root@LAPTOP-PRC71A0C:~/bestidiocs2024# man -k pipe |grep close
pclose (3) - pipe stream to or from a process
4 使用系统调用创建一个管道,父进程向管道写入数据,子进程从管道读取数据。在父进程中使用 write 系统调用写入字符串 “你的八位学号+姓名” ,并在子进程中使用 read 系统调用读取数据并打印。提交代码,编译运行过程截图(可文本)(7分)
//pipedemo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
int len, i, apipe[2];
char buf[BUFSIZ];
if ( pipe ( apipe ) == -1 ){
perror("could not make pipe");
exit(1);
}
printf("Got a pipe! It is file descriptors: { %d %d }\n",
apipe[0], apipe[1]);
while ( fgets(buf, BUFSIZ, stdin) ){
len = strlen( buf );
if ( write( apipe[1], buf, len) != len ){
perror("writing to pipe");
break;
}
for ( i = 0 ; i<len ; i++ )
buf[i] = 'X' ;
len = read( apipe[0], buf, BUFSIZ ) ;
if ( len == -1 ){
perror("reading from pipe");
break;
}
if ( write( 1 , buf, len ) != len ){
perror("writing to stdout");
break;
}
}
}
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch05/process/pipe# gcc pipedemo.c
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch05/process/pipe# ./a.out
Got a pipe! It is file descriptors: { 3 4 }
20221417魏正一
20221417魏正一
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch05/process/pipe# git add a.out
The following paths are ignored by one of your .gitignore files:
ch05/process/pipe/a.out
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch05/process/pipe# git commit -m "进程间通信"
[master (root-commit) 455df68] 进程间通信
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100755 a.out
5 提交git log结果(3分)
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch05/process/pipe# git log
commit 455df681c0f81d360ad8e168f314384be704fa79 (HEAD -> master)
Author: 魏正一 <11565599+wei-zhengyi@user.noreply.gitee.com>
Date: Tue Dec 17 10:27:50 2024 +0800
进程间通信
提交要求 (1’)
1 记录实践过程和 AI 问答过程,尽量不要截图,给出文本内容
2 (选做)推荐所有作业托管到 gitee或 github 上
3 (必做)提交作业 markdown文档,命名为“学号-姓名-作业题目.md”
4 (必做)提交作业 markdown文档转成的 PDF 文件,命名为“学号-姓名-作业题目.pdf”