vi in_queue.c (入队)
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define LEN 10
int semid;
struct sembuf p,v;
struct loop_queue {
int head;
int tail;
char mem[LEN];
};
int next(int n) {
n=(n+1)%LEN;
return n;
}
int init_queue(struct loop_queue *fifo) {
fifo->head=0;
fifo->tail=0;
}
int enqueue(struct loop_queue *fifo,char c) {
if(fifo->head==next(fifo->tail)) {
fprintf(stderr,"Queue Full.\n");
return -1;
}
(fifo->mem)[fifo->tail]=c;
fifo->tail=next(fifo->tail);
return 0;
}
int dequeue(struct loop_queue *fifo,char *c) {
if(fifo->head==fifo->tail) {
printf("Queue Null.\n");
return -1;
}
*c=(fifo->mem)[fifo->head];
fifo->head=next(fifo->head);
return 0;
}
int P_op() {
int ret;
ret=semop(semid,&p,1);
return ret;
}
int V_op() {
int ret;
ret=semop(semid,&v,1);
return ret;
}
int main() {
key_t key;
int shmid;
struct loop_queue *fifo;
char c;
int i,n,qn,ret;
p.sem_num=0;
p.sem_op=-1;
p.sem_flg=0;
v.sem_num=0;
v.sem_op=1;
v.sem_flg=0;
key=ftok("/tmp/count",'e');
if(key==-1) {
fprintf(stderr,"ftok() error.\n");
exit(-1);
}
semid=semget(key,1,IPC_CREAT|0600);
if(semid==-1) {
perror("semget()");
exit(-1);
}
ret=semctl(semid,0,SETVAL,1);
if(ret==-1) {
perror("semctl()");
exit(-1);
}
shmid=shmget(key,sizeof(struct loop_queue),IPC_CREAT|0600);
if(shmid==-1) {
perror("shmget()");
exit(-1);
}
fifo=(struct loop_queue *)shmat(shmid,NULL,SHM_RND);
if((int)fifo==-1) {
perror("shmat()");
exit(-1);
}
P_op();
init_queue(fifo);
V_op();
while(1) {
c=rand()%26+'a';
n=rand()%LEN+1;
qn=0;
P_op();
for(i=0;i<n;i++) {
ret=enqueue(fifo,c);
if(ret==0) qn++;
}
V_op();
printf("%d x %c enqueue.\n",qn,c);
sleep(rand()%3+1);
}
exit(0);
}
vi out_queue.c(出队)
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define LEN 10
int semid;
struct sembuf p,v;
struct loop_queue {
int head;
int tail;
char mem[LEN];
};
int next(int n) {
n=(n+1)%LEN;
return n;
}
int init_queue(struct loop_queue *fifo) {
fifo->head=0;
fifo->tail=0;
}
int enqueue(struct loop_queue *fifo,char c) {
if(fifo->head==next(fifo->tail)) {
fprintf(stderr,"Queue Full.\n");
return -1;
}
(fifo->mem)[fifo->tail]=c;
fifo->tail=next(fifo->tail);
return 0;
}
int dequeue(struct loop_queue *fifo,char *c) {
if(fifo->head==fifo->tail) {
// printf("Queue Null.\n");
return -1;
}
*c=(fifo->mem)[fifo->head];
fifo->head=next(fifo->head);
return 0;
}
int main() {
key_t key;
int shmid;
struct loop_queue *fifo;
char c;
int i,n,qn,ret;
p.sem_num=0;
p.sem_op=-1;
p.sem_flg=0;
v.sem_num=0;
v.sem_op=1;
v.sem_flg=0;
key=ftok("/tmp/count",'e');
if(key==-1) {
fprintf(stderr,"ftok() error.\n");
exit(-1);
}
semid=semget(key,1,IPC_CREAT|0600);
if(semid==-1) {
perror("semget()");
exit(-1);
}
ret=semctl(semid,0,SETVAL,1);
if(ret==-1) {
perror("semctl()");
exit(-1);
}
shmid=shmget(key,sizeof(struct loop_queue),IPC_CREAT|0600);
if(shmid==-1) {
perror("shmget()");
exit(-1);
}
fifo=(struct loop_queue *)shmat(shmid,NULL,SHM_RND);
if((int)fifo==-1) {
perror("shmat()");
exit(-1);
}
while(1) {
ret=dequeue(fifo,&c);
if(ret==-1) {
continue;
}
printf("%c\n",c);
fflush(stdout);
usleep(100);
}
exit(0);
}