Linux多进程实例与代码优化

32 篇文章 0 订阅
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include <pthread.h>  
  5.   
  6. #define BUFFER_SIZE 16  
  7.   
  8. struct prodcons  
  9. {  
  10.     int buffer[BUFFER_SIZE];  
  11.     pthread_mutex_t lock;  
  12.     int readpos,writepos;  
  13.     pthread_cond_t notempty;  
  14.     pthread_cond_t notfull;  
  15. };  
  16.   
  17. void init(struct prodcons* b)  
  18. {  
  19.     pthread_mutex_init(&b->lock,NULL);  
  20.     pthread_cond_init(&b->notempty,NULL);  
  21.     pthread_cond_init(&b->notfull,NULL);  
  22.     b->readpos = 0;  
  23.     b->writepos = 0;  
  24. }  
  25.   
  26. void put(struct prodcons* b,int data)  
  27. {  
  28.     pthread_mutex_lock(&b->lock);  
  29.     while((b->writepos +1)%BUFFER_SIZE==b->readpos)  
  30.     {  
  31.         printf("wait for not full.\n");  
  32.         pthread_cond_wait(&b->notfull,&b->lock);  
  33.     }  
  34.     b->buffer[b->writepos] = data;  
  35.     b->writepos++;  
  36.     if(b->writepos >= BUFFER_SIZE)b->writepos=0;  
  37.     pthread_cond_signal(&b->notempty);  
  38.     pthread_mutex_unlock(&b->lock);  
  39. }  
  40.   
  41. int get(struct prodcons *b)  
  42. {  
  43.     int data;  
  44.     pthread_mutex_lock(&b->lock);  
  45.     while(b->writepos == b->readpos)  
  46.     {  
  47.         printf("wait for not empty");  
  48.         pthread_cond_wait(&b->notempty,&b->lock);  
  49.     }  
  50.     data = b->buffer[b->readpos];  
  51.     b->readpos++;  
  52.     if(b->readpos >=BUFFER_SIZE)b->readpos=0;  
  53.     pthread_cond_signal(&b->notfull);  
  54.     pthread_mutex_unlock(&b->lock);  
  55.     return data;  
  56. }  
  57.   
  58. #define OVER (-1)  
  59. struct prodcons buffer;  
  60.   
  61. void *producer(void* data)  
  62. {  
  63.     int n;  
  64.     for(n=0;n<100;n++)  
  65.     {  
  66.         printf("put---->%d.\n",n);  
  67.         put(&buffer,n);  
  68.     }  
  69.     put(&buffer,OVER);  
  70.     printf("producer stopped!\n");  
  71.     return NULL;  
  72. }  
  73.   
  74. void *consumer(void *data)  
  75. {  
  76.     int d;  
  77.     while(1)  
  78.     {  
  79.         d = get(&buffer);  
  80.         if(d==OVER)break;  
  81.         printf("        %d---->get.\n",d);  
  82.     }  
  83.     printf("consumer stopped!\n");  
  84.     return NULL;  
  85. }  
  86.   
  87. int main(void)  
  88. {  
  89.     pthread_t th_a,th_b;  
  90.     void *retval;  
  91.     init(&buffer);  
  92.     pthread_create(&th_a,NULL,producer,0);  
  93.     pthread_create(&th_b,NULL,consumer,0);  
  94.     pthread_join(th_a,&retval);  
  95.     pthread_join(th_b,&retval);  
  96.     return 0;  
  97. }  

这时一个生产者与消费者的程序,程序实现了生产者与消费者并行运行,并且共用一个缓存区。其中使用到一个互斥锁和信号量来共同控制对缓存区的互斥访问,缓存的状态(空,满)的协调。但是全都在一个文件之中以至于main.c十分臃肿,下面时自己的进行的代码优化

main.c

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include <pthread.h>  
  5. #include "pro_con.h"  
  6.   
  7. int main(void)  
  8. {  
  9.     pthread_t th_a,th_b;  
  10.     void *retval;  
  11.     fun.init(&buffer);  
  12.     pthread_create(&th_a,NULL,fun.producer,0);  
  13.     pthread_create(&th_b,NULL,fun.consumer,0);  
  14.     pthread_join(th_a,&retval);  
  15.     pthread_join(th_b,&retval);  
  16.     return 0;  
  17. }  
