11//Maciej Andrearczyk, 333856
22//
33//Proces serwera.
4+ //Jeden muteks do ochrony zmiennych,
5+ //zmienna warunkowa na każdy typ zasobów.
6+ //Przy otrzymaniu SIG_INT czekam, az wszystkie wątki się zakończą
7+ //po czym usuwam kolejki.
48#include "err.h"
59#include "mesg.h"
610#include <sys/types.h>
@@ -110,7 +114,8 @@ void *do_thread(void *data)
110114 thread_counter ++ ;
111115
112116 while (resources [th_k ] < n_1 + n_2 )
113- pthread_cond_wait (type_cond + th_k , & mutex );
117+ if (pthread_cond_wait (type_cond + th_k , & mutex ) != 0 )
118+ syserr ("Error in cond wait type_cond\n" );
114119
115120 resources [th_k ] -= n_1 ;
116121 resources [th_k ] -= n_2 ;
@@ -146,14 +151,16 @@ void *do_thread(void *data)
146151
147152 resources [th_k ] += n_1 ;
148153 resources [th_k ] += n_2 ;
149- pthread_cond_signal (type_cond + th_k );
154+ if (pthread_cond_signal (type_cond + th_k ) != 0 )
155+ syserr ("Error in cond signal type_cond\n" );
150156
151157 thread_counter -- ;
152158 if (pthread_mutex_unlock (& mutex ) != 0 )
153159 syserr ("Error in unlock | thread 2\n" );
154160 //MUTEX UNLOCK
155161
156- pthread_cond_signal (& fin_cond );
162+ if (pthread_cond_signal (& fin_cond ) != 0 )
163+ syserr ("Error in cond signal fin_cond\n" );
157164 free (data );
158165 return 0 ;
159166}
@@ -202,6 +209,7 @@ void free_sysres()
202209 */
203210void create_thread_tools ()
204211{
212+ atexit (free_sysres );
205213 if ((pthread_mutex_init (& mutex , 0 )) != 0 )
206214 syserr ("mutex init\n" );
207215
@@ -226,22 +234,13 @@ void create_thread_tools()
226234void create_queues ()
227235{
228236 if ( (req_qid = msgget (REQ_KEY , 0666 | IPC_CREAT | IPC_EXCL )) == -1 )
229- {
230- free_sysres ();
231237 syserr ("Error in msgget | request\n" );
232- }
233238
234239 if ( (conf_qid = msgget (CONF_KEY , 0666 | IPC_CREAT | IPC_EXCL )) == -1 )
235- {
236- free_sysres ();
237240 syserr ("Error in msgget | confirmation\n" );
238- }
239241
240242 if ( (fin_qid = msgget (FIN_KEY , 0666 | IPC_CREAT | IPC_EXCL )) == -1 )
241- {
242- free_sysres ();
243243 syserr ("Error in msgget | finish\n" );
244- }
245244}
246245
247246int main (int argc , char * argv [])
@@ -288,13 +287,14 @@ int main(int argc, char* argv[])
288287 type_pid [k_req ], type_N [k_req ]);
289288 type_pid [k_req ] = 0 ;
290289 type_N [k_req ] = 0 ;
291- pthread_create (& th , & attr , do_thread , msg_buf );
290+ if (pthread_create (& th , & attr , do_thread , msg_buf ) != 0 )
291+ syserr ("Error in pthread_create\n" );
292292 }
293293 }
294294
295295 while (thread_counter > 0 )
296- pthread_cond_wait (& fin_cond , & mutex );
296+ if (pthread_cond_wait (& fin_cond , & mutex ) != 0 )
297+ syserr ("Error in cond wait\n" );
297298
298- free_sysres ();
299299 exit (0 );
300300}
0 commit comments