1

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
8
  • I have tried this but I am not able to debug what I am doing wrong Commented May 22, 2020 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 Commented May 22, 2020 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. Commented May 22, 2020 at 18:30
  • 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. Commented May 22, 2020 at 18:39

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.