#include
#include
#include
#include
#include
#include
#define NAME "/dev/input/mouse1"
int main()
{
// 思路为,创建子进程,然后分别进行读键盘(父亲)和鼠标(儿子)工作
int ret=-1;int fd=-1;char buf[200];
ret=fork();
if(ret==0){//child
fd=open(NAME,O_RDONLY);
if(fd<0)
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){//parent
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、使用进程技术的优势
3、使用进程技术的劣势
4、解决方案——线程技术
1、使用线程技术同时读取键盘和鼠标
#include
#include
#include
#include
#include
#include
#include
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;//线程id
ret=pthread_create(&th,NULL,func,NULL);//创建线程-子线程键盘
if(ret<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、线程技术的优势
4、线程间通信方式
1.线程创建与回收
pthread_creat:主线程用来创建子线程pthread_join:主线程用来(阻塞)等待回收子线程pthread_detach:主线程用来分离子线程,分离后主线程不必再去回收子线程2.线程取消
pthread_cancel:其他线程调用该函数去取消(结束)子线程pthread_setcancelstate:子线程用来设置自己是否允许被取消pthread_setcanceltype:子线程用来设置自己取消类型(立即取消or等执行到属于cancellation point 的函数的时候才会取消)3.线程函数退出
pthread_exit与return:都可用于线程函数退出,注意exit是用于进程退出pthread_cleanup_push与pthread_cleanup_pop:根线程退出时需要调用的清理函数相关4.获取线程 ID
pthread_self5.发送信号
pthread_killtask1
用户从终端输入任意字符以回车键结束,程序统计个数并显示,输入“end”则结束。
sem_init:信号量初始化sem_post:发送信号量,表示当前状态。sem_wait:即是用来判断,能用就用,不能用就等sem_destroy:销毁信号量#include
#include
#include
#include
#include
char buf[200]={0};
sem_t sem;
unsigned int flag=0;
void* func(void* arg)// 子线程程序,作用是统计 buf 中的字符个数并打印
{
//子线程首先应该有个循环,
//循环中阻塞在等待主线程激活的时候,子线程激活后就去获取buf中的字符
//长度,然后打印;完成后再次被阻塞
sem_wait(&sem);
while(flag==0){//while(strncmp(buf,"end",3)!=0)
printf("本次输入了%d 个字符\n", strlen(buf));
memset(buf,0,sizeof(buf));
sem_wait(&sem);
}
pthread_exit(NULL);//return;皆可用于函数线程退出
}
int main()
{
int ret=-1;pthread th=-1;
sem_init(&sem,0,0);
ret=pthread(&th,NULL,func,NULL);
if(ret!=0){
perror("pthread");exit(-1);//exit为退出进程函数
}
printf("输入一个字符串,以回车结束.\n");
while(scanf("%s",buf)){
// 去比较用户输入的是不是 end,如果是则退出,如果不是则继续
if(!strcmp(buf,"end",3)){
printf("程序结束\n");flag = 1;
sem_post(&sem);
break;
}
//主线程去发信号激活子线程来计数。
//子线程被阻塞,主线程可以激活,这就是线程的同步问题
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.什么是互斥锁
pthread_mutex_init 、 pthread_mutex_destory 、 pthread_mutex_lock 、 pthread_mutex_unlock2.用互斥锁实现上节功能
#include
#include
#include
#include
char buf[200] = {0};
pthread_mutex_t mutex;
unsigned int flag = 0;
// 子线程程序,作用是统计 buf 中的字符个数并打印
void *func(void *arg)
{
//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()
{
int ret=1-;pthread th=-1;
pthread_mutex_init(&mutex,NULL);
if (ret != 0) 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;break;
}
sleep(1);
}
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0) exit(-1);
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);return 0;
}
1.什么是条件变量
2.相关函数
pthread_cond_initpthread_cond_destroypthread_cond_waitpthread_cond_signal/pthread_cond_broacast3、3.使用条件变量来实现上节功能。
#include
#include
#include
#include
char buf[200] = {0};
pthread_mutex_t mutex;pthread_cond_t cond; //条件变量,互斥锁搭配使用
unsigned int flag = 0;
// 子线程程序,作用是统计 buf 中的字符个数并打印
void* func(void* arg)
{
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_crate(&th,NULL,func,NULL);
if(ret!=0) exit(-1);
printf("输入一个字符串,以回车结束\n");
while (1){
scanf("%s", buf);
pthread_cond_signal(&cond);
// 去比较用户输入的是不是 end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3)){
printf("程序结束\n");flag = 1;break;
}
}
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0) exit(-1);
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond);
return 0;
}
详细线程介绍请看本人另一篇博客线程同步