Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i run these two proceses normally in c language?

this code works fine but consumer process does not work, i tried to get producer_pid value and check what is going on but when i write the printf("%d\n",producer_pid) it gives two values when do not put \n it gives one value. i am new to all computer programming i am educating myself, if the question is so bad i am sorry

i think producer process is not ending any idea that could help me?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUFFER_SIZE 2

int buffer[BUFFER_SIZE];
int sayac = 0;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void produce(int item) {
    buffer[sayac] = item;
    sayac++;
}

int consume() {
    int item = buffer[sayac - 1];
    sayac--;
    return item;
}

void *producer_thread(void *arg) {
    int *pipe_fd = (int *)arg;

    for (int i = 1; i <= 10; i++) {
        pthread_mutex_lock(&mutex);
        while (sayac == BUFFER_SIZE) {
            pthread_mutex_unlock(&mutex);
            usleep(100000);  // Kısa bir süre bekleyerek döngüyü serbest bırak
            pthread_mutex_lock(&mutex);
        }

        produce(i);
        printf("Ăśretici: %d ĂĽretildi.\n", i);

        write(pipe_fd[1], &i, sizeof(i));

        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
    close(pipe_fd[1]);
    return NULL;
}

void *consumer_thread(void *arg) {
    int *pipe_fd = (int *)arg;

    for (int i = 1; i <= 10; i++) {
        pthread_mutex_lock(&mutex);
        while (sayac == 0) {
            pthread_mutex_unlock(&mutex);
            usleep(100000);  // Kısa bir süre bekleyerek döngüyü serbest bırak
            pthread_mutex_lock(&mutex);
        }

        int item = consume();
        printf("TĂĽketici: %d tĂĽketildi.\n", item);

        int received_data;
        read(pipe_fd[0], &received_data, sizeof(received_data));

        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
    close(pipe_fd[0]);
    return NULL;
}

int main() {
    int pipe_fd[2];
    if (pipe(pipe_fd) == -1) {
        perror("Pipe oluşturulamadı");
        return 1;
    }

    pid_t producer_pid = fork();
    printf("%d ---------------",producer_pid);

    if (producer_pid == 0) {
        // Yavru ĂĽretici iĹźlem kodu
        close(pipe_fd[0]); // Okuma tarafını kapat
        producer_thread((void *)pipe_fd);
        exit(0);
    } else if (producer_pid > 0) {
        pid_t consumer_pid = fork();

        if (consumer_pid == 0) {
            // Yavru tĂĽketici iĹźlem kodu
            close(pipe_fd[1]); // Yazma tarafını kapat
            consumer_thread((void *)pipe_fd);
            exit(0);
        } else if (consumer_pid > 0) {
            // Ana iĹźlem kodu
            close(pipe_fd[0]); // Okuma tarafını kapat
            close(pipe_fd[1]); // Yazma tarafını kapat

            wait(NULL);
            wait(NULL);

            printf("Ana işlem tamamlandı.\n");
        } else {
            fprintf(stderr, "Tüketici işlem oluşturulamadı.\n");
            return 1;
        }
    } else {
        fprintf(stderr, "Üretici işlem oluşturulamadı.\n");
        return 1;
    }

    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the main problem here is that you’re opening a pthread mutex to sync between processes opened using fork().

pthread_mutex, as its name would suggest, is designed to sync between threads.
It will not be able to sync between processes created using fork since the processes will have a different memory space.

For your implementation, there isn’t much of a point to even use pthread_mutex. write() and read() are blocking, so you can have both processes sync by using the pipe.

On a different subject, when you use printf(), sometimes the OS will wait and not print all of the log. use fflush() to see all your logs.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading