发新话题
打印

fork进程的打印问题

fork进程的打印问题

为什么父进程的printf没打印出来,只打出了child的信息??9 h7 P' x3 L: ~3 ~9 Z

! h. p4 F/ U8 m; M% u  D#include <string.h>, w* z- s. ~. N9 n, N1 F( b. P
#include <stdio.h>
) q: A2 }+ ~2 b#include <unistd.h>+ \( W0 r- u: W" H( U
#include <sys/wait.h>+ g5 r" j+ `1 ?. D* C, m
#include <stdlib.h>7 e' ^& @+ H' B- u3 [
int main()
$ u4 u$ e5 ~6 \  N& r{% i$ t0 J/ J- e$ M: h0 W
pid_t pid;0 w; a( f% R, Q  X) f& D
setbuf(stdout,NULL);
# G  D7 A% k. i$ A/ ^if((pid=fork()<0))! D$ E4 s7 l* \3 u1 r, A) C* Q+ e, i
{
" f$ G0 v; o9 l7 \2 J2 V        printf("fork error\n");. t, e! K! b' p
}
  g4 D$ x  u/ p$ K3 _! W- E' Lelse if(pid >0)" _8 d) s+ C2 ], L) w3 B
{8 r0 b+ Z3 X8 E4 b* ?
printf("parent id:%d\n",getpid());
- q# W, `4 P7 _( Vprintf("parent'parent id:%d\n",getppid());
& a2 S' _4 \1 C# P# x//exit(0);
0 M: j: }) [: \( C9 e}3 _; b: {2 f' a: y+ d, E
else if(pid ==0)' t9 y9 t$ t8 X- q2 ~# M9 d
{
& g& w' Q: [- |+ {/ x) [* T- hsleep(3);
" }. Z/ I6 e9 x$ W: aprintf("========================\n");
# ^8 j. y0 q/ Y0 o; C/ q: Q}
9 I" e) i) r- I" e( Y/ i( ]exit(0);/ i- i  ~2 Z/ S0 r7 J% {
}
- q" J; N/ u& S2 Y) O- n( n/ q% P; M0 p! W7 ^; G8 _2 p) F: H5 `4 P
郁闷,请指教      

TOP

if((pid=fork()<0))???
0 s: f& z+ E: s仔细点哦,眼神不好,是不是要配个眼镜?      
上帝说,有问题,找GOOGLE 写程序是很神圣的事情!同样只是装系统,卖菜的大娘会的事情不见得就跟卖菜一样了。

TOP

呵呵,优先级问题比较搞      

TOP

发新话题