cis-452-lab-7/count-semaphore.c

42 lines
850 B
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sem.h>
int main()
{
unsigned long max = 1UL;
int semId;
semId = semget(IPC_PRIVATE, 1, 00600);
semctl(semId, 0, SETVAL, 1);
struct sembuf unlock = {0, 1, SEM_UNDO};
for (max = 1UL; max < 33366475776UL; max++)
{
// Incrementing semaphore.
if (semop(semId, &unlock, 1) < 0)
{
printf("Max semaphore value: %lu\n", max);
// Flagging semaphore for deletion.
if (semctl(semId, 0, IPC_RMID) < 0)
{
perror("can't deallocate\n");
exit(1);
}
exit(1);
}
}
semctl(semId, 0, IPC_RMID);
return 0;
}