一、再论进程
1、多进程实现同时读取键盘和鼠标
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
// 思路就是创建子进程,然后父子进程中分别进行读键盘和鼠标的工作
int ret = -1;
int fd = -1;
char buf[200];
ret = fork();
if (ret == 0)
{
// 子进程
fd = open("/dev/input/mouse1", O_RDONLY);
if (fd < 0)
{
perror("open:");
return -1;
}
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(fd, buf, 50);
printf("读出鼠标的内容是:[%s].\n", buf);
}
}
else if (ret > 0)
{
// 父进程
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(0, buf, 5);
printf("读出键盘的内容是:[%s].\n", buf);
}
}
else
{
perror("fork");
}
return 0;
}
2、使用进程技术的优势
- CPU时分复用,单核心CPU可以实现宏观上的并行
- 实现多任务系统需求(多任务的需求是客观的)
3、进程技术的劣势
- 进程间切换开销大
- 进程间通信麻烦而且效率低
4、解决方案就是线程技术
- 线程技术保留了进程技术实现多任务的特性。
- 线程的改进就是在线程间切换和线程间通信上提升了效率。
- 多线程在多核心CPU上面更有优势。
二、线程的引入
1、使用线程技术同时读取键盘和鼠标
- 线程技术保留了进程技术实现多任务的特性。
- 相当于创建了一个分支任务,也能被cpu调度执行
- gcc file1.c -lpthread -o file 链接线程库
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
char buf[200];
void *func(void *arg)
{
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(0, buf, 5);
printf("读出键盘的内容是:[%s].\n", buf);
}
}
int main(void)
{
int ret = -1;
int fd = -1;
pthread_t th = -1;
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
return -1;
}
// 因为主线程是while(1)死循环,所以可以在这里pthread_detach分离子线程
// 主任务
fd = open("/dev/input/mouse1", O_RDONLY);
if (fd < 0)
{
perror("open:");
return -1;
}
while (1)
{
memset(buf, 0, sizeof(buf));
printf("before read.\n");
read(fd, buf, 50);
printf("读出鼠标的内容是:[%s].\n", buf);
}
return 0;
}
2、linux中的线程简介
- 一种轻量级进程
- 线程是参与内核调度的最小单元
- 一个进程中可以有多个线程
3、线程技术的优势
- 像进程一样可被OS调度
- 同一进程的多个线程之间很容易高效率通信(全局变量即可通信,相当于两个函数之间的通信)
- 在多核心CPU(对称多处理器架构SMP)架构下效率最大化
三、线程常见函数
1、线程创建与回收
- pthread_create 主线程用来创造子线程的
- pthread_join 主线程用来等待(阻塞)回收子线程(有独立的栈)
- pthread_detach 主线程用来分离子线程,分离后主线程不必再去回收子线程
2、线程取消
- pthread_cancel 一般都是主线程调用该函数去取消(让它赶紧死)子线程
- pthread_setcancelstate 子线程设置自己是否允许被取消
- pthread_setcanceltype 要允许自己死了,才能选择死法
3、线程函数退出相关
- pthread_exit与return退出
- pthread_cleanup_push
- pthread_cleanup_pop
4、获取线程id
- pthread_self
四、线程同步之信号量==(again)==
1、任务:用户从终端输入任意字符然后统计个数显示,输入end则结束
2、使用多线程实现:主线程获取用户输入并判断是否退出,子线程计数
- 为什么需要多线程实现
- 问题和困难点是?
- 理解什么是线程同步
3、信号量的介绍和使用
- pthread_exit与return退出
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
char buf[200] = {0};
sem_t sem;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
sem_wait(&sem); //信号量 防止一来printf就输出
//while (strncmp(buf, "end", 3) != 0)
while (flag == 0)
{
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
sem_wait(&sem);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
sem_init(&sem, 0, 0);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (scanf("%s", buf))
{
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1; //通知子线程该结束了
sem_post(&sem);
//exit(0);
break;
}
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号激活子线程来计数。
// 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
sem_post(&sem);
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
sem_destroy(&sem);
return 0;
}
五、线程同步之互斥锁
1、什么是互斥锁
- 互斥锁又叫互斥量(mutex)
- 相关函数:
1、pthread_mutex_init
2、pthread_mutex_destroy
3、pthread_mutex_lock
4、pthread_mutex_unlock - 互斥锁和信号量的关系:可以认为互斥锁(但只有1和0)是一种特殊的信号量(不只是1和0)
- 互斥锁主要用来实现关键段保护
2、用互斥锁来实现上节的代码
- man 3 pthread_mutex_init时提示找不到函数,说明你没有安装pthread相关的man手册。
- 安装方法:1、虚拟机上网;2、sudo apt-get install manpages-posix-dev
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
//while (strncmp(buf, "end", 3) != 0)
sleep(1); //保证主线程先上锁
while (flag == 0)
{
pthread_mutex_lock(&mutex);
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_mutex_unlock(&mutex);
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
//exit(0);
break;
}
sleep(1); //紧接着让子线程上锁
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号激活子线程来计数。
// 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
return 0;
}
六、线程同步之条件变量
1、什么是条件变量
2、相关函数
- pthread_cond_init
- pthread_cond_destroy
- pthread_cond_wait
- pthread_cond_signal/pthread_cond_broadcast
3、使用条件变量来实现上节代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
while (flag == 0)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex); //子线程被唤醒,条件变量必须上锁
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
//pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_cond_signal(&cond); //唤醒子线程
//pthread_mutex_unlock(&mutex);
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
break;
}
//sleep(1);
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号激活子线程来计数。
// 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
4、线程同步总结
——资料来源于朱老师物联网大讲堂