IO进程(学习)2024.8.22

2024-08-23 08:52
文章标签 学习 进程 io 22 2024.8

本文主要是介绍IO进程(学习)2024.8.22,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

信号

信号函数

信号处理函数

#include <signal.h>
sighandler_t signal(int signum, sighandler_t handler);
功能:信号处理函数
参数:signum:要处理的信号
           handler:信号处理方式
                        SIG_IGN:忽略信号
                        SIG_DFL:执行默认操作
                        handler:捕捉信号  
                        void handler(int sig){} //函数名可以自定义
返回值:成功:设置之前的信号处理方式
               失败:-1

#include <stdio.h>
#include <signal.h>
#include <unistd.h>void handler(int sig)
{if (sig == SIGQUIT){printf("是SIGQUIT信号\n");}else{printf("是其他信号\n");}
}int main(int argc, char const *argv[])
{// signal(SIGINT, SIG_DFL); // 执行默认操作// signal(SIGINT, SIG_IGN); // 忽略信号signal(SIGINT, handler); // 捕捉信号pause();return 0;
}

信号的处理过程

程序运行在用户空间时->进程由于系统调用或中断进入内核->转向用户空间执行信号处理函数->信号处理函数完毕后进入内核->返回用户空间继续执行程序

练习:用信号的知识实现司机和售票员问题。
1.售票员捕捉SIGINT(代表开车)信号,向司机发送SIGUSR1信号,司机打印(let's gogogo)
2.售票员捕捉SIGQUIT(代表停车)信号,向司机发送SIGUSR2信号,司机打印(stop the bus)
3.司机捕捉SIGTSTP(代表到达终点站)信号,向售票员发送SIGUSR1信号,售票员打印(please get off the bus)
4.司机等待售票员下车,之后司机再下车。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>pid_t pid;  // 售票员的进程ID
pid_t ppid; // 司机的进程ID// 售票员的信号处理函数
void shoupiaoyuan(int sig)
{if (sig == SIGINT){kill(ppid, SIGUSR1); // 向司机发送SIGUSR1信号}if (sig == SIGQUIT){kill(ppid, SIGUSR2); // 向司机发送SIGUSR2信号}if (sig == SIGUSR1){printf("Please get off the bus!\n");exit(0);}
}// 司机的信号处理函数
void siji(int sig)
{if (sig == SIGUSR1){printf("Let's gogogo!\n");}if (sig == SIGUSR2){printf("Stop the bus!\n");}if (sig == SIGTSTP){kill(pid, SIGUSR1); // 向售票员发送SIGUSR1信号wait(NULL);exit(0);}
}int main()
{ppid = getpid();pid = fork();if (pid < 0){perror("forka创建失败");return -1;}if (pid == 0) // 子进程:售票员{// 等待信号while (1){signal(SIGTSTP, SIG_IGN);      // 忽略SIGTSTP信号signal(SIGINT, shoupiaoyuan);  // 捕捉信号SIGINTsignal(SIGQUIT, shoupiaoyuan); // 捕捉信号SIGQUITsignal(SIGUSR1, shoupiaoyuan); // 捕捉信号SIGUSR1pause();}}else // 父进程:司机{// 等待信号while (1){signal(SIGINT, SIG_IGN);  // 忽略SIGINTsignal(SIGQUIT, SIG_IGN); // 忽略SIGQUITsignal(SIGUSR1, siji);    // 捕捉信号SIGUSR1signal(SIGUSR2, siji);    // 捕捉信号SIGUSR2signal(SIGTSTP, siji);    // 捕捉信号SIGTSTPpause();}}return 0;
}

共享内存

概念

共享内存指的是操作系统在物理内存中申请一块空间,应用程序可以映射到这块空间,进行直接读写操作

特点

1.共享内存是一种最为高效的进程间通信方式,进程可以直接读写内存,而不需要任何数据的拷贝
2.为了在多个进程间交换信息,内核专门留出了一块内存区,可以由需要访问的进程将其映射到自己的私有地址空间
3.进程就可以直接读写这一内存区而不需要进行数据的拷贝,从而大大提高的效率
4.由于多个进程共享一段内存,因此也需要依靠某种同步机制,如互斥锁和信号量等

步骤

1.创建唯一key值        ftok
2.创建或打开共享内存        shmget
3.映射共享内存        shmat
4.取消映射        shmdt
5.删除共享内存        shmctl

函数接口

创建key值

#include <sys/types.h>
#include <sys/ipc.h>

key_t ftok(const char *pathname, int proj_id);
功能:创建key值
参数:pathname:文件名
           proj_id:取整型数的低8位数值(一般是写一个字符 'a')
返回值:成功:key值
              失败:-1
key值是根据pathname的inode号和proj_id的低8位组合而成的。

 

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>int main(int argc, char const *argv[])
{key_t key;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);return 0;
}

创建或打开共享内存

#include <sys/shm.h>

int shmget(key_t key, size_t size, int shmflg);
功能:创建或打开共享内存
参数:
        key  键值
        size   共享内存的大小
                             创建                      检测错误             创建共享内存的时候的权限
        shmflg   IPC_CREAT       |       IPC_EXCL       |             0666  
返回值:成功   shmid  共享内存的id
              出错    -1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>int main(int argc, char const *argv[])
{key_t key;int shmid;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);if (shmid <= 0){if (errno == EEXIST){shmid = shmget(key, 64, 0666);}else{perror("创建共享内存失败");return -1;}}printf("%d\n", shmid);return 0;
}

映射共享内存

void  *shmat(int  shmid,const  void  *shmaddr,int  shmflg);
功能:映射共享内存,即把指定的共享内存映射到进程的地址空间用于访问
参数:
        shmid   共享内存的id号
        shmaddr   一般为NULL,表示由系统自动完成映射
                         如果不为NULL,那么有用户指定
        shmflg:SHM_RDONLY就是对该共享内存只进行读操作
                                0        可读可写
返回值:成功:完成映射后的地址,
               出错:(void *)-1的地址

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>int main(int argc, char const *argv[])
{key_t key;int shmid;char *p;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);if (shmid <= 0){if (errno == EEXIST){shmid = shmget(key, 64, 0666);}else{perror("创建共享内存失败");return -1;}}printf("shmid号为:%d\n", shmid);p = shmat(shmid, NULL, 0);if (p == (char *)-1){perror("映射共享内存失败");return -1;}strcpy(p, "hello");printf("%s\n", p);return 0;
}

取消映射

int shmdt(const void *shmaddr);
功能:取消映射
参数:要取消的地址
返回值:成功:0  
               失败:-1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>int main(int argc, char const *argv[])
{key_t key;int shmid;char *p;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);if (shmid <= 0){if (errno == EEXIST){shmid = shmget(key, 64, 0666);}else{perror("创建共享内存失败");return -1;}}printf("shmid号为:%d\n", shmid);p = shmat(shmid, NULL, 0);if (p == (char *)-1){perror("映射共享内存失败");return -1;}strcpy(p, "hello");printf("%s\n", p);// 阻塞观察内存状态// getchar();// 取消映射shmdt(p);return 0;
}

删除共享内存

int  shmctl(int  shmid,int  cmd,struct  shmid_ds   *buf);
功能:(删除共享内存),对共享内存进行各种操作
参数:
        shmid   共享内存的id号
        cmd     IPC_STAT 获得shmid属性信息,存放在第三参数
                    IPC_SET 设置shmid属性信息,要设置的属性放在第三参数
                    IPC_RMID:删除共享内存,此时第三个参数为NULL即可
        struct  shmid_ds   *buf:是一个结构体指针,但我们是删除共享内存,所以并没有意义,我们直接设置为NULL就可以了
返回:成功0 
           失败-1

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>int main(int argc, char const *argv[])
{key_t key;int shmid;char *p;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);if (shmid <= 0){if (errno == EEXIST){shmid = shmget(key, 64, 0666);}else{perror("创建共享内存失败");return -1;}}printf("shmid号为:%d\n", shmid);p = shmat(shmid, NULL, 0);if (p == (char *)-1){perror("映射共享内存失败");return -1;}strcpy(p, "hello");printf("%s\n", p);// 阻塞观察内存状态// getchar();// 取消映射shmdt(p);// 删除共享内存shmctl(shmid, IPC_RMID, NULL);return 0;
}

