I have some code in C which works perfectly on MacOS. Shortly, I've a process that takes a mutex, writes on a pipe, unlocks the mutex and send a signal to a condition variable. Then, there's another process which listens to this pipe and whenever gets a signal, reads the buffer and writes the content on a local file.
The problem is that the exact same code, doesn't work on Linux Ubuntu.
I tried to change the implementation of mutex and condition variable, following tips around the Internet, but didn't work.
Here some code for a better explanation:
Firstly, I initialize mutex and condition as process shared
pthread_mutexattr_t mutexAttr = {};
pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&mutex, &mutexAttr);
pthread_condattr_t condAttr = {};
pthread_condattr_setpshared(&condAttr, PTHREAD_PROCESS_SHARED);
pthread_cond_init(&condition, &condAttr);
This is the function which runs on the process that reads the pipe and writes to the file
char buffer[8192];
while(1) {
bzero(buffer, sizeof buffer);
pthread_cond_wait(&condition, &mutex);
pthread_mutex_lock(&mutex);
read(pipe_fd[0], buffer, sizeof buffer);
_log(buffer);
pthread_mutex_unlock(&mutex);
}
And finally this is the code which runs on another process and writes on the pipe
char *buffer = malloc(SIZE);
sprintf(buffer, "name: %s | size: %d | ip: %s | server_port: %d\n", name, size, client_ip, port);
pthread_mutex_lock(&mutex);
if (write(pipe_fd[1], buffer, strlen(buffer)) < 0) {
pthread_mutex_unlock(&mutex);
free(buffer);
return -1;
}
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condition);
Seems like on Ubuntu when I call pthread_cond_signal(&condition) it doesn't return errors but it also doesn't trigger the condition variable, so the process calling pthread_cond_wait(&condition, &mutex) never passes.
The stranger thing is that mutex is working good instead, pipe too.