The program needs to take ten inputs, this is too happen simultaneously while other processes happen, using fork(). Instead it just goes "Enter input:

Enter input: Enter input: Enter input:Enter input: Enter input: Enter input: Enter input: ..."


int main(int argc, char * argv[]) 
{ 

int n1 = fork();
int n2 = fork();
    if (n1 == 0 && n2 > 0 ){
       getMarks();
                            }
    else if (n1 > 0 && n2 == 0) { 
        printf("child2")
                                }

    else if (n1 > 0 && n2 > 0){
     printf("Parent");
                               }
    else {printf("Child3");
         }



}

void getMarks(){
    printf("Enter ten marks: \n");
    int i = 0;
    int highestMark = 0;
    for (i; i < 10; i++){
        int num = 0;
        printf("Enter Mark: ");
        scanf("%d", &num);
        if (num > highestMark) { 
            highestMark = num; }

    }   
    printf("Highest Mark is : %d", highestMark);

}

The expected result would be for the other childs and parent to print into the console, while also being able to give input into getMarks()

1 Answers

0
Joshua On

Uggh don't do that then.

Your first character of entry got sent to the shell. Your second character of entry got sent to the program. scanf can't recover from input errors very well.

To avoid this problem, don't read standard input from child processes whose parent isn't alive anymore. Arrange your code so this doesn't ever happen.