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 listenfd, *connfd, port, clientlen, i; struct sockaddr_in clientaddr; pthread_t tid; port_queue prod_var; /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } port = atoi(argv[1]); connfdqp = queueInit (); prod_var.port = port; prod_var.q = connfdqp; Pthread_create(&tid, NULL, producer, &prod_var); printf("Producer thread created %u\n", (unsigned int) tid); Pthread_detach(tid); printf ("Producer thread detached\n"); for (i = 0; i < MAXTHREAD; i++) { Pthread_create(&tid, NULL, consumer, connfdqp); printf("Consumer thread created %u\n", (unsigned int) tid); Pthread_detach(tid); printf("Consumer thread detached\n"); } printf("Main thread exited\n"); Pthread_exit(NULL); }
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[]) { if (argc != 2) { fprintf(stderr, "usage: main-first <loopcount>\n"); exit(1); } max = atoi(argv[1]); pthread_t p1, p2; printf("main: begin [counter = %d] [%x]\n", counter, (unsigned int) &counter); pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;; myarg args; args.a="A"; args.b=lock; myarg args1; args1.a="B"; args1.b=lock; Pthread_create(&p1, NULL, mythread, &args); Pthread_create(&p2, NULL, mythread, &args1); // join waits for the threads to finish Pthread_join(p1, NULL); Pthread_join(p2, NULL); printf("main: done\n [counter: %d]\n [should: %d]\n", counter, max*2); return 0; }
int main(int argc, char **argv) { pthread_t tid_produce, tid_consume; if (argc != 2) err_quit("Usage: buffer <pathname>"); fd = Open(argv[1], O_RDONLY); Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); Pthread_setconcurrency(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_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main(int argc, char **argv) { int niters; pthread_t tid1, tid2; //check input argument if (argc != 2) { printf("usage: %s <niters>\n", argv[0] ); exit(0); } niters = atoi(argv[1]); //create threads and wait for them to finish Pthread_create(&tid1, NULL, thread, &niters); Pthread_create(&tid2, NULL, thread, &niters); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); //check result if (cnt != (2 * niters)) printf("BOOM! cnt=%d\n", cnt); else printf("OK cnt=%d\n", cnt); exit(0); }
template<bool check_depleted> void abc_para_test() { concat<char> str , *dep1 = new concat<char> , *dep2 = new concat<char> ; assert(!str.ready()); assert(!dep1->closed()); assert(!dep2->closed()); pthread_t thread[2]; Pthread_create(&thread[0], NULL, abc, dep1); Pthread_detach(thread[0]); Pthread_create(&thread[1], NULL, abc, dep2); Pthread_detach(thread[1]); str.give(dep1); str.give(dep2); str.close(); for (int i = 0; i < 2; i++) { for (char let = 'a'; let <= 'z'; let++) { if (check_depleted) { assert(!str.depleted()); assert(str.ready()); } assert(str.get() == let); } } assert(!str.ready()); assert(str.depleted()); assert(str.closed()); }
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; }
/* 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 != 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) { pthread_t tid_produce, tid_consume; if (argc != 2) err_quit("Usage: pc <#items>"); nitems = atoi(argv[1]); //create three semaphore 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); //create one producer thread and one consumer thread Pthread_setconcurrency(2); Pthread_create(&tid_produce, NULL, produce, NULL); Pthread_create(&tid_consume, NULL, consume, NULL); //wait for the two threads Pthread_join(tid_produce, NULL); Pthread_join(tid_consume, NULL); //remove the semaphores 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[]) { 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; }
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) { 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); nthreads = min(atoi(argv[2]), MAXNTHREADS); Pthread_setconcurrency(nthreads); // start all the producer threads for (i = 0; i < nthreads; i++) { count[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &count[i]); } //wait for all the producer threads for (i = 0; i < nthreads; i++) { Pthread_join(tid_produce[i], NULL); printf("count[%d] = %d\n", i, count[i]); } //start then wait for the consumer thread Pthread_create(&tid_consume, NULL, consume, NULL); Pthread_join(tid_consume, NULL); 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 tidA, tidB; Pthread_create(&tidA, NULL, &doit, NULL); Pthread_create(&tidB, NULL, &doit, NULL); /* 4wait for both threads to terminate */ Pthread_join(tidA, NULL); Pthread_join(tidB, NULL); exit(0); }
int main(int argc, char **argv) { ifstream fin; fin.open(argv[1]); ms.InitMS(fin); cout<<ms; ms.Setsocket(); int sockfd_tcp, sockfd_udp, maxfd; struct sockaddr_in srvaddr; fd_set allset, rset; socklen_t len = sizeof(struct sockaddr_in); sockfd_tcp = ms.Getsockfd_tcp(); sockfd_udp = ms.Getsockfd_udp(); maxfd = max(sockfd_tcp, sockfd_udp) + 1; FD_ZERO(&allset); FD_SET(sockfd_tcp, &allset); FD_SET(sockfd_udp, &allset); char buf[MAXLINE]; int i; pthread_t tid_probe; Pthread_create(&tid_probe, NULL, &probing, NULL); //create an another thread to timely probe the time_sheet in order to find the failed server while(1) { Signal(SIGINT, sig_int_handle); Signal(SIGTERM, sig_term_handle); if (sigsetjmp(jmpbuf, 1)!=0 || sigsetjmp(jmpbuf, 2)!=0) //if SIGINT or SIGTERM signal caught, master close all sockets and exits { ms.Closesockets(); cout<<"Master exits, all sockets are closed"<<endl; exit(1); } rset = allset; if ((Select(maxfd, &rset, NULL, NULL, NULL)) < 1) //master listen to its udp socket to receive ping from server continue; if (FD_ISSET(sockfd_udp, &rset)) { i = recvfrom(sockfd_udp, buf, MAXLINE, 0, (SA*)&srvaddr, &len); if (i < 0) { cerr<<"recv error"<<endl; continue; } else { pthread_t tid; Pthread_create(&tid, NULL, &handle_ping, (void *)buf); //when received server ping, handle it } } } return 1; }
int main() { pthread_t tid1, tid2; Pthread_create(&tid1, NULL, count, NULL); Pthread_create(&tid2, NULL, count, NULL); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); if (cnt != (unsigned)NITERS*2) printf("BOOM! cnt=%d\n", cnt); else printf("OK cnt=%d\n", cnt); exit(0); }
int main(int argc, char *argv[]){ if(argc!=2){ fprintf(stderr, "parameter useage: theread<value>\n"); exit(1); } loops = atoi(argv[1]); pthread_t p1,p2; printf("Initial value of Counter : %d\n",counter); Pthread_create(&p1,NULL,worker,NULL); Pthread_create(&p2,NULL,worker,NULL); Pthread_join(p1,NULL); Pthread_join(p2,NULL); printf("Final value of Counter : %d\n",counter); return 0; }
int main(int argc, char **argv) { int listenfd_e,listenfd_t, *iptr,*jptr; char port_echo[6],port_time[6]; pthread_t tid; socklen_t addrlen, len; struct sockaddr *cliaddr; int maxfdp1; fd_set rset; strcpy(port_echo,"9870"); //assumed to be the well-known port number for echo service strcpy(port_time,"9880"); //assumed to be the well-known port number for time service listenfd_e = Tcp_listen(NULL, port_echo, &addrlen); //set up listening socket for echo service listenfd_t= Tcp_listen(NULL, port_time, NULL); //set up listening socket for time service cliaddr = Malloc(addrlen); FD_ZERO(&rset); for ( ; ; ) { len = addrlen; iptr = Malloc(sizeof(int)); jptr = Malloc(sizeof(int)); FD_SET(listenfd_e, &rset); FD_SET(listenfd_t, &rset); maxfdp1 = max(listenfd_e,listenfd_t) + 1; // printf("Entering main select..."); Select(maxfdp1, &rset, NULL, NULL, NULL); // printf("Exited main select"); if (FD_ISSET(listenfd_e, &rset)) { // Listening socket for echo service is readable *iptr = Accept(listenfd_e, cliaddr, &len); Pthread_create(&tid, NULL, &doecho, iptr); } if (FD_ISSET(listenfd_t, &rset)) { // Listening socket for time service is readable *jptr = Accept(listenfd_t, cliaddr, &len); Pthread_create(&tid, NULL, &dotime, jptr); } } }
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() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); exit(0); //line:conc:hellobug:exit }
int main(int argc, char *argv[]) { int listenfd, connfd; socklen_t len, addrlen; struct sockaddr *cliaddr; pthread_t tid; if (argc == 2) { listenfd = Tcp_listen(NULL, argv[1], &addrlen); } if (argc == 3) { listenfd = Tcp_listen(argv[1], argv[2],&addrlen); } if (argc != 3 && argc != 2) { err_quit("usage : tcpserver_thread [<host>] <port or service>"); } cliaddr = Malloc(addrlen); for ( ; ; ) { len = addrlen; connfd = Accept(listenfd, cliaddr, &len); Pthread_create(&tid, NULL, &doit, (void*)connfd); } return 0; }
int main(int argc, char *argv[]) { int listenfd; socklen_t clientlen; struct sockaddr_storage clientaddr; pthread_t tid; init_cache(); signal(SIGPIPE, SIG_IGN); /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); while (1) { int *connfdp = Malloc(sizeof(int)); clientlen = sizeof(clientaddr); *connfdp = Accept(listenfd, (SA *)&clientaddr, &clientlen); Pthread_create(&tid, NULL, doit_thread, connfdp); } delete_cache(); return 0; }
int child_make(struct child_s *ptr) { pthread_t thread; Pthread_create(&thread, NULL, child_main, (void *)ptr); if(!thread) return 0; else return -1; }
int main(int argc, char** argv) { int listenfd, *connfdp, port; socklen_t clientlen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; if (argc != 2) { fprintf(stderr, "usage: %s <port (above 1024)>\n", argv[0]); exit(0); } port = atoi(argv[1]); if (port <= 1024) { fprintf(stderr, "usage: %s <port (above 1024)>\n", argv[0]); exit(0); } message_queue mq; mq.tm = (tid_message*)calloc(1, sizeof(tid_message)); mq.tm->tid = 0; mq.tm->message = 0; mq.tm->tid_array = 0; mq.tm->tid_count = 0; mq.tm->tid_message = 0; listenfd = Open_listenfd(port); while (1) { connfdp = Malloc(sizeof(int)); *connfdp = Accept(listenfd, (SA*)&clientaddr, &clientlen); Pthread_create(&tid, NULL, thread, connfdp); } }
int main() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); sleep(5); exit(0); }
int main(int argc, char** argv) { int listenfd, connfd; pthread_t tid; socklen_t addrlen, len; struct sockaddr* cliaddr; if(argc == 2) { listenfd = Tcp_listen(NULL, argv[1], &addrlen); } else if(argc == 3) { listenfd = Tcp_listen(argv[1], argv[2], &addrlen); } else { err_quit("usage: tcpserv01 [<host>] <service or port>"); } cliaddr = Malloc(addrlen); while(1) { len = addrlen; connfd = Accept(listenfd, cliaddr, &len); Pthread_create(&tid, NULL, &doit, (void*)&connfd); } }
int main() //line:conc:hello:main { pthread_t tid; //line:conc:hello:tid Pthread_create(&tid, NULL, thread, NULL); //line:conc:hello:create Pthread_join(tid, NULL); //line:conc:hello:join exit(0); //line:conc:hello:exit }
int main(int argc, char **argv) { int listenfd, *connfdp, port; socklen_t clientlen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); Signal(SIGPIPE, sigpipe_handler); cache_init(); listenfd = Open_listenfd(port); while (1) { connfdp = Malloc(sizeof(int)); *connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen); Pthread_create(&tid, NULL, thread, connfdp); } return 0; }