Lab 8 walkthrough — Shared memory creation and sharing contents between processes
Lab 8 walkthrough — Shared memory creation and sharing contents between processes
Recorded by Dr. Krithika L.B. Slot lab08-walkthrough
Theory link: 3.1 IPC
Lab 8 walkthrough — Shared memory creation and sharing contents between processes
Recorded by Dr. Krithika L.B. Slot lab08-walkthrough
Aim. To create a System V shared-memory segment, write data into it from one process and read the same data from another process.
Shared memory is the fastest IPC (Session 3.1): after setup, processes communicate with plain memory reads and writes.
Create (with IPC_CREAT) or locate a segment; returns its id.
Attach the segment; returns a pointer you can use like any array.
Detach from this process.
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.
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.
#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;
}#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;
}#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;
}gcc shm_writer.c -o shm_writer and gcc shm_reader.c -o shm_reader./shm_writer, type a message.ipcs -m to see the segment (key 0x000004d2 = 1234)../shm_reader.gcc shm_fork.c -o shm_fork && ./shm_fork$ ./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$ ./shm_reader
Read from shared memory: Operating Systems Lab 8$ ./shm_fork
Parent read: Hello parent, this is your child!shm_open and mmap.Prepare these for the lab evaluation.
Revealed 0The kernel is involved only in setup; data is exchanged by ordinary memory accesses without copying.
The address at which the segment is attached in the caller's address space.
No; it only detaches it. shmctl with IPC_RMID removes it.
ipcs -m lists; ipcrm -m <shmid> removes.
Race conditions — they need synchronization (semaphores), as in Lab 9.