这是字符函数的一部分,它将 256 到 1024 字节范围内的数据发送到缓冲区
semaphore_p(semMutexAll);//critical section
int charByte =rand() % 768 + 256;
for(i=0 ; i<charByte ; i++)
semaphore_p(semBuff);//contor buffer overflow
semaphore_v(semMutexAll);
这是函数 cpu 的一部分,它在 10 毫秒内从缓冲区读取 2 个字节
while(1)
{
semaphore_v(semBuff);
semaphore_v(semBuff);
usleep(10000);
}
这是信号量
semBuff = semget((key_t)1234,1,0666|IPC_CREAT); //Gets shared memory Id --> 1234 key
semChar = semget((key_t)1235,1,0666|IPC_CREAT);
semImage = semget((key_t)1236,1,0666|IPC_CREAT);
semAudio = semget((key_t)1237,1,0666|IPC_CREAT);
semVideo = semget((key_t)1238,1,0666|IPC_CREAT);
semMutexCpu = semget((key_t)1239,1,0666|IPC_CREAT);
semMutexAll = semget((key_t)1230,1,0666|IPC_CREAT);
set_semvalue(semBuff,BUFFSIZE);
set_semvalue(semChar,MAXCHR);
set_semvalue(semImage,MAXIMAGE);
set_semvalue(semAudio,MAXAUDIO);
set_semvalue(semVideo,MAXVIDEO);
set_semvalue(semMutexCpu,1);
set_semvalue(semMutexAll,1);
这里是共享内存
key_t ShmKEY;
key_t ShmQ;
int ShmContID;
int ShmQID;
int status;
queue *buffArea;
buffArea = (queue*)malloc(sizeof(queue));
initQueue(buffArea);
control * params;
params = (control*)malloc(sizeof(control));
ShmKEY = ftok("./", 'A');
ShmQ = ftok("./", 'B');
if((ShmContID = shmget(ShmKEY,sizeof(params),IPC_CREAT | 0666)) == -1)
{
//Gets shared memory Id --> IPC_PRIVATE key
perror("shmget");
exit(1);
}
if((ShmQID = shmget(ShmQ,sizeof(buffArea),IPC_CREAT | 0666)) == -1)
{
//Gets shared memory Id --> IPC_PRIVATE key
perror("shmget");
exit(1);
}
这是信号量标头
#ifndef SHAREDSMPH_H_
#define SHAREDSMPH_H_
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
static int semBuff;
static int semChar;
static int semImage;
static int semAudio;
static int semVideo;
static int semMutexCpu;
static int semMutexAll;
static int set_semvalue(int sem_id,int val)
{
union semun sem_union;
sem_union.val=val;
if(semctl(sem_id,0,SETVAL,sem_union) == -1)
{
fprintf(stderr, "Failed to semvalue\n");
return(0);
}
return (1);
}
static void del_semvalue(int sem_id)
{
union semun sem_union;
if(semctl(sem_id,0,IPC_RMID,sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
static int get_semvalue(int sem_id)
{
union semun sem_union;
int value = semctl(sem_id,0,GETVAL,sem_union);
//printf("semvalue:%d\n",value);
return value;
}
static int semaphore_p(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num=0;
sem_b.sem_op=-1; /* P */
sem_b.sem_flg=SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1)
{
printf("semaphore_p failed\n");
return (0);
}
return (1);
}
static int semaphore_v(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num=0;
sem_b.sem_op=1; /* V */
sem_b.sem_flg=SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1)
{
printf("semaphore_v failed\n");
return (0);
}
return (1);
}
#endif
这段代码有问题吗?特别是信号量,问题是信号量值总是在不同的进程中初始化。当我在进程中调用 semaphore_v 和 semaphore_p 函数时,它们工作准确,但例如 semBuff 信号量总是初始化为 1024。