Skip to content

Instantly share code, notes, and snippets.

@vtronko
Created June 7, 2016 15:45
Show Gist options
  • Save vtronko/65c01a137abe887dfab22a30b362f033 to your computer and use it in GitHub Desktop.
Save vtronko/65c01a137abe887dfab22a30b362f033 to your computer and use it in GitHub Desktop.
// common.h
#define MEMORY_KEY 13
#define ARRAY_SIZE 8
struct wrapper {
sem_t s_read;
sem_t s_write;
int readcount;
int array[ARRAY_SIZE];
};
// reader.c
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include "common.h"
int memoryID;
struct wrapper *memory;
float mean(int* array, size_t size) {
int sum = 0;
for (int i = 0; i < size; ++i)
sum += array[i];
return sum / size;
}
int main() {
key_t sharedMemoryKey = ftok(".",MEMORY_KEY);
if(sharedMemoryKey==-1)
{
perror("ftok():");
exit(1);
}
memoryID=shmget(sharedMemoryKey,sizeof(struct wrapper),0);
if(memoryID==-1)
{
perror("shmget(): ");
exit(1);
}
memory = shmat(memoryID,NULL,0);
if(memory== (void*)-1)
{
perror("shmat():");
exit(1);
}
while (1) {
sem_wait(&memory->s_read);
int working = ++memory->readcount;
// forbid access for writer while
// at least one reader is working
if (memory->readcount == 1)
sem_wait(&memory->s_write);
sem_post(&memory->s_read);
// reading stuff
float result = mean(memory->array, ARRAY_SIZE);
printf("Mean value: %f\tReaders working: %d\n", result, working);
sem_wait(&memory->s_read);
memory->readcount--;
// if all readers are done - open access for writer
if (!memory->readcount)
sem_post(&memory->s_write);
sem_post(&memory->s_read);
usleep(5000);
}
}
// writer.c
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include "common.h"
int memoryID;
struct wrapper *memory;
int rc;
void atexit_function() {
rc = shmctl(memoryID, IPC_RMID, NULL);
rc = shmdt(memory);
sem_destroy(&memory->s_read);
sem_destroy(&memory->s_write);
}
int main() {
atexit(atexit_function);
//creating key for shared memory
srand(time(NULL));
key_t sharedMemoryKey = ftok(".", MEMORY_KEY);
if (sharedMemoryKey == -1) {
perror("ftok():");
exit(1);
}
memoryID = shmget(sharedMemoryKey, sizeof(struct wrapper), IPC_CREAT | 0600);
if (memoryID == -1) {
perror("shmget():");
exit(1);
}
memory = shmat(memoryID, NULL, 0);
if (memory == (void *) -1) {
perror("shmat():");
exit(1);
}
printf("Initialization !\n");
memset(&memory->array, 0, sizeof(memory->array));
sem_init(&memory->s_write, 1, 1);
sem_init(&memory->s_read, 1, 1);
memory->readcount = 0;
while (1) {
sem_wait(&memory->s_write);
printf("%Writing new values: [ ");
for (int i = 0; i < ARRAY_SIZE; ++i) {
(memory->array)[i] = rand() % 100;
printf("%d ", (memory->array)[i]);
}
printf("]\n");
sem_post(&memory->s_write);
usleep(100000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment