Created
December 16, 2015 14:46
-
-
Save good5dog5/c382897df3b5d1d5eb19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include <unistd.h> | |
#include<math.h> | |
#include<stdbool.h> | |
#include<pthread.h> | |
#include<sys/types.h> | |
int value = 1; | |
static void *runner (void *param); | |
int main(int argc, char ** argv) | |
{ | |
puts("--------\n\n"); | |
pid_t pid = fork(); | |
if (pid > 0) { | |
printf("A=%d\n", value); | |
} | |
else if (pid==0) { | |
pid_t pid = fork(); | |
if (pid > 0) { | |
printf("B=%d\n", value); | |
} | |
else if (pid==0) { | |
pid_t pid = fork(); | |
pthread_t tid; | |
pthread_attr_t attr; | |
pthread_attr_init(&attr); | |
pthread_create(&tid, &attr, runner, NULL); | |
pthread_join(tid, NULL); | |
if (pid > 0) | |
printf("C=%d\n", value); | |
else | |
printf("D=%d\n", value); | |
} | |
else { | |
exit(1); | |
} | |
} | |
else { | |
exit(1); | |
} | |
return 0; | |
} | |
static void *runner (void *param) | |
{ | |
value += 1; | |
pthread_exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment