#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <time.h> void init_daemon() { int pid; int i; // 在后台运行 if ((pid = fork()) < 0) { exit(1); } else if (pid > 0) { exit(0); } // 是第一子进程,后台继续执行 setsid(); // 第一子进程成为新的会话组长和进程组长 // 禁止重新打开控制终端 if ((pid = fork()) < 0) { exit(1); } else if (pid > 0) { exit(0); } else { close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); chdir("/"); umask(0); } } int main() { FILE *fp; time_t t; struct tm *tmb; char buffer[200]; fp = fopen("/tmp/daemon.log", "a+"); if (fp == NULL) { exit(1); } while (1) { time(&t); tmb = localtime(&t); strftime(buffer, 200, "%Y-%m-%d %H:%M:%S", tmb); fprintf(fp, "I'm here at %s\n", buffer); fclose(fp); sleep(60); // 等待一分钟 } return 0; }
时间: 2024-04-26 11:20:57 浏览: 13
这段代码是一个简单的守护进程,可以在后台持续运行并记录当前时间到日志文件中。该程序的主要流程如下:
1. 定义一个函数 init_daemon(),该函数用于将进程变成守护进程,即脱离终端,后台运行。具体实现方法是通过两次调用 fork() 函数实现的。
2. 在主函数中,首先打开一个日志文件 "/tmp/daemon.log",如果文件打开失败则退出程序。
3. 进入一个无限循环,每次循环中获取当前时间并将其格式化,然后将格式化后的时间记录到日志文件中,并关闭文件。程序会等待一分钟后再次进入下一个循环。
注意到程序在后台运行,因此无法直接观察其输出。如果想要查看日志文件 "/tmp/daemon.log" 中的内容,可以使用命令 tail -f /tmp/daemon.log。
相关问题
为程序添加备注注意对齐#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limi
/*
* 该函数实现了类似于Linux终端中的ls -l命令的功能,可以显示文件的详细信息。
* 参数file为要显示信息的文件名;
* 参数st为文件的stat结构体,包含了文件的各种信息,如权限、大小、修改时间等。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void shell_ls_l(char *file,struct stat st) {
char limit[10]="rwxrwxrwx"; // 文件权限对应的字符
char perms[11]="----------"; // 文件权限字符串
int i=0;
int j=0;
int k=0;
struct passwd *user; // 用户信息结构体
struct group *group; // 用户组信息结构体
// 设置文件权限字符串
for(i=0;i<9;i++) {
if(st.st_mode & (1<<(8-i))) {
perms[i]=limit[i];
}
}
// 根据文件类型设置第一个字符
switch(st.st_mode & S_IFMT) {
case S_IFLNK:
perms[0]='l';
break;
case S_IFDIR:
perms[0]='d';
break;
case S_IFCHR:
perms[0]='c';
break;
case S_IFBLK:
perms[0]='b';
break;
case S_IFIFO:
perms[0]='p';
break;
case S_IFSOCK:
perms[0]='s';
break;
default:
perms[0]='-';
break;
}
printf("%s ",perms); // 输出文件权限
printf("%d ",(int)st.st_nlink); // 输出硬链接数
user=getpwuid(st.st_uid); // 获取文件拥有者信息
group=getgrgid(st.st_gid); // 获取文件拥有者组信息
printf("%s %s ",user->pw_name,group->gr_name); // 输出拥有者和所属组
printf("%8ld ",st.st_size); // 输出文件大小
char *time_str=ctime(&st.st_mtime); // 获取文件最后修改时间
time_str[strlen(time_str)-1]='\0'; // 去掉不需要的换行符
printf("%s ",time_str); // 输出最后修改时间
printf("%s\n",file); // 输出文件名
}
参考下列代码,比较使用系统调用和库函数的文件拷贝程序的速度性能#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main() { char c; int in, out; in = open(“file.in”, O_RDONLY); out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while(read(in,&c,1) == 1) write(out,&c,1); exit(0); } #include <stdio.h> #include <stdlib.h> int main() { int c; FILE *in, *out; in = fopen(“file.in”,”r”); out = fopen(“file.out”,”w”); while((c = fgetc(in)) != EOF) fputc(c,out); exit(0); }
使用系统调用的文件拷贝程序的代码:
```
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main() {
char c;
int in, out;
in = open("file.in", O_RDONLY);
out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1) == 1)
write(out,&c,1);
exit(0);
}
```
使用库函数的文件拷贝程序的代码:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
int c;
FILE *in, *out;
in = fopen("file.in","r");
out = fopen("file.out","w");
while((c = fgetc(in)) != EOF)
fputc(c,out);
exit(0);
}
```
为了比较性能,我们可以使用 `time` 命令来分别测试这两个程序的运行时间。在终端中输入如下命令:
```
time ./program1 # program1 为使用系统调用的程序的可执行文件名
```
然后再输入如下命令:
```
time ./program2 # program2 为使用库函数的程序的可执行文件名
```
这两个命令会输出程序的运行时间统计信息,包括用户 CPU 时间、系统 CPU 时间和实际时间。根据实际时间来比较这两个程序的性能。