Skip to content

Instantly share code, notes, and snippets.

@good5dog5
Created December 16, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save good5dog5/c382897df3b5d1d5eb19 to your computer and use it in GitHub Desktop.
Save good5dog5/c382897df3b5d1d5eb19 to your computer and use it in GitHub Desktop.
#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