add shm_open example
This commit is contained in:
parent
e4e5710eef
commit
ea173678f2
20
shm_open/build.zig
Normal file
20
shm_open/build.zig
Normal file
@ -0,0 +1,20 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "shm-open-example",
|
||||
.root_source_file = .{ .path = "main.c" },
|
||||
.optimize = optimize,
|
||||
.target = target
|
||||
});
|
||||
exe.linkLibC();
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
const run_step = b.step("run", "Run simple program");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
b.installArtifact(exe);
|
||||
}
|
125
shm_open/main.c
Normal file
125
shm_open/main.c
Normal file
@ -0,0 +1,125 @@
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define SHM_NAME "/shared_memory"
|
||||
|
||||
struct shared_data {
|
||||
pthread_mutex_t mutex;
|
||||
int counter;
|
||||
};
|
||||
|
||||
struct shm_shared_data {
|
||||
int shm_fd;
|
||||
struct shared_data *shared_data;
|
||||
};
|
||||
|
||||
void increment_counter(struct shared_data *data) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
if (pthread_mutex_lock(&data->mutex) != 0) {
|
||||
perror("pthread_mutex_lock");
|
||||
}
|
||||
|
||||
data->counter++;
|
||||
|
||||
if (pthread_mutex_unlock(&data->mutex) != 0) {
|
||||
perror("pthread_mutex_unlock");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct shm_shared_data shared_data_open() {
|
||||
struct shared_data *shared_data;
|
||||
|
||||
// Create or open a shared memory object
|
||||
int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
if (shm_fd == -1) {
|
||||
perror("shm_open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int shm_size = sizeof(struct shared_data);
|
||||
|
||||
if (ftruncate(shm_fd, shm_size) == -1) {
|
||||
perror("ftruncate");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Map the shared memory object into the process's address space
|
||||
shared_data = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
|
||||
if (shared_data == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct shm_shared_data shm_shared_data = {
|
||||
.shm_fd = shm_fd,
|
||||
.shared_data = shared_data
|
||||
};
|
||||
|
||||
return shm_shared_data;
|
||||
}
|
||||
|
||||
void shared_data_close(struct shm_shared_data shm_data) {
|
||||
// Unmap the shared memory and close the file descriptor
|
||||
if (munmap(shm_data.shared_data, sizeof(struct shared_data)) == -1) {
|
||||
perror("munmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (close(shm_data.shm_fd) == -1) {
|
||||
perror("close");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void *thread_callback(void *p) {
|
||||
struct shm_shared_data shm_data = shared_data_open();
|
||||
|
||||
increment_counter(shm_data.shared_data);
|
||||
|
||||
shared_data_close(shm_data);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct shm_shared_data shm_data = shared_data_open();
|
||||
|
||||
pthread_mutexattr_t mutex_attr;
|
||||
pthread_mutexattr_init(&mutex_attr);
|
||||
pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
|
||||
|
||||
if (pthread_mutex_init(&shm_data.shared_data->mutex, &mutex_attr) < 0) {
|
||||
printf("Failed to initialise mutex\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pthread_t thread1;
|
||||
pthread_create(&thread1, NULL, thread_callback, NULL);
|
||||
|
||||
pthread_t thread2;
|
||||
pthread_create(&thread2, NULL, thread_callback, NULL);
|
||||
|
||||
pthread_join(thread1, NULL);
|
||||
pthread_join(thread2, NULL);
|
||||
|
||||
printf("Counter is %d\n", shm_data.shared_data->counter);
|
||||
|
||||
shared_data_close(shm_data);
|
||||
|
||||
// Unlink the shared memory object (aka delete shared memory)
|
||||
if (shm_unlink(SHM_NAME) == -1) {
|
||||
perror("shm_unlink");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user