0

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
BhavinT
  • 338
  • 1
  • 10
  • Possible duplicates: https://stackoverflow.com/q/37640139/3185968 https://stackoverflow.com/q/4298678/3185968 https://stackoverflow.com/q/48117670/3185968 https://stackoverflow.com/q/27476316/3185968 https://stackoverflow.com/q/25413868/3185968 – EOF May 22 '20 at 18:18
  • I have tried this but I am not able to debug what I am doing wrong – BhavinT May 22 '20 at 18:22
  • yes it is going in that loop that's why it is printing both the data values Data1 and Data2 are printing together which is in that loop @MohitReddy – BhavinT May 22 '20 at 18:24
  • My bad, yes the parents second child will be entering that second block. I dont know what is the logic of the function ``generateData()`` but the parent executes last elseif() block and you are not updating any variables there. Even though the child updates it wont reflect in parents memory because child has seperate heap, stack, data-section.. etc.. Refer ``fork()`` man page you will understand better. – m0hithreddy May 22 '20 at 18:30
  • For your quick understanding try to update variables in last elseif() block and you see that update in any function the parent calls further. – m0hithreddy May 22 '20 at 18:32
  • for parent I want to acquire both the values how can I save it can you please help me – BhavinT May 22 '20 at 18:35
  • 1
    It seems like your intentions here are computing the DATA1 and DATA2 values parallelly. Though you are successful in computing them you are not able to store them in parents memory. You need to adopt some IPC methods to do so. Best and simple would be pipes. – m0hithreddy May 22 '20 at 18:39
  • Oh got it thankyou this really helped me thankyou so much@MohithReddy – BhavinT May 22 '20 at 18:41

1 Answers1

3

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 
m0hithreddy
  • 1,752
  • 1
  • 10
  • 17