Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 8: Shared memory creation and sharing contents between processes

Theory link: 3.1 IPC

Experiment 8 of 15 Module 3 2 hours

Video walkthrough

Lab 8 walkthrough — Shared memory creation and sharing contents between processes

Video coming soon

Lab 8 walkthrough — Shared memory creation and sharing contents between processes

Recorded by Dr. Krithika L.B. Slot lab08-walkthrough

Aim

Aim. To create a System V shared-memory segment, write data into it from one process and read the same data from another process.

Explanation

Shared memory is the fastest IPC (Session 3.1): after setup, processes communicate with plain memory reads and writes.

writer→segment (key 1234)→reader
shmget(key, size, flags)

Create (with IPC_CREAT) or locate a segment; returns its id.

shmat(id, NULL, 0)

Attach the segment; returns a pointer you can use like any array.

shmdt(ptr)

Detach from this process.

shmctl(id, IPC_RMID, NULL)

Remove the segment from the system.

Both programs use the same key (1234) so they find the same segment. Program 3 does it in one program with a parent and child.

Environment

These programs use POSIX system calls (fork, pthread, shm, semaphore) and must be compiled and run on Linux or WSL — not with Windows MinGW/Turbo C.

Code

Program 1 · Writer

shm_writer.c
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(void)
{
    int shmid = shmget(1234, 1024, IPC_CREAT | 0666);    /* create 1 KB segment */
    char *data = (char *)shmat(shmid, NULL, 0);           /* attach */

    printf("Enter a message: ");
    fgets(data, 1024, stdin);                             /* write straight into shared memory */
    printf("Written to shared memory: %s", data);

    shmdt(data);                                          /* detach */
    return 0;
}

Program 2 · Reader

shm_reader.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(void)
{
    int shmid = shmget(1234, 1024, 0666);                 /* locate existing segment */
    char *data = (char *)shmat(shmid, NULL, 0);

    printf("Read from shared memory: %s", data);

    shmdt(data);
    shmctl(shmid, IPC_RMID, NULL);                        /* remove segment */
    return 0;
}

Program 3 · Parent and child share a segment

shm_fork.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>

int main(void)
{
    int shmid = shmget(IPC_PRIVATE, 100, IPC_CREAT | 0666);
    char *data = (char *)shmat(shmid, NULL, 0);           /* attached before fork: child inherits it */

    if (fork() == 0) {
        strcpy(data, "Hello parent, this is your child!");
        shmdt(data);
    } else {
        wait(NULL);                                       /* let the child write first */
        printf("Parent read: %s\n", data);
        shmdt(data);
        shmctl(shmid, IPC_RMID, NULL);
    }
    return 0;
}

How to run

  1. gcc shm_writer.c -o shm_writer and gcc shm_reader.c -o shm_reader
  2. Terminal 1: ./shm_writer, type a message.
  3. Run ipcs -m to see the segment (key 0x000004d2 = 1234).
  4. Terminal 2 (or after the writer ends): ./shm_reader.
  5. gcc shm_fork.c -o shm_fork && ./shm_fork

Sample input & output

writer

Input
Operating Systems Lab 8
Output
Output
$ ./shm_writer
Enter a message: Operating Systems Lab 8
Written to shared memory: Operating Systems Lab 8
$ ipcs -m
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x000004d2 32768      student    666        1024       0

reader

Input
(none)
Output
Output
$ ./shm_reader
Read from shared memory: Operating Systems Lab 8

parent and child

Input
(none)
Output
Output
$ ./shm_fork
Parent read: Hello parent, this is your child!

Try it yourself

  1. Store a structure (roll number, name, marks) in shared memory instead of a string.
  2. Write a writer that stores the numbers 1–10 and a reader that prints their sum.
  3. What happens if the reader runs before the writer? Add error checking for shmget returning −1.
  4. Rewrite the writer and reader with POSIX shm_open and mmap.

Viva questions

Prepare these for the lab evaluation.

Revealed 0
Why is shared memory the fastest IPC?

The kernel is involved only in setup; data is exchanged by ordinary memory accesses without copying.

What does shmat return?

The address at which the segment is attached in the caller's address space.

Does shmdt delete the segment?

No; it only detaches it. shmctl with IPC_RMID removes it.

Which commands list and remove segments from the shell?

ipcs -m lists; ipcrm -m <shmid> removes.

What problem remains when two processes write the same segment?

Race conditions — they need synchronization (semaphores), as in Lab 9.