int main(int argc, char const *argv[]) { int shared_segment_size = 64; int items = 5000; key_t key = IPC_PRIVATE; //ALLOCATE SHARED MEMORY SEGMENT int segment_id = shmget(key, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); //ATTACH SHARED MEMORY SEGMENT char *shared_memory = (char*) shmat (segment_id, 0, 0); //WRITE STRING TO SHARED MEMORY SEGMENT (initialize each byte as "empty") int i; for(i=0; i<shared_segment_size; i++){ shared_memory[i] = 'e'; } //DETACH SHARED MEMORY shmdt(shared_memory); //Create semaphores int mutex = semaphore_allocation(key, SHM_R | SHM_W); int empty = semaphore_allocation(key, SHM_R | SHM_W); int full = semaphore_allocation(key, SHM_R | SHM_W); semaphore_initalize(mutex, 1); semaphore_initalize(empty, shared_segment_size); semaphore_initalize(full, 0); //convert id's to strings to pass as arguments char itemsString[10]; char segment_idString[10]; char mutexString[10]; char emptyString[10]; char fullString[10]; sprintf(itemsString, "%d", items); sprintf(segment_idString, "%d", segment_id); sprintf(mutexString, "%d", mutex); sprintf(emptyString, "%d", empty); sprintf(fullString, "%d", full); char *producerArgList[] = {"./problem2Producer.out", itemsString, segment_idString, mutexString, emptyString, fullString, NULL}; char *consumerArgList[] = {"./problem2Consumer.out", itemsString, segment_idString, mutexString, emptyString, fullString, NULL}; int producerStatus = spawn("./problem2Producer.out", producerArgList); int consumerStatus = spawn("./problem2Consumer.out", consumerArgList); wait(&producerStatus); wait(&consumerStatus); //DEALLOCATE THE SHARED MEMORY SEGMENT shmctl(segment_id, IPC_RMID, 0); //DEALLOCATE SEM semaphore_deallocate(mutex); semaphore_deallocate(empty); semaphore_deallocate(full); return 0; }
int main(int argc, const char* argv[]) { int segment_id; int semaphore_id; char* shared_memory; // Pick a key for the semaphore set const int semaphore_key = 6969; if (argc != 2) { printf("Usage: client segment-key\n"); exit(1); } segment_id = shmget(atoi(argv[1]), getpagesize(), 0666); if (segment_id < 0) { perror("Could not get segment"); exit(1); } semaphore_id = semaphore_allocate(key); if (semaphore_id < 0) { perror("Error allocating semaphore!"); exit(1); } if (semaphore_initialize(semaphore_id) < 0) { perror("Error intiializing semaphore!"); exit(1); } shared_memory = shmat(segment_id, NULL, 0); if (shared_memory < (char*)0) { perror("Could not attach segment"); exit(1); } semaphore_wait(semaphore_id); printf("%s\n", shared_memory); semaphore_post(semaphore_id); *shared_memory = '*'; shmdt(shared_memory); semaphore_deallocate(key); return 0; }
int main(int argc, char* const argv[]) { int fd; char *file_memory; char *fileName = "problem3File.mmap"; int fileLength = 64; int items = 5000; key_t key = IPC_PRIVATE; //Prepate a file large enough to hold unsigned integer fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); lseek (fd, fileLength+1, SEEK_SET); //make sure file is large enough write (fd, "", 1); lseek(fd, 0, SEEK_SET); //move write pointer back to beginning //CREATE MEMORY MAPPING file_memory = (char*)mmap(0, fileLength, PROT_WRITE, MAP_SHARED, fd, 0); close(fd); //closing file access in HD //WRITE STRING TO MEMORY MAPPED FILE (initialize each byte as "empty") int i; for(i=0; i<fileLength; i++){ file_memory[i] = 'e'; } //Create semaphores int mutex = semaphore_allocation(key, SHM_R | SHM_W); int empty = semaphore_allocation(key, SHM_R | SHM_W); int full = semaphore_allocation(key, SHM_R | SHM_W); semaphore_initalize(mutex, 1); semaphore_initalize(empty, fileLength); semaphore_initalize(full, 0); //convert id's to strings to pass as arguments char itemsString[10]; char fileLengthString[10]; char mutexString[10]; char emptyString[10]; char fullString[10]; sprintf(itemsString, "%d", items); sprintf(fileLengthString, "%d", fileLength); sprintf(mutexString, "%d", mutex); sprintf(emptyString, "%d", empty); sprintf(fullString, "%d", full); char *producerArgList[] = {"./problem3Producer.out", itemsString, fileName, fileLengthString, mutexString, emptyString, fullString, NULL}; char *consumerArgList[] = {"./problem3Consumer.out", itemsString, fileName, fileLengthString, mutexString, emptyString, fullString, NULL}; int producerStatus = spawn("./problem3Producer.out", producerArgList); int consumerStatus = spawn("./problem3Consumer.out", consumerArgList); wait(&producerStatus); wait(&consumerStatus); //DEALLOCATE SEM semaphore_deallocate(mutex); semaphore_deallocate(empty); semaphore_deallocate(full); //Release the memory --> memory will automatically be released when munmap (file_memory, fileLength); return 0; }