pro_con.c

  1. //------------------Include Files--------------------------------  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4. #include "pro_con.h"  
  5. //-------------------------Function Prototype------------------------  
  6. static void c_init(struct prodcons* b);                     //初始化信号量  
  7. static void c_put(struct prodcons* b,int data);             //向缓存中放入数据  
  8. static int c_get(struct prodcons* b);  
  9. static void* c_producer(void* data);  
  10. static void* c_consumer(void* data);  
  11.   
  12.   
  13. //---------------------------Variable-----------------------------  
  14. prodcons_fun fun=  
  15. {  
  16.     c_init,  
  17.     c_put,  
  18.     c_get,  
  19.     c_producer,  
  20.     c_consumer  
  21. };  
  22. struct prodcons buffer;  
  23.   
  24. /*  
  25.  *============Function=======================================  
  26.  * Name         :   init  
  27.  * Description  :   初始化缓存指针信息  
  28.  *============================================================  
  29.  */   
  30. static void c_init(struct prodcons* b)               //初始化信号量  
  31. {  
  32.     pthread_mutex_init(&b->lock,NULL);  
  33.     pthread_cond_init(&b->notempty,NULL);  
  34.     pthread_cond_init(&b->notfull,NULL);  
  35.     b->readpos = 0;  
  36.     b->writepos = 0;  
  37. }  
  38. /*  
  39.  *============Function========================================  
  40.  * Name             : put  
  41.  * Description      :向缓存中放入数据  
  42.  *=============================================================  
  43.  */  
  44.   
  45. static void c_put(struct prodcons* b,int data)  
  46. {  
  47.     pthread_mutex_lock(&b->lock);  
  48.     while((b->writepos + 1)%BUFFER_SIZE==b->readpos)  
  49.     {  
  50.         printf("wait for not full.\n");  
  51.         pthread_cond_wait(&b->notfull,&b->lock);  
  52.     }  
  53.     b->buffer[b->writepos] = data;  
  54.     b->writepos++;  
  55.     if(b->writepos >= BUFFER_SIZE)b->writepos=0;  
  56.     pthread_cond_signal(&b->notempty);  
  57.     pthread_mutex_unlock(&b->lock);  
  58. }  
  59.   
  60. /*  
  61.  *===========Function===========================================  
  62.  * Name                 :get  
  63.  * Description          :从缓存中获取数据  
  64.  *=============================================================  
  65.  */  
  66. static int c_get(struct prodcons* b)  
  67. {  
  68.     int data;  
  69.     pthread_mutex_lock(&b->lock);  
  70.     while(b->writepos == b->readpos)  
  71.     {  
  72.         printf("wait for not empty");  
  73.         pthread_cond_wait(&b->notempty,&b->lock);  
  74.     }  
  75.     data = b->buffer[b->readpos];  
  76.     b->readpos++;  
  77.     if(b->readpos >= BUFFER_SIZE)b->readpos=0;  
  78.     pthread_cond_signal(&b->notfull);  
  79.     pthread_mutex_unlock(&b->lock);  
  80.     return data;  
  81. }  
  82.   
  83. /*  
  84.  *================Function========================================  
  85.  * Name                :producer  
  86.  * Description          :实现一个生产者程序,当生产-1时,程序终止  
  87.  *===============================================================  
  88.  */  
  89. static void* c_producer(void* data)  
  90. {  
  91.     int n;  
  92.     for(n=0;n<100;n++)  
  93.     {  
  94.         printf("put---->%d.\n",n);  
  95.         c_put(&buffer,n);  
  96.     }  
  97.     c_put(&buffer,OVER);  
  98.     printf("producer stopped!\n");  
  99.     return NULL;  
  100. }  
  101.   
  102. /*  
  103.  *==============Function======================================  
  104.  * Name               :consumer  
  105.  * Description        :消费掉缓存中生产出来的数据,当消费-1时,程序终止  
  106.  *=================================================================  
  107.  */  
  108. static void* c_consumer(void *data)  
  109. {  
  110.     int d;  
  111.     while(1)  
  112.     {  
  113.         d = c_get(&buffer);  
  114.         if(d==OVER)break;  
  115.         printf("        %d---->get.\n",d);  
  116.     }  
  117.     printf("consumer stopped!\n");  
  118.     return NULL;  
  119. }  
