Fork_test.cc revision 67a26a69
150da4f87SShuo Chen#include "../Thread.h" 250da4f87SShuo Chen 350da4f87SShuo Chen#include <stdio.h> 450da4f87SShuo Chen#include <sys/types.h> 550da4f87SShuo Chen#include <unistd.h> 650da4f87SShuo Chen 750da4f87SShuo Chen__thread int x = 0; 850da4f87SShuo Chen 950da4f87SShuo Chenvoid print() 1050da4f87SShuo Chen{ 1150da4f87SShuo Chen printf("pid=%d tid=%d x=%d\n", getpid(), muduo::CurrentThread::tid(), x); 1250da4f87SShuo Chen} 1350da4f87SShuo Chen 1450da4f87SShuo Chenint main() 1550da4f87SShuo Chen{ 1650da4f87SShuo Chen printf("parent %d\n", getpid()); 1750da4f87SShuo Chen print(); 1850da4f87SShuo Chen x = 1; 1950da4f87SShuo Chen print(); 2050da4f87SShuo Chen pid_t p = fork(); 2150da4f87SShuo Chen 2250da4f87SShuo Chen if (p == 0) 2350da4f87SShuo Chen { 2450da4f87SShuo Chen printf("chlid %d\n", getpid()); 2550da4f87SShuo Chen // child 2650da4f87SShuo Chen print(); 2750da4f87SShuo Chen x = 2; 2850da4f87SShuo Chen print(); 2950da4f87SShuo Chen 3067a26a69SShuo Chen if (fork() == 0) 3150da4f87SShuo Chen { 3250da4f87SShuo Chen printf("grandchlid %d\n", getpid()); 3350da4f87SShuo Chen print(); 3450da4f87SShuo Chen x = 3; 3550da4f87SShuo Chen print(); 3650da4f87SShuo Chen } 3750da4f87SShuo Chen } 3850da4f87SShuo Chen else 3950da4f87SShuo Chen { 4050da4f87SShuo Chen // parent 4150da4f87SShuo Chen print(); 4250da4f87SShuo Chen } 4350da4f87SShuo Chen} 44