⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 one.c

📁 此程序在运行的时候,产生一个子进程.此子进程每隔1秒钟,向父进程发送一个SIGUSR1信号,父进程收到信号后,打印当前系统时间.当经过10秒钟后,子进程结束运行,并发送SIGUSR2信号,通知父进程
💻 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 + -