经典桥段:李老师卖票
1 通过共享内存实现
//shmid 创建并获取共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
//shmat 映射共享内存 解除映射
#include <sys/types.h>
#include <sys/shm.h>
//删除共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef struct _shm
{
int flag; //锁
int ticket; //票
}SHM;
void sell_ticket(SHM* shmp)
{
while(1)
{
int time = rand()%10 + 1;
usleep(time * 100000);
if(shmp->flag == 1)
{
shmp->flag = 0;
if(shmp->ticket == 0)
{
shmp->flag = 1;
break;
}
printf("卖了第%d张票\n",shmp->ticket);
shmp->ticket--;
shmp->flag = 1;
}
}
}
int main(int argc,char ** argv)
{
srand((unsigned int)time(NULL));
//创建贡献内存
int shmid = shmget((key_t)1234,sizeof(SHM),0666|IPC_CREAT); //加一个创建宏
if(shmid == -1)
{
perror("shmget");
return -1;
}
//映射共享内存
SHM * shmp = (SHM*)shmat(shmid,NULL,0);
if(argc == 2)
{
shmp->flag = 1;
shmp->ticket = 100;
}
sell_ticket(shmp);
if(argc == 2)
{
//解除共享内存
shmdt(shmp);
//删除共享内存
shmctl(shmid,IPC_RMID,NULL); //删除宏
}
return 0;
}
2 通过信号量实现的卖票系统
头文件
#ifndef __SEMAPHORE_H__
#define __SEMAPHORE_H__
//控制信号量 semctl 用于初始化和删除 operate(操作)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
//操作信号量 semop 用于P操作和V操作 control(控制)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
//创建信号量 semget
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
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
(Linux specific) */
};
//初始化信号量
int sem_init(int sem_id)
{
union semun sem;
sem.val = 1;
int ret = semctl(sem_id, 0, SETVAL, sem);
return ret;
}
//信号量p操作
int sem_p(int sem_id)
{
struct sembuf sem;
sem.sem_num = 0;
sem.sem_op = -1; //p操作
sem.sem_flg = SEM_UNDO;
int ret = semop(sem_id, &sem, 1);
return ret;
}
//信号量v操作
int sem_v(int sem_id)
{
struct sembuf sem;
sem.sem_num = 0;
sem.sem_op = 1; //v操作
sem.sem_flg = SEM_UNDO;
int ret = semop(sem_id, &sem, 1);
return ret;
}
//删除信号量
int sem_del(int sem_id)
{
int ret = semctl(sem_id, 0, IPC_RMID);
return ret;
}
#endif
实现函数
//shmid 创建并获取共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
//shmat 映射共享内存 解除映射
#include <sys/types.h>
#include <sys/shm.h>
//删除共享内存
#include <sys/ipc.h>
#include <sys/shm.h>
//信号量
#include "semaphore.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef struct _shm
{
int ticket; //票
}SHM;
void sell_ticket(SHM* shmp,int sem_id)
{
while(1)
{
int time = rand()%10 + 1;
usleep(time * 1000);
//p操作
sem_p(sem_id);
if(shmp->ticket == 0) //票卖完
{
sem_v(sem_id);
break;
}
printf("卖了第%d张票\n",shmp->ticket);
shmp->ticket--;
//v操作
sem_v(sem_id);
}
}
int main(int argc,char ** argv)
{
srand((unsigned int)time(NULL));
//创建共享内存
int shmid = shmget((key_t)1234,sizeof(SHM),0666|IPC_CREAT); //加一个创建宏
if(shmid == -1)
{
perror("shmget");
return -1;
}
//创建信号量
int sem_id = semget((key_t)2345 , 1, 0666|IPC_CREAT);
if(sem_id == -1)
{
perror("shmget");
return -1;
}
//映射共享内存
SHM * shmp = (SHM*)shmat(shmid,NULL,0);
if(argc == 2)
{
//初始化信号量
sem_init(sem_id);
shmp->ticket = 100;
}
sell_ticket(shmp,sem_id);
if(argc == 2)
{
//解除共享内存
shmdt(shmp);
//删除共享内存
shmctl(shmid,IPC_RMID,NULL); //删除宏
//删除信号量
sem_del(sem_id);
}
return 0;
}