【Linux学习笔记】进程通信之消息队列和信号量
【Linux学习笔记】进程通信之消息队列和信号量
🔥个人主页:大白的编程日记
🔥专栏:Linux学习笔记
文章目录
- 【Linux学习笔记】进程通信之消息队列和信号量
- 前言
- 一. system V消息队列
- 二. system V信号量
- 2.1 并发编程,概念铺垫
- 2.2 信号量
- 2.3 内核是如何组织管理IPC资源的
- 2.4 在minishell中添加管道的实现
- 后言
前言
哈喽,各位小伙伴大家好!上期我们讲了进程间通信之共享内存
今天我们讲的是进程通信之消息队列和信号量。话不多说,我们进入正题!向大厂冲锋!
一. system V消息队列
- 消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法
- 每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值
- 特性方面
- IPC资源必须删除,否则不会自动清除,除非重启,所以system V IPC资源的生命周期随内核
二. system V信号量
信号量主要用于同步和互斥的,下面先来看看什么是同步和互斥。
2.1 并发编程,概念铺垫
- 多个执行流(进程), 能看到的同一份公共资源:共享资源
- 被保护起来的资源叫做临界资源
- 保护的方式常见:互斥与同步
- 任何时刻,只允许一个执行流访问资源,叫做互斥
- 多个执行流,访问临界资源的时候,具有一定的顺序性,叫做同步
- 系统中某些资源一次只允许一个进程使用,称这样的资源为临界资源或互斥资源。
- 在进程中涉及到互斥资源的程序段叫临界区。你写的代码=访问临界资源的代码(临界区)+不访问临界资源的代码(非临界区)
- 所谓的对共享资源进行保护,本质是对访问共享资源的代码进行保护
2.2 信号量
特性方面
- IPC资源必须删除,否则不会自动清除,除非重启,所以system V IPC资源的生命周期随内核
理解方面
- 信号量是一个计数器
作用方面
- 保护临界区
本质方面
- 信号量本质是对资源的预订机制
操作方面
- 申请资源,计数器–,P操作
- 释放资源,计数器++,V操作
电影院的例子
2.3 内核是如何组织管理IPC资源的
2.4 在minishell中添加管道的实现
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>#define MAX_CMD 1024
char command[MAX_CMD];
int do_face()
{memset(command, 0x00, MAX_CMD);printf("minishell$ ");fflush(stdout);if (scanf("%s", command) == 0) {getchar();return -1;}return 0;
}char **do_parse(char *buff)
{int arg = 0;static char *argv[32];char *ptr = buff;while(*ptr != '\0') {if (!isspace(*ptr)) {argv[arg++] = ptr;while((!isspace(*ptr)) && (*ptr) != '\0') {ptr++;}continue;}*ptr = '\0';ptr++;}argv[arg] = NULL;return argv;
}int do_redirect(char *buff)
{char *ptr = buff, *file = NULL;int type = 0, fd, redirect_type = -1;while(*ptr != '\0') {if (*ptr == '>') {*ptr++ = '\0';redirect_type++;if (*ptr == '>') {*ptr++ = '\0';redirect_type++;}}while(isspace(*ptr)) {ptr++;}}file = ptr;while((!isspace(*ptr)) && *ptr != '\0') {ptr++;}*ptr = '\0';if (redirect_type == 0) {fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0664);} else {fd = open(file, O_CREAT|O_APPEND|O_WRONLY, 0664);}dup2(fd, 1);
}int do_command(char *buff)
{int pipe_num = 0, i;char *ptr = buff;int pipefd[32][2] = {{-1}};int pid = -1;pipe_command[pipe_num] = ptr;while(*ptr != '\0') {if (*ptr == '|') {pipe_num++;*ptr++ = '\0';pipe_command[pipe_num] = ptr;continue;}ptr++;}pipe_command[pipe_num + 1] = NULL;return pipe_num;
}int do_pipe(int pipe_num)
{int pid = 0, i;int pipefd[10][2] = {{-1}};char *argv = {NULL};for (i = 0; i <= pipe_num; i++) {pipe(pipefd[i]);}for (i = 0; i <= pipe_num; i++) {pid = fork();if (pid == 0) {do_redirect(pipe_command[i]);argv = do_parse(pipe_command[i]);close(pipefd[i][1]);dup2(pipefd[i][0], 0);if (i != pipe_num) {close(pipefd[i + 1][0]);dup2(pipefd[i + 1][1], 1);}execvp(argv[0], argv);} else {close(pipefd[i][0]);close(pipefd[i][1]);waitpid(pid, NULL, 0);}}return 0;
}int main(int argc, char *argv[])
{int num = 0;while(1) {if (do_face() < 0)continue;num = do_command(command);do_pipe(num);}return 0;
}
后言
这就是进程通信之消息队列和信号量。大家自己好好消化!今天就分享到这! 感谢各位的耐心垂阅!咱们下期见!拜拜~