Skip to content

Instantly share code, notes, and snippets.

@vtronko
Created June 7, 2016 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vtronko/ff510f1053b579d8c558ad2bb83c1b84 to your computer and use it in GitHub Desktop.
Save vtronko/ff510f1053b579d8c558ad2bb83c1b84 to your computer and use it in GitHub Desktop.
// common.h
#define MEMORY_KEY 145
#define SIZE_OF_ARRAY 10
struct wrapper
{
int array[SIZE_OF_ARRAY];
sem_t empty;
sem_t pmutex;
sem_t cmutex;
sem_t full;
int n;
};
// producer.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 "common.h"
int memoryID;
struct wrapper *memory;
int rc;
void atexit_function() {
rc = shmctl(memoryID, IPC_RMID, NULL);
rc = shmdt(memory);
sem_destroy(&memory->pmutex);
sem_destroy(&memory->cmutex);
sem_destroy(&memory->empty);
sem_destroy(&memory->full);
}
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);
}
//initialization
printf("Initializataion !\n");
memset(&memory->array, 0, sizeof(memory->array));
sem_init(&memory->pmutex, 0, 1);
sem_init(&memory->cmutex, 0, 1);
sem_init(&memory->empty, 1, SIZE_OF_ARRAY);
sem_init(&memory->full, 1, 0);
memory->n = 0;
if (memoryID == -1) {
perror("shmget(): ");
exit(1);
}
while (1)
{
int r = rand() % 10;
sem_wait(&memory->empty);
sem_wait(&memory->pmutex);
(memory->array)[memory->n]=r;
memory->n++;
printf("Adding task\t Value:%d\tNumber of tasks waiting:%d \n",r,memory->n);
usleep(10000);
sem_post(&memory->pmutex);
sem_post(&memory->full);
}
}
// consumer.c
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
#include <sys/shm.h>
int factorial(int a) {
if (!a)
return 1;
return a * factorial(a - 1);
}
int memoryID;
struct wrapper *memory;
int main() {
srand(time(NULL));
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->full);
sem_wait(&memory->cmutex);
memory->n--;
int n = memory->n;
int temp = (memory->array)[n];
printf("Removed item: %d\tFactorial:%d\tNumber of tasks left:%d\n",
temp, factorial(temp),n);
usleep(10000);
sem_post(&memory->cmutex);
sem_post(&memory->empty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment