信号量 与共享内存
来源:互联网 发布:充电数据监控 编辑:程序博客网 时间:2024/05/19 17:03
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <string.h>
int main(int argc,char *argv[])
{
int running=1;
int shid;
int semid;
int value;
void *sharem=NULL;
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_flg = SEM_UNDO;
if((semid=semget((key_t)123456,1,0666|IPC_CREAT))==-1)
{
perror("semget");
exit(EXIT_FAILURE);
}
if (semctl(semid, 0, SETVAL, 0) == -1)
{
printf("sem init error");
if(semctl(semid,0,IPC_RMID,0)!=0)
{
perror("semctl");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
shid=shmget((key_t)654321,(size_t)2048,0600|IPC_CREAT);
if(shid==-1)
{
perror("shmget");
exit(EXIT_FAILURE);
}
sharem=shmat(shid,NULL,0);
if(sharem==NULL)
{
perror("shmat");
exit(EXIT_FAILURE);
}
while(running)
{
if((value=semctl( semid, 0, GETVAL ))==0)
{
printf("write data operate\n");
printf("please input something:");
scanf("%s",sharem);
sem_b.sem_op = 1;
if (semop(semid, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_p failed\n");
exit(EXIT_FAILURE);
}
}
if(strcmp(sharem,"end")==0)
running--;
}
shmdt(sharem);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include "shmemory.h"
int main(int argc,char *argv[])
{
int running=1;
char *shm_p=NULL;
int shmid;
int semid;
int value;
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_flg = SEM_UNDO;
if((semid=semget((key_t)123456,1,0666|IPC_CREAT))==-1)
{
perror("semget");
exit(EXIT_FAILURE);
}
shmid=shmget((key_t)654321,(size_t)2048,0600|IPC_CREAT);
if(shmid==-1)
{
perror("shmget");
exit(EXIT_FAILURE);
}
shm_p=shmat(shmid,NULL,0);
if(shm_p==NULL)
{
perror("shmat");
exit(EXIT_FAILURE);
}
while(running)
{
if((value=semctl( semid, 0, GETVAL ))==1)
{
printf("read data operate\n");
sem_b.sem_op = -1;
if (semop(semid, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_p failed\n");
exit(EXIT_FAILURE);
}
printf("%s\n",shm_p);
}
if(strcmp(shm_p,"end")==0)
running--;
}
shmdt(shm_p);
if(shmctl(shmid,IPC_RMID,0)!=0)
{
perror("shmctl");
exit(EXIT_FAILURE);
}
if(semctl(semid,0,IPC_RMID,0)!=0)
{
perror("semctl");
exit(EXIT_FAILURE);
}
return 0;
}
- int semget(key_t key, int num_sems, int sem_flags);
- int semop(int sem_id, struct sembuf *sem_opa, size_t num_sem_ops);
- struct sembuf{
- short sem_num;//除非使用一组信号量,否则它为0
- short sem_op;//信号量在一次操作中需要改变的数据,通常是两个数,一个是-1,即P(等待)操作,
- //一个是+1,即V(发送信号)操作。
- short sem_flg;//通常为SEM_UNDO,使操作系统跟踪信号,
- //并在进程没有释放该信号量而终止时,操作系统释放信号量
- };
- int semctl(int sem_id, int sem_num, int command, ...);
- union semun{
- int val;
- struct semid_ds *buf;
- unsigned short *arry;
- };
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/sem.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *arry;
};
static int sem_id = 0;
static int set_semvalue();
static void del_semvalue();
static int semaphore_p();
static int semaphore_v();
int main(int argc, char *argv[])
{
char message = 'X';
int i = 0;
//创建信号量
sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);
if(argc > 1)
{
//程序第一次被调用,初始化信号量
if(!set_semvalue())
{
fprintf(stderr, "Failed to initialize semaphore\n");
exit(EXIT_FAILURE);
}
//设置要输出到屏幕中的信息,即其参数的第一个字符
message = argv[1][0];
sleep(2);
}
for(i = 0; i < 10; ++i)
{
//进入临界区
if(!semaphore_p())
exit(EXIT_FAILURE);
//向屏幕中输出数据
printf("%c", message);
//清理缓冲区,然后休眠随机时间
fflush(stdout);
sleep(rand() % 3);
//离开临界区前再一次向屏幕输出数据
printf("%c", message);
fflush(stdout);
//离开临界区,休眠随机时间后继续循环
if(!semaphore_v())
exit(EXIT_FAILURE);
sleep(rand() % 2);
}
sleep(10);
printf("\n%d - finished\n", getpid());
if(argc > 1)
{
//如果程序是第一次被调用,则在退出前删除信号量
sleep(3);
del_semvalue();
}
exit(EXIT_SUCCESS);
}
static int set_semvalue()
{
//用于初始化信号量,在使用信号量前必须这样做
union semun sem_union;
sem_union.val = 1;
if(semctl(sem_id, 0, SETVAL, sem_union) == -1)
return 0;
return 1;
}
static void del_semvalue()
{
//删除信号量
union semun sem_union;
if(semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
static int semaphore_p()
{
//对信号量做减1操作,即等待P(sv)
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;//P()
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_p failed\n");
return 0;
}
return 1;
}
static int semaphore_v()
{
//这是一个释放操作,它使信号量变为可用,即发送信号V(sv)
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;//V()
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_v failed\n");
return 0;
}
return 1;
}
运行结果如下:
- 信号量 与共享内存
- linux共享内存与信号量的使用
- 进程通信之共享内存与信号量
- 共享内存+信号量 实例
- 利用信号量共享内存
- 信号量,共享内存
- 共享内存和信号量
- 共享内存+信号量 实例
- 信号量和共享内存
- php 信号量(共享内存)
- 共享内存,信号量
- linux下C语言编程7-信号量与共享内存
- linux 进程间共享内存与信号量的使用
- Linux下IPC:信号量与共享内存详解
- 进程间通信 "共享内存" 与 “信号量”的使用
- Linux进程间通信--共享内存与信号量
- linux进程同步与通信(共享内存和信号量)
- ORACLE在各操作系统信号量与共享内存的维护
- Handler
- python03-list_tuple_dict_set
- CentOS 6安裝VNC、Xfce桌面
- heapsort实现代码
- 文章标题
- 信号量 与共享内存
- Androidstudio中自定义字体
- BestCoder #83 1003 zxa and leaf(二分查找/BFS)
- Windows系统搭建Mysql Cluster集群
- dataTransfer.getData()在dragover,dragenter,dragleave中无法获取数据的问题
- 推荐一个开源基于JavaScript和Openlayers开发的webgis二次开发控件,并赠送电子和卫星影像地图
- Windows JDK环境变量一键设置
- 手把手教你从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate(含源码下载)
- Android 在其他线程中更新UI线程的解决方法