pro_con.h

  1. //---------------Defines----------------------------------  
  2. #define OVER (-1)          //结束标志  
  3. #define BUFFER_SIZE 16     //使用缓存大小  
  4.   
  5.   
  6. //--------------Type define-----------------------------------------  
  7. struct prodcons  
  8. {  
  9.     int buffer[BUFFER_SIZE];   //缓存申请  
  10.     pthread_mutex_t lock;      //缓存锁  
  11.     int readpos,writepos;      //读写的位置指针  
  12.     pthread_cond_t notempty;  // 不空的信号量  
  13.     pthread_cond_t notfull;   //不满的信号量  
  14. };  
  15.   
  16. typedef struct   
  17. {  
  18.     void (*init)(struct prodcons* b);             //初始化缓存信息  
  19.     void (*put)(struct prodcons* b,int data);     //向缓存中放入数据  
  20.     int (*get)(struct prodcons* b);       //从缓存中获得数据  
  21.     void* (*producer)(void* data);        //  
  22.     void* (*consumer)(void* data);        //  
  23. }prodcons_fun;  
  24. //-------------Extern----------------------------------------------  
  25. extern prodcons_fun fun;  
  26. extern struct prodcons buffer;  

下面主要是讲讲extern关键字与static关键字(来源百度百科),以及在把函数封装在结构体之中。

extern:

extern可以置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义。例如:在main.c中根本没有声明变量fun与buffer,但是确可以直接使用。extern的原理很简单,就是告诉编译器:“你现在编译的文件中,有一个标识符虽然没有在本文件中定义,但是它是在别的文件中定义的全局变量,你要放行!”如extern int a;仅仅是一个变量的声明,其并不是在定义变量a,并未为a分配内存空间。变量a在所有模块中作为一种全局变量只能被定义一次,否则会出现连接错误。


static:

当用static修饰变量或者函数的时候,这个变量或者函数将变为静态的,变为静态的有下面几个好处:一:静态全局变量不能被其它文件所用;二:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名,因为同名也没有关系。


最后在prodcons_fun这个结构体中把所有用到的函数都封装了起来,在此要注意这个结构体的成员永远是  init,put,get,producer,consumer,这五个,因此当fun被初始化为c_init,c_put,c_get,c_producer,c_consumer时,最后调用,还是使用 init,put,get,producer,consumer。因为为初始化的过程中,只是把这几个具体实现函数的函数指针(函数入口地址)传递给了 init,put,get,producer,consumer,这些成员。

还要注意的是,在编译时,需要加上 -l pthread,因为pthread不是默认链接的函数库,如果不加,就会出现找不到有关进程的一些相互函数。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include <pthread.h>  
  5.   
  6. #define BUFFER_SIZE 16  
  7.   
  8. struct prodcons  
  9. {  
  10.     int buffer[BUFFER_SIZE];  
  11.     pthread_mutex_t lock;  
  12.     int readpos,writepos;  
  13.     pthread_cond_t notempty;  
  14.     pthread_cond_t notfull;  
  15. };  
  16.   
  17. void init(struct prodcons* b)  
  18. {  
  19.     pthread_mutex_init(&b->lock,NULL);  
  20.     pthread_cond_init(&b->notempty,NULL);  
  21.     pthread_cond_init(&b->notfull,NULL);  
  22.     b->readpos = 0;  
  23.     b->writepos = 0;  
  24. }  
  25.   
  26. void put(struct prodcons* b,int data)  
  27. {  
  28.     pthread_mutex_lock(&b->lock);  
  29.     while((b->writepos +1)%BUFFER_SIZE==b->readpos)  
  30.     {  
  31.         printf("wait for not full.\n");  
  32.         pthread_cond_wait(&b->notfull,&b->lock);  
  33.     }  
  34.     b->buffer[b->writepos] = data;  
  35.     b->writepos++;  
  36.     if(b->writepos >= BUFFER_SIZE)b->writepos=0;  
  37.     pthread_cond_signal(&b->notempty);  
  38.     pthread_mutex_unlock(&b->lock);  
  39. }  
  40.   
  41. int get(struct prodcons *b)  
  42. {  
  43.     int data;  
  44.     pthread_mutex_lock(&b->lock);  
  45.     while(b->writepos == b->readpos)  
  46.     {  
  47.         printf("wait for not empty");  
  48.         pthread_cond_wait(&b->notempty,&b->lock);  
  49.     }  
  50.     data = b->buffer[b->readpos];  
  51.     b->readpos++;  
  52.     if(b->readpos >=BUFFER_SIZE)b->readpos=0;  
  53.     pthread_cond_signal(&b->notfull);  
  54.     pthread_mutex_unlock(&b->lock);  
  55.     return data;  
  56. }  
  57.   
  58. #define OVER (-1)  
  59. struct prodcons buffer;  
  60.   
  61. void *producer(void* data)  
  62. {  
  63.     int n;  
  64.     for(n=0;n<100;n++)  
  65.     {  
  66.         printf("put---->%d.\n",n);  
  67.         put(&buffer,n);  
  68.     }  
  69.     put(&buffer,OVER);  
  70.     printf("producer stopped!\n");  
  71.     return NULL;  
  72. }  
  73.   
  74. void *consumer(void *data)  
  75. {  
  76.     int d;  
  77.     while(1)  
  78.     {  
  79.         d = get(&buffer);  
  80.         if(d==OVER)break;  
  81.         printf("        %d---->get.\n",d);  
  82.     }  
  83.     printf("consumer stopped!\n");  
  84.     return NULL;  
  85. }  
  86.   
  87. int main(void)  
  88. {  
  89.     pthread_t th_a,th_b;  
  90.     void *retval;  
  91.     init(&buffer);  
  92.     pthread_create(&th_a,NULL,producer,0);  
  93.     pthread_create(&th_b,NULL,consumer,0);  
  94.     pthread_join(th_a,&retval);  
  95.     pthread_join(th_b,&retval);  
  96.     return 0;  
  97. }  


