如题所述:

读者写者问题

有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写。

 

  1 /**
  2  * Description: reader & wirter (可中文)
  3  * Author: gaover
  4  * Email: gaover@126.com
  5  */
  6 #include <stdio.h>
  7 #include <stdlib.h>
  8 #include <string.h>
  9 #include <pthread.h>
 10 #include <errno.h>
 11 #include <stdarg.h>
 12 #include <unistd.h>
 13 #include <limits.h>
 14 #include <sys/ipc.h>
 15 #include <sys/msg.h>
 16 #include <sys/types.h>  
 17 #include <sys/time.h>
 18 #include <signal.h>
 19  
 20  
 21 #define FULL    5           // 仓库满状态,抢iphone哟,这么少
 22 #define EMPTY   0           //  仓库空状态
 23 #define INIT_DEPOT_NUM  2   // 初始被写状态
 24 #define READER_NUM 9     //  读者数量 
 25 #define WRITER_NUM 1      // 写者数量
 26 #define RUN     0           //  系统运行状态
 27 #define OVER    1           // 系统关闭状态
 28 
 29 int what;                //  写者写了什么?
 30 int shutdown;
 31 pthread_rwlock_t mylock = PTHREAD_RWLOCK_INITIALIZER;
 32 
 33 
 34 #define myprintf( ... ) \
 35     do{ \
 36     mprintf(__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__); \
 37     }while(0)
 38 
 39 
 40 
 41 
 42 int mprintf(const char *file_name, const char *func_name, int line, const char *fmt, ...)
 43 {
 44     va_list     strArgPtr;
 45     int la,lb;
 46     la = fprintf(stdout, "[%-15s]-[%-10s]-[%04d]", file_name, func_name, line);
 47     va_start(strArgPtr, fmt);
 48     lb = vfprintf(stdout, fmt, strArgPtr);
 49     va_end(strArgPtr);
 50     printf("\n");
 51     fflush(stdout);
 52     return (la+lb);
 53 }
 54 
 55 void * fun_writer(void *arg)
 56 {
 57     int i;
 58     int name = (int)arg;
 59     for(;;)
 60     {
 61             if( pthread_rwlock_wrlock(&mylock))
 62             {
 63                 myprintf("pthread_rwlock_lock error errmsg[%s]\n", strerror(errno));
 64                 exit(0);
 65             }
 66 //            while(1)
 67             {
 68                 if(shutdown == OVER)
 69                 {
 70                     myprintf("writer id [%d] go home now", name);
 71                     if(pthread_rwlock_unlock(&mylock))
 72                     {
 73                         myprintf("pthread_rwlock_unlock error errmsg[%s]\n", strerror(errno));
 74                         exit(0);
 75                     }
 76                     pthread_exit(NULL);    
 77                 }
 78                 srand( time(NULL) ); 
 79                 what = rand()%10000 + name;
 80                 myprintf("writer id [%d] put what [%d] into depot",name, what);
 81                         
 82 //                if(pthread_cond_wait(&what_cond, &mylock))
 83 //                {
 84 //                    myprintf("wait error errmsg[%s]\n", strerror(errno));
 85 //                    exit(0);
 86 //                }
 87             }
 88             if(pthread_rwlock_unlock(&mylock))
 89             {
 90                 myprintf("pthread_rwlock_unlock error errmsg[%s]\n", strerror(errno));
 91                 exit(0);
 92             }
 93 //            if(pthread_cond_broadcast(&consume_cond))
 94 //            {
 95 //                myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno));
 96 //                exit(0);
 97 //            }
 98             sleep(1);
 99     }
100 }
101 
102 void * fun_reader(void *arg)
103 {
104     int i;
105     int name = (int)arg;
106     for(;;)
107     {
108             if( pthread_rwlock_rdlock(&mylock))
109             {
110                 myprintf("pthread_rwlock_lock error errmsg[%s]\n", strerror(errno));
111                 exit(0);
112             }
113 //            while(1)
114             {
115                 if(shutdown == OVER)
116                 {
117                     myprintf("reader id [%d] go home now", name);
118                     if(pthread_rwlock_unlock(&mylock))
119                     {
120                         myprintf("pthread_rwlock_unlock error errmsg[%s]\n", strerror(errno));
121                         exit(0);
122                     }
123                     pthread_exit(NULL);    
124                 }
125 
126                 myprintf("reader id [%d] read what from depot [%d]", name, what);   
127 //                if(pthread_cond_wait(&consume_cond, &mylock))
128 //                {
129 //                    myprintf("wait error errmsg[%s]\n", strerror(errno));
130 //                    exit(0);
131 //                }
132             }
133             if(pthread_rwlock_unlock(&mylock))
134             {
135                 myprintf("pthread_rwlock_unlock error errmsg[%s]\n", strerror(errno));
136                 exit(0);
137             }
138 //            if(pthread_cond_broadcast(&what_cond))
139 //            {
140 //                myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno));
141 //                exit(0);
142 //            }
143             sleep(1);
144     }
145 }
146 
147 void allover(int sig)
148 {
149         if( pthread_rwlock_wrlock(&mylock))
150         {
151             myprintf("pthread_rwlock_lock error errmsg[%s]\n", strerror(errno));
152             exit(0);
153         }
154         shutdown = OVER;
155         if( pthread_rwlock_unlock(&mylock))
156         {
157             myprintf("pthread_rwlock_lock error errmsg[%s]\n", strerror(errno));
158             exit(0);
159         }
160 //        if(pthread_cond_broadcast(&consume_cond))
161 //        {
162 //            myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno));
163 //            exit(0);
164 //        }
165 //        if(pthread_cond_broadcast(&what_cond))
166 //        {
167 //            myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno));
168 //            exit(0);
169 //        }
170 }
171 
172 void over(int arg)
173 {
174      kill(SIGKILL, getpid());   
175 }
176 
177 int appInit()
178 {
179     shutdown = RUN;
180     int i;
181     what = INIT_DEPOT_NUM;
182     if ( signal(SIGUSR1, allover)== SIG_ERR)
183     {
184         myprintf("signal  failure");    
185         return -1;    
186     }
187     if ( signal(SIGTERM, allover)== SIG_ERR)
188     {
189         myprintf("signal  failure");    
190         return -1;    
191     }
192     if ( signal(SIGINT, allover)== SIG_ERR)
193     {
194         myprintf("signal  failure");    
195         return -1;    
196     }
197     if ( signal(SIGQUIT, allover)== SIG_ERR)
198     {
199         myprintf("signal  failure");    
200         return -1;    
201     }
202 //    pthread_rwlock_init(&mylock, NULL);
203 //    pthread_cond_init(&cond, NULL);
204     return 0; 
205 }
206 
207 int appDone()
208 {
209     int i;
210 //    pthread_cond_destroy(&cond);
211 //    pthread_rwlock_destroy(&mylock);
212     return 0;
213 }
214 
215 int main(int argc, char *argv[])
216 {
217  
218     pthread_t writer[WRITER_NUM];
219     pthread_t reader[READER_NUM];
220 
221     int ret;
222     int i;
223     ret = appInit();
224     if(ret)
225     {
226         myprintf("appinit failure");
227         return -1;    
228     }
229     
230     for( i = 0; i < READER_NUM; i++)
231     {
232         ret = pthread_create(&reader[i], NULL, fun_reader, (void*)i);
233         if(ret)
234         {
235             myprintf("pthread_create ta error [%s]", strerror(errno));
236             return 1;
237         }        
238     }
239     
240     for( i = 0; i < WRITER_NUM; i++)
241     {
242         ret = pthread_create(&writer[i], NULL, fun_writer, (void*)i);
243         if(ret)
244         {
245             myprintf("pthread_create ta error [%s]", strerror(errno));
246             return 1;
247         }         
248     }
249     for( i = 0; i < READER_NUM; i++)
250         pthread_join(reader[i], NULL);
251     for( i = 0; i < WRITER_NUM; i++)
252         pthread_join(writer[i], NULL);
253 
254     myprintf("main thread exit");
255     ret = appDone();
256     if(ret)
257     {
258         myprintf("appDone failure");
259         return -1;    
260     }
261     return 0;
262 }
View Code

 

