系统V共享内存原理
信号灯与其他进程间通信方式不大相同,
它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。信号灯有以下两种类型:
二值信号灯:最简单的信号灯形式,信号灯的值只能取0或1,类似于互斥锁。计算信号灯:信号灯的值可以取任意非负值(当然受内核本身的约束)。
详细介绍请查看 :
http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index2.html
/*
*
* Filename: producer.c
*
* Description: 生产者进程
*
* Version: 1.0
* Created: 09/30/2011 04:52:23 PM
* Revision: none
* Compiler: gcc(g++)
*
* Author: |Zhenghe Zhang|, |[email protected]|
* Company: |Shenzhen XXX Technology Co., Ltd.|
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSHM 5 //定义缓冲区数组的下标变量个数
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf;/* buffer for IPC_STAT & IPC_SET */
unsigned short *array;/* array for GETALL & SETALL */
struct seminfo *__buf;/* buffer for IPC_INFO */
void *__pad;
};
int main()
{
key_t ipckey;
key_t semkey;
int shmid;
char *addr_c;
/* 定义信号灯集,该信号灯集包含3个信号量 fullid, emptyid, mutexid */
int semid;
ipckey = ftok("/home/zhang/shmipcx", 10001);
if(ipckey == -1)
{
perror("ftok");
exit(1);
}
shmid = shmget(ipckey, 1024, IPC_CREAT | 0666);
if(shmid == -1)
{
perror("shmget");
exit(1);
}
addr_c = (char*)shmat(shmid, NULL, 0);
if(*((int*)addr_c) == -1)
{
perror("shmat");
exit(1);
}
/* 定义信号量数据结构 */
struct sembuf P,V;
union semun arg1, arg2, arg3;
semkey = ftok("/home/zhang/shmipcy", 10001);
if(semkey == -1)
{
perror("ftok");
exit(1);
}
/* 创建信号灯集 */
semid = semget(semkey, 3, IPC_CREAT | 0666); //如果创建新集合(一般在服务器进程中), 则必须指定nsems
if(semid < 0)
{
perror("semget semid");
exit(1);
}
/*初始化信号灯集中的信号量 */
arg1.val = 0; //初始时缓冲区中无数据 (fullid, 缓冲区满信号量)
if(semctl(semid, 0, SETVAL, arg1) == -1)
{
perror("semctl setval error");
exit(1);
}
arg2.val = MAXSHM; //初始时缓冲区中有5个空闲的数组元素 (emptyid, 缓冲区空信号量)
if(semctl(semid, 1, SETVAL, arg2) == -1)
{
perror("semctl setval error");
exit(1);
}
arg3.val = 1; //初始时互斥信号为1,允许一个进程进入 (mutexid, 互斥信号量)
if(semctl(semid, 2, SETVAL, arg3) == -1)
{
perror("semctl setval error");
exit(1);
}
/* 初始化 P, V操作 */
P.sem_num = 0;
P.sem_op = -1;
P.sem_flg = SEM_UNDO;
V.sem_num = 0;
V.sem_op = 1;
V.sem_flg = SEM_UNDO;
int i = 0;
while(i < 10)
{
P.sem_num = 1; //设置操作信号量emptyid
semop(semid, &P, 1); //对 emptyid执行P操作
P.sem_num = 2; //设置操作信号量mutexid
semop(semid, &P, 1); //对 mutexid执行 P操作
addr_c[i] = i + 'a';
printf("***addr_c[%d] = %c\n", i, addr_c[i]);
V.sem_num = 2; //设置操作信号量mutexid
semop(semid, &V, 1); //对mutexid执行 V 操作
V.sem_num = 0; //设置操作信号量fullid
semop(semid, &V, 1); //对fullid执行 V 操作
i++;
sleep(1);
}
sleep(60); //等待消费者进程退出
if(shmdt(addr_c) == -1)
{
perror("shmdt");
exit(1);
}
/* 删除共享内存区 */
if(shmctl(shmid, IPC_RMID, NULL) == -1)
{
perror("shmctl");
exit(1);
}
/* 撤消信号集 */
if(semctl(semid, 0, IPC_RMID, 0) == -1) //第三个参数cmd为IPC_RMID时,整个信号集被删除,第二个参数semnum被忽略。
{
perror("semctl");
exit(1);
}
return 0;
}
/*
*
* Filename: consumer.c
*
* Description: 消费者进程
*
* Version: 1.0
* Created: 09/30/2011 04:52:23 PM
* Revision: none
* Compiler: gcc(g++)
*
* Author: |Zhenghe Zhang|, |[email protected]|
* Company: |Shenzhen XXX Technology Co., Ltd.|
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSHM 5 //定义缓冲区数组的下标变量个数
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf;/* buffer for IPC_STAT & IPC_SET */
unsigned short *array;/* array for GETALL & SETALL */
struct seminfo *__buf;/* buffer for IPC_INFO */ //test!!
void *__pad;
};
int main()
{
key_t ipckey;
key_t semkey;
int shmid;
char *addr_c;
/* 定义信号灯集,该信号灯集包含3个信号量 fullid, emptyid, mutexid */
int semid;
ipckey = ftok("/home/zhang/shmipcx", 10001);
if(ipckey == -1)
{
perror("ftok");
exit(1);
}
shmid = shmget(ipckey, 1024, IPC_EXCL | 0666);
if(shmid == -1)
{
perror("shmget");
exit(1);
}
addr_c = (char*)shmat(shmid, NULL, 0);
if(*((int*)addr_c) == -1)
{
perror("shmat");
exit(1);
}
/* 定义信号量数据结构 */
struct sembuf P, V;
//union semun arg1, arg2, arg3;
semkey = ftok("/home/zhang/shmipcy", 10001);
if(semkey == -1)
{
perror("ftok");
exit(1);
}
/* 创建信号灯集 */
semid = semget(semkey, 0, IPC_EXCL | 0666); //如果引用一个现存的集合(一个客户进程), 则将nsems指定为0
if(semid < 0)
{
perror("semget semid");
exit(1);
}
/* 初始化 P V操作 */
P.sem_num = 0;
P.sem_op = -1; //方式是减1(解锁)
P.sem_flg = SEM_UNDO;
V.sem_num = 0;
V.sem_op = 1; //方式是加1(解锁)
V.sem_flg = SEM_UNDO;
int i = 0;
while(i < 10)
{
P.sem_num = 0;
semop(semid, &P, 1); //对fullid执行 P 操作
P.sem_num = 2;
semop(semid, &P, 1); //对mutexid执行 P 操作
printf("***addr_c[%d] = %c\n", i, addr_c[i]);
V.sem_num = 2;
semop(semid, &V, 1); //对mutexid执行 V 操作
V.sem_num = 1;
semop(semid, &V, 1); //对empty执行 V 操作
i++;
sleep(2);
}
if(shmdt(addr_c) == -1)
{
perror("shmdt");
exit(1);
}
return 0;
}