简体   繁体   中英

Increasing Counter variable by child process and unnamed posix semaphore is not working

I am trying to create 4 child processes from the main process and in turn the child process will go into critical region lock it and increase the variable counter

when all processes end final counter is printed

in the given code below i am unable to increase the counter

PS: I have read some posts on unnamed semaphores and have seen some posts on stack overflow. either they don't resolve my issue or they are examples of threads and semaphores and not child processes and semaphores

Also i insist on using unnamed semaphore since i am new to this i might be doing some foolish mistake.

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

sem_t mutex;
int counter = 1;


void child(int id){
    // critical region
    sem_wait(&mutex);

    printf("Counter right now for %d is %d\n" , id, counter);
    printf("Increasing counter for %d\n", id);
    (counter)++;
    printf("Counter now for %d is %d\n", id, counter);

    sem_post(&mutex);
}

int main(){
    pid_t pid;
    sem_init(&mutex, 1, 1);

    for(int kid = 1; kid < 5; kid++){
        pid = fork();
        if(pid==0){
            child(kid);
            printf("Counter after child %d is %d\n\n", kid, counter);
            exit(0);
        }
        else{
            printf("parent process %d with counter = %d\n\n", kid, counter);
        }
    }

    int status;
    for(int kid=1; kid<5; kid++){
        wait(&status);
    }

    printf("final counter is %d\n", counter);
    sem_destroy(&mutex);
}

the output i am getting is 1 the output i am expecting is 5

You need to research how fork works and shared memory . Separate processes get separate memory. While copies of some things from the parent are made to the child process, they do not share the same memory.

When you create a child process, that child has a copy of all the variables, that means that the memory space they are using are different for each proccess. So when you are doing child(kid); all the childs will enter here and all of them will increase his own variable. You can try to create threads instead of create new processes

I have written this solution for you, using threads:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
int  counter = 0;
sem_t mutex;

void *increment(void *arg) {
    sem_wait(&mutex);
    int *val;
    val = (int *) arg;
    printf("Counter right now for %d is %d\n" , *val, counter);
    printf("Increasing counter for %d\n", *val);
    counter++;
    printf("Counter now for %d is %d\n", *val, counter);
    sem_post(&mutex);
}
int  main() {
    int i;
    sem_init(&mutex, 0, 1);
    pthread_t  h;
    for (i = 1 ; i < 6 ; i++ ) {
        pthread_create(&h, NULL , increment , &i);
        pthread_join(h, NULL);
    }
    printf ("Final value of counter: %d\n", counter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM