博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IPC机制---共享内存编程
阅读量:2351 次
发布时间:2019-05-10

本文共 2818 字,大约阅读时间需要 9 分钟。

#include 
#include
#include
#include
#include
#include
#include "common.h"#define MAX_STRING 5000typedef struct { int semID; int counter; char string[MAX_STRING+1];} MY_BLOCK_T;int main(int argc, char *argv[]){ int shmid, ret, i; MY_BLOCK_T *block; struct sembuf sb; char user; /* Make sure there's a command */ if(argc >= 2) { /* Create the shared memory segment and init it with the semaphore */ if(!strncmp(argv[1], "create", 6)) { /* Create the shared memory segment and semaphore */ printf("Creating the shared memory segment\n"); /* Create the shared memory segment */ shmid = shmget(MY_SHM_ID, sizeof(MY_BLOCK_T), (IPC_CREAT | 0666)); /* Attach to the segment */ block = (MY_BLOCK_T *)shmat(shmid, (const void *)0, 0); /* Initialize our write pointer */ block->counter = 0; /* Create the semaphore */ block->semID = semget(MY_SEM_ID, 1, (IPC_CREAT | 0666)); /* Increment the semaphore */ sb.sem_num = 0; sb.sem_op = 1; sb.sem_flg = 0; semop(block->semID, &sb, 1); /* Now detach from the segment */ shmdt((void *)block); } else if(!strncmp(argv[1], "use", 3)) { /* Use the segment */ /* Must specify also a letter (to write to the buffer) */ if(argc < 3) exit(-1); user = (char)argv[2][0]; /* Grab the shared memory segment */ shmid = shmget(MY_SHM_ID, 0, 0); /* Attach to it */ block = (MY_BLOCK_T *)shmat(shmid, (const void *)0, 0); for(i = 0; i < 2500; i++) { /* Give up the CPU temporarily */ sleep(0); /* Grab the semaphore */ sb.sem_num = 0; sb.sem_op = -1; sb.sem_flg = 0; if(semop(block->semID, &sb, 1) != -1) { /* Write our letter to the segment buffer(only we have the semaphore). This is our critical section */ block->string[block->counter++] = user; /* Release the semaphore */ sb.sem_num = 0; sb.sem_op = 1; sb.sem_flg = 0; if(semop(block->semID, &sb, 1) == -1) { printf("Failed to release the semaphore\n"); } } else { printf("Failed to acquire the semaphore\n"); } } /* We're done, unmap the shared memory segment. */ret = shmdt((void *)block); } else if(!strncmp(argv[1], "read", 6)) { /* Here, we'll read the buffer in the shared segment */ shmid = shmget(MY_SHM_ID, 0, 0); if(shmid != -1) { block = (MY_BLOCK_T *)shmat(shmid, (const void *)0, 0); /* Terminate the buffer */ block->string[block->counter+1] = 0; printf("%s\n", block->string); ret = shmdt((void *)block); } else { printf("Unable to read segment.\n"); } }else if(!strncmp(argv[1], "remove", 6)) { shmid = shmget(MY_SHM_ID, 0, 0); if(shmid >= 0) { block = (MY_BLOCK_T *)shmat(shmid, (const void *)0, 0); /* Remove the semaphore */ ret = semctl(block->semID, 0, IPC_RMID); /* Remove the shared segment */ ret = shmctl(shmid, IPC_RMID, 0); if(ret == 0) { printf("Successfully removed the segment.\n"); } } } else { printf("Unknown command %s\n", argv[1]); } } return 0;}

转载地址:http://dshvb.baihongyu.com/

你可能感兴趣的文章
iOS和Android的app界面设计规范
查看>>
Android 代码混淆异常
查看>>
Android drawable微技巧,你所不知道的drawable的那些细节
查看>>
理解Fragment生命周期
查看>>
最靠谱的禁止ViewPager滑动方法
查看>>
android错误之android.content.res.Resources$NotFoundException:
查看>>
Android监听软键盘打开收起事件(软键盘自带收起按钮)
查看>>
huffman code and encode
查看>>
exception in c++
查看>>
java并发编程lock
查看>>
阿里云技术教程系列-ECS远程连接 Linux 实例
查看>>
Linux新建用户并允许docker
查看>>
Drools Workbench 7.5.0.Final安装运行
查看>>
Docker快速部署Redis
查看>>
Spring boot shiro session cache ecache redis 共存配置
查看>>
一看就懂的设计模式--设计模式分类
查看>>
一看就懂的设计模式--模板方法
查看>>
一看就懂的设计模式--享元模式
查看>>
一看就懂的设计模式--策略模式
查看>>
spring Cloud 组建图
查看>>