zoukankan      html  css  js  c++  java
  • linux下信号量可设值的函数操作

    #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 ");
    else
    {
    ret =semctl(MDM_execSem,0,SETVAL,arg);
    if (ret < 0 )
    {
    printf("semctl semctl error ");
    semctl(MDM_execSem,0,IPC_RMID,arg);
    }
    else
    {
    printf("MDM_execSem init ok,val = %d ",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 ");
    //printf("semctl get old value=%d ",ret);
    ret=semop(*semid, &buf_add, 1);
    if(ret== -1)
    {
    printf("OSSemPost_new err ! ");
    return ret;
    }
    else
    {
    // ret=semctl(*semid,0,GETVAL,0);
    // printf("OSSemPost_new succ,old val=%d,new value %d ",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 ");
    // printf("semctl get value=%d ",ret);
    if(ret0)
    {
    buf.sem_op=0-ret0;
    ret=semop(*semid, &buf, 1);
    if(ret== -1)
    {
    // printf("OSSemSet_new err , val=%d,op=%d! ",ret0,buf.sem_op);
    *perr=1;
    }
    else
    {
    // ret =semctl(*semid,0,GETVAL,0);
    // printf("OSSemSet_new succ,old val = %d , new val = %d ",ret0,ret);
    *perr=0;
    }
    }
    // else
    // {
    // printf("value=%d,do not need clear! ",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 ",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! ");
    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 ",ret0,ret);
    *perr=0;
    }
    }

    ps:

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

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

  • 相关阅读:
    zoj_3710Friends
    javamail例子
    HttpURLConnection类的用法
    发送邮件协议
    栈的定义
    tomcat中添加jconsole服务
    HttpURLConnection类的用法
    javamail例子
    tomcat中添加jconsole服务
    栈的定义
  • 原文地址:https://www.cnblogs.com/xiaoyudian/p/8624571.html
Copyright © 2011-2022 走看看