#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
class Base
{
public:
static pid_t ppid_c;
static void setppid(pid_t j){ ppid_c = j;}
static pid_t getppid_c (){ return ppid_c;}
};
pid_t Base::ppid_c = 0;
void test (int signo)
{
printf ("before sleep\n");
sleep (3);
printf ("after sleep 3 sec\n");
}
int main ()
{
pid_t pid = fork ();
if (pid > 0)
{
signal (SIGUSR1, test);
printf ("ppid = %d", getpid ());
fflush (stdout); /////////////////////////////////////////////////////////问题所在
signal (SIGINT, SIG_IGN);
int st;
waitpid (pid, &st, 0);
}else
if (pid == 0)
{
setsid ();
sleep (10);
Base::setppid (getppid ());
printf ("chiled sleep over\n");
printf ("child ppid = %d", Base::getppid_c ());
kill (Base::getppid_c (), SIGUSR1);
printf ("child program");
exit (0);
}
}
在加上注释行以前..父进程居然不打印..郁闷了半天..最后组长想到这个问题printf ("ppid = %d", getpid ());
里边没有加\n 所以输出流先缓存起来.没有直接输出, 得手动清理
posted on 2006-10-31 17:56
崔少伟 阅读(274)
评论(0) 编辑 收藏 引用 所属分类:
linux develop