函数fork

博文链接:

1. 代码示例:

 1 #include "apue.h"
 2 int glob = 6;
 3 char buf[] = "a write to stdout\n";
 4 int main(void)
 5 {
 6     int var;
 7     int pid;
 8     var = 88;
 9     if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1)
10     {
11         err_sys("write error!\n");
12     }
13     printf("before fork!\n");
14     if ((pid = fork())<0)
15     {
16         err_sys("fork error!\n");
17     }
18     else if (pid == 0) // this is child process for pid == 0 
19     {
20         glob++;
21         var++;
22     }
23     else  //this is parent process
24     {
25         sleep(2); //wait for child finishing
26     }
27     printf("pid= %d,glob= %d,var= %d\n", getpid(), glob, var);
28     exit(0);
29 }
View Code

相关文章:

  • 2022-12-23
  • 2021-07-16
  • 2021-09-20
  • 2021-12-17
  • 2021-04-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
猜你喜欢
  • 2021-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2022-02-17
相关资源
相似解决方案