1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /*shared structure between the two processes */ struct share_both { unsigned int num_proc1; unsigned int num_proc2; }; /* * The parent process clones a thread by sharing its memory space. The parent * process just toggles the value of num_proc1 in loop. */ /* Declare local shared data */ struct share_both common; /* clones a thread with memory space same as parent thread*/ if ((pid = clone(func1, buff+8188, CLONE_VM, &common)) < 0) { perror("clone"); exit(1); } /* toggles the value of num_proc1 in loop */ for (j = 0; j < 200; j++) for(i = 0; i < 100000; i++) { if (common.num_proc2) common.num_proc1 = 0; else common.num_proc1 = 1; } /* * The function below is called by the cloned thread, which just toggles the * value of num_proc2 every time in the loop. */ int func1(struct share_both *com) { int i, j; /* toggles the value of num_proc2 in loop */ for (j = 0; j < 200; j++) for (i = 0; i < 100000; i++) { if (com->num_proc1) com->num_proc2 = 0; else com->num_proc2 = 1; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |