2014/3/4实验代码
来源:互联网 发布:淘宝卖家地址哪里改 编辑:程序博客网 时间:2024/06/08 07:42
//daemon_init.c
//创建一个守护进程
/***********************************************************************************************************************************************************************/
//daemon_test.c
//创建一个守护进程,每隔60秒把当前系统时间存入/tmp/test.log文件中
//创建一个守护进程
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
void init_daemon(void) //initialize daemon
{
int pid;
int i;
if(pid=fork()) //end parent process
exit(0);
else if(pid<0) //quit if fail to create
exit(1);
setsid(); //create new dialogue
if(pid=fork())
exit(0); //quit if it's the first child process
else if(pid<0)
exit(1); //quit if fail to create
for(i=0;i<NOFILE;i++) //close opened file description
close(i);
chdir("/tmp"); //change directory to /tmp
umask(0); //reset file create mask code
return;
}/***********************************************************************************************************************************************************************/
//daemon_test.c
//创建一个守护进程,每隔60秒把当前系统时间存入/tmp/test.log文件中
#include<stdio.h>
#include<time.h>
void init_daemon(void);
int main(int argc,char* argv[])
{
FILE* fp;
time_t t;
init_daemon(); //initialize a daemon
while(1)
{
sleep(60);
if((fp=fopen("test.log","a"))>=0) //open file "test.log"
{
t=time(0);
fprintf(fp,"I'm here at %s\n",asctime(localtime(&t))); //print local time
fclose(fp);
}
}
return 0;
}
/***********************************************************************************************************************************************************************/
//测试main函数的参数含义
/***********************************************************************************************************************************************************************/
//主进程等待子进程结束
/***********************************************************************************************************************************************************************/
//测试main函数的参数含义
#include<stdio.h>
int main(int argc,char* argv[])
{
printf("%d\n",argc);
printf("%s\n",argv[0]);
printf("%s\n",argv[1]);
printf("%s\n",argv[2]);
printf("%c\n",argv[1][0]);
}
//执行./main_argument_test shadow walker的结果:
3
./main_argument_test
shadow
walker
s
/***********************************************************************************************************************************************************************/
//主进程等待子进程结束
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
pid_t pid;
char* msg;
int k;
int exit_code;
pid=fork();
switch(pid)
{
case 0: printf("Child process creation succeed.\n");
msg="Child process is running";
k=5;
exit_code=37;
break;
case -1: printf("Process creation failed.\n");
exit(1);
default:exit_code=0;
break;
}
if(pid!=0)
{
int stat_val;
pid_t child_pid;
child_pid=wait(&stat_val);
printf("Child process has exited, pid=%d\n",child_pid);
if(WIFEXITED(stat_val))
{
printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
}
else
{
printf("Child exited abnormally\n");
}
}
else
{
while(k>0)
{
puts(msg);
sleep(1);
k--;
}
}
}
0 0
- 2014/3/4实验代码
- [Java] 实验4参考代码
- [Java] 实验3参考代码
- 实验代码
- 数据结构实验的代码
- 流水灯实验代码
- 数据结构实验c++代码
- Kalman滤波实验代码
- 二叉树实验代码
- oracle实验和代码
- 数据库实验代码备份
- 实验二代码
- c++多线程实验代码
- 图形学实验代码
- 汇编实验代码
- GPIO实验代码
- node实验代码
- windows第二次实验代码
- 多线程pthread 和 cpu affinity头文件问题
- SVN服务器搭建和使用(二)
- 【贪心法】+-字符串
- 2014/2/28编程总结
- Linux虚拟机与Windows物理机之间传输文件的方法
- 2014/3/4实验代码
- VS2008库文件的使用
- LinuxCast学习笔记二十七:raid_basic
- Linux 控制台聊天程序
- 转载-Mac系统U盘制作(Windows下)
- 安装websphere6.1,运行install时没有反应
- 嵌入式Linux平台搭建日记2014/3/7
- n!的另类算法
- HDOJ1017 A Mathematical Curiosity