步骤1:同上一节

步骤2:建立目录

  cd workdir/linux/application

  mkdir -p 9-process/fork

步骤3:复制

  cp  /mnt/hgfs/share/2.Linux实验部分/09.Linux系统fork等系统实验调用实验/实验代码/93.c 9-process/fork -a

步骤4:执行

  gcc 93.c -o fork

  ./fork

附:程序源码

  93.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/wait.h>
 4 #include <unistd.h>
 5 #include <stdlib.h>
 6 #include <fcntl.h>
 7 
 8 int    global=22;
 9 char    buf[]="the test content!\n";
10 
11 int main(void)
12 {    
13     int test=0,stat;
14     pid_t pid;
15     if(write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf))
16     {  perror("write error!"); }
17     printf(" fork test!\n");
18     /* fork */
19     pid = fork();   /*we should check the error*/
20     if (pid == -1)
21     {
22         perror("fork");
23         exit(0);    
24     }
25     else if (pid == 0)
26     {
27         global++;   test++;
28         printf("global=%d test=%d Child,my PID is %d\n",global,test,getpid());
29         exit(0);
30     }
31     /*else be the parent*/
32     global+=2;
33     test+=2;
34     printf("global=%d test=%d Parent,my PID is %d\n",global,test,getpid());
35     exit(0);
36     //printf("global=%d test=%d Parent,my PID is %d",global,test,getpid());
37     //_exit(0);
38 }
39     

 

posted on 2018-08-01 20:30  kingofloong  阅读(334)  评论(0)    收藏  举报