• IO和进程day06(线程续、同步线程互斥)


    今日任务:

    1.代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//创建互斥锁
    11. pthread_cond_t cond=PTHREAD_COND_INITIALIZER;//创建条件变量
    12. int flag=0;//0为str空,1为有str值
    13. char str[64]={0};
    14. void *fun1(void*arg){
    15. //读取
    16. int fd_rd=open("./1.txt",O_RDONLY);
    17. if(fd_rd==-1){
    18. perror("open");
    19. return NULL;
    20. }
    21. while(1){
    22. //上锁
    23. pthread_mutex_lock(&mutex);
    24. if(flag==0){
    25. memset(str,0,sizeof(str));
    26. if(read(fd_rd,str,sizeof(str))==0){
    27. flag=1;
    28. memset(str,0,sizeof(str));
    29. pthread_cond_signal(&cond);
    30. pthread_mutex_unlock(&mutex);
    31. pthread_exit(NULL);
    32. break;
    33. }
    34. flag=1;
    35. pthread_cond_signal(&cond);
    36. }else{
    37. pthread_cond_wait(&cond,&mutex);
    38. }
    39. //解锁
    40. pthread_mutex_unlock(&mutex);
    41. }
    42. pthread_exit(NULL);
    43. }
    44. void *fun2(void*arg){
    45. //写出
    46. while(1){
    47. pthread_mutex_lock(&mutex);
    48. if(flag==1){
    49. if(0==strlen(str)){//退出条件
    50. puts("read end");
    51. pthread_mutex_unlock(&mutex);
    52. pthread_exit(NULL);
    53. break;
    54. }
    55. printf("%s",str);
    56. flag=0;
    57. pthread_cond_signal(&cond);
    58. }else{
    59. pthread_cond_wait(&cond,&mutex);
    60. }
    61. pthread_mutex_unlock(&mutex);
    62. }
    63. pthread_exit(NULL);
    64. }
    65. int main(int argc, const char *argv[])
    66. {
    67. //创建两个线程,一个读取,一个写出
    68. pthread_t th1,th2;
    69. if(pthread_create(&th1,NULL,fun1,NULL)!=0){
    70. fprintf(stderr,"create failed");
    71. return -1;
    72. }
    73. if(pthread_create(&th2,NULL,fun2,NULL)!=0){
    74. fprintf(stderr,"create failed");
    75. return -1;
    76. }
    77. pthread_join(th1,NULL);
    78. pthread_join(th2,NULL);
    79. //销毁互斥锁
    80. pthread_mutex_destroy(&mutex);
    81. //摧毁条件变量
    82. pthread_cond_destroy(&cond);
    83. return 0;
    84. }

    运行结果:

    2.代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//创建锁
    6. pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
    7. pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
    8. pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
    9. int flag=1;
    10. void *fun1(void*arg){
    11. while(1){
    12. //上锁
    13. pthread_mutex_lock(&mutex);
    14. if(flag==1){
    15. printf("A");
    16. flag=2;
    17. pthread_cond_signal(&cond2);
    18. }else{
    19. pthread_cond_wait(&cond1,&mutex);
    20. }
    21. //解锁
    22. pthread_mutex_unlock(&mutex);
    23. }
    24. pthread_exit(NULL);
    25. }
    26. void *fun2(void*arg){
    27. while(1){
    28. //上锁
    29. pthread_mutex_lock(&mutex);
    30. if(flag==2){
    31. printf("B");
    32. flag=3;
    33. pthread_cond_signal(&cond3);
    34. }else{
    35. pthread_cond_wait(&cond2,&mutex);
    36. }
    37. //解锁
    38. pthread_mutex_unlock(&mutex);
    39. }
    40. pthread_exit(NULL);
    41. }
    42. void *fun3(void*arg){
    43. while(1){
    44. //上锁
    45. pthread_mutex_lock(&mutex);
    46. if(flag==3){
    47. printf("C");
    48. flag=1;
    49. pthread_cond_signal(&cond1);
    50. }else{
    51. pthread_cond_wait(&cond3,&mutex);
    52. }
    53. //解锁
    54. pthread_mutex_unlock(&mutex);
    55. }
    56. pthread_exit(NULL);
    57. }
    58. int main(int argc, const char *argv[])
    59. {
    60. pthread_t th1,th2,th3;
    61. if(pthread_create(&th1,NULL,fun1,NULL)!=0){
    62. fprintf(stderr,"thread create");
    63. return -1;
    64. }
    65. if(pthread_create(&th2,NULL,fun2,NULL)!=0){
    66. fprintf(stderr,"thread create");
    67. return -1;
    68. }
    69. if(pthread_create(&th3,NULL,fun3,NULL)!=0){
    70. fprintf(stderr,"thread create");
    71. return -1;
    72. }
    73. pthread_join(th1,NULL);
    74. pthread_join(th2,NULL);
    75. pthread_join(th3,NULL);
    76. //销毁锁
    77. pthread_mutex_destroy(&mutex);
    78. //销毁条件变量
    79. pthread_cond_destroy(&cond1);
    80. pthread_cond_destroy(&cond2);
    81. pthread_cond_destroy(&cond3);
    82. return 0;
    83. }

    运行结果

    3.代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. char buf[]="1234567";
    7. sem_t sem1;
    8. sem_t sem2;
    9. void * fun1(void*arg){
    10. while(1){
    11. if(sem_wait(&sem1)!=0){
    12. perror("fun1 wait");
    13. return NULL;
    14. }
    15. //逆置
    16. for (int i=0; i<sizeof(buf)/2; i++)
    17. {
    18. char temp=buf[i];
    19. buf[i]=buf[strlen(buf)-1-i];
    20. buf[strlen(buf)-1-i]=temp;
    21. }
    22. if(sem_post(&sem2)!=0){
    23. perror("fun1 ");
    24. }
    25. }
    26. }
    27. void* fun2(void*arg){
    28. while(1){
    29. if(sem_wait(&sem2)!=0){
    30. perror("fun1 wait");
    31. return NULL;
    32. }
    33. printf("%s\n",buf);
    34. if(sem_post(&sem1)!=0){
    35. perror("fun1 ");
    36. }
    37. }
    38. }
    39. int main(int argc, const char *argv[])
    40. {
    41. //创建信号量
    42. if(-1==sem_init(&sem1,0,1)){
    43. perror("sem create");
    44. }
    45. if(-1==sem_init(&sem2,0,0)){
    46. perror("sem create");
    47. }
    48. //创建进程
    49. pthread_t th1,th2;
    50. if(pthread_create(&th1,NULL,fun1,NULL)!=0){
    51. fprintf(stderr,"th1 create failed");
    52. return -1;
    53. }
    54. if(pthread_create(&th2,NULL,fun2,NULL)!=0){
    55. fprintf(stderr,"th2 create failed");
    56. return -1;
    57. }
    58. pthread_join(th1,NULL);
    59. pthread_join(th2,NULL);
    60. //销毁
    61. sem_destroy(&sem1);
    62. sem_destroy(&sem2);
    63. return 0;
    64. }

    运行结果:

    今日思维导图

    今天 的脑子不太够用

  • 相关阅读:
    [案例] java项目故障诊断和性能调优(Linux版)
    【数据结构】C++二叉树的实现(二叉链表),包括初始化,前序、中序、后序、层次遍历,计算节点数、叶子数、高度、宽度,二叉树的复制和销毁
    sora生成高质量视频的原理
    electron+js 通过图片地址复制图片
    JS高级 之 ES5 实现继承
    三磷酸腺苷牛血清白蛋白纳米粒|去甲肾上腺素人血清白蛋白纳米粒|多巴胺卵清白蛋白纳米粒(制备)
    前端如何通过代码模拟用户操作以及puppeteer的使用
    springboot+vue+elementui家教平台系统java267
    poj1521
    阿里seata真香,肝一下saga模式源码
  • 原文地址:https://blog.csdn.net/weixin_53762703/article/details/132842920