Domanda

I want create a semaphore counter, with this code:

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);
}   

When I executed my program, I have no errors but it hangs and it don't create this sem. Any suggestion about what could be the problem? Have I make some mistake with falgs? Thaks

È stato utile?

Soluzione

From documentation

SETALL

Set semval for all semaphores of the set using arg.array,

For SETALL you need array of values

    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] becaues you create only one semaphore.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top