I have a reader-writer shared memory in linux problem, i wrote the code in c

This is the writer code:

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

#define TRUE 1
#define FALSE 0

struct SHM_SIZE {
    char str[512];
    int flag, readcount;
};

sem_t *resourcemutex;
int shmid, EXIT;
void sigint_handler(int sig);
struct SHM_SIZE *ptr;

int main() {
    char str[512], ss[512];
    key_t key;
    key = 1053;
    EXIT=FALSE;

    if (signal(SIGINT, sigint_handler) == SIG_ERR) {
        perror("SIGQUIT");
        exit(1);
    }

    resourcemutex = sem_open("PRAK5-3RESC", O_CREAT, 0644, 1);
    if (resourcemutex == SEM_FAILED) {
        perror("unable to execute semaphore");
        sem_unlink("PRAK5-3RESC");
        exit(-1);
    }

    shmid = shmget(key, sizeof(struct SHM_SIZE), IPC_CREAT|0644);
    if (shmid < 0) {
        perror("shmget error...");
        exit(-1);
    }

    ptr = shmat(shmid, NULL, 0);
    
    int i = 0;

    while (EXIT == FALSE) {
        printf("Masukkan Kata/kalimat : ");
        scanf("%[^\n]", ss); getchar(); //The input to send to resourcemutex
        fflush(stdin);
        sem_wait(resourcemutex);
        strcpy(ptr->str, ss);
        ptr->flag = 1;
        sem_post(resourcemutex);
        i++;

        }
    sem_close(resourcemutex);

    return 0;
}

void sigint_handler(int sig) {
    EXIT = TRUE;  //to close resourcemutex
    
    sem_close(resourcemutex);
    sem_unlink("PRAK5-3RESC");
}

This is the reader code:

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

#define TRUE 1
#define FALSE 0

struct SHM_SIZE {
    char str[512];
    int flag, readcount;
};

int EXIT;
int shmid;
sem_t *readermutex, *resourcemutex;
struct SHM_SIZE *ptr;

void sigint_handler(int sig);

int main() {
    int i, j;
    char ss[512], str[512];
    key_t key;
    EXIT=FALSE;

    if (signal(SIGINT, sigint_handler) == SIG_ERR) {
        perror("SIGQUIT");
        exit(1);
    }

    readermutex = sem_open("PRAK5-3READ", O_CREAT, 0644, 1);
    if (readermutex == SEM_FAILED) {
        perror("unable to create semaphore");
        sem_unlink("PRAK5-3READ");
        exit(-1);
    }

    resourcemutex = sem_open("PRAK5-3RESC", O_CREAT, 0644, 1);
    if (resourcemutex == SEM_FAILED) {
        perror("unable to create semaphore");
        sem_unlink("PRAK5-3READ");
        exit(-1);
    }

    shmid = shmget(key, sizeof(struct SHM_SIZE), IPC_CREAT | 0644);
    if (shmid < 0) {
        perror("failure in shmget");
        exit(-1);
    }

    ptr = shmat(shmid, NULL, 0);

    while (EXIT == FALSE) {
        sem_wait(resourcemutex);
        if (ptr->flag == 0) {
            usleep(1000);
            continue;
        } else {
            printf("Reading %s\n", ptr->str);
            ptr->flag = 0;
        }
        sem_post(resourcemutex);
    }
    return 0;
}

void sigint_handler(int sig) { //close the mutex
    EXIT = TRUE;

    sem_close(readermutex);
    sem_close(resourcemutex);
    sem_unlink("PRAK5-3READ");//close the mutex
    sem_unlink("PRAK5-3RESC");//close the mutex
    shmdt(ptr);
    shmctl(shmid, IPC_RMID, NULL);
    exit(0);
}

To explain more further, the code have to be like this: Terminal 1 (writer): Please enter the words and the sentence: whatever i put in here should print to resourcemutex, the reader code should read the resourcemutex and print it to readermutex so that it can be printed to the linux screen.

Terminal 2 (reader): Whatever i write in terminal 1 should be printed here

It's like a shared memory using mutex, but idk it's not working.

The result: My Result

i don't know why it can't be printed in the reader and there is a problem when u want to execute the write program its always failed and return some segmentation fault (core) or smth

Upvotes: 0

Views: 43

Answers (0)

Related Questions