Shared memory is an inter-process communication (IPC) mechanism that allows multiple processes to access the same memory area. It is highly efficient because data does not need to be copied between processes but is read and written directly in the shared memory. In this article, we will introduce how to use shared memory in the C language and provide code examples to aid understanding.
1. Basic Concepts of Shared Memory
In an operating system, each process has its own address space. By using shared memory, different processes can access the same physical memory area. This makes data exchange more efficient.
1.1 Key Functions
<span>shmget</span>: Creates a new shared memory segment or retrieves an existing one.<span>shmat</span>: Attaches the shared memory segment to the current process’s address space.<span>shmdt</span>: Detaches the attached shared memory segment from the current process.<span>shmctl</span>: Controls the shared memory segment, such as deleting it.
2. Example Code
Below, we will demonstrate how to implement basic shared memory functionality in C with a simple example. We will create two programs, one for writing data and the other for reading data.
2.1 Writer Program (writer.c)
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SHM_SIZE 1024 // Define the size of the shared area
int main() { int shmid; key_t key = ftok("shmfile",65); // Create a unique key char *str;
// Create and get a new shared area shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
// Attach the area to the current process's address space str = (char*) shmat(shmid, NULL, 0);
printf("Writing data to shared area: "); fgets(str, SHM_SIZE, stdin); // Read string from standard input
printf("Data written: %s\n", str);
// Detach the area shmdt(str);
return 0;}
2.2 Reader Program (reader.c)
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024 // Define the same size as writer.c
int main() { int shmid; key_t key = ftok("shmfile",65); // Create a unique key char *str;
// Get the previously created shared area shmid = shmget(key, SHM_SIZE,0666);
if (shmid == -1) { perror("Unable to get shared area"); exit(1); }
str = (char*) shmat(shmid,NULL,0);
printf("Data read from shared area: %s\n", str);
// Detach the area shmdt(str);
return 0;}
3. Compiling and Running the Programs
To compile these two programs, you can use the following commands:
gcc writer.c -o writer
gcc reader.c -o reader
First, run the writer program, and then run the reader program:
./writer # Enter some text and press Enter to submit
./reader # View the data just entered
4. Considerations
- Permissions: Ensure you have sufficient permissions to create and access the required resources.
- Cleanup: If a shared area is no longer needed, you can call
<span>shmctl(shmid, IPC_RMID, NULL)</span>to delete it to avoid resource leaks.
Conclusion
This article introduced basic shared memory programming techniques in C, including how to create, attach, detach, and delete shared memory. Through this simple example, you should be able to understand how to utilize shared memory for data exchange between different processes. In practical applications, shared memory is often combined with other synchronization mechanisms (such as semaphores) to ensure data consistency in multi-threaded or multi-process environments. I hope this article is helpful to you!