some fixes ig

This commit is contained in:
David H
2026-06-10 15:35:03 +02:00
parent 838354c23a
commit 6307ec0c7c
3 changed files with 22 additions and 7 deletions
+4 -2
View File
@@ -7,6 +7,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
SharedData *shared_data;
int shm_fd;
@@ -15,7 +16,7 @@ int c_shm_init() {
printf("Client connecting to server...\n");
/* open shm segment */
shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
shm_fd = shm_open(SHM_NAME, O_RDWR, 0664);
if (shm_fd < 0) {
perror("shm_open");
printf("[ERROR] Make sure that a shared memory server is up and running!\n");
@@ -43,8 +44,9 @@ void c_shm_send(int shm_fd, const char* message) {
/* message is ready */
shared_data->ready = 1;
gettimeofday(&shared_data->start, NULL);
printf("Sent: %s\n", message);
/* Wait for response */
while (shared_data->ready == 1) {
sleep(1);
+13 -1
View File
@@ -7,16 +7,19 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
SharedData *shared_data;
int shm_fd;
int s_shm_init() {
/* remove seg if it already is there */
shm_unlink(SHM_NAME);
/* create a shm segment */
shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0664);
if (shm_fd < 0) {
perror("shm_open");
exit(1);
@@ -49,13 +52,22 @@ int s_shm_init() {
void s_shm_accept(int shm_fd) {
/* wait for clinet to set flag */
while (shared_data->ready == 0) {
sleep(1);
}
printf("Client connected\n");
printf("Received: %s\n", shared_data->message);
struct timeval end;
gettimeofday(&end, NULL);
long elapsed = (end.tv_sec - shared_data->start.tv_sec) * 1000000 + (end.tv_usec - shared_data->start.tv_usec);
printf("Read time: %ld microseconds\n", elapsed);
/* respond */
memset(shared_data->message, 0, BUFFER_SIZE);
strncpy(shared_data->message, "Message received!", BUFFER_SIZE - 1);
+5 -4
View File
@@ -1,7 +1,8 @@
#ifndef PROTOCOLS_H
#define PROTOCOLS_H
#include <sys/time.h>
/*buffer size is for any icp same (why? -> bcs i decided it to be so)*/
/*buffer size */
#define BUFFER_SIZE 256
/*defs for unix socket example*/
@@ -13,12 +14,12 @@
#define FIFO_CLIENT_TO_SERVER "/tmp/fifo_client_to_server"
/*defs for shared mem*/
#define SHM_NAME "/posix_shm_example"
#define SHM_NAME "/shmem_lab"
typedef struct {
char message[BUFFER_SIZE];
char message[65535];
int ready;
struct timeval start;
} SharedData;
#endif