BhavinT
BhavinT

Reputation: 332

Update global variable from function C

I am writing a C program. I have defined a global variable whose value will be updated from the main function. But the problem is that it is not happening. Can you please tell me what am I doing wrong. Here is my code.

#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;
}

I want to update value of DATA1 and DATA2 from generateData function.When I am trying to print the data for the third else if loop it is showing me DATA1=0, DATA2=0

Result:

p2:0
p1:0
DATA1:1
p1:1
DATA1:0
p1:2
DATA1:1
DATA1=0, DATA2=0

Upvotes: 1

Views: 1394

Answers (1)

m0hithreddy
m0hithreddy

Reputation: 1829

Though you are successful in computing DATA1 and DATA2, they are not being updated in parents memory. Refer to fork() man page for additional details. You to need to adopt some IPC approaches to return back the data from child to parent. Below code uses pipe() to achieve this.

#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;
}

OUTPUT

1 2 

Upvotes: 3

Related Questions