多线程PV

  1 #include <STDIO.H>     
  2 #include <windows.h>
  3 //#include "stdafx.h"  
  4 #include <process.h> // _beginthread && _endthread  
  5 //#include "BaseOperation.h"  
  6 #define N 10     
  7     
  8 typedef int semaphore;  /* 信号量是一种特殊的整型变量 */    
  9      
 10  semaphore mutex=1;     /* 互斥访问 */    
 11  semaphore empty=N;     /* 记录缓冲池中空的缓冲区数 */    
 12  semaphore full=0;      /* 记录缓冲池中满的缓冲区数*/    
 13     
 14  semaphore buf[N];      /* 有N个缓冲区数的缓冲池buf[N],并实现循环缓冲队列 */    
 15  semaphore in=0, out=0;    
 16      
 17   void p(semaphore *x)  /* p操作 */    
 18  {    
 19     *x=(*x)-1;    
 20  }    
 21     
 22  void v(semaphore *y)   /* v操作 */    
 23  {    
 24     *y=(*y)+1;    
 25  }    
 26     
 27   void produce_item(int *item_ptr)    
 28  {    
 29     /*printf("produce an item
");*/    
 30     *item_ptr='F';      /* 'F' is "Full满" */    
 31  }    
 32     
 33  void enter_item(int x)    
 34  {    
 35     in=(out+1)%N;    
 36     buf[in]=x;           
 37     printf("enter_item %c to buf[%d]
", buf[in], in);    
 38  }    
 39     
 40 void remove_item(int *yy)    
 41 {    
 42     out=(out+1)%N;    
 43     printf("remove_item %c from buf[%d]", buf[out], out);    
 44     *yy=buf[out];    
 45     buf[out]='E';      /* 'E' is "Empty空" */    
 46     printf("  so the buf[%d] changed to empty--%c
", out, buf[out]);    
 47 }    
 48 void consume_item(int y)    
 49 {    
 50     printf("cosume the item :the screem print %c
", y);    
 51 }    
 52     
 53 void producer(void);    
 54 void consumer(void);    
 55   int item;  
 56 DWORD WINAPI ThreadProc1( LPVOID lpParam ) 
 57 {
 58 
 59 int i=0,j=0;
 60 while(1)
 61 {
 62 produce_item(&item);
 63 
 64 //延时
 65 for(i=0;i<200000000;i++)
 66 {
 67 ;
 68 }
 69 }
 70 } 
 71 /* 生产者 */    
 72 
 73 DWORD WINAPI ThreadProc2( LPVOID lpParam ) 
 74 {
 75 
 76 int i=0,j=0;
 77 while(1)
 78 {
 79 consumer();
 80 
 81 //延时
 82 for(i=0;i<200000000;i++)
 83 {
 84 ;
 85 }
 86 }
 87 } 
 88 
 89 void producer(void)    
 90 {    
 91         
 92     
 93     while(1){    
 94         //_beginthread(produce_item(&item),0,NULL);
 95         CreateThread( 
 96 NULL, // default security attributes
 97 0, // use default stack size 
 98 ThreadProc1, // thread function 
 99 NULL, // argument to thread function 
100 0, // use default creation flags 
101 NULL); // returns the thread identifier 
102         p(&empty);          /* 递减空缓冲区数 */    
103         p(&mutex);          /* 进入临界区 */    
104         enter_item(item);   /* 将一个新的数据项放入缓冲池 */    
105         v(&mutex);          /* 离开临界区 */    
106         v(&full); /* 递增满缓冲区数 */    
107         if(full==N)         /* 若缓冲池满的话,唤醒消费者进程 */    
108             CreateThread( 
109 NULL, // default security attributes
110 0, // use default stack size 
111 ThreadProc2, // thread function 
112 NULL, // argument to thread function 
113 0, // use default creation flags 
114 NULL); // returns the thread identifier   
115     }     
116 }    
117     
118 /* 消费者 */    
119 void consumer(void)    
120 {    
121     int get_item;    
122     
123     while(1){    
124             p(&full);           /* 递减满缓冲区数 */    
125             p(&mutex);          /* 进入临界区 */    
126             remove_item(&get_item); /* 从缓冲池中取走一个数据项 */    
127             v(&mutex);          /* 离开临界区 */    
128             v(&empty);          /* 递增空缓冲区数 */    
129             consume_item(get_item); /* 对数据项进行操作(消费)*/    
130             if(empty==N)        /* 若缓冲池全空的话,唤生产者进程 */    
131                 producer();             
132     }    
133  }    
134     
135  /* 调用生产者-消费者进程实现进程间同步 */    
136  main()    
137 {    
138     producer();    
139     
140     return 0;    
141 }   
  1 #include <STDIO.H>     
  2 #include <windows.h>
  3 //#include "stdafx.h"  
  4 #include <process.h> // _beginthread && _endthread  
  5 //#include "BaseOperation.h"  
  6 #define N 10     
  7     
  8 typedef int semaphore;  /* 信号量是一种特殊的整型变量 */    
  9      
 10  semaphore mutex=1;     /* 互斥访问 */    
 11  semaphore empty=N;     /* 记录缓冲池中空的缓冲区数 */    
 12  semaphore full=0;      /* 记录缓冲池中满的缓冲区数*/    
 13     
 14  semaphore buf[N];      /* 有N个缓冲区数的缓冲池buf[N],并实现循环缓冲队列 */    
 15  semaphore in=0, out=0;    
 16      
 17   void p(semaphore *x)  /* p操作 */    
 18  {    
 19     *x=(*x)-1;    
 20  }    
 21     
 22  void v(semaphore *y)   /* v操作 */    
 23  {    
 24     *y=(*y)+1;    
 25  }    
 26     
 27   void produce_item(int *item_ptr)    
 28  {    
 29     /*printf("produce an item
");*/    
 30     *item_ptr='F';      /* 'F' is "Full满" */    
 31  }    
 32     
 33  void enter_item(int x)    
 34  {    
 35     in=(out+1)%N;    
 36     buf[in]=x;           
 37     printf("enter_item %c to buf[%d]
", buf[in], in);    
 38  }    
 39     
 40 void remove_item(int *yy)    
 41 {    
 42     out=(out+1)%N;    
 43     printf("remove_item %c from buf[%d]", buf[out], out);    
 44     *yy=buf[out];    
 45     buf[out]='E';      /* 'E' is "Empty空" */    
 46     printf("  so the buf[%d] changed to empty--%c
", out, buf[out]);    
 47 }    
 48 void consume_item(int y)    
 49 {    
 50     printf("cosume the item :the screem print %c
", y);    
 51 }    
 52     
 53 void producer(void);    
 54 void consumer(void);    
 55   int item;  
 56 DWORD WINAPI ThreadProc1( LPVOID lpParam ) 
 57 {
 58 
 59 int i=0,j=0;
 60 while(1)
 61 {
 62 produce_item(&item);
 63 
 64 //延时
 65 Sleep(1);
 66 }
 67 } 
 68 /* 生产者 */    
 69 
 70 DWORD WINAPI ThreadProc2( LPVOID lpParam ) 
 71 {
 72 
 73 int i=0,j=0;
 74 while(1)
 75 {
 76 consumer();
 77 
 78 //延时
 79 Sleep(1);
 80 }
 81 } 
 82 
 83 void producer(void)    
 84 {    
 85         
 86     
 87     while(1){    
 88         //_beginthread(produce_item(&item),0,NULL);
 89         CreateThread( 
 90 NULL, // default security attributes
 91 0, // use default stack size 
 92 ThreadProc1, // thread function 
 93 NULL, // argument to thread function 
 94 0, // use default creation flags 
 95 NULL); // returns the thread identifier 
 96         p(&empty);          /* 递减空缓冲区数 */    
 97         p(&mutex);          /* 进入临界区 */    
 98         enter_item(item);   /* 将一个新的数据项放入缓冲池 */    
 99         v(&mutex);          /* 离开临界区 */    
100         v(&full); /* 递增满缓冲区数 */    
101         if(full==N)         /* 若缓冲池满的话,唤醒消费者进程 */    
102             CreateThread( 
103 NULL, // default security attributes
104 0, // use default stack size 
105 ThreadProc2, // thread function 
106 NULL, // argument to thread function 
107 0, // use default creation flags 
108 NULL); // returns the thread identifier   
109     }     
110 }    
111     
112 /* 消费者 */    
113 void consumer(void)    
114 {    
115     int get_item;    
116     
117     while(1){    
118             p(&full);           /* 递减满缓冲区数 */    
119             p(&mutex);          /* 进入临界区 */    
120             remove_item(&get_item); /* 从缓冲池中取走一个数据项 */    
121             v(&mutex);          /* 离开临界区 */    
122             v(&empty);          /* 递增空缓冲区数 */    
123             consume_item(get_item); /* 对数据项进行操作(消费)*/    
124             if(empty==N)        /* 若缓冲池全空的话,唤生产者进程 */    
125                 producer();             
126     }    
127  }    
128     
129  /* 调用生产者-消费者进程实现进程间同步 */    
130  main()    
131 {    
132     producer();    
133     
134     return 0;    
135 }   
原文地址:https://www.cnblogs.com/mxdmxdmxd78/p/5489697.html