这时一个生产者与消费者的程序,程序实现了生产者与消费者并行运行,并且共用一个缓存区。其中使用到一个互斥锁和信号量来共同控制对缓存区的互斥访问,缓存的状态(空,满)的协调。但是全都在一个文件之中以至于main.c十分臃肿,下面时自己的进行的代码优化

main.c

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include <pthread.h>  
  5. #include "pro_con.h"  
  6.   
  7. int main(void)  
  8. {  
  9.     pthread_t th_a,th_b;  
  10.     void *retval;  
  11.     fun.init(&buffer);  
  12.     pthread_create(&th_a,NULL,fun.producer,0);  
  13.     pthread_create(&th_b,NULL,fun.consumer,0);  
  14.     pthread_join(th_a,&retval);  
  15.     pthread_join(th_b,&retval);  
  16.     return 0;  
  17. }  

pro_con.c

  1. //------------------Include Files--------------------------------  
  2. #include <stdio.h>  
  3. #include <pthread.h>  
  4. #include "pro_con.h"  
  5. //-------------------------Function Prototype------------------------  
  6. static void c_init(struct prodcons* b);                     //初始化信号量  
  7. static void c_put(struct prodcons* b,int data);             //向缓存中放入数据  
  8. static int c_get(struct prodcons* b);  
  9. static void* c_producer(void* data);  
  10. static void* c_consumer(void* data);  
  11.   
  12.   
  13. //---------------------------Variable-----------------------------  
  14. prodcons_fun fun=  
  15. {  
  16.     c_init,  
  17.     c_put,  
  18.     c_get,  
  19.     c_producer,  
  20.     c_consumer  
  21. };  
  22. struct prodcons buffer;  
  23.   
  24. /*  
  25.  *============Function=======================================  
  26.  * Name         :   init  
  27.  * Description  :   初始化缓存指针信息  
  28.  *============================================================  
  29.  */   
  30. static void c_init(struct prodcons* b)               //初始化信号量  
  31. {  
  32.     pthread_mutex_init(&b->lock,NULL);  
  33.     pthread_cond_init(&b->notempty,NULL);  
  34.     pthread_cond_init(&b->notfull,NULL);  
  35.     b->readpos = 0;  
  36.     b->writepos = 0;  
  37. }  
  38. /*  
  39.  *============Function========================================  
  40.  * Name             : put  
  41.  * Description      :向缓存中放入数据  
  42.  *=============================================================  
  43.  */  
  44.   
  45. static void c_put(struct prodcons* b,int data)  
  46. {  
  47.     pthread_mutex_lock(&b->lock);  
  48.     while((b->writepos + 1)%BUFFER_SIZE==b->readpos)  
  49.     {  
  50.         printf("wait for not full.\n");  
  51.         pthread_cond_wait(&b->notfull,&b->lock);  
  52.     }  
  53.     b->buffer[b->writepos] = data;  
  54.     b->writepos++;  
  55.     if(b->writepos >= BUFFER_SIZE)b->writepos=0;  
  56.     pthread_cond_signal(&b->notempty);  
  57.     pthread_mutex_unlock(&b->lock);  
  58. }  
  59.   
  60. /*  
  61.  *===========Function===========================================  
  62.  * Name                 :get  
  63.  * Description          :从缓存中获取数据  
  64.  *=============================================================  
  65.  */  
  66. static int c_get(struct prodcons* b)  
  67. {  
  68.     int data;  
  69.     pthread_mutex_lock(&b->lock);  
  70.     while(b->writepos == b->readpos)  
  71.     {  
  72.         printf("wait for not empty");  
  73.         pthread_cond_wait(&b->notempty,&b->lock);  
  74.     }  
  75.     data = b->buffer[b->readpos];  
  76.     b->readpos++;  
  77.     if(b->readpos >= BUFFER_SIZE)b->readpos=0;  
  78.     pthread_cond_signal(&b->notfull);  
  79.     pthread_mutex_unlock(&b->lock);  
  80.     return data;  
  81. }  
  82.   
  83. /*  
  84.  *================Function========================================  
  85.  * Name                :producer  
  86.  * Description          :实现一个生产者程序,当生产-1时,程序终止  
  87.  *===============================================================  
  88.  */  
  89. static void* c_producer(void* data)  
  90. {  
  91.     int n;  
  92.     for(n=0;n<100;n++)  
  93.     {  
  94.         printf("put---->%d.\n",n);  
  95.         c_put(&buffer,n);  
  96.     }  
  97.     c_put(&buffer,OVER);  
  98.     printf("producer stopped!\n");  
  99.     return NULL;  
  100. }  
  101.   
  102. /*  
  103.  *==============Function======================================  
  104.  * Name               :consumer  
  105.  * Description        :消费掉缓存中生产出来的数据,当消费-1时,程序终止  
  106.  *=================================================================  
  107.  */  
  108. static void* c_consumer(void *data)  
  109. {  
  110.     int d;  
  111.     while(1)  
  112.     {  
  113.         d = c_get(&buffer);  
  114.         if(d==OVER)break;  
  115.         printf("        %d---->get.\n",d);  
  116.     }  
  117.     printf("consumer stopped!\n");  
  118.     return NULL;  
  119. }  

