/****************************************** * An example source module to accompany... * "Using POSIX Threads: * Programming with Pthreads" * Brad nichols, Dick Buttlar, Jackie Farrell * O'Reilly & Associates, Inc. ****************************************** * simple.c * * Simple serial example. Calls three procedures. */ void do_one_thing(int *); void do_another_thing(int *); void do_wrap_up(int, int); int r1 = 0, r2 = 0; main() { do_one_thing(&r1); do_another_thing(&r2); do_wrap_up(r1, r2); } void do_one_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing one thing\n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_another_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing another \n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_wrap_up(int one_times, int another_times) { int total; total = one_times + another_times; printf("All done, one thing %d, another %d for a total of %d\n", one_times, another_times, total); } /******************************************* * * An example source module to accompany... * "Using POSIX Threads: * Programming with Pthreads" * Brad nichols, Dick Buttlar, Jackie Farrell * O'Reilly & Associates, Inc. * ********************************************* * simple_processes.c * * Simple multi-process example. */ #include #include #include #include void do_one_thing(int *); void do_another_thing(int *); void do_wrap_up(int, int); int shared_mem_id; int *shared_mem_ptr; int *r1p; int *r2p; main() { pid_t child1_pid, child2_pid; int status=0; /* initialize shared memory segment */ if ((shared_mem_id = shmget(IPC_PRIVATE, 2*sizeof(int), 0777))<0) { perror("shmget failed"); } if((shared_mem_ptr=(int *)shmat(shared_mem_id, (void *) 0, 0)) == (void *)-1) perror("shmat failed"); r1p = shared_mem_ptr; r2p = (shared_mem_ptr + 1); *r1p = 0; *r2p = 0; if ((child1_pid = fork()) == 0) { /* first child */ do_one_thing(r1p); exit(); } /* parent */ if ((child2_pid = fork()) == 0) { /* second child */ do_another_thing(r2p); exit(); } /* parent */ waitpid(child1_pid, status, 0); waitpid(child2_pid, status, 0); do_wrap_up(*r1p, *r2p); } void do_one_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing one thing\n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_another_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing another \n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_wrap_up(int one_times, int another_times) { int total; total = one_times + another_times; printf("All done, one thing %d, another %d for a total of %d\n", one_times, another_times, total); } /*********************************************** * * "Using POSIX Threads: * Programming with Pthreads" * Brad nichols, Dick Buttlar, Jackie Farrell * O'Reilly & Associates, Inc. * *********************************************** * simple_threads.c * * Simple multi-threaded example. */ #ifdef SUN #include "../pthread_sol2.7.h" #endif #include #include #include void do_one_thing(int *); void do_another_thing(int *); void do_wrap_up(int, int); int r1 = 0, r2 = 0; main() { pthread_t thread1, thread2; pthread_create(&thread1, NULL, (void *) do_one_thing, (void *) &r1); pthread_create(&thread2, NULL, (void *) do_another_thing, (void *) &r2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); do_wrap_up(r1, r2); } void do_one_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing one thing\n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_another_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { sleep(1); printf("doing another \n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_wrap_up(int one_times, int another_times) { int total; total = one_times + another_times; printf("All done, one thing %d, another %d for a total of %d\n", one_times, another_times, total); } /********************************************* * * "Using POSIX Threads: * Programming with Pthreads" * Brad nichols, Dick Buttlar, Jackie Farrell * O'Reilly & Associates, Inc. * ********************************************* * simple_mutex.c * * Simple multi-threaded example using mutex * to protect shared data. * */ #ifdef SUN #include "../pthread_sol2.7.h" #endif #include void do_one_thing(int *); void do_another_thing(int *); void do_wrap_up(int, int); int r1 = 0, r2 = 0, r3 = 0; pthread_mutex_t r3_mutex; main(int argc, char **argv) { pthread_t thread1, thread2; pthread_mutex_init(&r3_mutex, NULL); if (argc < 2) { printf("usage %s number\n", argv[0]); exit(1); } r3 = atoi(argv[1]); pthread_create(&thread1, NULL, (void *) do_one_thing, (void *) &r1); pthread_create(&thread2, NULL, (void *) do_another_thing, (void *) &r2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); do_wrap_up(r1, r2); } void do_one_thing(int *pnum_times) { int i,x; pthread_mutex_lock(&r3_mutex); if (r3 > 3) { x = r3; r3--; } else { x = 1; } printf("do_one_thing: r3=%d,x=%d\n",r3,x); pthread_mutex_unlock(&r3_mutex); for (i = 0; i < 4; i++) { sleep(1); printf("doing one thing\n"); (*pnum_times)++; } } void do_another_thing(int *pnum_times) { int i,x; pthread_mutex_lock(&r3_mutex); if (r3 > 3) { x = r3; r3--; } else { x = 1; } printf("do_another_thing: r3=%d,x=%d\n",r3,x); pthread_mutex_unlock(&r3_mutex); for (i = 0; i < 4; i++) { sleep(1); printf("doing another \n"); (*pnum_times)++; } } void do_wrap_up(int one_times, int another_times) { int total; total = one_times + another_times; printf("All done, one thing %d, another %d for a total of %d\n", one_times, another_times, total); }