From 15b0378545ba2ed703d792057b4658dc043e8bf6 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sat, 6 Jan 2024 21:22:15 +0200 Subject: [PATCH] add shmat example --- shm_open/main.c | 3 -- shmat/build.zig | 20 +++++++++ shmat/main.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 shmat/build.zig create mode 100644 shmat/main.c diff --git a/shm_open/main.c b/shm_open/main.c index 8f283a9..fe832a4 100644 --- a/shm_open/main.c +++ b/shm_open/main.c @@ -1,12 +1,9 @@ -#include #include #include #include #include -#include #include #include -#include #include #define SHM_NAME "/shared_memory" diff --git a/shmat/build.zig b/shmat/build.zig new file mode 100644 index 0000000..b0902c7 --- /dev/null +++ b/shmat/build.zig @@ -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 = "shmat-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); +} diff --git a/shmat/main.c b/shmat/main.c new file mode 100644 index 0000000..79418c1 --- /dev/null +++ b/shmat/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +struct shared_data { + pthread_mutex_t mutex; + int counter; +}; + +struct shm_shared_data { + int shm_id; + 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() { + key_t key = ftok("shmfile", 65); // Generate a key for the shared memory segment + + // Create a shared memory segment + int shm_id = shmget(key, sizeof(struct shared_data), 0666 | IPC_CREAT); + if (shm_id == -1) { + perror("shmget"); + exit(EXIT_FAILURE); + } + + // Attach the shared memory segment to the address space of the process + struct shared_data *shared_data = shmat(shm_id, NULL, 0); + if (shared_data == (void*)-1) { + perror("shmat"); + exit(EXIT_FAILURE); + } + + struct shm_shared_data shm_shared_data = { + .shm_id = shm_id, + .shared_data = shared_data + }; + + return shm_shared_data; +} + +void shared_data_close(struct shm_shared_data shm_data) { + // Detach the shared memory segment + if (shmdt(shm_data.shared_data) == -1) { + perror("shmdt"); + 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); + + // Remove the shared memory segment + if (shmctl(shm_data.shm_id, IPC_RMID, NULL) == -1) { + perror("shmctl"); + exit(EXIT_FAILURE); + } + + return 0; +}