int main(int argc, char **argv) { int i, nthreads; pthread_t tid[MAXNTHREADS]; if (argc != 3) err_quit("usage: incr_pxsem1 <#loops> <#threads>"); nloop = atoi(argv[1]); nthreads = min(atoi(argv[2]), MAXNTHREADS); /* 4initialize memory-based semaphore to 0 */ Sem_init(&shared.mutex, 0, 0); /* 4create all the threads */ Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { Pthread_create(&tid[i], NULL, incr, NULL); } /* 4start the timer and release the semaphore */ Start_time(); Sem_post(&shared.mutex); /* 4wait for all the threads */ for (i = 0; i < nthreads; i++) { Pthread_join(tid[i], NULL); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared.counter != nloop * nthreads) printf("error: counter = %ld\n", shared.counter); exit(0); }
/* include main */ int main(int argc, char **argv) { int i, nproducers, nconsumers, countp[MAXNTHREADS], countc[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS]; if (argc != 4) err_quit("usage: prodcons6 <#items> <#producers> <#consumers>"); nitems = min(atoi(argv[1]), MAXNITEMS); nproducers = min(atoi(argv[2]), MAXNTHREADS); nconsumers = min(atoi(argv[3]), MAXNTHREADS); Set_concurrency(nproducers + nconsumers); /* 4create all producers and one consumer */ for (i = 0; i < nproducers; i++) { countp[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &countp[i]); } for (i = 0; i < nconsumers; i++) { countc[i] = 0; Pthread_create(&tid_consume[i], NULL, consume, &countc[i]); } /* wait for all producers and the consumer */ for (i = 0; i < nproducers; i++) { Pthread_join(tid_produce[i], NULL); printf("countp[%d] = %d\n", i, countp[i]); } for (i = 0; i < nconsumers; i++) { Pthread_join(tid_consume[i], NULL); printf("countc[%d] = %d\n", i, countc[i]); } exit(0); }
int main(int argc, char **argv) { pthread_t tid_produce, tid_consume; if (argc != 1) err_quit("usage: deadlock <#items>"); nitems = atoi(argv[1]); /* 4create three semaphores */ shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1); shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF); shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0); Set_concurrency(2); Pthread_create(&tid_produce, NULL, produce, NULL); Pthread_create(&tid_consume, NULL, consume, NULL); Pthread_join(tid_produce, NULL); Pthread_join(tid_consume, NULL); Sem_unlink(Px_ipc_name(SEM_MUTEX)); Sem_unlink(Px_ipc_name(SEM_NEMPTY)); Sem_unlink(Px_ipc_name(SEM_NSTORED)); exit(0); }
int main(int argc, char *argv[]) { int i, nthreads, count[MAXNITEMS]; pthread_t tid_produce[MAXNTHREADS], tid_consume; if (argc != 3) err_quit("usage: producons2 <#items> <#threads>"); nitems = min(atoi(argv[1]), MAXNITEMS); nthreads= min(atoi(argv[2]), MAXNTHREADS); Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { count[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &count[i]); } for (i = 0; i < nthreads; i++) { Pthread_join(tid_produce[i], NULL); printf("count[%d] = %d \n", i, count[i]); } Pthread_create(&tid_consume, NULL, consume, NULL); Pthread_join(tid_consume, NULL); return 0; }
int main(int argc, char *argv[]) { pthread_t tid_produce, tid_consume; if(argc != 2) err_quit("usage: prodcons1 <#items>"); nitems = atoi(argv[1]); /*创建信号量*/ shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1); shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF); shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0); Set_concurrency(2);/*线程并发处理*/ /*创建两个线程*/ Pthread_create(&tid_produce, NULL, produce, NULL); Pthread_create(&tid_consume, NULL, consume, NULL); /*主线程等待两个线程*/ Pthread_join(tid_produce, NULL); Pthread_join(tid_consume, NULL); /*释放信号量*/ Sem_unlink(Px_ipc_name(SEM_MUTEX)); Sem_unlink(Px_ipc_name(SEM_NEMPTY)); Sem_unlink(Px_ipc_name(SEM_NSTORED)); return 0; }
/* include main */ int main(int argc, char **argv) { int i, nthreads, count[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume; if (argc != 3) err_quit("usage: prodcons4 <#items> <#threads>"); nitems = min(atoi(argv[1]), MAXNITEMS); nthreads = min(atoi(argv[2]), MAXNTHREADS); Set_concurrency(nthreads + 1); /* 4create all producers and one consumer */ for (i = 0; i < nthreads; i++) { count[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &count[i]); } Pthread_create(&tid_consume, NULL, consume, NULL); /* wait for all producers and the consumer */ for (i = 0; i < nthreads; i++) { Pthread_join(tid_produce[i], NULL); printf("count[%d] = %d\n", i, count[i]); } Pthread_join(tid_consume, NULL); exit(0); }
int main(int argc, char **argv) { pthread_t tid_produce, tid_consume; if (argc != 2) err_quit("usage: mycat2 <pathname>"); fd = Open(argv[1], O_RDONLY); /* 4initialize three semaphores */ Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); /* 4one producer thread, one consumer thread */ Set_concurrency(2); Pthread_create(&tid_produce, NULL, produce, NULL); /* reader thread */ Pthread_create(&tid_consume, NULL, consume, NULL); /* writer thread */ Pthread_join(tid_produce, NULL); Pthread_join(tid_consume, NULL); Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main(int argc,char **argv) { pthread_t produce_tid,consume_tid; if(argc!=2) err_quit("usage: mycat <pathname> "); if((fd=open(argv[1],O_RDONLY))==-1) err_sys("open error"); Sem_init(&shared.mutex,0,1); Sem_init(&shared.nempty,0,NBUFF); Sem_init(&shared.nstored,0,0); Set_concurrency(2); Pthread_create(&produce_tid,NULL,produce,NULL); Pthread_create(&consume_tid,NULL,consume,NULL); Pthread_join(produce_tid,NULL); Pthread_join(consume_tid,NULL); Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main(int argc, char *argv[]) { int i, nthreads, count[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume; if(argc != 3) err_quit("usage: prodcons2 <#items> <#threads>"); nitems = min(atoi(argv[1]), MAXNITEMS); Set_concurrency(nthreads + 1);/*线程并发设置*/ /*创建线程生产者*/ for(i = 0; i < nthreads; ++i){ count[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &count[i]); } /*创建消费者线程*/ Pthread_create(&tid_consume, NULL, comsume, NULL); /*主线程等待子线程结束*/ for(i = 0; i < nthreads; ++i){ Pthread_join(tid_produce[i], NULL); printf ("count[%d] = %d\n",i, count[i]); } Pthread_join(tid_consume, NULL); return 0; }
int main(int argc, char **argv) { int i, nthreads; pthread_t tid[MAXNTHREADS]; if (argc != 3) err_quit("usage: incr_rwlock1 <#loops> <#threads>"); nloop = atoi(argv[1]); nthreads = min(atoi(argv[2]), MAXNTHREADS); /* 4obtain write lock */ Rw_wrlock(&shared.rwlock); /* 4create all the threads */ Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { Pthread_create(&tid[i], NULL, incr, NULL); } /* 4start the timer and release the write lock */ Start_time(); Rw_unlock(&shared.rwlock); /* 4wait for all the threads */ for (i = 0; i < nthreads; i++) { Pthread_join(tid[i], NULL); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared.counter != nloop * nthreads) printf("error: counter = %ld\n", shared.counter); exit(0); }
int main(int argc, char **argv) { int i, nthreads; pthread_t tid[MAXNTHREADS]; union semun arg; if (argc != 3) err_quit("usage: incr_svsem2 <#loops> <#threads>"); nloop = atoi(argv[1]); nthreads = min(atoi(argv[2]), MAXNTHREADS); /* 4create semaphore and initialize to 0 */ shared.semid = Semget(IPC_PRIVATE, 1, IPC_CREAT | SVSEM_MODE); arg.val = 0; Semctl(shared.semid, 0, SETVAL, arg); postop.sem_num = 0; /* and init the two semop() structures */ postop.sem_op = 1; postop.sem_flg = SEM_UNDO; waitop.sem_num = 0; waitop.sem_op = -1; waitop.sem_flg = SEM_UNDO; /* 4create all the threads */ Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { Pthread_create(&tid[i], NULL, incr, NULL); } /* 4start the timer and release the semaphore */ Start_time(); Semop(shared.semid, &postop, 1); /* up by 1 */ /* 4wait for all the threads */ for (i = 0; i < nthreads; i++) { Pthread_join(tid[i], NULL); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared.counter != nloop * nthreads) printf("error: counter = %ld\n", shared.counter); Semctl(shared.semid, 0, IPC_RMID); exit(0); }
/* include main */ int main(int argc, char **argv) { int i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS]; if (argc != 4) err_quit("usage: prodcons4 <#items> <#producers> <#consumers>"); nitems = atoi(argv[1]); nproducers = min(atoi(argv[2]), MAXNTHREADS); nconsumers = min(atoi(argv[3]), MAXNTHREADS); /* 4initialize three semaphores */ Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); /* 4create all producers and all consumers */ Set_concurrency(nproducers + nconsumers); for (i = 0; i < nproducers; i++) { prodcount[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]); } for (i = 0; i < nconsumers; i++) { conscount[i] = 0; Pthread_create(&tid_consume[i], NULL, consume, &conscount[i]); } /* 4wait for all producers and all consumers */ for (i = 0; i < nproducers; i++) { Pthread_join(tid_produce[i], NULL); printf("producer count[%d] = %d\n", i, prodcount[i]); } for (i = 0; i < nconsumers; i++) { Pthread_join(tid_consume[i], NULL); printf("consumer count[%d] = %d\n", i, conscount[i]); } Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }