Question: Shared memory between processes is an IPC communication method. The shared memory can be implemented in different ways. The below code illustrated on our slides
Shared memory between processes is an IPC communication method. The shared memory can be implemented in different ways. The below code illustrated on our slides is one of means. Create a producer file named shm_producer.c and another file named shm_consumer.c.
1a) Use the library rt to compile the source code. A dynamic link library can be compiled when lrt is specified. Note that -l indicates that you are using the dynamic link library while rt is the library name that means RealTime extension library here. The syntax of the compilation should be gcc sourcecode o output -lrt.
1b) Compile the source code shm_producer.c without -lrt. What is the error you have?
1c) Use find command to find out the dynamic library file named librt.so.1 (where the shared objects are provided by libc following POSIX.1b) in the folder of /usr/lib/. What is your command line? And what are your results?
1d) Run the output files in two terminals respectively. Paste the screenshots of the two terminals.
//This file is for producer process using shm_open and mmap.
// gcc sourcecode -o output -lrt
#include
#include
#include
#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 *name = "OS";
/* strings written to shared memory */
const char *message0 = "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 | O_RDWR, 0666);
/* configure the size of the shared memory object */
ftruncate (shm_fd, SIZE);
/* memory map the shared memory object */
ptr = mmap(NULL, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
/* write to the shared memory object */
sprintf(ptr, "%s", message0);
ptr += strlen(message0);
sprintf(ptr,"%s", message1);
ptr += strlen(message1);
printf("producing is done. ");
return 0;
}
//This file is for consumer process using shm_open and mmap.
// gcc sourcecode -o output -lrt
#include
#include
#include
#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 *name = "OS";
/* shared memory file descriptor */
int shm_fd;
/* pointer to shared memory obect */
void *ptr;
/* open the shared memory object */
shm_fd = shm_open (name, O_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;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
