cis-452-lab-7/shared-memory.c

69 lines
1.5 KiB
C
Raw Normal View History

2021-02-22 02:22:48 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
unsigned long max = 1UL;
unsigned long temp = 0UL;
int shmId;
for (max = 1; max < 10000000000000UL; max *= 2)
{
// Getting the shared memory segment.
if ((shmId = shmget(IPC_PRIVATE, max, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0)
{
temp = max;
break;
}
// Flagging shared memory for deletion.
if (shmctl(shmId, IPC_RMID, 0) < 0)
{
perror("can't deallocate\n");
exit(1);
}
}
for (max = temp / 2; max < temp; max += 67108864)
{
// Getting shared memory segment.
if ((shmId = shmget(IPC_PRIVATE, max, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0)
{
temp = max;
break;
}
// Flagging shared memory for deletion.
if (shmctl(shmId, IPC_RMID, 0) < 0)
{
perror("can't deallocate\n");
exit(1);
}
}
for (max = temp - 67108864; max < temp; max++)
{
// Getting the shared memory segment.
if ((shmId = shmget(IPC_PRIVATE, max, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0)
{
printf("Max value: %lu\n", max);
exit(0);
}
// Flagging shared memory for deletion.
if (shmctl(shmId, IPC_RMID, 0) < 0)
{
perror("can't deallocate\n");
exit(1);
}
}
return 0;
}