从函数C更新全局变量

时间:2020-05-22 18:13:24

标签: c

我正在编写一个C程序。我定义了一个全局变量,其值将从主函数中更新。但是问题是它没有发生。你能告诉我我在做什么错。这是我的代码。

{1234=110, 4321=11}

我想从generateData函数更新DATA1和DATA2的值。当我尝试打印第三个else if循环的数据时,它向我显示DATA1 = 0,DATA2 = 0

结果:

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

extern int DELAY=10;
int DATA1=0;
int DATA2=0;


int main(){
    generateData(0);
    return 0;
}
void generateData(int x){
    int y=x;
    int p1,p2;
    p1=fork();
    p2=fork();
    if(p1==0 && p2>0){
        for(int i=0;i<3;i++){
            printf("p1:%d\n",i);
            sleep(1);
            if(x==0){
                x=1;
                DATA1=x;
            }
            else if(x==1){
                x=0;
                DATA1=x;
            }
            // printf("%d\n",DATA1);
            // l=DATA1;
            printf("DATA1:%d\n",DATA1);
        }
    }
    else if(p2==0 && p1>0){
        for(int i=0;i<3;i++){
            printf("p2:%d\n",i);
            sleep(10);
            if(y==0){
                y=1;
                DATA2=y;
            }
            else if(y==1){
                y=0;
                DATA2=y;
            }
            // m=DATA2;
            printf("DATA2:%d\n",DATA2);
        }
    }
    else if(p1>0 && p2>0){
        wait(0);
        printf("DATA1=%d, DATA2=%d\n",DATA1,DATA2);
        kill(p1,SIGKILL);
        kill(p2,SIGKILL);
        checkTruthTable(DATA1,DATA2);
    } 
}

int checkTruthTable(int x, int y){
    return 0;
}

1 个答案:

答案 0 :(得分:3)

尽管您已经成功计算了DATA1DATA2,但是它们并没有在父级内存中进行更新。有关更多详细信息,请参见fork()手册页。您需要采用某些IPC方法将数据从子级返回到父级。下面的代码使用pipe()来实现。

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

int DATA1 = 0;
int DATA2 = 0;

void generateData()
{
    int fd1[2];
    pipe(fd1);

    pid_t fk1_return = fork();

    if (fk1_return == 0) {  // child 1 runs this.
        close(fd1[0]);  // Close read descriptor in child1;

        /* Compute DATA1 */
        DATA1 = 1;

        write(fd1[1], &DATA1, sizeof(int));
        close(fd1[1]);
        exit(0);
    }
    else if (fk1_return > 0) {  // parent runs this.
        int fd2[2];
        pipe(fd2);

        pid_t fk2_return = fork();

        if (fk2_return == 0) {  // child 2 runs this
            close(fd2[0]); // Close read descriptor in child 2;

            /* Compute DATA2 */
            DATA2 = 2;

            write(fd2[1], &DATA2, sizeof(int));
            close(fd2[1]);
            exit(0);
        }
        else if (fk2_return > 0) {  // parent runs this
            close(fd1[1]); // close write descriptor in parent
            close(fd2[1]); // close write descriptor in parent

            read(fd1[0],&DATA1, sizeof(int));   // Assuming partial reads wont happen
            waitpid(fk1_return, NULL, 0);   // Release child1 resources;
            close(fd1[0]);

            read(fd2[0], &DATA2, sizeof(int));
            waitpid(fk2_return, NULL, 0);   // Release child2 resources;
            close(fd2[0]);
        }
    }
}

int main() {
    generateData();
    printf("%d %d\n", DATA1, DATA2);

    return 0;
}

输出

1 2 
相关问题