Two Ways To Share
void *bump(void *arg) { *(int *)arg = 2; return NULL; }
int main(void) {
int *buf = malloc(sizeof(int));
*buf = 0;
if (fork() == 0) { *buf = 1; _exit(0); }
wait(NULL);
printf("after fork: %d\n", *buf);
pthread_t t;
pthread_create(&t, NULL, bump, buf);
pthread_join(t, NULL);
printf("after thread: %d\n", *buf);
}
What does this print?
Sign in to answer questions and track your progress
Sign In