Home > Mobile >  Global variable incrementing in child process but down decrease in parent
Global variable incrementing in child process but down decrease in parent

Time:11-18

Working on a consumer producer problem. During testing i'm having issues with decrementing the globar variable buffcount1 I threw in a print statment to see if the parent loop is functioning and it is but the problem is the value dont decrement

int pid = fork();
    if(pid == 0){
        close(fds[0]);
        while(1){
            
            if(buffcount1 != MAX){
                sem_wait(&empty);
                pthread_mutex_lock(&lock1);
                write(fds[1], &count2, sizeof(count2));
                count1  ;
                buffcount1  ;
                pthread_mutex_unlock(&lock1);
                usleep(((rand() % 5)   1) * 10000);
                printf("count1: %d\t buffcount1: %d\n", count1, buffcount1);
                 sem_post(&full);
            }
            
            
        }
    } else{
        while(1){
            pthread_mutex_lock(&lock1);
            buffcount1--;
            pthread_mutex_unlock(&lock1);
            usleep(((rand() % 5)   1) * 20000);       
        }
    
    }

Tried doing it with and without the mutex among other things but nothing works

CodePudding user response:

This question sounds like you just wanted a basic example of a producer that increments a value, and a consumer that decrements a value with a cycle counter.

There are a few things to point out here. First is that you're using fork() which creates a new process, not a new thread. The two processes would no longer be sharing memory, so both your mutex and semaphore functions are not looking at the same things. What you might be looking for instead is to create pthreads. Here's a simple example of a producer and consumer thread, where the producer increments a work counter and a cycle counter, and the consumer decrements the work counter. Both stop working when the cycle counter gets to 10.

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

struct work
{
    int cycleCount;
    int workCount;
    sem_t lock;
};

void *producer(void *ptr)
{
    struct work *data = (struct work *)ptr;
    int working = 1;
    while (working)
    {
        sem_wait(&data->lock);
        data->workCount  ;
        data->cycleCount  ;

        if (data->cycleCount == 10)
        {
            working = 0;
        }
        sem_post(&data->lock);
    }

    return 0;
}

void *consumer(void *ptr)
{
    struct work *data = (struct work *)ptr;
    int working = 1;
    while (working)
    {
        sem_wait(&data->lock);
        data->workCount--;

        if (data->cycleCount == 10 && data->workCount == 0)
        {
            working = 0;
        }
        sem_post(&data->lock);
    }

    return 0;
}

int main()
{
    pthread_t prod;
    pthread_t cons;
    int prodRet = 0;
    int prodCon = 0;

    struct work data;
    sem_init(&data.lock, 0, 1);
    data.cycleCount = 0;
    data.workCount = 0;

    pthread_create(&prod, 0, producer, (void *)&data);
    pthread_create(&cons, 0, consumer, (void *)&data);

    pthread_join(prod, (void *)&prodRet);
    pthread_join(cons, (void *)&prodCon);

    printf("cycleCount: %d  ;  workCount: %d\n", data.cycleCount, data.workCount);
    return 0;
}

Here, in main, we init a semaphore, and then pthread_create two threads, one producer, and one consumer. We give them both the same work struct as an argument, and both operate on the work struct, using the lock to synchronize access to the work and cycle counters.

  • Related