简体   繁体   English

C叉区别父子

[英]C fork distinguish between father and sons

My problem is the stock doesnt change i think there is something wrong in the if statement pid[i] == 0. I doenst get the prints from the "father process part" of my code only from the childs. 我的问题是库存没有改变,我认为if语句pid [i] == 0中出了点问题。

#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/types.h>

#define NUM_CHILDS 3
#define LOOPS 6
#define FILLING_UP 20
#define SHMSEGSIZE sizeof(int)

int main() {
        int shmID1, shmID2, *stock, *flag, loop_i, pid[NUM_CHILDS], i;
        loop_i = 1;
        shmID1 = shmget(IPC_PRIVATE, SHMSEGSIZE, IPC_CREAT | 0644);
        shmID2 = shmget(IPC_PRIVATE, SHMSEGSIZE, IPC_CREAT | 0644);
        stock = (int *) shmat(shmID1, 0, 0);
        flag = (int *) shmat(shmID2, 0, 0);
        *stock = 20;
        *flag = 1;

        for (i = 0; i < NUM_CHILDS; i++) {
                pid[i] = fork();
                if(pid[i] == -1) {
                        printf("error by crating a child!\n\n");
                        return -1;
                }
                if (pid[i] == 0) {
                        printf("Child %d: %d", i, pid[i]);

                        while(*flag==1) {
                                if(*stock>0) {
                                        *stock--;
                                        usleep(100000);
                                }
                        }
                        shmdt(flag);
                        shmdt(stock);
                        return 0;

                }
                else {
                        while(loop_i <= LOOPS) {
                                usleep(100000);
                                printf("Actual stock: %d\n", *stock);
                                if(*stock<=0) {
                                        *stock += FILLING_UP;
                                        loop_i++;
                                        printf("Stock is filled up");
                                }
                        }
                        *flag = 0;
                }
        }

        for (i = 0; i < NUM_CHILDS; i++) {
                waitpid(pid[i], NULL, 0);
        }
        printf("Programm ends", LOOPS, *stock);
        shmdt(flag);
        shmdt(stock);
        shmctl(shmID1, IPC_RMID, 0);
        shmctl(shmID2, IPC_RMID, 0);
        return 0;
}

You should reset loop_i to 1. Otherwise the while loop in the parent will run LOOPS times for the first child and 0 times for the other children. 您应该将loop_i重置为1。否则,父级中的while循环将对第一个孩子运行LOOPS次,对其他孩子运行0次。

loop_i = 1;
while(loop_i <= LOOPS) {
    ...
}

The fork() in Linux is used to create new process. Linux中的fork()用于创建新进程。 Also after forking, it returns 0 in child process and returns pid of child process in parent process. 同样在分叉之后,它在子进程中返回0,并在父进程中返回子进程的pid。 So in parent process pid!=0. 因此在父进程中pid!= 0。 Hence the statement inside the if(pid==0) will not execute in parent process. 因此,if(pid == 0)内部的语句将不会在父进程中执行。

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

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