Linux12线程同步,并发问题(未完)

看线程id ,个数 ps-eLf

Top(显示一些信息关于cpu的)?
设置cpu的性能,taskset,指定某个进程在某个核心上执行。
Cpu亲和性
使用多处理器资源,同时做两件以上事情,时候用线程
在这里插入图片描述
线程间通信问题(同步)四种方法:
用户可以用:
信号量;
互斥锁
条件变量
读写锁
内核空间的方法:自旋锁

1.线程的同步(信号量,互斥锁,条件变量,读写锁)

1.1创建一个线程

1.线程(进程内部的一条执行路径或序列)(调度执行角度)进程:一个正在运行的程序(分配资源角度)并发,同步,
线程函数
2.操作系统描述的实现:
3.编译时要加线程的库
(gcc -o main main.c -lpthread)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<assert.h>
  4 #include<string.h>
  5 #include<unistd.h>
  6 #include<sys/msg.h>
  7 void * thread_fun(void *arg)
  8 {
  9 
 10     int i=0;
 11     for(;i<7;++i)
 12     {
 13         printf("fun run\n");
 14         sleep(1);
 15     }
 16     pthread_exit("fun over");
 17 }
 18 int main()
 19 {
 20     pthread_t id;
 21 
 22     pthread_create(&id,NULL,thread_fun,NULL);
 23 
 24     int i=0;
 25     for(;i<3;++i)
 26     {
 27         printf("main run\n");
 28         sleep(1);
 29     }
 30     char *s=NULL;
 31     pthread_join(id,(void**)&s);//等待子线程完成
 32     printf("s==%s\n",s);
 33     exit(0);//退出进程,线程中不调用
 34    //pthread_exit(NULL)// 退出线程
 35 }

结果
在这里插入图片描述

1.2信号量(p,v操作)互斥锁

main.c

1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<semaphore.h>
  5 #include<pthread.h>
  6 #include<unistd.h>
  7 
  8 //sem_t sem;
  9 pthread_mutex_t mutex; //在主函数之前打印东西?查资料
 10 void * thread_fun(void *arg)//线程
 11 {
 12     int i=0;
 13     for(;i<5;++i)
 14     {
 15        // sem_wait(&sem);//p操作
 16         pthread_mutex_lock(&mutex);//互斥锁
 17         write(1,"B",1);
 18         int n=rand()%3;
 19         sleep(n);
 20         write(1,"B",1);
 21         //sem_post(&sem);//v操作
 22         pthread_mutex_unlock(&mutex);
 23         n=rand()%3;
 24         sleep(n);
 25     }
 26 }
 27 
 28 int main()
 29 {
 30     pthread_t id;
 31 
 32     //sem_init(&sem,0,1);//创建信号量
 33     pthread_mutex_init(&mutex,NULL);
 34     //pthread_create(&id,NULL,thread_fun,NULL)
 35     pthread_create(&id,NULL,thread_fun,NULL);
 36 
 37     int i=0;
 38     for(;i<5;++i)
 39     {
 40         //sem_wait(&sem);//p操作
 41         pthread_mutex_lock(&mutex);//加锁
 42         write(1,"A",1);
 43         int n=rand()%3;
 44         sleep(n);
 45         write(1,"A",1);
 46       //  sem_post(&sem);//v操作
 47         pthread_mutex_unlock(&mutex);//解锁
 48         n=rand()%3;
 49         sleep(n);
 50     }
 51     pthread_join(id,NULL);
 52     //sem_destroy(&sem);
 53     pthread_mutex_destroy(&mutex);
 54     exit(0);
 55 }

在这里插入图片描述

1.3条件变量

