linux下信号量可设值的函数操作

时间:2022-07-12 15:16:53

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.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 */
};
#define MDMAKEY 567
union semun arg;

void MDM_initExecSem(void)
{
int ret;
arg.val = 0;
key_t key;

if ((key = ftok(".", 11)) == -1)
{
printf("ftok error");
return;
}

MDM_execSem=semget(key,1,IPC_CREAT|0666);//创建了一个权限为666的信号量
if(MDM_execSem==-1)
printf("semget creat err \n");
else
{
ret =semctl(MDM_execSem,0,SETVAL,arg);
if (ret < 0 )
{
printf("semctl semctl error \n");
semctl(MDM_execSem,0,IPC_RMID,arg);
}
else
{
printf("MDM_execSem init ok,val = %d \n",ret);
}
}
}

int OSSemPost_new(int *semid)
{
struct sembuf buf_add={0,1,IPC_NOWAIT};
int ret,ret0;
ret0=semctl(*semid,0,GETVAL,0);
//printf("add value \n");
//printf("semctl get old value=%d \n",ret);
ret=semop(*semid, &buf_add, 1);
if(ret== -1)
{
printf("OSSemPost_new err !\n");
return ret;
}
else
{
// ret=semctl(*semid,0,GETVAL,0);
// printf("OSSemPost_new succ,old val=%d,new value %d \n",ret0,ret);
return ret;
}
}

void OSSemSet_new(int *semid, INT16U cnt, INT8U *perr)
{
int ret,ret0;
struct sembuf buf ={ 0, -1, IPC_NOWAIT};
ret0 =semctl(*semid,0,GETVAL,0);
// printf("clear value \n");
// printf("semctl get value=%d \n",ret);
if(ret0)
{
buf.sem_op=0-ret0;
ret=semop(*semid, &buf, 1);
if(ret== -1)
{
// printf("OSSemSet_new err , val=%d,op=%d!\n",ret0,buf.sem_op);
*perr=1;
}
else
{
// ret =semctl(*semid,0,GETVAL,0);
// printf("OSSemSet_new succ,old val = %d , new val = %d \n",ret0,ret);
*perr=0;
}
}
// else
// {
// printf("value=%d,do not need clear! \n",ret);
// *perr=2;
// }
}

void OSSemPend_new(int *semid, INT16U timeout_ms, INT8U *perr)
{
// struct sembuf buf={0,-1,SEM_UNDO};
struct sembuf buf={0,-1,0};
struct timespec ts;
int ret,ret0;


ret0=semctl(*semid,0,GETVAL,0);
// printf("OSSemPend_new old val %d,want %d \n",ret,ret-1);
ts.tv_sec = (timeout_ms / 1000);
ts.tv_nsec = (timeout_ms - ts.tv_sec*1000L)*1000000L;
ret=semtimedop(*semid, &buf, 1, &ts);
if(ret==-1)
{
// printf("OSSemPend_new time out! \n");
if(errno=11)
{
*perr=0;
}
else
*perr=1;
}
else
{
// ret=semctl(*semid,0,GETVAL,0);
// printf("OSSemPend_new succ,old val=%d,new value %d \n",ret0,ret);
*perr=0;
}
}

 

 

ps:

ucos下信号量可以设值,在linux环境下好不容易才找的这个函数可以设置信号量的值;

主干其实就semctl,semop这些函数,因当时代码移植需要,进行了一次封装;