@@ -98,63 +98,71 @@ struct sigaction sig_helper;
9898 */
9999void * do_thread (void * data )
100100{
101- //printf("WĄTECZEK!\n");
102-
103101 Msg th_msg ;
104102 int th_k , n_1 , n_2 ;
105103 int pid_1 , pid_2 ;
106104
107105 sscanf ((char * ) data , "%d %d %d %d %d" , & th_k , & pid_1 , & n_1 , & pid_2 , & n_2 );
108- //printf("AA %d %d %d %d %d\n", th_k, pid_1, n_1, pid_2, n_2);
109106
110107 //MUTEX LOCK
111- pthread_mutex_lock (& mutex );
108+ if (pthread_mutex_lock (& mutex ) != 0 )
109+ syserr ("Error in mutex lock | thread 1\n" );
112110 thread_counter ++ ;
113111
114112 while (resources [th_k ] < n_1 + n_2 )
115113 pthread_cond_wait (type_cond + th_k , & mutex );
116114
115+ resources [th_k ] -= n_1 ;
116+ resources [th_k ] -= n_2 ;
117+
118+ printf ("Wątek %ld przydziela %d+%d zasobów %d klientom %d %d, pozostało %d zasobów\n" ,
119+ (long )pthread_self (), n_1 , n_2 , th_k , pid_1 , pid_2 , resources [th_k ]);
120+
121+ if (pthread_mutex_unlock (& mutex ) != 0 )
122+ syserr ("Error in mutex unlock | thread 1\n" );
123+ ///MUTEX UNLOCK
124+
117125 th_msg .msg_type = pid_1 ;
118126 strcpy (th_msg .data , "" );
119127 sprintf (th_msg .data , "%d" , pid_2 );
120- printf ("CO TO KURWA %s %d\n" , th_msg .data , (int )strlen (th_msg .data ));
121128 if ( msgsnd (conf_qid , (char * ) & th_msg , strlen (th_msg .data ), 0 ) != 0 )
122129 syserr ("Error in msgsnd | conf pid1\n" );
123130
124131 th_msg .msg_type = pid_2 ;
125132 strcpy (th_msg .data , "" );
126133 sprintf (th_msg .data , "%d" , pid_1 );
127- printf ("CO TO KURWA %s %d\n" , th_msg .data , (int )strlen (th_msg .data ));
128134 if ( msgsnd (conf_qid , (char * ) & th_msg , strlen (th_msg .data ), 0 ) != 0 )
129135 syserr ("Error in msgsnd | conf pid2\n" );
130136
131- resources [th_k ] -= n_1 ;
132- resources [th_k ] -= n_2 ;
133- printf ("Wątek %ld przydziela %d+%d zasobów %d klientom %d %d, pozostało %d zasobów\n" ,
134- (long )pthread_self , n_1 , n_2 , th_k , pid_1 , pid_2 , resources [th_k ]);
135-
136137 if ( msgrcv (fin_qid , & th_msg , MAX_DATA_SIZE , pid_1 , 0 ) == -1 )
137- syserr ("Error in msgrcv | fin_qid pid 1" );
138+ syserr ("Error in msgrcv | fin_qid pid 1\n " );
138139
139140 if ( msgrcv (fin_qid , & th_msg , MAX_DATA_SIZE , pid_2 , 0 ) == -1 )
140- syserr ("Error in msgrcv | fin_qid pid 2" );
141+ syserr ("Error in msgrcv | fin_qid pid 2\n" );
142+
143+ //MUTEX LOCK
144+ if (pthread_mutex_lock (& mutex ) != 0 )
145+ syserr ("Error in lock | thread 2\n" );
141146
142147 resources [th_k ] += n_1 ;
143148 resources [th_k ] += n_2 ;
149+ pthread_cond_signal (type_cond + th_k );
144150
145151 thread_counter -- ;
146- pthread_mutex_unlock (& mutex );
147- ///MUTEX UNLOCK
152+ if (pthread_mutex_unlock (& mutex ) != 0 )
153+ syserr ("Error in unlock | thread 2\n" );
154+ //MUTEX UNLOCK
155+
148156 pthread_cond_signal (& fin_cond );
149- return 0 ;
157+ exit ( 0 ) ;
150158}
151159
152160/*
153161 * Ustawia flagę mówiącą o otrzymaniu SIGINT.
154162 */
155163void setFlag ()
156164{
157- // printf("Ustawiam isStopped\n");
165+ printf ("Ustawiam isStopped\n" );
158166 isStopped = 1 ;
159167}
160168
@@ -163,7 +171,7 @@ void setFlag()
163171 */
164172void free_sysres ()
165173{
166- // printf("\nZWALNIAM ZASOBY!\n");
174+ printf ("\nZWALNIAM ZASOBY!\n" );
167175
168176 if (msgctl (req_qid , IPC_RMID , 0 ) == -1 )
169177 syserr ("Error in msgctl\n" );
@@ -185,7 +193,7 @@ void free_sysres()
185193 if (pthread_mutex_destroy (& mutex ) != 0 )
186194 syserr ("Error in destroy mutex \n" );
187195
188- // printf("\nZWOLNILEM\n");
196+ printf ("\nZWOLNILEM\n" );
189197}
190198
191199/*
@@ -263,7 +271,6 @@ int main(int argc, char* argv[])
263271 char msg_buf [100 ];
264272 while ((size_rcv = msgrcv (req_qid , & msg , MAX_DATA_SIZE , 0 , 0 )) && isStopped == 0 )
265273 {
266- printf ("DOSTALEM REQUEST! :) %s\n" , msg .data );
267274
268275 int k_req , n_req ;
269276 sscanf (msg .data , "%d %d" , & k_req , & n_req );
@@ -285,10 +292,7 @@ int main(int argc, char* argv[])
285292 }
286293
287294 while (thread_counter > 0 )
288- {
289- //printf("SIGNAL, czekam na koniec watkow\n");
290295 pthread_cond_wait (& fin_cond , & mutex );
291- }
292296
293297 free_sysres ();
294298 exit (0 );
0 commit comments