cond.c

 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<semaphore.h>
  5 #include<pthread.h>
  6 #include<unistd.h>
  7 
  8 pthread_cond_t cond;
  9 pthread_mutex_t mutex;
 10 void *fun1(void *arg)
 11 {
 12     char *s=(char*)arg;//s指向buff
 13     while(1)
 14     {
 15         pthread_mutex_lock(&mutex);//加锁
 16         pthread_cond_wait(&cond,&mutex);//添加到等待队列中,解锁,唤醒时加锁
 17         pthread_mutex_unlock(&mutex);//解锁;
 18         if(strncmp(s,"end",3)==0)
 19         {
 20          break;
 21         }
 22         printf("fun1:%s\n",s);
 23     }
 24     printf("fun1 over!!\n");
 25 }
 26 void *fun2 (void *arg)
 27 {
 28     char *s=(char*)arg;//s指向buff
 29     while(1)
 30     {
 31        // pthread_cond_wait();
 32         pthread_mutex_lock(&mutex);//(添加时不需要唤醒,别人不许添加,)加锁
 33         pthread_cond_wait(&cond,&mutex);//添加到等待队列中,解锁,唤醒时出队列要加锁
 34         pthread_mutex_unlock(&mutex);//解锁;
 35         if(strncmp(s,"end",3)==0)
 36         {
 37          break;
 38         }
 39         printf("fun2:%s\n",s);
 40     }
 41     printf("fun2 over!!\n");
 42 }
 43 
 44 int main()
 45 {
 46     pthread_t id[2];
 47     char buff[128]={0};
 48 
 49     pthread_mutex_init(&mutex,NULL);
 50     pthread_cond_init(&cond,NULL);
 51 
 52     pthread_create(&id[0],NULL,fun1,(void*)buff);
 53     pthread_create(&id[1],NULL,fun2,(void*)buff);
 54     while(1)
 55     {
 56         char readline[128]={0};
 57         fgets(readline,128,stdin);
 58         strcpy(buff,readline);//向buff写入数据
 59 

 60         if(strncmp(readline,"end",3)==0)
 61         {
 62 
 63             break;
 64         }
 65         pthread_mutex_lock(&mutex);//唤醒时可以不加锁
 66         pthread_cond_signal(&cond);//唤醒在条件变量上等待的线程(某一个)
 67         pthread_mutex_unlock(&mutex);
 68     }
 69         pthread_mutex_lock(&mutex);
 70         pthread_cond_broadcast(&cond);//唤醒所有的线程
 71         pthread_mutex_unlock(&mutex);
 72 
 73         pthread_join(id[0],NULL);
 74         pthread_join(id[1],NULL);
 75 
 76         pthread_mutex_destroy(&mutex);//销毁锁
 77         pthread_cond_destroy(&cond);//销毁条件变量
 78 
 79         exit(0);
 80 }

结果:
在这里插入图片描述
两个线程交替打印,一个使用时另外一个不能使用。输入end后两个线程同时结束。

1.4读写锁

在这里插入图片描述

2.线程的并发

2.1五个线程并发执行

1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<assert.h>
  4 #include<string.h>
  5 #include<unistd.h>
  6 #include<sys/msg.h>
  7 #include<pthread.h>
  8 
  9 #define MAXID  5
 10 
 11 void *thread_fun(void *arg)
 12 {
 13     //int index=(int )arg;//转为int
 14     int index=*(int* )arg;//转为int
 15     int i=0;
 16     for(;i<4;i++)
 17     {
 18         printf("index=%d\n",index);
 19         sleep(1);
 20     }
 21 }
 22 
 23 int main()
 24 {
 25     pthread_t id[MAXID];
 26 
 27     int i=0;
 28     for(;i<MAXID;i++)
 29     {
 30         //pthread_create(&id[i],NULL,thread_fun,(void*)i);//void*类型指定大小,要四个字节
 31         pthread_create(&id[i],NULL,thread_fun,(void*)&i);//void*类型指定大小,要四个字节
 32     }
 33 
 34     for(i=0;i<MAXID;i++)
 35     {
 36         pthread_join(id[i],NULL);
 37     }
 38     exit(0);
 39 }

结果:
在这里插入图片描述

2.2与处理器个数有关的问题

 1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<assert.h>
  4 #include<string.h>
  5 #include<unistd.h>
  6 #include<sys/msg.h>
  7 #include<pthread.h>
  8 #include<semaphore.h>
  9 
 10 #define MAXID  5
 11 
 12 sem_t sem;//
 13 int g=1;
 14 
 15 void *thread_fun(void *arg)
 16 {
 17     int i=0;
 18     {
 19         for(;i<1000;i++)//最后g小于等于5000,
 20         {
 21             sem_wait(&sem);
 22             printf("d=%d\n",g++);
 23             sem_post(&sem);
 24         }
 25     }
 26 }
 27 
 28 int main()
 29 {
 30     pthread_t id[MAXID];
 31     sem_init(&sem,0,1);//初始化信号量值为1;
 32     int i=0;
 33     for(;i<MAXID;i++)
 34     {
 35         //pthread_create(&id[i],NULL,thread_fun,(void*)i);//void*类型指定大小,要四个字节
 36         pthread_create(&id[i],NULL,thread_fun,NULL);//void*类型指定大小,要四个字节
 37     }
 38 
 39     for(i=0;i<MAXID;i++)
 40     {
 41         pthread_join(id[i],NULL);
 42     }
 43     sem_destroy(&sem);
 44     exit(0);
 45 }

在这里插入图片描述

单核处理器打印出来为小于5000,原因是多个线程并发执行在一个处理器上,
多核处理器为5000

3.问题

3.1一个进程可以创建多少个线程

3.2线程安全函数(可重入函数:)

3.3多线程中执行fork会怎么样

猜你喜欢

转载自blog.csdn.net/sunshinecandy/article/details/88766645
今日推荐