Select Page

Harrisburg Area Community College C program using POSIX Shared Memory Lab Report

Question Description

Shared memory is considered to be the fastest IPC mechanisms since there is no copying between different processes’ memory locations, the information needs to be written into the shared memory location once. The other option is to copy different memory locations from the sourcing process to the receiving process.

POSIX shared memory is based on the UNIX philosophy, all Input and Output operations have to be done on an object file. POSIX shared memory uses a memory-mapped file that associates a memory region with a file, hence read and write operations are treated as a reading and writing to a file using a file descriptor.

To use shared memory operations, you need first to define and create a shared memory object. To do so we use the “ shm_open” system call:

int shm_open (const char *name, int oflag, mode_t mode);

  • const char * name : The first parameter specifies the name of the shared memory object. This is the name processes you will use to access this shared memory.
  • int Oflag : specifies a bit mask constructed using OR logical operations to descript the different flags and options associated with this shared memory object. For example, O_CREAT to create the shared memory object if it does not exist, O_RDWR for the read and write permissions.
  • mode_t mode : The third parameter used to describe the mode, which indicates the file permissions of the shared object similar to Linux file permissions wherethe first number represents the Owner permission; the second represents the Group permissions; and the last number represents the permissions for all other users. The numbers are a binary representation of the rwx string:
    • r = 4
    • w = 2
    • x = 1

For example, to set permissions to read rwxr_____, you would enter 740.

A successful call to shm_open returns an integer that represents a file descriptor for the shared memory object. The object is created with a size equal to zero. The system call requires the following headers:

#include <sys/mman.h>

#include <sys/stat.h>

#include <fcntl.h>

The next step is to decide the size of the object established, for this operation we use the function ftruncate :

int ftruncate (int fd, off_t length);

  • int fd: The function takes a file descriptor fd as a first parameter
  • off_t length: changes the file size to length bytes.

ftruncate returns zero on success and -1 in case of error or failure to increase size.

To use the function, you need to include the following headers:

#include <unistd.h>

#include <sys/types.h>

The final step before using the shared memory object is to connect the process’s memory address space to the file descriptor such that you can access the file using regular memory addresses. This memory address is mapped into the process’s virtual memory. To do complete the connection, we use the mmap function.

void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

        • void * addr : specifies the address at which it should be mapped. In most cases, we do not care at what address mapping is done and a value of NULL for addr should suffice.
        • size_t length: is the length of the shared memory object that should be mapped. To keep things simple, we will map the whole object, and the length for us will be the length of the shared memory object.
        • int prot: can have the values, PROT_EXEC, PROT_READ, PROT_WRITE, PROT_NONE, and MAP_SHARED.
          PROT_READ | PROT_WRITE value, Which means we can read and write into the shared memory.
          MAP_SHARED, which means that the updates to the mapped shared memory are visible to all other processes immediately.
        • int fd : the file descriptor for the shared memory received from an earlier shm_open
        • off_t offset: is the location in the shared memory object at which the mapping starts; we will use the value zero for offset and map the shared memory object starting from the beginning.

mmap returns a pointer to the memory-mapped file that is used for accessing the shared memory object or -1 in case of failure or error. mmap requires the following headers to be included in your code:

#include <sys/mman.h>

When the shared memory object is no longer needed, the function shm_unlink() is used to remove the object from the memory.

int shm_unlink (const char *name);

  • const char * name : the name of the shared memory object as described under shm_open function above.
  • shm_unlink uses the same headers as the shm_open function.

The producer creates a shared memory object named SHARED and writes a string “Hello World!” into the shared memory object. You will need to use sprintf() function and a pointer ptr to write and format strings into the shared memory location. The pointer ptr will increment to point to the next available empty space in the shared memory buffer.

The consumer process reads and outputs the content of the shared memory. The consumer access the shared memory using the same name as the producer. The consumer follows the same steps as producer except that it uses printf function to print the content of the memory location pointed by ptr, which should be the same as the information provided in the producer code. One major difference here is the usage of shm_unlink function which removes the shared memory object.

Notice:

When compiling codes that use the POSIX shared memory library, you will need to add the following option to your GCC command line: -lrt. This will add the POSIX RealTime extension library to your code. Your command line should look something like :

gcc program_with_POSIX_SHM.c -o output -lrt

Lab Instructions:

Write the following two C programs with the following specifications:

Code should be thoroughly commented

Write two C programs, producer, and consumer programs that communicate using a POSIX shared memory.

Producer Code

The producer program will accept input from the user and write that input into a shared memory location.

The program will create and write to a POSIX shared memory. The program will terminate when the user enters “End” as an input.

Initial conditions and values:

  • The shared memory location name is “ SHARED “.

Functionality

  • Accept input from user, write the user string into the shared memory location.
  • The program terminates when the user enters “End”.

Consumer Code

Write a C program that will open a POSIX shared memory; the program will read and print the contents of the shared memory and closes the shared memory object afterward.

Initial conditions and values:

  • The shared memory location name is “ SHARED “.

Functionality

  • Print the content of the shared memory location.
  • Exit the program when all memory contents are displayed.

Sample output is shown in the program

"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you "A" results."

Order Solution Now