Strange output in a synchronization problem using binary semaphores in C

28 views Asked by At

Recently i came across this exercise that for some reason always gives me strange output, here is the code followed by an output example:

HERE'S THE CONSUMER PROCESS

#include <stdio.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include "sem.h"

#define nome_file "consumator_productor.txt"
#define DIM 1048
#define SEM_KEY_WRITE (key_t)1234 
#define SEM_KEY_READ (key_t)5678

int SEM_P(int);
int SEM_V(int);
int SEM_DEL(int);
int SEM_SET(int, int);

int main(void){
    int running = 1;
    int fd, WRITE_ID, READ_ID, c;
    char buffer[DIM]; //needed variables
    
    if((WRITE_ID = semget(SEM_KEY_WRITE, 1, IPC_CREAT | 0666)) == -1) exit(EXIT_FAILURE);
    SEM_SET(WRITE_ID, 1);
    if((READ_ID = semget(SEM_KEY_READ, 1, IPC_CREAT | 0666))== -1) exit(EXIT_FAILURE);
    SEM_SET(READ_ID, 0); //dichiaro e inizializzo i semafori

    while(running){
        if(SEM_P(READ_ID) == -1) exit(EXIT_FAILURE); //aspetto il via alla lettura
        fd = open(nome_file, O_RDONLY, 0440);
        read(fd, buffer, sizeof(buffer));
        close(fd);
        if(strncmp(buffer, "end", 3) == 0){ //controllo se trovo end
            running = 0;
        } else {
            printf("%s\n", buffer);
        }
        if(SEM_V(WRITE_ID) == -1) exit(EXIT_FAILURE); //do il via alla scrittura
    }
    if(SEM_DEL(WRITE_ID) == -1) exit(EXIT_FAILURE);
    if(SEM_DEL(READ_ID) == -1) exit(EXIT_FAILURE);
    exit(EXIT_SUCCESS);
}

HERE'S THE PRODUCTOR PROCESS:

#include <stdio.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include "sem.h"

#define nome_file "consumator_productor.txt"
#define DIM 1048
#define SEM_KEY_WRITE (key_t)1234 
#define SEM_KEY_READ (key_t)5678

int SEM_P(int);
int SEM_V(int);
int SEM_DEL(int);
int SEM_SET(int, int);

int main(void){
    int running = 1;
    int fd, WRITE_ID, READ_ID;
    char buffer[DIM];

    if((WRITE_ID = semget(SEM_KEY_WRITE, 1, IPC_CREAT | 0666)) == -1) exit(EXIT_FAILURE);
    if((READ_ID = semget(SEM_KEY_READ, 1, IPC_CREAT | 0666)) == -1) exit(EXIT_FAILURE);
    

    while(running){
        if(SEM_P(WRITE_ID) == -1) exit(EXIT_FAILURE); //aspetto il via alla scrittura
        fd = open(nome_file, O_CREAT | O_TRUNC | O_WRONLY, 0660);
        printf("Write on your file:\n");
        fgets(buffer, sizeof(buffer), stdin);
        write(fd, buffer, strlen(buffer));
        if(strncmp(buffer, "end", 3) == 0){
            running = 0;
        }
        close(fd);
        if(SEM_V(READ_ID) == -1) exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

HERE'S THE CONSUMATOR OUTPUT:

os@debian:~/Esercizi/semafori/libreria$ ./consumator
Ciao, ciao e ciao // i wrote it using productor process
// i didn't
Mia, mio e miei //i wrote it
o //i didn't
//i didn't
Ok ora basta. //i wrote it
i //i didn't
o //i didn't


I tried executing the 2 processes multiple times and each time they gave me a different strange output.

0

There are 0 answers