线程相关的操作大类都涉及到了,再多点就是这几个操作相关的属性操作,这是微操。之前的代码都是对概念的理解及使用。相当于知道有这么个东西的存在了,接下来找点其它题做。

推荐本书,《linux环境高级编程》看了这本书,linux下系统应用级的编程是没有问题的,网上买也就60左右。

1-6代码都简单,唯一不简单的是坚持。以前,什么编程类书籍都看过,前不久想写点程序玩下,发现这些看过的都没印象,完全写不出程序。虽然工作内没问题,如果仅是这样,那真是不太好的事。

想想看了perl编程,有一只草泥马在封面上的那本,说还有印象的原因可能是与C语言相似,加上这两年复制粘贴大量的C代码,一些类C的编程语言没什么难度,当然精通是另外一回事。

说说看书的好处吧。 虽然工作中对编程要求不太高,但书看多了,对程序的理解还真是不一样,当然让我写可能写不出来。平时练的太少,这也是此系列出现的原因。巩固。

所以说,看了还得练。古话有云:学不思而罔,思不学则挂。正是这个道理,以前就怕自己哪种技术不知道,看了一堆书,算法导论,汇编,perl,linux啥啥,tcp/ip,看的多,不怎么巩固,一味求多,现在能记着的九牛一毛。想想真是浪费不少时间。

准备去弄一个份研究性的工作。工作四年,说后悔的事只有浪费不少时间。当然也不能为难自己,没有任何人指导,按着现在才明白是真理的东西,然则在以前是让人很头疼的。

写这个系列的目的,主要是为了自己的学习与思考,当然如果有某位兄台有意推荐下研究性工作也是欢迎的,不过(可恶的不过),博客应该没多少人看。权当自己学习记录吧。

另外我这有大量技术类书籍二手出售,有意者call 我。gaover@126.com。 过几天,把淘宝连接发上来。

 

posted on 2014-09-04 11:03  Notflybird  阅读(196)  评论(0)    收藏  举报