进程组: 一个进程包含多个进程
会话: 多个组组成一个会话.
创建守护进程模型:
编写一个守护进程,每隔2S钟获取一次系统时间,并将这个时间写入磁盘文件。
分析:
mydaemon.c
//创建守护进程
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void myfunc(int signo)
{
//打开文件
int fd = open("mydemon.log", O_RDWR | O_CREAT | O_APPEND, 0755);
if(fd<0)
{
return;
}
//获取当前的系统时间
time_t t;
time(&t);
char *p = ctime(&t);
//将时间写入文件
write(fd, p, strlen(p));
close(fd);
return;
}
int main()
{
//父进程fork子进程, 然后父进程退出
pid_t pid = fork();
if(pid<0 || pid>0)
{
exit(1);
}
//子进程调用setsid函数创建会话
setsid();
//改变当前的工作目录
chdir("/home/itcast/log");
//改变文件掩码
umask(0000);
//关闭标准输入,输出和错误输出文件描述符
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//核心操作
//注册信号处理函数
struct sigaction act;
act.sa_handler = myfunc;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
//设置时钟
struct itimerval tm;
tm.it_interval.tv_sec = 2;
tm.it_interval.tv_usec = 0;
tm.it_value.tv_sec = 3;
tm.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tm, NULL);
printf("hello world\n");
while(1)
{
sleep(1);
}
}
优化:
//编写守护进程: 每隔2秒获取一次系统时间,并将时间写入文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fd;
int flag = 0;
//信号处理函数
void sighandler(int signo)
{
//获取当前系统时间
time_t tm;
time(&tm);
char *p = ctime(&tm);
if(flag==0)
{
//新建文件
fd = open("./mydaemon.log", O_RDWR | O_CREAT | O_APPEND, 0777);
if(fd<0)
{
perror("open error");
return;
}
flag = 1;
}
//写文件
write(fd, p, strlen(p));
return;
}
int main()
{
//fork子进程,父进程退出
pid_t pid = fork();
if(pid<0 || pid>0)
{
exit(0);
}
//子进程调用setsid函数创建新会话
setsid();
//改变当前工作目录chdir
chdir("/home/itcast");
//重设文件掩码
umask(0000);
//关闭标准输入,标准输出, 标准错误输出这三个文件描述符
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//核心工作
struct sigaction act;
act.sa_handler = sighandler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
//调用setitimer函数设置时钟
struct itimerval tm;
tm.it_interval.tv_sec = 2;
tm.it_interval.tv_usec = 0;
tm.it_value.tv_sec = 3;
tm.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tm, NULL);
while(1)
{
//获取文件大小
int size = lseek(fd, 0, SEEK_END);
if(size>100)
{
close(fd);
rename("./mydaemon.log", "./mydaemon.log.bak");
flag =0;
}
}
close(fd);
return 0;
}
//创建子线程
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, NULL, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//目的是为了让子线程能够执行起来
sleep(1);
return 0;
}
pthread_create1.c
//创建子线程: 传递参数
#include
#include
#include
#include
#include
#include
struct Test
{
int data;
char name[64];
};
//线程执行函数
void *mythread(void *arg)
{
//int n = *(int *)arg;
struct Test *p = (struct Test *)arg;
//struct Test *p = arg;
//printf("n==[%d]\n", n);
printf("[%d][%s]\n", p->data, p->name);
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
}
int main()
{
int n = 99;
struct Test t;
memset(&t, 0x00, sizeof(struct Test));
t.data = 88;
strcpy(t.name, "xiaowen");
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
//int ret = pthread_create(&thread, NULL, mythread, &n);
int ret = pthread_create(&thread, NULL, mythread, &t);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//目的是为了让子线程能够执行起来
sleep(1);
return 0;
}
//循环创建子线程,并且打印是第几个子线程
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
int i = *(int *)arg;
printf("[%d]:child thread, pid==[%d], id==[%ld]\n", i, getpid(), pthread_self());
sleep(100);
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
int ret;
int i = 0;
int n = 5;
int arr[5];
pthread_t thread[5];
for(i=0; i<n; i++)
{
arr[i] = i;
ret = pthread_create(&thread[i], NULL, mythread, &arr[i]);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//目的是为了让子线程能够执行起来
sleep(100);
return 0;
}
//线程退出函数测试
#include
#include
#include
#include
#include
#include
struct Test
{
int data;
char name[64];
};
int g_var = 9;
struct Test t;
//线程执行函数
void *mythread(void *arg)
{
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//printf("[%p]\n", &g_var);
//pthread_exit(&g_var);
memset(&t, 0x00, sizeof(t));
t.data = 99;
strcpy(t.name, "xiaowen");
pthread_exit(&t);
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, NULL, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//回收子线程
void *p = NULL;
pthread_join(thread, &p);
//int n = *(int *)p;
struct Test *pt = (struct Test *)p;
printf("child exit status:[%d],[%s],[%p]\n", pt->data, pt->name, p);
return 0;
}
//设置子线程为分离属性
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
sleep(10);
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, NULL, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//设置线程为分离属性
pthread_detach(thread);
//子线程设置分离属性,则pthread_join不再阻塞,立刻返回
ret = pthread_join(thread, NULL);
if(ret!=0)
{
printf("pthread_join error:[%s]\n", strerror(ret));
}
//目的是为了让子线程能够执行起来
sleep(1);
return 0;
}
//创建子线程
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
while(1)
{
int a;
int b;
//设置取消点
//pthread_testcancel();
printf("-----\n");
}
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, NULL, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//取消子线程
pthread_cancel(thread);
pthread_join(thread, NULL);
return 0;
}
//比较线程ID是否相等
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
}
int main()
{
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, NULL, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//比较线程ID
//if(pthread_equal(thread, pthread_self())!=0)
if(pthread_equal(pthread_self(), pthread_self())!=0)
{
printf("two thread id is same\n");
}
else
{
printf("two thread id is not same\n");
}
//目的是为了让子线程能够执行起来
sleep(1);
return 0;
}
在创建线程的时候设置线程属性为分离属性:
1 pthread_attr_t attr;
2 pthread_attr_init(&attr);
3 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4 pthread_create(&thread, &attr, mythread, NULL);
5 pthread_attr_destroy(&attr);
pthread_attr.c
//在创建子线程的时候设置分离属性
#include
#include
#include
#include
#include
#include
//线程执行函数
void *mythread(void *arg)
{
printf("child thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
sleep(2);
}
int main()
{
//定义pthread_attr_t类型的变量
pthread_attr_t attr;
//初始化attr变量
pthread_attr_init(&attr);
//设置attr为分离属性
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//创建子线程
pthread_t thread;
int ret = pthread_create(&thread, &attr, mythread, NULL);
if(ret!=0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid==[%d], id==[%ld]\n", getpid(), pthread_self());
//释放线程属性
pthread_attr_destroy(&attr);
//验证子线程是否为分离属性
ret = pthread_join(thread, NULL);
if(ret!=0)
{
printf("pthread_join error:[%s]\n", strerror(ret));
}
return 0;
}