代码之家  ›  专栏  ›  技术社区  ›  user3266496

信号量计数器:程序挂起

  •  0
  • user3266496  · 技术社区  · 11 年前

    我想创建一个信号量计数器,代码如下:

    union semun arg_assistant;
    int max_ass = atoi(argv[1]);
    printf("Num massimo di assistant %d\n", max_ass);
    fflush(stdout);
    if ((sem_a = semget(IPC_PRIVATE, 1, 0600)) == -1) {
        perror("semget");
        exit(EXIT_FAILURE);
    }
    arg_assistant.val = max_ass;
    if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
        perror("semctl");
        exit(EXIT_FAILURE);
    }   
    

    当我执行我的程序时,我没有任何错误,但它挂起,并且不会创建这个sem。有什么建议可以解决问题吗?我是不是搞错了falgs? Thaks公司

    1 回复  |  直到 11 年前
        1
  •  0
  •   Dabo    11 年前

    从…起 documentation

    集合,集合

    使用arg.array为集合的所有信号量设置信号值,

    对于 SETALL 您需要值数组

        unsigned short int  sem_array[1] ;
        sem_array[0] = max_ass;
        arg_assistant.array = sem_array;
    
        if (semctl(sem_a, 0, SETALL, arg_assistant) == -1) {
           perror("semctl");
           exit(EXIT_FAILURE);
        }
    

    sem_array[1] 因为您只创建一个信号量。