Лекции по курсу "Системное программирование для UNIX". Литвинов Д.Г. - 72 стр.

UptoLike

Составители: 

72
}
sem_lock.sem_num = member;
if((semop(sid, &sem_lock, 1)) == -1)
{
fprintf(stderr, "Lock failed\n");
exit(1);
}
else
printf("Semaphore resources decremented by one (locked)\n");
dispval(sid, member);
}
void unlocksem(int sid, int member)
{
struct sembuf sem_unlock={ member, 1, IPC_NOWAIT };
int semval;
if( member<0 || member>(get_member_count(sid)-1))
{
fprintf(stderr, "semaphore member %d out of range\n", member);
return;
}
/* Заперто ли множество семафоров? */
semval = getval(sid, member);
if(semval == SEM_RESOURCE_MAX) {
fprintf(stderr, "Semaphore not locked!\n");
exit(1);
}
sem_unlock.sem_num = member;
/* Попытка запереть множество семафоров */
if((semop(sid, &sem_unlock, 1)) == -1)
{
fprintf(stderr, "Unlock failed\n");
exit(1);
}
else
printf("Semaphore resources incremented by one (unlocked)\n");
dispval(sid, member);
}
void removesem(int sid)
{
semctl(sid, 0, IPC_RMID, 0);
printf("Semaphore removed\n");
}
unsigned short get_member_count(int sid)
{
union semun semopts;
struct semid_ds mysemds;
semopts.buf = &mysemds;
/* Выдает количество элементов во множестве семафоров */
return(semopts.buf->sem_nsems);
}
int getval(int sid, int member)
     }

     sem_lock.sem_num = member;

     if((semop(sid, &sem_lock, 1)) == -1)
     {
        fprintf(stderr, "Lock failed\n");
        exit(1);
     }
     else
        printf("Semaphore resources decremented by one (locked)\n");

     dispval(sid, member);
}

void unlocksem(int sid, int member)
{
   struct sembuf sem_unlock={ member, 1, IPC_NOWAIT };
   int semval;

     if( member<0 || member>(get_member_count(sid)-1))
     {
        fprintf(stderr, "semaphore member %d out of range\n", member);
        return;
     }

     /* Заперто ли множество семафоров? */
     semval = getval(sid, member);
     if(semval == SEM_RESOURCE_MAX) {
        fprintf(stderr, "Semaphore not locked!\n");
        exit(1);
     }
     sem_unlock.sem_num = member;

     /* Попытка запереть множество семафоров */
     if((semop(sid, &sem_unlock, 1)) == -1)
     {
        fprintf(stderr, "Unlock failed\n");
        exit(1);
     }
     else
        printf("Semaphore resources incremented by one (unlocked)\n");

     dispval(sid, member);
}

void removesem(int sid)
{
   semctl(sid, 0, IPC_RMID, 0);
   printf("Semaphore removed\n");
}

unsigned short get_member_count(int sid)
{
   union semun semopts;
   struct semid_ds mysemds;

     semopts.buf = &mysemds;

     /* Выдает количество элементов во множестве семафоров */
     return(semopts.buf->sem_nsems);
}

int getval(int sid, int member)



72