antonpuz
antonpuz

Reputation: 3316

context switch between two processes in linux

i am writing a program that sumulates a terminal, i wrote another dummy progra which only takes input and print it out. the dummy prog:

int main(int argc, char *argv[]) {
char text[128] = {0};
while(1){
    fgets (text, 128, stdin);
    printf("%s\n", text);
}
return 0;
}

in my main program i run the dummy program with fork->exec. my question is which one of them gets the input from the user? is there context switch between them so one process takes the input at a time or the father process will take all the input unless i call wait.

EDIT: The input will not be shared.

then i want to call the child process to the foreground i try to use tcsetpgrp but with no success. i do the following:

if((son = fork())==0){//son process
}else{//father
printf("the old group id is:%d\n", getpgid(son));
setpgid(son,son);
k = printf("the new group id is:%d\n",getpgid(son));
j = tcgetpgrp(0, getpgid(son))
}

j is set to 25 for some reason and both the pgid before the setpgrp and after is the same.

Upvotes: 0

Views: 969

Answers (1)

Atle
Atle

Reputation: 1877

The main program is connected to the terminal and gets the input. If you use threading instead of creating a fork, the I/O will be shared between the threads.

If only one thread reads, it gets all the input. If two or more threads read, they do not share the data, and the outcome is kindof unpredictable.

Upvotes: 1

Related Questions