📄 one.c
字号:
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
void my_sighandler(int signum)
{
time_t nowtime=0;
struct tm *tim;
if(signum==SIGUSR1)
{
if(time(&nowtime)==-1)
perror("time");
tim=localtime(&nowtime);
printf("%d-%d-%d,%d:%d:%d\n",tim->tm_mon,
tim->tm_mday,
tim->tm_year+1900,//transform 106 to 2006
tim->tm_hour,
tim->tm_min,
tim->tm_sec);
}
else if(signum==SIGUSR2)
{
printf("quit!\n");
exit(0);
}
}
int main()
{
int i;
int pid,status;
signal(SIGUSR1,my_sighandler);
signal(SIGUSR2,my_sighandler);
pid=fork();
if(pid<0)
{
printf("fork error!\n");
exit(1);
}
else if(pid==0)
{
printf("This is child process %d!\n",getpid());
for(i=0;i<10;i++)
{
kill(getppid(),SIGUSR1);
sleep(1);
}
kill(getppid(),SIGUSR2);
}
else if(pid>0)
{
printf("This is process %d!\n",getpid());
wait(&status);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -