终端时间显示,n中断时间显示
2023-08-02 21:07:55
2023-08-02 21:07:56
2023-08-02 21:07:57
2023-08-02 21:07:58
n
child=n end!!!!!!!!!!
ubuntu@ubuntu:D5$ cat k3.c
#include <head.h>
void gettime();
void gettime()
{
time_t t1;
time(&t1);
struct tm* info = localtime(&t1);
printf("%4d-%02d-%02d %02d:%02d:%02d\n",\
info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
info->tm_hour,info->tm_min,info->tm_sec);
}
int main()
{
char s1;
pid_t cpid = fork();
if(cpid > 0)
{
while(1)
{
sleep(1);
pid_t c1 = waitpid(-1,NULL,WNOHANG);
if(c1 > 0)
{
return 0;
}
else
{
gettime();
}
}
}
else if(0 == cpid)
{
while(1)
{
scanf("%s",&s1);
printf("child=%s ",&s1);
if(s1 == 'n')
{
printf("end!!!!!!!!!!\n");
exit(0);
}
}
}
else
{
perror("fork");
return -1;
}
return 0;
}
守护进程
#include <head.h>
int main()
{
pid_t cpid = fork();
if(0 == cpid)
{
pid_t sid = setsid();
printf("%d",sid);
chdir("/");
umask(0);
for(int i=0;i<getdtablesize();i++)
close(i);
while(1)
{
sleep(1);
}
}
return 0;
}