简体   繁体   English

通过子进程和未命名的 posix 信号量增加计数器变量不起作用

[英]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我正在尝试从主进程创建 4 个子进程,然后子进程将 go 进入关键区域锁定它并增加变量计数器

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. PS:我已经阅读了一些关于未命名信号量的帖子,并看到了一些关于堆栈溢出的帖子。 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我得到的 output 是 1 我期待的 output 是 5

You need to research how fork works and shared memory .您需要研究fork的工作原理和共享 memory Separate processes get separate memory.单独的进程得到单独的 memory。 While copies of some things from the parent are made to the child process, they do not share the same memory.虽然从父进程复制一些东西到子进程,但它们不共享相同的 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.当您创建一个子进程时,该子进程拥有所有变量的副本,这意味着它们使用的 memory 空间对于每个进程都是不同的。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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