操作命令

ipcs -m:查看系统中创建的共享内存
ipcrm -m shmid:删除创建的共享内存

练习:通过共享内存实现进程间通信,一个进程从终端输入数据(input.c),另一个进程打印数据(output.c),循环执行,当输入quit时循环结束

input.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>struct data
{int flag;char buf[32];
};int main()
{key_t key;int shmid;struct data *p = NULL;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);shmid = shmget(key, 64, IPC_CREAT | 0666);if (shmid < 0){perror("创建共享内存失败");return -1;}printf("shmid号为:%d\n", shmid);p = (struct data *)shmat(shmid, NULL, 0);if (p == (struct data *)-1){perror("映射共享内存失败");return -1;}p->flag = 0;while (1){scanf("%s", p->buf);p->flag = 1;if (strcmp(p->buf, "quit") == 0){break;}}shmdt(p);return 0;
}

output.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
struct data
{int flag;char buf[32];
};int main()
{key_t key;int shmid;struct data *p = NULL;key = ftok("./test", 'a');if (key < 0){perror("创建key值失败");return -1;}printf("key值为:%#x\n", key);shmid = shmget(key, 64, 0666);if (shmid < 0){perror("获取共享内存失败");return -1;}printf("shmid号为:%d\n", shmid);p = (struct data *)shmat(shmid, NULL, 0);if (p == (struct data *)-1){perror("映射共享内存失败");return -1;}p->flag = 0;while (1){if (p->flag == 1){if (strcmp(p->buf, "quit") == 0){break;}printf("读取到的数据: %s\n", p->buf);p->flag = 0;}}shmdt(p);shmctl(shmid, IPC_RMID, NULL);return 0;
}

这篇关于IO进程(学习)2024.8.22的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098916

相关文章

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

Linux五种IO模型的使用解读

《Linux五种IO模型的使用解读》文章系统解析了Linux的五种IO模型(阻塞、非阻塞、IO复用、信号驱动、异步),重点区分同步与异步IO的本质差异,强调同步由用户发起,异步由内核触发,通过对比各模... 目录1.IO模型简介2.五种IO模型2.1 IO模型分析方法2.2 阻塞IO2.3 非阻塞IO2.4

Java中最全最基础的IO流概述和简介案例分析

《Java中最全最基础的IO流概述和简介案例分析》JavaIO流用于程序与外部设备的数据交互,分为字节流(InputStream/OutputStream)和字符流(Reader/Writer),处理... 目录IO流简介IO是什么应用场景IO流的分类流的超类类型字节文件流应用简介核心API文件输出流应用文

C#使用SendMessage实现进程间通信的示例代码

《C#使用SendMessage实现进程间通信的示例代码》在软件开发中,进程间通信(IPC)是关键技术之一,C#通过调用WindowsAPI的SendMessage函数实现这一功能,本文将通过实例介绍... 目录第一章:SendMessage的底层原理揭秘第二章:构建跨进程通信桥梁2.1 定义通信协议2.2

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

一文解密Python进行监控进程的黑科技

《一文解密Python进行监控进程的黑科技》在计算机系统管理和应用性能优化中,监控进程的CPU、内存和IO使用率是非常重要的任务,下面我们就来讲讲如何Python写一个简单使用的监控进程的工具吧... 目录准备工作监控CPU使用率监控内存使用率监控IO使用率小工具代码整合在计算机系统管理和应用性能优化中,监

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于