Producer and Consumer can both interact with shared memory

This commit is contained in:
TriantaTV 2022-10-13 23:25:16 -05:00
parent a22970ff41
commit 688411c38b
5 changed files with 9 additions and 12 deletions

View File

@ -1,4 +0,0 @@
#ifndef CONSUMER_H
#define CONSUMER_H
#endif

View File

@ -1,5 +0,0 @@
#ifndef PRODUCER_H
#define PRODUCER_H
#endif

View File

@ -15,8 +15,11 @@ int main(int argc, char* argv[])
fprintf(stderr, "Usage: %s SharedID# \n", argv[0]);
exit(1);
}
printf("Consumer created.\n");
int sharedID = StringToNumber(argv[1]);
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
sem_wait(&sharedMem->semConsumer);
printf("Checking for 5 in shared struct.\n");
printf("%d\n", sharedMem->buffer[0]);
return 0;
}

View File

@ -18,8 +18,11 @@ int main(int argc, char* argv[])
exit(1);
}
srandom((unsigned int) time(NULL));
printf("Producer created.\n");
int sharedID = StringToNumber(argv[1]);
SharedStruct* sharedMem = shmat(sharedID, NULL, 0);
printf("Adding 5 to shared struct.\n");
sharedMem->buffer[0] = 5;
sem_post(&sharedMem->semConsumer);
return 0;
}