1、信号量的定义:
struct semaphore {
spinlock_t lock;
unsigned int count;
struct list_head wait_list;
};
在linux中,信号量用上述结构体表示,我们可以通过该结构体定义一个信号量。
2、信号量的初始化:
可用void sema_init(struct semaphore *sem, int val);直接创建,其中val为信号量初值。
也可以用两个宏来定义和初始化信号量的值为1或0:
DECLARE_MUTEX(name) : 定义信号量name并初始化为1
DECLARE_MUTEX_LOCKED(name) : 定义信号量name并初始化为0
还可以用下面的函数初始化:
void init_MUTEX(struct semaphore *sem); //初始化信号量的值为1
void init_MUTEX_LOCKED(struct semaphore *sem); //初始化信号量的值为0
3、信号量的原子操作:
p操作:
* void down(struct semaphore *sem); //用来获取信号量,如果信号量值大于或等于0,获取
信号量,否则进入睡眠状态,睡眠状态不可唤醒
* void down_interruptible(struct semephore *sem); //用来获取信号量,如果信号量大于或
等于0,获取信号量,否则进入睡眠状
态,等待信号量被释放后,激活该程。
* void down_trylock(struct semaphore *sem); //试图获取信号量,如果信号量已被其他进程
获取,则立刻返回非零值,调用者不会睡眠
v操作:
* void up(struct semaphore *sem); //释放信号量,并唤醒等待该资源进程队列的第一个进程
前面一系列练习已经把进程控制、线程、进程间通信的大概知识过了一遍,现在进入综合练习,首先练习经典问题:生产者和消费者问题
1.问题概述
多个生产/消费者在有界缓冲上操作。它利用N个字节的共享内存作为有界循环缓冲区,利用写一字符模拟放一个产品,利用读一字符模拟消费一个产品。当缓冲区空时消费者应阻塞睡眠,而当缓冲区满时生产者应当阻塞睡眠。一旦缓冲区中有空单元,生产者进程就向空单元中入写字符,并报告写的内容和位置。一旦缓冲区中有未读过的字符,消费者进程就从该单元中读出字符,并报告读取位置。生产者不能向同一单元中连续写两次以上相同的字符,消费者也不能从同一单元中连续读两次以上相同的字符。
2.问题分析
首先看阶层,有两个,分别是生产者和消费者,他们之间的缓冲区是共享内存,首先想到一点:System V共享内存实现这一个缓冲区;又因为缓冲区是临界资源,所以要用一个互斥信号量实现;生产者和消费者要采用PV信号量操作实现进程同步
因为要求多个进程能同步,所以进程访问缓冲的指针也需要共享内存实现
大致框图:
生产者->[位置指针]--------(共享内存)缓冲-------[位置指针]->消费者
3.原语
进程:Producer - 生产者进程,Consumer - 消费者进程
共有的数据结构:
buffer: array [0..k-1] of integer;
in,out: 0..k-1;
— in记录第一个空缓冲区,out记录第一个不空的缓冲区
prod_key(缓冲区空的个数),cons_key(缓冲区满的个数),mutex(临界区): semaphore;
— prod_key控制缓冲区不满,cons_key控制缓冲区不空,mutex保护临界区;
初始化prod_key=k,cons_key=0,mutex=1
producer(生产者进程):
Item_Type item;
{
while (true)
{
produce(&item);
p(prod_key);
p(mutex);
buffer[in]:=item;
in:=(in+1) mod k;
v(mutex);
v(cons_key);
}
}
consumer(消费者进程):
Item_Type item;
{
while (true)
{
p(cons_key);
p(mutex);
item:=buffer[out];
out:=(out+1) mod k;
v(mutex);
v(prod_key);
consume(&item);
}
}
4.IPC操作函数
我们编写一个函数实现对IPC 信息队列、共享内存、信号量的包装,以备接下来更好的编写
程序
头文件ipc.h:声明ipc操作函数和一些变量
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <sys/msg.h>
- #define BUFSZ 256
-
- int get_ipc_id(char *proc_file,key_t key);
- char *set_shm(key_t shm_key,int shm_num,int shm_flag);
- int set_msq(key_t msq_key,int msq_flag);
- int set_sem(key_t sem_key,int sem_val,int sem_flag);
- int down(int sem_id);
- int up(int sem_id);
-
- typedef union semuns
- {
- int val;
- } Sem_uns;
-
- typedef struct msgbuf
- {
- long mtype;
- char mtext[1];
- } Msg_buf;
-
- key_t buff_key;
- int buff_num;
- char *buff_ptr;
-
- key_t pput_key;
- int pput_num;
- int *pput_ptr;
-
- key_t cget_key;
- int cget_num;
- int *cget_ptr;
-
- key_t prod_key;
- int prod_sem;
-
-
- key_t cons_key;
- int cons_sem;
- int sem_val;
- int sem_flg;
- int shm_flg;
-
- key_t mtx_key;
- int mtx_sem;
ipc.c:这里包装了一些ipc的操作函数,包括信息队列、共享内存、信号量的创建/获得,及PV操作
这里逐一分析它们的实现过程:(在注释中)
-
- #include "ipc.h"
-
-
-
-
-
-
- int get_ipc_id(char *proc_file,key_t key)
- {
- FILE *pf;
- int i,j;
- char line[BUFSZ],colum[BUFSZ];
- if((pf = fopen(proc_file,"r")) == NULL)
- {
- perror("Proc file not open");
- exit(EXIT_FAILURE);
- }
- fgets(line, BUFSZ,pf);
- while(!feof(pf))
- {
- i = j = 0;
- fgets(line, BUFSZ,pf);
- while(line[i] == ' ')
- i++;
- while(line[i] !=' ')
- colum[j++] = line[i++];
- colum[j] = '/0';
- if(atoi(colum) != key)
- continue;
- j=0;
- while(line[i] == ' ')
- i++;
- while(line[i] !=' ')
- colum[j++] = line[i++];
- colum[j] = '/0';
- i = atoi(colum);
- fclose(pf);
- return i;
- } fclose(pf);
- return -1;
- }
-
-
-
-
-
-
-
- int P_operation(int sem_id)
- {
- struct sembuf buf;
- buf.sem_op = -1;
- buf.sem_num = 0;
- buf.sem_flg = SEM_UNDO;
- if((semop(sem_id,&buf,1)) <0)
- {
- perror("down error ");
- exit(EXIT_FAILURE);
- } return EXIT_SUCCESS;
- }
-
- int V_operation(int sem_id)
- {
- struct sembuf buf;
- buf.sem_op = 1;
- buf.sem_num = 0;
- buf.sem_flg = SEM_UNDO;
- if((semop(sem_id,&buf,1)) <0)
- {
- perror("up error ");
- exit(EXIT_FAILURE);
- } return EXIT_SUCCESS;
- }
-
-
-
-
-
-
-
-
-
- int set_sem(key_t sem_key,int sem_val,int sem_flg)
- {
- int sem_id;
- Sem_uns sem_arg;
-
- if((sem_id = get_ipc_id("/proc/sysvipc/sem",sem_key)) < 0 )
- {
-
- if((sem_id = semget(sem_key,1,sem_flg)) < 0)
- {
- perror("semaphore create error");
- exit(EXIT_FAILURE);
- }
-
- sem_arg.val = sem_val;
- if(semctl(sem_id,0,SETVAL,sem_arg) <0)
- {
- perror("semaphore set error");
- exit(EXIT_FAILURE);
- }
- }
- return sem_id;
- }
-
-
-
-
-
-
-
-
-
- char * set_shm(key_t shm_key,int shm_num,int shm_flg)
- {
- int i,shm_id;
- char * shm_buf;
-
- if((shm_id = get_ipc_id("/proc/sysvipc/shm",shm_key)) < 0 )
- {
-
- if((shm_id = shmget(shm_key,shm_num,shm_flg)) <0)
- {
- perror("shareMemory set error");
- exit(EXIT_FAILURE);
- }
-
- if((shm_buf = (char *)shmat(shm_id,0,0)) < (char *)0)
- {
- perror("get shareMemory error");
- exit(EXIT_FAILURE);
- }
-
- for(i=0; i<shm_num; i++)
- shm_buf[i] = 0;
- }
-
- if((shm_buf = (char *)shmat(shm_id,0,0)) < (char *)0)
- {
- perror("get shareMemory error");
- exit(EXIT_FAILURE);
- }
- return shm_buf;
- }
-
-
-
-
-
-
-
-
- int set_msq(key_t msq_key,int msq_flg)
- {
- int msq_id;
-
- if((msq_id = get_ipc_id("/proc/sysvipc/msg",msq_key)) < 0 )
- {
-
- if((msq_id = msgget(msq_key,msq_flg)) < 0)
- {
- perror("messageQueue set error");
- exit(EXIT_FAILURE);
- }
- }
- return msq_id;
- }
5.生产者程序实现:
首先建立(已存在时为打开)一系列的信号量和共享内存,接着就按照操作原语去实现了,代码如下:
-
- #include "ipc.h"
- int main(int argc,char *argv[])
- {
- int rate;
-
- if(argv[1] != NULL)
- rate = atoi(argv[1]);
- else
- rate = 3;
-
-
- buff_key = 101;
- buff_num = 8;
- pput_key = 102;
- pput_num = 1;
- shm_flg = IPC_CREAT | 0644;
-
- buff_ptr = (char *)set_shm(buff_key,buff_num,shm_flg);
-
- pput_ptr = (int *)set_shm(pput_key,pput_num,shm_flg);
-
- prod_key = 201;
- mtx_key = 202;
- cons_key = 301;
- sem_flg = IPC_CREAT | 0644;
-
- sem_val = buff_num;
-
- prod_sem = set_sem(prod_key,sem_val,sem_flg);
-
- sem_val = 0;
-
- cons_sem = set_sem(cons_key,sem_val,sem_flg);
-
- sem_val = 1;
-
- mtx_sem = set_sem(mtx_key,sem_val,sem_flg);
-
- while(1)
- {
-
- P_operation(prod_sem);
-
- P_operation(mtx_sem);
-
- buff_ptr[*pput_ptr] = 'A'+ *pput_ptr;
-
- sleep(rate);
- printf("%d producer put: %c to Buffer[%d]/n",getpid(),buff_ptr[*pput_ptr],*pput_ptr);
-
- *pput_ptr = (*pput_ptr+1) % buff_num;
-
- V_operation(mtx_sem);
-
- V_operation(cons_sem);
- }
- return EXIT_SUCCESS;
- }
6.消费者程序实现
如同生产者上述,代码如下:
-
- #include "ipc.h"
- int main(int argc,char *argv[])
- {
- int rate;
-
- if(argv[1] != NULL)
- rate = atoi(argv[1]);
- else
- rate = 3;
-
- buff_key = 101;
- buff_num = 8;
- cget_key = 103;
- cget_num = 1;
- shm_flg = IPC_CREAT | 0644;
-
- buff_ptr = (char *)set_shm(buff_key,buff_num,shm_flg);
-
- cget_ptr = (int *)set_shm(cget_key,cget_num,shm_flg);
-
- prod_key = 201;
- mtx_key = 202;
- cons_key = 301;
- sem_flg = IPC_CREAT | 0644;
-
- sem_val = buff_num;
-
- prod_sem = set_sem(prod_key,sem_val,sem_flg);
-
- sem_val = 0;
-
- cons_sem = set_sem(cons_key,sem_val,sem_flg);
-
- sem_val = 1;
-
- mtx_sem = set_sem(mtx_key,sem_val,sem_flg);
-
- while(1){
-
- P_operation(cons_sem);
-
- P_operation(mtx_sem);
-
- sleep(rate);
- printf("%d consumer get: %c fromBuffer[%d]/n",getpid(),buff_ptr[*cget_ptr],*cget_ptr);
-
- *cget_ptr = (*cget_ptr+1) % buff_num;
-
- V_operation(mtx_sem);
-
- V_operation(prod_sem);
- }
- return EXIT_SUCCESS;
- }
7.编写Makefile
由于本项目工程有多个文件,所以需要Makefile来方便编译
- hdrs = ipc.h
- opts = -g -c
- c_src = consumer.c ipc.c
- c_obj = consumer.o ipc.o
- p_src = producer.c ipc.c
- p_obj = producer.o ipc.o
- all: producer consumer
- consumer: $(c_obj)
- gcc $(c_obj) -o consumer
- consumer.o: $(c_src) $(hdrs)
- gcc $(opts) $(c_src)
- producer: $(p_obj)
- gcc $(p_obj) -o producer
- producer.o: $(p_src) $(hdrs)
- gcc $(opts) $(p_src)
- clean:
- rm consumer producer *.o
8.编译:
$ make
运行时打开多个终端窗口,输入
$./producer 1
另一个窗口输入:
$./consumer 1
.......
这时可以看到同步过程