日期:2014-05-16  浏览次数:20604 次

急求!!进程间通讯问题!!!! (C语言)
这道题找了好久也没有类似的。
1)建立两个进程,其中一个在后台运行
2)建立第三个进程,用它终止前一个在后台运行的进程。
3)打印每个进程终止时间
program name/process id termination time in mon:day:hour:min:sec:msec
ex: cmd1/33451 3:21:12:24:15:55
  cmd2/33451 3:21:12:24:17:15
Note:must use sigaction() system call to do IPC !!!!!!!!!!
大家帮帮忙好么??
如果有书上有类似的代码也可以推荐一下。
小弟刚开始学习 linux programming 
如果能给代码,将不胜感激!!!!!!!!!

------解决方案--------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/times.h>
#include <signal.h>
int flag=0;
void sig_user1(int signum)
{
flag=1;
}
void putdate(int num)
{
time_t cur;
struct tm *local;
struct timeval times;
time(&cur);
local=localtime(&cur);
gettimeofday(&times,NULL);
printf("cmd%d pid %d %04d-%02d-%02d %02d:%02d:%02d %3d\n",num,getpid(),local->tm_year+1900,local->tm_mon+1,local->tm_mday,local->tm_hour,local->tm_min,local->tm_sec,times.tv_usec/1000);
}
int main()
{
struct sigaction act;
pid_t frist,second;

act.sa_handler=sig_user1;
act.sa_flags=0;
sigaction(SIGUSR1,&act,NULL);
if((frist=fork())>0)
 {
if((second=fork())>0)
{
while(wait(NULL)>0);
sleep(3);
putdate(1);
}else
{
sleep(10);
kill(frist,SIGUSR1);
sleep(5);
putdate(3);
exit(0);
}
 }
else
 {
while(!flag)
pause();
putdate(2);
exit(0);
 }
}