0% found this document useful (0 votes)
6 views

shm stuff

The document provides a C program that demonstrates the use of shared memory for inter-process communication. It creates a shared memory segment, allows a parent process to write to it, forks a child process that reads from and writes to the same memory segment, and then cleans up the shared memory. The document also includes explanations of key functions and concepts related to shared memory in C programming.

Uploaded by

radhakrishn1088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

shm stuff

The document provides a C program that demonstrates the use of shared memory for inter-process communication. It creates a shared memory segment, allows a parent process to write to it, forks a child process that reads from and writes to the same memory segment, and then cleans up the shared memory. The document also includes explanations of key functions and concepts related to shared memory in C programming.

Uploaded by

radhakrishn1088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

The code:

#include <stdio.h>​
#include <stdlib.h>​
#include <sys/ipc.h>​
#include <sys/shm.h>​
#include <unistd.h>​
#include <string.h>​
#include <sys/wait.h>​

#define SHM_SIZE 1024 ​

int main() {​
int shmid;​
char *shm;​

shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);​
if (shmid == -1) {​
perror("shmget");​
exit(1);​
}​

shm = (char *)shmat(shmid, NULL, 0);​
if (shm == (char *)-1) {​
perror("shmat");​
exit(1);​
}​

strcpy(shm, "Hello from Parent");​
printf("Parent wrote: %s\n", shm);​

pid_t pid = fork();​
if (pid == -1) {​
perror("fork");​
exit(1);​
} else if (pid == 0) { ​

shm = (char *)shmat(shmid, NULL, 0);​
if (shm == (char *)-1) {​
perror("shmat");​
exit(1);​
}​

printf("Child read: %s\n", shm);​
strcpy(shm, "Hello from Child");​
printf("Child wrote: %s\n", shm);​

if (shmdt(shm) == -1) {​
perror("shmdt");​
exit(1);​
}​
exit(0);​
} else { ​

wait(NULL);​

printf("Parent read: %s\n", shm);​

if (shmdt(shm) == -1) {​
perror("shmdt");​
exit(1);​
}​

if (shmctl(shmid, IPC_RMID, NULL) == -1) {​
perror("shmctl");​
exit(1);​
}​
}​

return 0;​
}
First explain what shared mem is

Now code stuff

#define SHM_SIZE 1024

●​ This line is defining a constant SHM_SIZE with 1024 bytes. This sets the size of the
shared memory segment. (Think of it as allocating a block of 1024 bytes so that all both
processes can use)

shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);

●​ shmget() : Is the system call to create a new shared memory segment or access an
already existing one. To use these we need to invoke <sys/ipc.h> as a header file.
●​ IPC_PRIVATE: a key indicating that a unique shared memory segment is created.
Restricting access to it only by inheritance. Only through fork can one gain access to the
shared memory created.
●​ SHM_SIZE: Allocates 1024 bytes as defined by our macro.
●​ Flags that we need to pass
○​ 0666
■​ Sets the permissions so that the segment is readable and writable by the
owner, group, and others.
○​ IPC_CREAT
■​ Inform the system to create the segment if it does not already exist​

●​ Now incase something goes wrong here , shmget() returns -1.

shm = (char *)shmat(shmid, NULL, 0);

●​ shmat(): This function attaches the shared memory segment (identified by shmid) to
the process’s address space.
●​ Arguments:
○​ shmid: The shared memory identifier returned by shmget().
○​ NULL: Let the system choose the best address to attach the segment.
○​ 0: Specifies the default read/write mode.
●​ The returned pointer shm is the starting address of the shared memory block in the
process's address space. If the attachment fails, shmat() returns (char *)-1.
strcpy(shm, "Hello from Parent");​
printf("Parent wrote: %s\n", shm);

●​ Basic coding here


●​ Strcpy copies the string to shm and then we print it out

if (pid == 0) { ​
shm = (char *)shmat(shmid, NULL, 0);​
if (shm == (char *)-1) {​
perror("shmat");​
exit(1);​
}​

printf("Child read: %s\n", shm);​
strcpy(shm, "Hello from Child");​
printf("Child wrote: %s\n", shm);​

if (shmdt(shm) == -1) {​
perror("shmdt");​
exit(1);​
}​
exit(0);​
}

●​ Now the child process inherits memory mappings from the parent so we technically do
not need to reinitialise shm, it's a good practice tho.
●​ Child read the message from shm and prints it
●​ Then the child overwrites the content of shared memory.
●​ shmdt() is used to detach the shared memory segment from childs address space, thus
the child now no longer has access to that memory. (It still exists, just not accessible.)
wait(NULL);​
printf("Parent read: %s\n", shm);​

if (shmdt(shm) == -1) {​
perror("shmdt");​
exit(1);​
}​

if (shmctl(shmid, IPC_RMID, NULL) == -1) {​
perror("shmctl");​
exit(1);​
}

●​ We wait for child to finish its process.


●​ Parent reads the shm again.
●​ Parent then detaches.
●​ Parent removes the shared memory from the segment.
○​ IPC_RMID is a flag to tell the system to mark that segment for deletion.

Normal output:
Gdb time live karte he
Commands in case i forget

Backtrace to check frame


Frame 0 or 1 whatever

Info inferiors
Inferior 1
print shmid
Print shm

https://limewire.com/d/b3444326-561b-40b6-bfc7-9e40a855216d#fTd8fBMYX-wRZi4t6E2TQok
RwVaqtadcoso9lkYOV5s

gcc -g shm_program.c -o shm_program

gdb -x gdb_commands.txt ./shm_program

You might also like