多线程与信号

ops/2024/10/18 18:24:42/
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

消息处理 

<code class="language-cpp">#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <semaphore.h>
#include <signal.h>
#include <pthread.h>
struct msgbuf
{long mtype;  // 消息的标识(整数)char mtext[100];    // 消息的正文};int msg_id1;
void *t1(void *arg) {
struct msgbuf buf1;
while(1){ssize_t msgrcv_ret1 = msgrcv(msg_id1, &buf1,sizeof(buf1), 2, 0);if(-1 == msgrcv_ret1) {printf("rcv msg failed\n");}if(strcmp(buf1.mtext,"bye\n")==0){break;}printf("MSG1:%s\n",buf1.mtext);}               }
int main()
{pthread_t tid1;pthread_create(&tid1, NULL, t1, NULL);//1.创建KEY值key_t key = ftok("./", 0);if(-1 ==key){printf("creat key failed");return -1;}//2.创建消息队列int msg_id = msgget(key, IPC_CREAT | 0777);if(-1 == msg_id){printf("creat msg failed\n");return -1;}//创建KEY1//1.创建KEY值key_t key1 = ftok("./", 1);if(-1 ==key){printf("creat key failed");return -1;}//2.创建消息队列msg_id1 = msgget(key1, IPC_CREAT | 0777);if(-1 == msg_id1){printf("creat msg failed\n");return -1;}//3.往消息队列里面发送消息//需要定义一个结构体变量bufstruct msgbuf buf;//需要给结构体成员赋值buf.mtype = 1;while(1){memset(buf.mtext,0,100);//消息从键盘获取fgets(buf.mtext,100,stdin);//以回车结尾int msgsnd_ret = msgsnd(msg_id,&buf,sizeof(buf), 0); //默认是阻塞的发送if(-1 == msgsnd_ret){printf("send msg failed\n"); return -1;}if(strcmp(buf.mtext, "bye\n") == 0){break;}}msgctl(msg_id, IPC_RMID, NULL);return 0;	}code>
<code class="language-cpp">#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>struct msgbuf
{long mtype;  // 消息的标识(整数)char mtext[100];    // 消息的正文};
int msg_id1;
void *t1(void *arg) {//需要定义一个结构体变量bufstruct msgbuf buf1;//需要给结构体成员赋值buf1.mtype = 2;while(1){memset(buf1.mtext,0,100);//消息从键盘获取fgets(buf1.mtext,100,stdin);//以回车结尾int msgsnd_ret1 = msgsnd(msg_id1,&buf1,sizeof(buf1), 0); //默认是阻塞的发送if(-1 == msgsnd_ret1){printf("send msg failed\n");return NULL;}if(strcmp(buf1.mtext, "bye\n") == 0){break;}}}
int main()
{pthread_t tid1;pthread_create(&tid1, NULL, t1, NULL);//key值key_t key = ftok("./", 0);if(-1 ==key){printf("creat key failed");return -1;}//创建消息队列int msg_id = msgget(key, IPC_CREAT | 0777);if(-1 == msg_id){printf("creat msg failed\n");return -1;}//key值key_t key1 = ftok("./", 1);if(-1 ==key1){printf("creat key failed");return -1;}//创建消息队列msg_id1 = msgget(key1, IPC_CREAT | 0777);if(-1 == msg_id1){printf("creat msg failed\n");return -1;}//接受消息的消息struct msgbuf buf;while(1){ssize_t msgrcv_ret = msgrcv(msg_id, &buf,sizeof(buf), 1, 0);if(-1 == msgrcv_ret){printf("rcv msg failed\n");}if(strcmp(buf.mtext,"bye\n")==0){break;}printf("MSG:%s\n",buf.mtext);}}code>

信号处理 

<code class="language-cpp">#include <stdio.h>
#include <fcntl.h>              
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <signal.h>sem_t *space, *data;
char *p;
int shm_id;void cleanup(int sig) {// 释放信号量和共享内存sem_close(space);sem_unlink("/1");sem_close(data);sem_unlink("/2");shmdt(p);shmctl(shm_id, IPC_RMID, NULL);_exit(0);
}int main() {key_t key = ftok("./", 2);shm_id = shmget(key, 2, IPC_CREAT | 0666);p = shmat(shm_id, NULL, 0);space = sem_open("/1", O_CREAT, 0666, 1);data = sem_open("/2", O_CREAT, 0666, 0);// 注册SIGINT信号处理函数signal(SIGINT, cleanup);char *msg = "0123456789";int i = 0;while (1) {sem_wait(space);memcpy(p, msg + i, 1);sem_post(data);i = (i + 1) % 10;}// 解除映射和清理资源cleanup(0); // 虽然在无限循环中不会到达这里࿰c;但确保资源得到释放
}code>
<code class="language-cpp">#include <stdio.h>
#include <fcntl.h>              
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <signal.h>sem_t *space, *data;
char *p;
int shm_id;void cleanup(int sig) {// 释放信号量和共享内存sem_close(space);sem_unlink("/1");sem_close(data);sem_unlink("/2");shmdt(p);_exit(0);
}int main() {key_t key = ftok("./", 2);shm_id = shmget(key, 2, IPC_CREAT | 0666);p = shmat(shm_id, NULL, 0);space = sem_open("/1", O_CREAT);data = sem_open("/2", O_CREAT);// 注册SIGINT信号处理函数signal(SIGINT, cleanup);while (1) {sem_wait(data);fprintf(stderr, "%s", p);sem_post(space);}// 解除映射和清理资源cleanup(0); // 虽然在无限循环中不会到达这里࿰c;但确保资源得到释放
}code>


http://www.ppmy.cn/ops/94345.html

相关文章

8.12LVS

一&#xff0e;LVS概述 1.什么是lvs&#xff1a; lvs是一个实现负载均衡集群开源软件项目&#xff0c;lvs逻辑上可以分为调度层&#xff0c;server集群层和共享层 2.特点&#xff1a;免费&#xff0c;开源&#xff0c;四层负载均衡 3.Lvs工作原理 4.lvs调度算法: 静态调度…

IO流用到的设计模式

IO流用到的设计模式 装饰器&#xff08;Decorator&#xff09;模式 可以在不改变原有对象的情况下拓展其功能。例如&#xff0c;可以通过 BufferedInputStream&#xff08;字节缓冲输入流&#xff09;来增强 FileInputStream 的功能。适配器&#xff08;Adapter Pattern&#…

2024年,除了幕布思维导图外三款好用的工具推荐!!

工作当中我们的成果都是靠着各种的工作汇报来体现的&#xff0c;其中就包括了excel、ppt和思维导图这类的形式了&#xff0c;是不是就需要给上级做这类的汇报体现工作的价值&#xff0c;而使用好用的一些工具可以让我们的工作事半功倍起来&#xff0c;所以今天整理了幕布思维导…

注册数据查询工具

注册数据查询工具&#xff1a;ICANN Lookup

爬虫配置代理:保护隐私有效地抓取数据

爬虫配置代理的详细指南 在进行网络爬虫时&#xff0c;使用代理可以帮助我们更有效地抓取数据&#xff0c;避免IP被封禁&#xff0c;并提高隐私保护。本文将详细介绍如何在爬虫中配置代理&#xff0c;包括不同的代理类型、如何选择合适的代理以及在Python中实现代理的具体步骤…

OpenAI 发布 GPT-4o 模型安全评估报告:风险等级为“中等”|TodayAI

OpenAI 近日发布了最新的 GPT-4o 系统卡&#xff0c;这是一份研究文件&#xff0c;详细介绍了公司在推出其最新 AI 模型之前所进行的安全措施和风险评估。根据该评估报告&#xff0c;GPT-4o 的总体风险等级被评定为 “中等” 。 GPT-4o 于今年 5 月首次公开发布。在其发布之前…

SQL Zoo 9-.Window functions

以下数据均来自SQL Zoo 1.Show the lastName, party and votes for the constituency S14000024 in 2017.&#xff08;显示2017年选区“S14000024”的姓氏、政党和选票&#xff09; SELECT lastName, party, votesFROM geWHERE constituency S14000024 AND yr 2017 ORDER BY…

Android 启动动画太生硬

跟 android:launchMode"singleTask"属性无关系 请禁用路由 ARouter.getInstance() .build(Routes.Main.MAIN) .withTransition(R.anim.activity_anim_in, R.anim.activity_anim_out).navigation() 正确做法是 val intent Intent(thisSplashActivity,MainActivit…