1.守护进程创建
定义:周期性的执行某项任务或等待某个事件发生的进程,它的运行不依赖任何shell终端,它的生存周期较长,从开机开始运行直到关机结束。
守护进程的创建步骤:
1.创建一个子进程,让父进程退出 fork
2.在子进程中创建新的会话 setsid();
3.改变子进程的工作目录 chdir(“/”)
4.取消文件权限掩码 umask(0);
5.关闭所有文件描述符 getdtablesize
include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
int i = 0;
//1.fork创建子进程
pid_t pid = -1;
pid = fork();
if(-1 == pid)
{
return -1;
}
if(pid > 0)
{
exit(0);
}
printf("\n1:fork ok!\r\n");
//2.set new section创建新的会话
setsid();
printf("2:create new section ok! pid = %d\r\n", getpid());
//3.chdir
chdir("/");
//4.umask(0)
umask(0);
//5.close all file
int num = getdtablesize();
for(i = 0; i < num; i++)
{
close(i);
}
sleep(20);
return 0;
}
2.线程
定义:线程是一种轻量级的进程,用task_struct来标识它,它没有自己独立内存空间,它共享创建它的进程的地址空间。
2.线程创建
使用第三方线程库提供的API
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
作用:创建一个线程
参数1:*thread:指向线程ID的指针
参数2:*attr:线程属性,通常给NULL
参数3:指向线程执行函数的函数指针
参数4:传给线程执行函数的入参
返回值:0成功,非0失败
#include <stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
printf("test by all@1201---------\r\n");
}
int main()
{
printf("hello world\n");
pthread_t tID = -1;
if(0 != pthread_create(&tID, NULL, ThreadFunc, NULL))
{
printf("create pthread error!--------\r\n");
return -1;
}
sleep(1);
printf("hello 22101-----------\r\n");
return 0;
}
~
~
int pthread_join(pthread_t thread, void **retval);
作用:阻塞等待线程结束,并回收状态值
参数1:pthread:线程ID
参数2:**retval:指向线程退出状态值的指针
void pthread_exit(void *retval);
作用:线程退出函数
*retval
#include <stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
printf("test by all@1201---------\r\n");
pthread_exit("thread exit test---");
}
int main()
{
printf("hello world\r\n");
pthread_t tID = -1;
if(0 != pthread_create(&tID, NULL, ThreadFunc, NULL))
{
printf("create pthread error!-------\r\n");
return -1;
}
char *pStr = NULL;
pthread_join(tID, (void *)&pStr);
printf("%s\r\n", pStr);
printf("hello 22101----------\r\n");
return 0;
}
#include <stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
if(NULL == arg)
{
return (void *)NULL;
}
int b = *(int *)arg;
printf("test by all@1201---------b = %d\r\n", b);
}
int main()
{
printf("hello world\r\n");
pthread_t tID = -1;
int a = 10;
if(0 != pthread_create(&tID, NULL, ThreadFunc, &a))
{
printf("create pthread error!-------\r\n");
return -1;
}
pthread_join(tID, NULL);
printf("hello 22101----------\r\n");
return 0;
}
int pthread_cancel(pthread_t thread);
3.多线程的通信 同步 互斥
在一个进程中可以创建多个线程,那么多个线程间会存在数据通信问题,会存在两个线程相互配合完成一件事情,会存在两个线程对公共资源的静态访问问题。
#include <stdio.h>
#include <pthread.h>
void *ThreadFunc1(void *arg)
{
printf("test by func1---------\r\n");
}
void *ThreadFunc2(void *arg)
{
printf("test by func2------------\r\n");
}
int main()
{
printf("hello world\r\n");
pthread_t tID1 = -1;
pthread_t tID2 = -1;
if(0 != pthread_create(&tID1, NULL, ThreadFunc1, NULL))
{
printf("create pthread1 error!-------\r\n");
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc2, NULL))
{
printf("create pthread2 error!\r\n");
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
printf("hello 22101----------\r\n");
return 0;
}
13,1 顶端
3.1 多线程的数据通信----》使用全局变量
./pthead_com1 hello
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#define BUF_SIZE 20
char buf[BUF_SIZE] = {0};
void *ThreadFunc1(void *arg)
{
if(NULL == arg)
{
return (void *)NULL;
}
strcpy(buf, (char *)arg);
printf("buf:%s\r\n", buf);
}
void *ThreadFunc2(void *arg)
{
sleep(1);
printf("test by func2---------buf=%s\r\n", buf);
}
int main(int argc, char *argv[])
{
printf("hello world\r\n");
pthread_t tID1 = -1;
pthread_t tID2 = -1;
if(0 != pthread_create(&tID1, NULL, ThreadFunc1,(void *)argv[1]))
{
printf("create pthread1 error!-----------\r\n");
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc2, NULL))
{
printf("create pthread2 error!---------\r\n");
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
printf("hello 22101------------\r\n");
return 0;
}
3.2 多线程的同步----》无名信号量 sem_t sem;
同步:两个线程按照一定的顺序相互配合完成一件事情
pthread1----》hello 1S sem_t sema;----》P/V 操作函数控制sema和semb
pthread2-----》world 1S sem_t semb;
p操作:sem_wait(&sema)
含义:sem_wait会检测信号量sema的值是否大于0,若大于0,将sema减一同时函数返回。若等于0,阻塞当前线程。
V操作:sem_post(&sema)---》非阻塞函数
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sema;
sem_t semb;
void *ThreadFunc1(void *arg)
{
int i = 10;
while(i--)
{
sem_wait(&sema);
printf("hello--------\r\n");
sleep(1);
sem_post(&semb);
}
}
void *ThreadFunc2(void *arg)
{
int i = 10;
while(i--)
{
sem_wait(&semb);
printf("world------------\r\n");
sleep(1);
sem_post(&sema);
}
}
int main()
{
printf("hello world\r\n");
pthread_t tID1 = -1;
pthread_t tID2 = -1;
if(-1 == sem_init(&sema, 0, 1) || -1 == sem_init(&semb, 0, 0))
{
return -1;
}
if(0 != pthread_create(&tID1, NULL, ThreadFunc1, NULL))
{
printf("create pthread1 error!---------\r\n");
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc2, NULL))
{
printf("create pthread2 error!--------\r\n");
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
return 0;
}
3.3 多线程的互斥
互斥的引入:多个线程可能会使用同一段公共资源,若是同时访问这段公共资源,那么就会造成多个线程对公共资源的竞争,从而造成访问结果混乱。可以使用互斥锁来解决。
1.多个线程能否使用同一个线程执行函数,可以。
#include <stdio.h>
#include <pthread.h>
void *ThreadFunc(void *arg)
{
printf("into thread func----------\r\n");
}
int main()
{
pthread_t tID1 = -1;
pthread_t tID2 = -1;
if(0 != pthread_create(&tID1, NULL, ThreadFunc, (void *)arr1))
{
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc, (void *)arr2))
{
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
return 0;
}
2.两个线程给线程执行函数传入不同的参数,最后执行结果是否发生混乱?会发生混乱。
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#define BUF_SIZE 11
void *ThreadFunc(void *arg)
{
printf("into thread func----------\r\n");
if(NULL == arg)
{
return (void *)NULL;
}
char *pArr = (char *)arg;
static char buf[BUF_SIZE] = {0};//buf只能被初始化一次
int i = 0;
for(; i < 10; i++)
{
buf[i] = pArr[i];
usleep(5000);//5毫秒=5000微秒
}
printf("buf:%s\r\n",buf);
}
int main()
{
pthread_t tID1 = -1;
pthread_t tID2 = -1;
char arr1[] = "123456789";
char arr2[] = "abcdefghi";
if(0 != pthread_create(&tID1, NULL, ThreadFunc, (void *)arr1))
{
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc, (void *)arr2))
{
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
return 0;
}
3.使用互斥锁解决两线程互斥访问公共资源
pthread_mutex_t mutex;//线程互斥锁
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define BUF_SIZE 11
pthread_mutex_t mutex;
void *ThreadFunc(void *arg)
{
printf("into thread func----------\r\n");
if(NULL == arg)
{
return (void *)NULL;
}
pthread_mutex_lock(&mutex);
char *pArr = (char *)arg;
static char buf[BUF_SIZE] = {0};
int i = 0;
for(; i < 10; i++)
{
buf[i] = pArr[i];
usleep(5000);
}
printf("buf:%s\r\n",buf);
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t tID1 = -1;
pthread_t tID2 = -1;
char arr1[] = "123456789";
char arr2[] = "abcdefghi";
if(-1 != pthread_mutex_init(&mutex, NULL))
{
return -1;
}
if(0 != pthread_create(&tID1, NULL, ThreadFunc, (void *)arr1))
{
return -1;
}
if(0 != pthread_create(&tID2, NULL, ThreadFunc, (void *)arr2))
{
return -1;
}
pthread_join(tID1, NULL);
pthread_join(tID2, NULL);
return 0;
}