-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducer.c
More file actions
56 lines (55 loc) · 1.72 KB
/
producer.c
File metadata and controls
56 lines (55 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <unistd.h>
#include <fcntl.h>
#include "shm-01.h"
#include <time.h>
int main(){
srand(time(NULL));
int r=rand();
key_t ShmKEY;
int ShmID;
struct Memory *ShmPTR;
ShmKEY=ftok(".", 'x');//generates shared memory key
ShmID = shmget(ShmKEY, sizeof(struct Memory), IPC_CREAT | 0666);//generates shared memory id
if(ShmID<0){//catches error of id not generated
printf("shmget error (producer) \n");
exit(1);
}
printf("Producer has received a shared memory of 2 integers...\n");
ShmPTR = (struct Memory *) shmat(ShmID, NULL, 0);//attatches shared memory to struct pointer
if((int) ShmPTR==-1){//catches attatchment error
printf("shmat error (producer)\n");
exit(1);
}
printf("Producer has attatched the shared memory...\n");
int loop=10;
printf("Producer prepared to make %d items.\n", (loop*2));
int index=0;//producer makes 2 items per loop
ShmPTR -> status = TAKEN;//sets initial status to taken (empty)
sleep(1);
for(int i=0; i<loop; i++){
while(ShmPTR->status != TAKEN)//waits if buffer is full
;
r = rand()%50;//generates "item"
ShmPTR->data[0]=0;
ShmPTR->data[index]=r;//stores item in buff
printf("\t\t\t\t\t\tStored two items: %d ", ShmPTR->data[index]);
r = rand()%50;
index++;
ShmPTR->data[1]=0;
ShmPTR->data[index] = r;//stores item in buff
printf("and %d.\n", ShmPTR->data[index]);
index=0;
ShmPTR->status = FILLED;//flags consumer that buffer is full
printf("Producer sent FILLED status to consumer.\n");
}
shmdt((void *) ShmPTR);//detatches shared memory
shmctl(ShmID, IPC_RMID, NULL);//removes shared memory id
printf("Producer cleaned up.\n");
return 0;
}