赞
踩
生产者-消费者问题,也叫做缓存绑定问题(bounded-buffer),是一个多进程同步问题。
- 即有两个进程:制造少和消费者,共享一个固定大小的缓存
- 制造商的工作是制造一段数据,放进缓存,如此重复。
- 消费者一次消费一段数据,从缓存中取出。
- 要保证不让制造商在缓存还是满的时候仍要向内写数据,不让消费者试图从空的缓存中取出数据。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include "ipc.h"
int main ( int argc , char * argv[] )
{
int i,producer_pid,consumer_pid,item,shmid;
semaphore mutex,empty,full;//定义互斥量mutex,定义信号量empty,full;
union semun sem_union;
void* shared_memory = (void*)0;
struct shared_use_st * shared_stuff;
/*
int semget ( key_t key , int num_sems , int sem_flags );
1.第一个参数key是一个用来允许不相关的进程访问相同信号量的整数值,
所有的信号量信号量是为不同的程序提供一个key来简介访问的,
对于每一个信号量系统生成一个信号量标识符。符号量键值只可以由semget
获得,所有其他的信号量函数所用的信号量标识符都是由semget所返回的。
2.num_sems参数是所需要的信号量数目,这个值通常是1
3.sem_flags参数是一个标记集合
IPC_CREAT与IPC_EXCL的组合来保证我们可以的到一个新的唯一的信号量
如果得不到会报错
4.返回值如果是一个整数,这是用于其他信号量函数的标识符,
如果失败,则会返回-1
*/
if ( ( mutex = semget ( (key_t) KEY_MUTEX , 1 , 0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if (( empty = semget( (key_t) KEY_EMPTY , 1 , 0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if (( full = semget((key_t) KEY_FULL , 1 , 0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((shmid = shmget ((key_t)KEY_SHM,sizeof (struct shared_use_st),0666|IPC_CREAT))==-1)
{
fprintf ( stderr , "Failerd to create shared memory!");
exit(EXIT_FAILURE);
}
sem_union.val = 1;
if ( semctl(mutex,0,SETVAL,sem_union) == -1 )
{
fprintf ( stderr , "Failed to set semaphore!");
exit(EXIT_FAILURE);
}
sem_union.val = 0;
if ( semctl(full,0,SETVAL,sem_union) == -1 )
{
fprintf ( stderr , "Failed to set semaphore!");
exit(EXIT_FAILURE);
}
sem_union.val = BUFFER_SIZE;
if ( semctl(empty, 0 , SETVAL , sem_union) == -1 )
{
fprintf ( stderr , "Failed to set semaphore!");
exit(EXIT_FAILURE);
}
/*
void* shmat ( int shmid , const void* shmaddr , int shmflg )
链接共享内存标识符为shmid的共享内存,链接成功后把共享内存区对象
映射到调用进程的地址空间,随后可像本地空间一样访问。
shmid 共享内存标识符
shmaddr 指定共享内存出现在进程内存地址的什么位置,直接指定为NULL让内核自己决定
一个合适的地址位置.
SHM_RDONLY 为只读模式,其他为读写模式
*/
if (( shared_memory = shmat( shmid ,(void*)0 , 0)) == (void*)-1 )
{
fprintf ( stderr , "shmat failed\n");
exit(EXIT_FAILURE);
}
shared_stuff = ( struct shared_use_st * ) shared_memory;
for ( i = 0; i < BUFFER_SIZE ; i ++ )
{
shared_stuff->buffer[i] = 0;
}
shared_stuff->low = 0;
shared_stuff->high = 0;
shared_stuff->cur = 0;
exit(EXIT_SUCCESS);
}
/*
int shmdt ( const void* shmaddr )
与shmat相反,是用来断开与共享内存附加点的地址,进制本进程访问瓷片共享内存
shmaddr:链接共享内存的起始地址
成功返回0,除错返回-1
*/
#include "ipc.h"
int main ( int argc , char * argv[] )
{
int i,item,shmid;
semaphore mutex,empty,full;
union semun sem_union;
void* shared_memory = (void*)0;
struct shared_use_st *shared_stuff;
if ((mutex = semget((key_t)KEY_MUTEX,1,0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((empty = semget((key_t)KEY_EMPTY,1,0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((full = semget((key_t)KEY_FULL,1,0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((shmid=shmget((key_t)KEY_SHM,sizeof(struct shared_use_st),0666|IPC_CREAT)) == -1 )
{
fprintf ( stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((shared_memory = shmat(shmid,(void*)0,0)) == (void*)-1)
{
fprintf ( stderr , "shmat failed\n");
exit(EXIT_FAILURE);
}
shared_stuff = ( struct shared_use_st * ) shared_memory;
for ( i = 0 ; i < 30 ; i++ )
{
item = ++(shared_stuff->cur);
sleep(1);
printf ( "Producing item %d\n" , item );
sem_p ( empty );//减少一个空位
sem_p ( mutex );//锁上互斥锁
(shared_stuff->buffer)[(shared_stuff->high)] = item;
(shared_stuff->high) = ((shared_stuff->high)+1) % BUFFER_SIZE;
printf ( "Inserting item %d\n" , item );
sem_v ( mutex );
sem_v ( full );
}
if ( shmdt(shared_memory) == -1 )
{
fprintf ( stderr , "shmat failed\n");
exit(EXIT_FAILURE);
}
printf ( "Finish!\n" );
getchar();
exit(EXIT_SUCCESS);
}
#include "ipc.h"
int main ( int argc , char* argv[] )
{
int i,item,shmid;
semaphore mutex,empty,full;
void* shared_memory = ( void* ) 0;
struct shared_use_st* shared_stuff;
if ((mutex=semget((key_t)KEY_MUTEX,1,0666|IPC_CREAT)) == -1 )
{
fprintf (stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((empty=semget((key_t)KEY_EMPTY,1,0666|IPC_CREAT)) == -1 )
{
fprintf (stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((full=semget((key_t)KEY_FULL,1,0666|IPC_CREAT)) == -1 )
{
fprintf (stderr , "Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ((shared_memory = shmat(shmid,(void*)0,0) ) ==(void*)-1 )
{
fprintf ( stderr , "shmat failed\n");
exit(EXIT_FAILURE);
}
shared_stuff = (struct shared_use_st*)shared_memory;
for(i=0;i<30;i++)
{
sem_p(full);
sem_p(mutex);
item = shared_stuff->buffer[shared_stuff->low];
(shared_stuff->buffer)[(shared_stuff->low)]=0;
shared_stuff->low = ((shared_stuff->low)+1)%BUFFER_SIZE;
printf ( "Removing item %d\n" , item );
sem_v(mutex);
sem_v(empty);
printf ( "Consuming item %d\n" ,item );
sleep(2);
}
if ( shmdt(shared_memory) == -1 )
{
fprintf ( stderr , "shmat failed\n" );
exit ( EXIT_FAILURE);
}
printf ( "Finish!\n" );
getchar();
exit(EXIT_SUCCESS);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。