/* * Client routine -- multiple copies will request server. */ void *client_routine (void *arg) { int my_number = (int)arg, loops; char prompt[32]; char string[128], formatted[128]; int status; sprintf (prompt, "Client %d> ", my_number); while (1) { tty_server_request (REQ_READ, 1, prompt, string); if (strlen (string) == 0) break; for (loops = 0; loops < 4; loops++) { sprintf ( formatted, "(%d#%d) %s", my_number, loops, string); tty_server_request (REQ_WRITE, 0, NULL, formatted); sleep (1); } } status = pthread_mutex_lock (&client_mutex); if (status != 0) err_abort (status, "Lock client mutex"); client_threads--; if (client_threads <= 0) { status = pthread_cond_signal (&clients_done); if (status != 0) err_abort (status, "Signal clients done"); } status = pthread_mutex_unlock (&client_mutex); if (status != 0) err_abort (status, "Unlock client mutex"); return NULL; }
void *client_routine(void *args) { int id = (int)args, i, status; char prompt[PROMPT_MAX]; char text[TEXT_MAX], formatted[TEXT_MAX]; sprintf(prompt, "Client %d>\n", id); tty_server_request(REQ_READ, 1, prompt, text); for (i = 0; i < 4; ++i) { if (strlen(text) == 0) { break; } sprintf(formatted, "(%d#%d) %s\n", id, i, text); tty_server_request(REQ_WRITE, 0, NULL, formatted); sleep(1); } status = pthread_mutex_lock(&client_mutex); if (status != 0) { err_abort(status, "Lock client mutex"); } client_thread--; status = pthread_cond_signal(&client_cond); if (status != 0) { err_abort(status, "Signal client thread quit"); } status = pthread_mutex_unlock(&client_mutex); if (status != 0) { err_abort(status, "Unlock client mutex"); } return NULL; }
int main (int argc, char *argv[]) { pthread_t thread; int count; int status; #ifdef sun /* * On Solaris 2.5, threads are not timesliced. To ensure * that our threads can run concurrently, we need to * increase the concurrency level to CLIENT_THREADS. */ DPRINTF (("Setting concurrency level to %d\n", CLIENT_THREADS)); thr_setconcurrency (CLIENT_THREADS); #endif /* * Create CLIENT_THREADS clients. */ client_threads = CLIENT_THREADS; for (count = 0; count < CLIENT_THREADS; count++) { status = pthread_create (&thread, NULL, client_routine, (void*)count); if (status != 0) err_abort (status, "Create client thread"); } status = pthread_mutex_lock (&client_mutex); if (status != 0) err_abort (status, "Lock client mutex"); while (client_threads > 0) { status = pthread_cond_wait (&clients_done, &client_mutex); if (status != 0) err_abort (status, "Wait for clients to finish"); } status = pthread_mutex_unlock (&client_mutex); if (status != 0) err_abort (status, "Unlock client mutex"); printf ("All clients done\n"); tty_server_request (REQ_QUIT, 1, NULL, NULL); return 0; }
int main() { int status, i; pthread_t thread; // create client thread client_thread = CLIENT_NUMBER; for (i = 0; i < client_thread; ++i) { status = pthread_create(&thread, NULL, client_routine, (void *)i); if (status != 0) { err_abort(status, "Create client thread"); } } // wait client thread quit status = pthread_mutex_lock(&client_mutex); if (status != 0) { err_abort(status, "Lock client mutex"); } while (client_thread > 0) { status = pthread_cond_wait(&client_cond, &client_mutex); if (status != 0) { err_abort(status, "Wait client condition"); } } status = pthread_mutex_unlock(&client_mutex); if (status != 0) { err_abort(status, "Unlock client mutex"); } printf("All client thread quit\n"); // quit server thread tty_server_request(REQ_QUIT, 1, NULL, NULL); return 0; }