Question: COMP 3063 Class activity #3 (1 point) Dr. Mary Kim In Class Exercise Creating Shared Memory and make two processes communicate through it February 14,
COMP 3063 Class activity #3 (1 point) Dr. Mary Kim In Class Exercise Creating Shared Memory and make two processes communicate through it February 14, 2019 Create Producer and Consumer file 1) Please refer to the textbook pp. 132-133 to create two files, shm_prod.c and shm con.c. You can copy the draft code from textbook(pdf in Ecourses) and paste it into vi editor. 2) Modify the code a) add header file to both of the files # include b) remove header file from both #include c) change the parameter O_RDRWto O_RDWR of shm_open system call in producer file (shm_prod.c) 3) Create a Makefile as following vi Makefile all: shm_ prod shm_cons shm_prod: shm_ prod.c gcc -o shm_prod shm_prod.c-Irt shm_cons: shm_cons.c gcc o shm_cons shm_cons.c-Irt Make sure that you have entered Tab key before gcc (not space) so that OS can detect gcc line as to be executable command. 4) After creating Makefile, change mode of the file chmod 755 Makefile Now you can read, write, or execute the Makefile. 5) Now execute make like this: make shm_prod compile shm prod.c and make executable file named shm prod compile shm-cons.c and make executable file named shm-con > make shmcons. - make all do both of two above. 6) Now, run shm_prod and shm_cons in this order and observe result. >shm_prod shmcons. - Once you get the result, show it to the instructor to get credit. 0 132 Chapter 3 Processes #include #include #include #include #include #include int main ) / the size (in bytes) of shared memory object const int SIZE 4096 / name of the shared memory object const char ame"OS" /* strings written to shared memory const char message.0"Hello" const char message1"World!" / shared memory file descriptor/ int shm fd; / pointer to shared memory obect/ void *ptr /* create the shared memory object/ shm fd shm open (name, O CREAT 1 0 RDRW, 0666) /* configure the size of the shared memory object ftruncate (shm fd, SIZE) / memory map the shared memory object/ tr mmap(O, SIZE, PROT WRITE, MAP SHARED, shm.fd, 0); /* write to the shared memory object/ sprintf (ptr, "%s", message O) ; ptrstrlen(message 0) sprintf(ptr,"%s",message 1); ptrstrlen(message 1) return 0 3.5 Examples of IPC Systems 133 #include #include #include #include #include int main() /* the size (in bytes) of shared memory object / const int SIZE 4096 * name of the shared memory object / const char *name "OS"; shared memory file descriptor */ int shm fd pointer to shared memory obect/ void *ptr; open the shared memory object/ shn.fd = shn.open (name, 0 RDONLY, 0666); *memory map the shared memory object / ptr mmap (0, SIZE, PROT READ, MAP SHARED, shm fd, 0); / read from the shared memory object*/ printf("%s", (char *)ptr); *remove the shared memory object */ shm unlink (name) return 0; COMP 3063 Class activity #3 (1 point) Dr. Mary Kim In Class Exercise Creating Shared Memory and make two processes communicate through it February 14, 2019 Create Producer and Consumer file 1) Please refer to the textbook pp. 132-133 to create two files, shm_prod.c and shm con.c. You can copy the draft code from textbook(pdf in Ecourses) and paste it into vi editor. 2) Modify the code a) add header file to both of the files # include b) remove header file from both #include c) change the parameter O_RDRWto O_RDWR of shm_open system call in producer file (shm_prod.c) 3) Create a Makefile as following vi Makefile all: shm_ prod shm_cons shm_prod: shm_ prod.c gcc -o shm_prod shm_prod.c-Irt shm_cons: shm_cons.c gcc o shm_cons shm_cons.c-Irt Make sure that you have entered Tab key before gcc (not space) so that OS can detect gcc line as to be executable command. 4) After creating Makefile, change mode of the file chmod 755 Makefile Now you can read, write, or execute the Makefile. 5) Now execute make like this: make shm_prod compile shm prod.c and make executable file named shm prod compile shm-cons.c and make executable file named shm-con > make shmcons. - make all do both of two above. 6) Now, run shm_prod and shm_cons in this order and observe result. >shm_prod shmcons. - Once you get the result, show it to the instructor to get credit. 0 132 Chapter 3 Processes #include #include #include #include #include #include int main ) / the size (in bytes) of shared memory object const int SIZE 4096 / name of the shared memory object const char ame"OS" /* strings written to shared memory const char message.0"Hello" const char message1"World!" / shared memory file descriptor/ int shm fd; / pointer to shared memory obect/ void *ptr /* create the shared memory object/ shm fd shm open (name, O CREAT 1 0 RDRW, 0666) /* configure the size of the shared memory object ftruncate (shm fd, SIZE) / memory map the shared memory object/ tr mmap(O, SIZE, PROT WRITE, MAP SHARED, shm.fd, 0); /* write to the shared memory object/ sprintf (ptr, "%s", message O) ; ptrstrlen(message 0) sprintf(ptr,"%s",message 1); ptrstrlen(message 1) return 0 3.5 Examples of IPC Systems 133 #include #include #include #include #include int main() /* the size (in bytes) of shared memory object / const int SIZE 4096 * name of the shared memory object / const char *name "OS"; shared memory file descriptor */ int shm fd pointer to shared memory obect/ void *ptr; open the shared memory object/ shn.fd = shn.open (name, 0 RDONLY, 0666); *memory map the shared memory object / ptr mmap (0, SIZE, PROT READ, MAP SHARED, shm fd, 0); / read from the shared memory object*/ printf("%s", (char *)ptr); *remove the shared memory object */ shm unlink (name) return 0