pro_con.h

  1. //---------------Defines----------------------------------  
  2. #define OVER (-1)          //结束标志  
  3. #define BUFFER_SIZE 16     //使用缓存大小  
  4.   
  5.   
  6. //--------------Type define-----------------------------------------  
  7. struct prodcons  
  8. {  
  9.     int buffer[BUFFER_SIZE];   //缓存申请  
  10.     pthread_mutex_t lock;      //缓存锁  
  11.     int readpos,writepos;      //读写的位置指针  
  12.     pthread_cond_t notempty;  // 不空的信号量  
  13.     pthread_cond_t notfull;   //不满的信号量  
  14. };  
  15.   
  16. typedef struct   
  17. {  
  18.     void (*init)(struct prodcons* b);             //初始化缓存信息  
  19.     void (*put)(struct prodcons* b,int data);     //向缓存中放入数据  
  20.     int (*get)(struct prodcons* b);       //从缓存中获得数据  
  21.     void* (*producer)(void* data);        //  
  22.     void* (*consumer)(void* data);        //  
  23. }prodcons_fun;  
  24. //-------------Extern----------------------------------------------  
  25. extern prodcons_fun fun;  
  26. extern struct prodcons buffer;  

下面主要是讲讲extern关键字与static关键字(来源百度百科),以及在把函数封装在结构体之中。

extern:

extern可以置于变量或者函数前,以表示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义。例如:在main.c中根本没有声明变量fun与buffer,但是确可以直接使用。extern的原理很简单,就是告诉编译器:“你现在编译的文件中,有一个标识符虽然没有在本文件中定义,但是它是在别的文件中定义的全局变量,你要放行!”如extern int a;仅仅是一个变量的声明,其并不是在定义变量a,并未为a分配内存空间。变量a在所有模块中作为一种全局变量只能被定义一次,否则会出现连接错误。


static:

当用static修饰变量或者函数的时候,这个变量或者函数将变为静态的,变为静态的有下面几个好处:一:静态全局变量不能被其它文件所用;二:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名,因为同名也没有关系。


最后在prodcons_fun这个结构体中把所有用到的函数都封装了起来,在此要注意这个结构体的成员永远是  init,put,get,producer,consumer,这五个,因此当fun被初始化为c_init,c_put,c_get,c_producer,c_consumer时,最后调用,还是使用 init,put,get,producer,consumer。因为为初始化的过程中,只是把这几个具体实现函数的函数指针(函数入口地址)传递给了 init,put,get,producer,consumer,这些成员。

还要注意的是,在编译时,需要加上 -l pthread,因为pthread不是默认链接的函数库,如果不加,就会出现找不到有关进程的一些相互函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值