赞
踩
把一个程序的输出直接连接在另外一个程序的输入。
管道分为有名管道和无名管道两种,它们的区别是:
无名管道只能在父子进程之间进行通信。
有名管道又称为命名管道,可以在任意两个进程之间进行通信。
通信模式 | 通信特点 | 例子 |
---|---|---|
单工 | 数据只在一个方向上传输,不能实现双方通信 | 电视、广播 |
半双工(切换的单工) | 允许数据在两个方向上传输,但是同一时间数据只能在同一个方向上传输 | 对讲机 |
全双工 | 允许数据在两个方向上同时传输 | 手机通话 |
- 读阻塞(进程阻塞):当管道中没有数据可读时,会产生读阻塞。
- 写阻塞:当管道已满,再往管道中写入数据时,会产生写阻塞。直到有空间可以写入时,再写。
- 管道破裂:只有写端,没有读端。
- 管道中不能使用lseek
是一种亲缘进程间的通信方法
无名管道存在于kernel中,A,B必须具有亲缘关系进程。同一时刻,只能有一个写端或一个读端。
- kernel的功能模块
内存管理单元(MMU)
主要负责物理内存——>虚拟内存空间的映射
内存的开辟,释放,存储,读取等一系列功能进程管理单元:
task(任务)创建,管理和销毁
任务调度策略:基于时间片的公平的轮转策略
基于优先级的抢占式任务调度策略文件系统
提供用户访问计算机资源的接口(软件)
存储,组织管理计算机文件和设备的软件网络管理
提供网络通信协议栈
驱动模块
硬件设备驱动
父子进程间,只要是fork()出来的,就会完美复制父进程的数据。如果在fork()之前创建管道,并获取管道的操作接口,子进程就能使用管道。
- 只能用于具有亲缘关系的进程之间的通信(也就是父子进程和兄弟进程之间)。
- 是一个半双工的通信方式,具有固定的读端和写端。
- 管道可以看做是一种特殊的文件,对管道的读写可以使用普通的read() ,write()函数,但管道不属于任何文件系统的,只存在于内存中。
- 创建
(pipe函数用来创建无名管道)- 操作
(read读;write写)- 关闭操作端口
(close)
本例引用于:http://blog.sina.com.cn/s/blog_5ed3e6210100d87d.html
https://blog.csdn.net/zggzgw/article/details/78120171
#include<unistd.h> #include<stdio.h> #include<string.h> #include<sys/types.h> #include<stdlib.h> #include<sys/wait.h> void read_pipe(int fd) { char message[100]; read(fd,message,100); printf("read pipe message:%s",message); } void write_pipe(int fd) { char *message="this is Tuesday!\n"; write(fd,message,strlen(message)+1); } int main() { int fd[2]; pid_t pid; int stat_val; if(pipe(fd)) { printf("create pipe failed!\n"); } pid=fork(); switch(pid) { case -1: printf("fork error!\n"); break; case 0: close(fd[1]); read_pipe(fd[0]); break; default: close(fd[0]); write_pipe(fd[1]); wait(&stat_val); break; } }
本引用于自:
https://blog.csdn.net/yangxueyangxue/article/details/122336664
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> int main(void) { char buf[32] = {0}; pid_t pid; // 定义一个变量来保存文件描述符 // 因为一个读端, 一个写端, 所以数量为 2 个 int fd[2]; // 创建无名管道 pipe(fd); printf("fd[0] is %d\n", fd[0]); printf("fd[2] is %d\n", fd[1]); // 创建进程 pid = fork(); if (pid < 0) { printf("error\n"); } if (pid > 0) { int status; close(fd[0]); write(fd[1], "hello", 5); close(fd[1]); wait(&status); exit(0); } if (pid == 0) { close(fd[1]); read(fd[0], buf, 32); printf("buf is %s\n", buf); close(fd[0]); exit(0); } return 0; }
是对无名管道的一种改进
- 它可以使互不相关的两个进程实现彼此通信
- 该管道可以通过路径名来指出,并且在文件系统中是可见的。在建立管道之后,两个进程就可以把它当作普通文件进行读写,使用非常方便。
- FIFO严格遵循先进先出原则,对管道及FIFO的读总是从开始处返回数据,对它们的写则把数据添加到末尾。有名管道不支持如Iseek()等文件的定位操作。
- 有名管道依然在内核态内存中
- 有名管道在文件系统中有节点(即在跟文件系统中可以找到)
- 有名管道的大小始终为0
- 有名管道的文件类型为p
- 有名管道严格遵循先进先出原则
- 有名管道不能使用文件重定位的函数Iseek
- 有名管道可以用在亲缘和非亲缘进程间(一般用于非亲缘进程间通信)
- 创建有名管道文件
(mkfifo即是命令也是函数;mknod也可以创建管道文件)- 打开有名管道
(open)- 读/写
(read/write)- 关闭
(close)
本例引用于:
https://blog.csdn.net/yangxueyangxue/article/details/122336664
write01.c
创建两个无关联的进程, 一个进程创建有名管道并写数据, 另一个进程通过管道读数据。
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char *argv[]) { int ret; char buf[32] = {0}; int fd; if (argc < 2) { printf("Usage:%s <fifo name> \n", argv[0]); return -1; } if (access(argv[1], F_OK) == -1) { ret = mkfifo(argv[1], 0666); if (ret == -1) { printf("mkfifo is error \n"); return -2; } printf("mkfifo is ok \n"); } fd = open(argv[1], O_WRONLY); while (1) { sleep(1); write(fd, "hello", 5); } close(fd); return 0; }
read01.c
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> int main(int argc, char *argv[]) { char buf[32] = {0}; int fd; if (argc < 2) { printf("Usage:%s <fifo name> \n", argv[0]); return -1; } fd = open(argv[1], O_RDONLY); while (1) { sleep(1); read(fd, buf, 32); printf("buf is %s\n", buf); memset(buf, 0, sizeof(buf)); } close(fd); return 0; }
以下示例引用于:
https://blog.csdn.net/zsy3757486/article/details/126765536
根据条件 从命令结果中提取对应内容
head -1 1.txt | cut -c 2
参数 | 含义 |
---|---|
-c | 按字符选取内容 |
head -n 文件名 | cut -d ':' -f n1,n2
OR(或者)
head -n 文件名 | cut -d ':' -f n1-n2
范围控制 | 含义 |
---|---|
n | 只显示第 n 段 |
n- | 显示从第 n 段一直到行尾 |
n1-n2 | 显示从第 n1 段到 n2 段 |
head -2 2.txt | cut -d ':' -f 1,2
参数 | 含义 |
---|---|
-d | 指定分隔符 |
-f | 显示指定段落内容 |
head -4 2.txt | cut -d ':' -f 1-4
其下皆由此引用:
https://blog.csdn.net/qq_45171957/article/details/123698265
tr -dc A-Za-z0-9_ </dev/urandom | head -c 8 | xargs
awk -F: '{print $1}' /etc/passwd | sort
history | awk '{print $2}' | sort | uniq -u | sort -rn | head -5
cat /etc/passwd | grep "/bin/bash" | cut -d: -f1,6
cut -d: -f1,6 表示以:为分隔符显示第1和第6列的内容-d指定分隔符,-f指定列
ls -l | cut -c 1 | grep "d" | wc -l
ls -l 长格式列出当前目录的所有内容,每行的第一个字符表示文件的类
cut -c 1 截取每行的第一个字符
grep “d” 获取文件类型是目录的行
wc -l 统计grep命令输出的行数,即子目录个数
cat 1.txt | paste -d: 2.txt -
paste -d: 2.txt - 表示以:为分割符合并两个文件,合并时2.txt文件的内容在前
-代表1.txt文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。