repo.or.cz
/
goroutine-traffic-statistics.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
blame
|
history
|
raw
|
HEAD
数据存储
[goroutine-traffic-statistics.git]
/
ch02
/
using-fork.c
blob
84a9c75c22182ff002d22da8391758d08104a9b9
1
#include <stdio.h>
2
#include <unistd.h>
3
4
int
main
(
int
argc
,
char
**
argv
) {
5
printf
(
"-- 程序开始 --
\n
"
);
6
7
int
counter
=
0
;
8
pid_t pid
=
fork
();
9
10
if
(
pid
==
0
) {
11
// 子进程
12
int
i
=
0
;
13
for
(;
i
<
5
;
i
++) {
14
printf
(
"子进程:counter=%d
\n
"
, ++
counter
);
15
}
16
}
else if
(
pid
>
0
) {
17
// 父进程
18
int
j
=
0
;
19
for
(;
j
<
5
;
j
++) {
20
printf
(
"父进程:counter=%d
\n
"
, ++
counter
);
21
}
22
}
else
{
23
printf
(
"fork()失败!
\n
"
);
24
return
1
;
25
}
26
printf
(
"-- 程序结束 --
\n
"
);
27
return
0
;
28
}