打印

请教有关进程的问题

请教有关进程的问题

#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>

int tprintf (const char*fmt, ...);

int main(void)
{
pid_t pid1,pid2;

printf("Hello from Parent Process, PID is %d.\n", getpid());

pid1 =fork();
pid2 =fork();


if (pid1 == 0)  //child1 process
{ sleep(3);
   printf("Hello from Child1 Process %d.\n",getpid());
}
else if (pid2 == 0)  //child2 process
{ sleep(3);
   printf("Hello from Child2 Process %d.\n",getpid());
}
else if (pid1 != -1)  //parent process
{
   tprintf("Parent forked child1 process %d.\n", pid1);
     sleep(1);
   tprintf("Parent forked child2 process %d.\n", pid2);
     sleep(1);
   tprintf("Parent is waiting for child to exit.\n");

   waitpid(pid1, NULL, 0);
   waitpid(pid2, NULL, 0);

   tprintf("child has exited.\n");
   tprintf("Parent had exited.\n");
}
else  tprintf(" Everything was done without error.\n");

return 0;
}

int tprintf (const char*fmt, ...)
{ va_list args;
        struct tm *tstruct;
        time_t tsec;
        tsec = time(NULL);
        tstruct = localtime (&tsec);
        printf("%02d:%02d:%02d: %5d|", tstruct->tm_hour, tstruct->tm_min, tstruct->tm_sec, getpid());
        va_start(args, fmt);
        return vprintf(fmt, args);
}
不知道为什么我只想创建两个进程,但是每次都变成三个了……

TOP

you got 2 child2 processes if i'm not wrong?

TOP

上面的代码是产生了两个child1,不过请问你为什么认为是两个child2呢

TOP

i think i know what's going on....it need to be something like this
复制内容到剪贴板
代码:
pid1 =fork();
if (pid1==0)//child1 process
{
   sleep(3);
   printf("Hello from Child1 Process %d.\n",getpid());
}
else //parent process
{
   pid2 =fork();
   if (pid2 == 0) //child2 process
   { sleep(3);
      printf("Hello from Child2 Process %d.\n",getpid());
   }
   else //parent process
   {
      tprintf("Parent forked child1 process %d.\n", pid1);
      sleep(1);
      tprintf("Parent forked child2 process %d.\n", pid2);
      sleep(1);
      tprintf("Parent is waiting for child to exit.\n");

      waitpid(pid1, NULL, 0);
      waitpid(pid2, NULL, 0);

      tprintf("child has exited.\n");
      tprintf("Parent had exited.\n");
   }
}
because after you fork pid1, you didn't check the status immediately, that means both child and parent process will call fork for pid2
that is
parent ----fork pid1----fork pid2---print (Parent forked child1 process )
pid1                --------fork pid2---print(Hello from Child1 Process )
pid2(from parent) ----(here we got pid1!=0, so we go to Hello from Child2 process)
pid2(from pid1)------(here we inherite the status with pid1 process, that means variable pid1 is 0!!!, so we go to "Hello from Child1 Process ")

so in reality there are 2 pid2, but the pid2 from pid1 will display Hello from Child1 Process, because it inherite the status of pid1 process, so that pid1 variable became 0 when checking it....

[ 本帖最后由 wangzhonnew 于 2008-4-7 03:12 编辑 ]

TOP

THANK YOU!!!!!!

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-10-11 03:50 京ICP证060528 号

Designed By 17DST