I know fork process does not share memory, and threads do, but then how can forked processes communicate one another?
Here is example, where one version with thread is commented out (and that version will end), and the other version with fork will never ends. The code is relying on the global variable done
:
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
bool done = false;
void *foo(void *arg){
sleep(1);
done = true;
return 0;
}
int main(){
//pthread_t t1;
//pthread_create(&t1, NULL, foo, NULL);
//
//printf("waiting...\n");
//while(!done){}
//printf("Ok. Moving on.\n");
printf("waiting...\n");
if(!fork()){
foo(NULL);
} else {
while(!done){}
printf("OK. moving on.\n");
}
}
So if forked processes do not share data (i.e. global variables?) unlike threads, how do they otherwise communicate in unix?
EDIT: this is definitely not a duplicate as I already seen similar topics like Forking vs Threading and other documents about fork/threads in *nix. I just want to know use cases of both. (e.g windows has no fork, only threads, so they probably had different use cases in mind?)