int main(int argc, char **argv) { int i, nloop, contpipe[2], datapipe[2]; pid_t childpid; if (argc != 4) err_quit("usage: bw_pipe <#loops> <#mbytes> <#bytes/write>"); nloop = atoi(argv[1]); totalnbytes = atoi(argv[2]) * 1024 * 1024; xfersize = atoi(argv[3]); buf = Valloc(xfersize); Touch(buf, xfersize); Pipe(contpipe); Pipe(datapipe); if ( (childpid = Fork()) == 0) { writer(contpipe[0], datapipe[1]); /* child */ exit(0); } /* 4parent */ Start_time(); for (i = 0; i < nloop; i++) reader(contpipe[1], datapipe[0], totalnbytes); printf("bandwidth: %.3f MB/sec\n", totalnbytes / Stop_time() * nloop); kill(childpid, SIGTERM); exit(0); }
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); }
int main(int argc, char **argv) { int i, nloop, pipe1[2], pipe2[2]; char c; pid_t childpid; if (argc != 2) err_quit("usage: lat_pipe <#loops>"); nloop = atoi(argv[1]); Pipe(pipe1); Pipe(pipe2); if ( (childpid = Fork()) == 0) { for ( ; ; ) { /* child */ if (Read(pipe1[0], &c, 1) != 1) err_quit("read error"); Write(pipe2[1], &c, 1); } exit(0); } /* 4parent */ doit(pipe2[0], pipe1[1]); Start_time(); for (i = 0; i < nloop; i++) doit(pipe2[0], pipe1[1]); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); exit(0); }
int main(int argc, char **argv) { int i, nloop, contpipe[2], msqid; pid_t childpid; if (argc != 4) err_quit("usage: bw_svmsg <#loops> <#mbytes> <#bytes/write>"); nloop = atoi(argv[1]); totalnbytes = atoi(argv[2]) * 1024 * 1024; xfersize = atoi(argv[3]); buf = Valloc(xfersize); Touch(buf, xfersize); buf->mtype = 1; Pipe(contpipe); msqid = Msgget(IPC_PRIVATE, IPC_CREAT | SVMSG_MODE); if ( (childpid = Fork()) == 0) { writer(contpipe[0], msqid); /* child */ exit(0); } Start_time(); for (i = 0; i < nloop; i++) reader(contpipe[1], msqid, totalnbytes); printf("bandwidth: %.3f MB/sec\n", totalnbytes / Stop_time() * nloop); kill(childpid, SIGTERM); Msgctl(msqid, IPC_RMID, NULL); exit(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, nloop, signo; pid_t childpid, parentpid; sigset_t newmask; if (argc != 2) err_quit("usage: lat_sigwait <#loops>"); nloop = atoi(argv[1]); Sigemptyset(&newmask); Sigaddset(&newmask, SIGUSR1); Sigprocmask(SIG_BLOCK, &newmask, NULL); /* block SIGUSR1 */ parentpid = getpid(); if ( (childpid = Fork()) == 0) { for (i = 0; i < nloop; i++) { /* child */ Sigwait(&newmask, &signo); Kill(parentpid, SIGUSR1); } exit(0); } /* 4parent */ Start_time(); for (i = 0; i < nloop; i++) { Kill(childpid, SIGUSR1); Sigwait(&newmask, &signo); } printf("latency: %.3f usec\n", Stop_time() / nloop); exit(0); }
int main(int argc, char **argv) { int i, nloop, doorfd, contpipe[2]; char c; pid_t childpid; door_arg_t arg; if (argc != 3) err_quit("usage: lat_door <pathname> <#loops>"); nloop = atoi(argv[2]); unlink(argv[1]); Close(Open(argv[1], O_CREAT | O_EXCL | O_RDWR, FILE_MODE)); Pipe(contpipe); if ( (childpid = Fork()) == 0) { doorfd = Door_create(server, NULL, 0); Fattach(doorfd, argv[1]); Write(contpipe[1], &c, 1); for ( ; ; ) /* child = server */ pause(); exit(0); } arg.data_ptr = &c; /* parent = client */ arg.data_size = sizeof(char); arg.desc_ptr = NULL; arg.desc_num = 0; arg.rbuf = &c; arg.rsize = sizeof(char); if (Read(contpipe[0], &c, 1) != 1) /* wait for child to create */ err_quit("pipe read error"); doorfd = Open(argv[1], O_RDWR); Door_call(doorfd, &arg); /* once to start everything */ Start_time(); for (i = 0; i < nloop; i++) Door_call(doorfd, &arg); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); unlink(argv[1]); 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); }
int main(int argc, char **argv) { int i, nloop; mqd_t mq1, mq2; char buff[MSGSIZE]; pid_t childpid; struct mq_attr attr; if (argc != 2) err_quit("usage: lat_pxmsg <#loops>"); nloop = atoi(argv[1]); attr.mq_maxmsg = MAXMSG; attr.mq_msgsize = MSGSIZE; mq1 = Mq_open(Px_ipc_name(NAME1), O_RDWR | O_CREAT, FILE_MODE, &attr); mq2 = Mq_open(Px_ipc_name(NAME2), O_RDWR | O_CREAT, FILE_MODE, &attr); if ( (childpid = Fork()) == 0) { for ( ; ; ) { /* child */ if (Mq_receive(mq1, buff, MSGSIZE, NULL) != 1) err_quit("mq_receive error"); Mq_send(mq2, buff, 1, 0); } exit(0); } /* 4parent */ doit(mq1, mq2); Start_time(); for (i = 0; i < nloop; i++) doit(mq1, mq2); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); Mq_close(mq1); Mq_close(mq2); Mq_unlink(Px_ipc_name(NAME1)); Mq_unlink(Px_ipc_name(NAME2)); exit(0); }
int main(int argc, char **argv) { int i, nthreads; pthread_t tid[MAXNTHREADS]; if (argc != 3) { fprintf(stderr, "usage: incr_rwlock1 <#loops> <#threads>"); exit(EXIT_FAILURE); } nloop = atoi(argv[1]); nthreads = min(atoi(argv[2]), MAXNTHREADS); /* obtain write lock */ Pthread_rwlock_wrlock(&shared.rwlock); /* create all the threads */ //Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { pthread_create(&tid[i], NULL, incr, NULL); } /* start the timer and release the write lock */ Start_time(); // 写锁释放之后,线程才可以运行 Pthread_rwlock_unlock(&shared.rwlock); /* wait 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); else printf("success: counter = %ld\n", shared.counter); exit(0); }
int main(int argc, char **argv) { int i, nprocs; pid_t childpid[MAXNPROC]; if (argc != 3) err_quit("usage: incr_rwlock5 <#loops> <#processes>"); nloop = atoi(argv[1]); nprocs = min(atoi(argv[2]), MAXNPROC); /* 4get shared memory for parent and children */ shared = My_shm(sizeof(struct shared)); /* 4initialize the read-write lock and obtain write lock */ Rwlock_init(&shared->rwlock, USYNC_PROCESS, NULL); Rw_wrlock(&shared->rwlock); /* 4create all the children */ for (i = 0; i < nprocs; i++) { if ( (childpid[i] = Fork()) == 0) { incr(NULL); exit(0); } } /* 4parent: start the timer and release the write lock */ Start_time(); Rw_unlock(&shared->rwlock); /* 4wait for all the children */ for (i = 0; i < nprocs; i++) { Waitpid(childpid[i], NULL, 0); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared->counter != nloop * nprocs) printf("error: counter = %ld\n", shared->counter); exit(0); }
int main(int argc, char **argv) { int i, nprocs; pid_t childpid[MAXNPROC]; if (argc != 3) err_quit("usage: incr_pxsem9 <#loops> <#processes>"); nloop = atoi(argv[1]); nprocs = min(atoi(argv[2]), MAXNPROC); /* 4get shared memory for parent and children */ shared = My_shm(sizeof(struct shared)); /* 4initialize memory-based semaphore to 0 */ Sem_init(&mutex, 1, 0); /* 4create all the children */ for (i = 0; i < nprocs; i++) { if ( (childpid[i] = Fork()) == 0) { incr(NULL); exit(0); } } /* 4parent: start the timer and release the semaphore */ Start_time(); Sem_post(&mutex); /* 4wait for all the children */ for (i = 0; i < nprocs; i++) { Waitpid(childpid[i], NULL, 0); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared->counter != nloop * nprocs) printf("error: counter = %ld\n", shared->counter); exit(0); }
int main(int argc, char **argv) { int i, nloop; CLIENT *cl; struct timeval tv; if (argc != 4) err_quit("usage: lat_sunrpc_client <hostname> <#loops> <protocol>"); nloop = atoi(argv[2]); cl = Clnt_create(argv[1], BW_SUNRPC_PROG, BW_SUNRPC_VERS, argv[3]); tv.tv_sec = 10; tv.tv_usec = 0; Start_time(); for (i = 0; i < nloop; i++) { if (clnt_call(cl, NULLPROC, xdr_void, NULL, xdr_void, NULL, tv) != RPC_SUCCESS) err_quit("%s", clnt_sperror(cl, argv[1])); } printf("latency: %.3f usec\n", Stop_time() / nloop); exit(0); }