int main(int argc, char **argv) { threadpool_t *pool; pthread_mutex_init(&lock, NULL); assert((pool = threadpool_create(THREAD, QUEUE, 0)) != NULL); fprintf(stderr, "Pool started with %d threads and " "queue size of %d\n", THREAD, QUEUE); while(threadpool_add(pool, &dummy_task, NULL, 0) == 0) { /* pthread_mutex_lock(&lock); */ /* tasks++; */ /* pthread_mutex_unlock(&lock); */ } /* fprintf(stderr, "Added %d tasks\n", tasks); */ while((tasks) > done) { usleep(10000); } assert(threadpool_destroy(pool, 0) == 0); /* fprintf(stderr, "Did %d tasks\n", done); */ return 0; }
int main(int argc, char **argv) { int i, dup_vote; int cores, block_size; cores = 16; block_size = 1024*1024; int *thread; file_struct *fs; threadpool_t *pool; thread = malloc(sizeof(int)); if(argc<2) { printf("[usage] vote_counter [-dupvote] [-cores C] [-blocksize B] [filename1] ... [filenameN]\n"); exit(-1); } char **file_list = malloc(sizeof(char *) * MAX_FILE_NUM); memset(file_list, 0, (sizeof(char*))*MAX_FILE_NUM); //analyze input arguments if(!(dup_vote = opts(argc, argv, file_list, &cores, &block_size, &thread))) { exit(-1); } printf("thread number %d\n", *thread); //printf("cores %d; block_size %d\n", cores, block_size); //create pool according to total file size & number if((pool = threadpool_create(*thread, cores, 0)) == NULL) { printf("[error] can not initalize thread\n"); exit(-1); } init_mutex(); dup_vote = dup_vote == 2 ? TRUE : FALSE; for (i=0; file_list[i]!=NULL; i++) { fs = malloc(sizeof(file_struct)); fs->filename = file_list[i]; fs->dup_vote = dup_vote; fs->block_size = block_size; //send read task to thread pool threadpool_add(pool, &readfile, fs, 0); } //wait until all thread finish their job threadpool_destroy(pool, 1); get_vote_result(3); return 0; }
int main(int argc, char **argv) { threadpool_t *pool; pthread_mutex_init(&lock, NULL); pool = threadpool_create(THREAD, QUEUE, 0); fprintf(stderr, "Pool started with %d threads and " "queue size of %d\n", THREAD, QUEUE); while(threadpool_add(pool, &dummy_task, NULL, 0) == 0) { pthread_mutex_lock(&lock); tasks++; pthread_mutex_unlock(&lock); } fprintf(stderr, "Added %d tasks\n", tasks); while(tasks / 2 > done) { sleep(1); } fprintf(stderr, "Did %d tasks before shutdown\n", done); threadpool_destroy(pool, 0); fprintf(stderr, "Did %d tasks\n", done); return 0; }
int main(int argc, char **argv) { threadpool_t *pool; /* 初始化互斥锁 */ pthread_mutex_init(&lock, NULL); /* 断言线程池创建成功 */ assert((pool = threadpool_create(THREAD, QUEUE, 0)) != NULL); fprintf(stderr, "Pool started with %d threads and " "queue size of %d\n", THREAD, QUEUE); /* 只要任务队列还没满,就一直添加 */ while(threadpool_add(pool, &dummy_task, NULL, 0) == 0) { pthread_mutex_lock(&lock); tasks++; pthread_mutex_unlock(&lock); } fprintf(stderr, "Added %d tasks\n", tasks); /* 不断检查任务数是否完成一半以上,没有则继续休眠 */ while((tasks / 2) > done) { usleep(10000); } /* 这时候销毁线程池,0 代表 immediate_shutdown */ assert(threadpool_destroy(pool, 0) == 0); fprintf(stderr, "Did %d tasks\n", done); return 0; }
int main(int argc, char **argv) { threadpool_t *pool; int i, k, ret=0; void *slave[SLAVE_NUM]; char *string; void *context = zmq_init(1); pthread_mutex_init(&lock, NULL); pool = threadpool_init(THREAD, QUEUE, 0); fprintf(stderr, "Pool started with %d threads and " "queue size of %d\n", THREAD, QUEUE); if (!pool) { return -1; } for (i = 0; i < 10; i++) { /*k = i % 2; if (k!=0 && k!=1) { ret = -100; break; }*/ ret = threadpool_add(pool, &replay, context, 0); if (ret) { break; } pthread_mutex_lock(&lock); tasks++; pthread_mutex_unlock(&lock); } fprintf(stderr, "RET: %d Added %d tasks\n", ret, tasks); while(tasks / 2 > done) { sleep(1); } fprintf(stderr, "Did %d tasks before shutdown\n", done); fprintf(stderr, "Did %d tasks\n", done); slave[0] = zmq_socket (context, ZMQ_REQ); zmq_connect (slave[0], "tcp://localhost:5562"); /* slave[1] = zmq_socket (context, ZMQ_REQ); zmq_connect (slave[1], "tcp://localhost:5563"); */ for (i = 0; i < SLAVE_NUM; i++) { s_send(slave[i], "END"); string = s_recv(slave[i]); free(string); zmq_close(slave[i]); } zmq_term(context); threadpool_destroy(pool, 0); return 0; }
int main(int argc, char *argv[]) { signal(SIGUSR1, handle_signal); global_init(); int listenfd = open_listenfd(); struct epoll_event event; struct sockaddr_in peeraddr; socklen_t socklen; int sock; int nready, i; char buf[1024]; epollfd = epoll_create1(EPOLL_CLOEXEC); event.data.fd = listenfd; event.events = EPOLLIN | EPOLLET; epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event); while (g_switch == 0) { nready = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nready == -1) { if (errno == EINTR) continue; handle_err(1, "epoll_wait"); } for (i = 0; i < nready; ++i) { if (events[i].data.fd == listenfd) { socklen = sizeof(peeraddr); sock = accept4(listenfd, (struct sockaddr *) &peeraddr, &socklen, SOCK_NONBLOCK); handle_err(sock == -1, "accept4"); printf("new client %s:%hu accepted\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); sprintf(buf, "recv from (%s:%hu)\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); write_log(buf); event.data.fd = sock; event.events = EPOLLIN | EPOLLET; epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &event); } else { int *ptr = malloc(sizeof(int)); *ptr = events[i].data.fd; threadpool_add(thp, do_task, (void *)(ptr), 0); } } } global_destroy(); return 0; }
int main() { int rc; zv_threadpool_t *tp = threadpool_init(THREAD_NUM); int i; for (i=0; i< 100; i++){ log_info("ready to add num %d", i); rc = threadpool_add(tp, sum_n, (void *)i); check(rc == 0, "rc == 0"); } if (threadpool_destroy(tp, 1) < 0) { log_err("destroy threadpool failed"); } return 0; }
//gcc -w threadpool.cc -o thread -lpthread //gcc src.c -std=c99 -o src //g++ -w -o test test.c threadpool.c -lpthread int main() { threadpool_t *thp = threadpool_create(3,100,12); printf("pool inited"); int *num = (int *)malloc(sizeof(int)*20); for (int i=0;i<10;i++) { num[i]=i; printf("add task %d\n",i); threadpool_add(thp,process,(void*)&num[i]); } sleep(10); threadpool_destroy(thp); return 0; }
int main(void) { threadpool_t *thp = threadpool_create(3,100,100); /*线程池里最小3个线程,最大100个,队列最大值12*/ printf("pool inited"); int *num = (int *)malloc(sizeof(int)*20); //int num[20]; int i; for (i=0;i<10;i++) { num[i]=i; printf("add task %d\n",i); threadpool_add(thp,process,(void*)&num[i]); } sleep(10); threadpool_destroy(thp); }
/* \brief Realiza verificacoes para a escrita do arquivo e coloca a tarefa no * pool de threads * * \param[out] client O cliente correspondente * \param[out] r_server A estrutura do servidor * * \return 0 Caso ok * \return -1 Caso haja erro ao colocar a tarefa para as threads */ int server_process_write_file(client_node *client, server *r_server) { int not_accept_flags = 0; not_accept_flags = PENDING_DATA | FINISHED | SIGNAL_WAIT; if (client->status & not_accept_flags || GET == client->method) return 0; if (client->status & WRITE_HEADER && !(client->status & READ_DATA)) return 0; if(0 != threadpool_add(server_write_file, client, &r_server->thread_pool)) return -1; client->status |= SIGNAL_WAIT; return 0; }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; threadpool_t *pool; struct sockaddr_in serv_addr, cli_addr; if(argc < 2){ fprintf(stderr, "ERROR, no port provided\n"); exit(1); } //ObjLib_Init(); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { printf("Error opening socket\n"); exit(1); } bzero(&serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); serv_addr.sin_addr.s_addr = INADDR_ANY; if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ printf("error in binding\n"); exit(1); } pool = threadpool_create(2, 10, 0); assert(pool!=NULL); listen(sockfd, 5); clilen = sizeof(cli_addr); while(1){ int err; newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) &clilen); if(newsockfd < 0) exit(1); if((err = threadpool_add(pool, &thread_function, (void *)&newsockfd, 0)) != 0){ printf("Error %d while adding job to the job queue\n", err); exit(1); } } //ObjLib_Exit(); close(sockfd); return 0; }
int main(int argc , char *argv[]) { int socket_desc , client_sock , c; struct sockaddr_in server , client; //Create socket socket_desc = create_socket(); //Prepare the sockaddr_in structure setup_server(&server); bind_server_to_socket(socket_desc, &server); //Listen listen(socket_desc , LISTEN_QUEUE_SIZE); //Accept and incoming connection c = sizeof(struct sockaddr_in); threadpool_t *pool = create_threadpool(); while((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) { if( threadpool_add(pool, connection_handler, (void*) &client_sock, 0)) { perror("could not create thread"); return 1; } //Now join the thread , so that we dont terminate before the threa } if (client_sock < 0) { perror("accept failed"); return 1; } return 0; }
/* \brief Realiza verificacoes para a leitura do arquivo e coloca a tarefa no * pool de threads * * \param[out] client O cliente correspondente * \param[out] r_server A estrutura do servidor * * \return 0 Caso ok * \return -1 Caso haja erro ao colocar a tarefa para as threads */ int server_process_read_file(client_node *client, server *r_server) { int bytes_to_read; int not_accept_flags = 0; not_accept_flags = PENDING_DATA | FINISHED | SIGNAL_WAIT; if (client->status & not_accept_flags || !client->bucket.transmission) return 0; bytes_to_read = BUFFER_LEN; if (client->bucket.remain_tokens < BUFFER_LEN) bytes_to_read = client->bucket.remain_tokens; client->b_to_transfer = bytes_to_read; if(0 != threadpool_add(server_read_file, client, &r_server->thread_pool)) return -1; client->status |= SIGNAL_WAIT; return 0; }
static void run(ctx_t *ctx, int nthreads, int nreqs) { if (ctx->pool) { /* start using the curl easy handle pool */ if (!rcvr_pool_open(ctx->pool)) DIE("pool open failed"); } threadpool_t *pool = threadpool_create(nthreads, nreqs, 0); for (int i = 0; i < nreqs; i++) { if (threadpool_add(pool, &do_request, ctx, 0) < 0) DIE("thread pool failed"); } while (ctx->nreq < nreqs) usleep(10); threadpool_destroy(pool, 0); if (ctx->pool) { /* close the pool after use */ if (!rcvr_pool_close(ctx->pool)) DIE("pool close failed"); } fprintf(stderr, "\n"); }
int main(int argc, char **argv) { /* check the number of argument */ if (argc != 2) { IAS_LOG_ERROR("Wrong parameter!"); return ERROR; } /* declare some parameters */ PARAMETERS parameters; //structure of parameters MQ_PARAMS mq_params; //structure of MQ parameters IAS_L0R_EPHEMERIS *l0r_ephemeris = NULL; //pointer to L0R ephemeris data structure long long num_frame_of_ephemeris; // number of ephemeris frames IAS_LOS_MODEL *model; //pointer to the model structure IAS_CPF *cpf = NULL; /* Structure for CPF */ double ephemeris_start_time; //start time of ephemeris data double ephemeris_end_time; //end time of ephemeris data IAS_ANC_EPHEMERIS_DATA *anc_ephemeris_data = NULL; int invalid_ephemeris_count = 0; MWDIMAGE_BUFFER_INFO *mwdImage_buffer_info; size_t process_times_needed; int status = ERROR; int i,j; memset(¶meters, 0, sizeof(parameters)); memset(&mq_params, 0, sizeof(mq_params)); /* Read parameters and MQ params from ODL */ status = read_parameters(argv[1],¶meters,&mq_params); if(status != SUCCESS) { IAS_LOG_ERROR("failed to read parameters!\n"); exit(EXIT_FAILURE); } /*MQ connection */ status = MQ_Init(); if(status != SUCCESS) { IAS_LOG_WARNING("MQ connect Error !\n"); } status = MQSend(4,"ADP module started !\n"); if(status != SUCCESS) { IAS_LOG_WARNING("module started , MQSend Error !\n"); } status = ias_sat_attr_initialize(parameters.satellite_id); if (status != SUCCESS) { IAS_LOG_ERROR("Initializing IAS Satellite Attributes Library"); return ERROR; } /* Initialize the model structure */ IAS_ACQUISITION_TYPE acquisition_type = IAS_EARTH; model = ias_los_model_initialize(acquisition_type); if (!model) { IAS_LOG_ERROR("Initializing model"); return EXIT_FAILURE; } /* read information from cpf, and set it into model*/ cpf = ias_cpf_read(parameters.cpf_filename); if(ias_los_model_set_cpf_for_MWD(cpf,model) != SUCCESS) { IAS_LOG_ERROR("Copy cpf value into model"); return ERROR; } /* read ephemeris file into lor_ephemeris */ status = read_ephemeris_data_for_MWD(¶meters,&l0r_ephemeris,&num_frame_of_ephemeris); if(status != SUCCESS ) { IAS_LOG_ERROR("Could not read ephemeris file into lor_ephemeris.\n"); exit(EXIT_FAILURE); } long long l0r_ephemeris_count = num_frame_of_ephemeris; /* Preprocess the ephemeris data. */ if (ias_ancillary_preprocess_ephemeris_for_MWD(cpf, l0r_ephemeris, l0r_ephemeris_count,acquisition_type,&anc_ephemeris_data, &invalid_ephemeris_count, &ephemeris_start_time,&ephemeris_end_time) != SUCCESS) { IAS_LOG_ERROR("Processing ephemeris data"); return ERROR; } if(ias_sc_model_set_ancillary_ephemeris(anc_ephemeris_data, &model->spacecraft) != SUCCESS) { IAS_LOG_ERROR("Setting ephemeris data into model"); return ERROR; } mwdImage_buffer_info = malloc(sizeof(*mwdImage_buffer_info)); memset(mwdImage_buffer_info,0,sizeof(*mwdImage_buffer_info)); UPDATE_LONGITUDE_LATITUDE_ARGS *update_longitude_latitude_args; update_longitude_latitude_args = (UPDATE_LONGITUDE_LATITUDE_ARGS*)malloc (sizeof(UPDATE_LONGITUDE_LATITUDE_ARGS)); /* Read mwdImage through several times */ /* Decide how many times to read file to the buffer */ status = get_process_time_needed(¶meters,&process_times_needed); if(status != SUCCESS) { IAS_LOG_ERROR("failed to get the process times needed!\n"); return ERROR; } for(i = 0; i < process_times_needed; i++) { status = read_mwdImage(¶meters,i,mwdImage_buffer_info, ephemeris_start_time,ephemeris_end_time); threadpool_t* pool; pool = threadpool_create(NUM_THREAD); for(j = 0; j < NUM_THREAD; j++) { update_longitude_latitude_args->start_oli_frame_to_update = j*mwdImage_buffer_info->num_oli_frame/NUM_THREAD; if(j == NUM_THREAD-1) { update_longitude_latitude_args->end_oli_frame_to_update = mwdImage_buffer_info->num_oli_frame; } else { update_longitude_latitude_args->end_oli_frame_to_update = j*mwdImage_buffer_info->num_oli_frame/NUM_THREAD +mwdImage_buffer_info->num_oli_frame/NUM_THREAD; } update_longitude_latitude_args->model = model; update_longitude_latitude_args->mwdImage_buffer_info = mwdImage_buffer_info; threadpool_add(pool,(void*)&update_longitude_latitude,(void*)update_longitude_latitude_args); } // /* Added the remain data to the last thread */ // update_longitude_latitude_args->start_oli_frame_to_update = j*(mwdImage_buffer_info->num_oli_frame/(NUM_THREAD-1))*(NUM_THREAD-2); // update_longitude_latitude_args->end_oli_frame_to_update = j*mwdImage_buffer_info->num_oli_frame; // threadpool_add(pool,(void*)&update_longitude_latitude,(void*)update_longitude_latitude_args); threadpool_destroy(pool); status = write_mwdImage(¶meters,i,mwdImage_buffer_info); } // // // IAS_SATELLITE_ID satellite_id; // int satellite_number = 8; // /* Get the satellite ID */ // satellite_id = ias_sat_attr_get_satellite_id_from_satellite_number( // satellite_number); // if (satellite_id == ERROR) // { // IAS_LOG_ERROR("Unable to determine satellite ID from satellite " // "number %d", satellite_number); // return ERROR; // } /* Since the only purpose for retrieving the satellite ID is to initialize the library, do that here */ // const char *system_model_file = "/home/cqw/Desktop/systematic_model.140032.h5"; // IAS_LOS_MODEL *model1; // model1 = ias_model_read(system_model_file); // double n_lines, n_sample; // int n_band, n_sca; // double target_elev; // double target_latd, target_long; // IAS_SENSOR_DETECTOR_TYPE dettype; // // n_lines = 7500; // n_sample = 100; // n_band = 7; // n_sca = 9; // target_elev = 0; // dettype = IAS_NOMINAL_DETECTOR; // // ias_los_model_input_line_samp_to_geodetic // (n_lines, n_sample,n_band, n_sca,target_elev,model,dettype, NULL ,&target_latd,&target_long); return SUCCESS; }
int main(int argc, char **argv) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if(sigaction(SIGPIPE, &sa, NULL)) { LOG_ERR("SIGPIPE ERR"); return 0; } int listenfd; listenfd = open_listenfd(8888); make_socket_non_blocking(listenfd); int epfd = tiny_epoll_create(0); struct epoll_event event; event.data.fd = listenfd; event.events = EPOLLIN | EPOLLET; tiny_epoll_add(epfd, listenfd, &event); tiny_threadpool_t *tp = threadpool_init(2); while(1) { int n = tiny_epoll_wait(epfd, events, MAXEVENTS, -1); int i = 0; for(;i < n;++i){ if (listenfd == events[i].data.fd) { while(1) { struct sockaddr_in clientaddr; socklen_t inlen = sizeof(clientaddr); int infd = accept(listenfd, (struct sockaddr*)&clientaddr,&inlen); if (infd == -1) { if((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; else{ LOG_ERR("accept err"); break; } } LOG_INFO("new client fd : %d",infd); make_socket_non_blocking(infd); event.data.fd = infd; event.events = EPOLLIN | EPOLLET; tiny_epoll_add(epfd, infd, &event); } } else { if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || !(events[i].events & EPOLLIN)) { LOG_ERR("epoll error fd : %d",events[i].data.fd); close(events[i].data.fd); continue; } LOG_INFO("new task from fd : %d", events[i].data.fd); threadpool_add(tp,accept_request,&(events[i].data.fd)); } } } LOG_INFO("begin destroy"); if(threadpool_destroy(tp,1) < 0) LOG_ERR("idestroy err"); return 0; }
int buse_main(const char* dev_file, struct buse_operations *aop, void *userdata) { int sp[2]; int nbd, err; struct nbd_request request; buse_session ses; ssize_t bytes_read = 0; u_int32_t flags = NBD_FLAG_HAS_FLAGS; pthread_mutex_lock(&glock); bool ready = true; if (!pool) { fprintf(stderr, "Starting thread pool of size [%d] and queue size [%d]\n", THREAD, QUEUE); pool = threadpool_create(THREAD, QUEUE, 0); if (pool == NULL) { fprintf(stderr, "Could not start threadpool\n"); ready = false; } } if (ready) devices++; fprintf(stderr, "Current number of devices mounted is [%d]\n", devices); pthread_mutex_unlock(&glock); if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) { fprintf(stderr, "Could not open socket\n"); ready = false; } nbd = open(dev_file, O_RDWR); int oflags = fcntl(sp[0], F_GETFL, 0); fcntl(sp[0], F_SETFL, oflags | O_NONBLOCK); int sflags = fcntl(sp[1], F_GETFL, 0); fcntl(sp[1], F_SETFL, sflags | O_NONBLOCK); if (aop->readonly) { //fprintf(stderr,"Readonly set\n"); flags |= NBD_FLAG_READ_ONLY; } #if defined NBD_SET_FLAGS && defined NBD_FLAG_SEND_TRIM flags |= NBD_FLAG_SEND_TRIM; #endif #if defined NBD_SET_FLAGS && defined NBD_FLAG_SEND_FLUSH flags |= NBD_FLAG_SEND_FLUSH; #endif if (nbd == -1) { fprintf(stderr, "Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.\n"); ready = false; } if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long) aop->blocksize) < 0) fprintf(stderr, "Ioctl/1.1a failed: %m\n"); if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, aop->size / (unsigned long) aop->blocksize) < 0) fprintf(stderr, "Ioctl/1.1b failed: %m\n"); ioctl(nbd, NBD_CLEAR_SOCK); if (!fork()) { /* The child needs to continue setting things up. */ close(sp[0]); ses.sk = sp[1]; if (ioctl(nbd, NBD_SET_SOCK, ses.sk) == -1) { fprintf(stderr, "ioctl(nbd, NBD_SET_SOCK, sk) failed.[%s]\n", strerror(errno)); } #if defined NBD_SET_FLAGS else if (ioctl(nbd, NBD_SET_FLAGS, flags) == -1) { fprintf(stderr, "ioctl(nbd, NBD_SET_FLAGS, NBD_FLAG_SEND_TRIM) failed.[%s]\n", strerror(errno)); } #endif else { err = ioctl(nbd, NBD_DO_IT); fprintf(stderr, "nbd device [%s] terminated with code %d\n", dev_file, err); if (err == -1) fprintf(stderr, "%s\n", strerror(errno)); } ioctl(nbd, NBD_CLEAR_QUE); ioctl(nbd, NBD_CLEAR_SOCK); } /* The parent opens the device file at least once, to make sure the * partition table is updated. Then it closes it and starts serving up * requests. */ close(sp[1]); //sk = sp[0]; //fprintf(stderr, "waiting from requests on [%s]\n",dev_file); ses.sk = sp[0]; ses.userdata = userdata; ses.aop = aop; ses.numworker = 0; ses.numavail = 0; ses.numrunning = 0; struct pollfd ufds[1]; ufds[0].fd = ses.sk; ufds[0].events = POLLIN | POLLPRI; // check for normal or out-of-band ses.main_running.prev = ses.main_running.next = &ses.main_running; if (pthread_mutex_init(&(ses.rlock), NULL) != 0) { fprintf(stderr, "unable to init ses.rlock \n"); } if (pthread_mutex_init(&(ses.wlock), NULL) != 0) { fprintf(stderr, "unable to init ses.wlock \n"); } if (pthread_mutex_init(&(ses.llock), NULL) != 0) { fprintf(stderr, "unable to init ses.llock \n"); } ses.go_on = ready; int rv; while (ses.go_on) { rv = poll(ufds, 1, -1); if (rv == -1) { perror("poll"); // error occurred in poll() ses.go_on = false; } else if (rv == 0) { //printf("Timeout occurred! Not Sure why\n"); } else { pthread_mutex_lock(&(ses.rlock)); bytes_read = read(ses.sk, &request, sizeof(request)); if (bytes_read > 0) { struct cmd_request *cr = calloc(1, sizeof(struct cmd_request)); //assert(bytes_read == sizeof(request)); memcpy(cr->handle, request.handle, sizeof(cr->handle)); cr->from = request.from; cr->len = request.len; cr->magic = request.magic; cr->type = request.type; cr->session = &ses; u_int32_t len; if (ntohl(cr->type) == NBD_CMD_WRITE) { len = ntohl(cr->len); //assert(aop->write); cr->buf = malloc(len); read_all(ses.sk, cr->buf, len); } if (ntohl(cr->type) == NBD_CMD_DISC) { /* Handle a disconnect request. */ // assert(aop->disc); fprintf(stderr, "Received Disconnect!\n"); ses.aop->disc(ses.userdata); ses.go_on = false; free(cr); } if (ses.go_on) list_add_worker(cr, &ses.main_running); pthread_mutex_unlock(&(ses.rlock)); if (ses.go_on) { if (threadpool_add(pool, &mt_do_work, cr, 0) != 0) { /* fprintf(stderr, "thread pool full\n"); */ mt_do_work(cr); } } } else { pthread_mutex_unlock(&(ses.rlock)); if (errno != EAGAIN) ses.go_on = false; } } } fprintf(stderr, "%s closing\n", dev_file); while (ses.numrunning > 0) { sleep(1); } ioctl(nbd, NBD_CLEAR_SOCK); pthread_mutex_lock(&glock); devices--; pthread_mutex_unlock(&glock); close(ses.sk); close(nbd); pthread_mutex_destroy(&(ses.llock)); pthread_mutex_destroy(&(ses.rlock)); pthread_mutex_destroy(&(ses.wlock)); if (bytes_read == -1) fprintf(stderr, "error %s\n", strerror(errno)); fprintf(stderr, "%s exited\n", dev_file); return 0; }
K threads_async(K x,K y) { async_job *job=threads_create_job(knk(1,r1(x)),r1(y)); threadpool_add(threadpool,threads_thread_callback,(V*)job,0);R(K)0;}
int main(int argc, char *argv[]) { int opt, long_index, worker_threads, queue_size; int sockfd, newsockfd, portno, clilen; threadpool_t *pool; struct sockaddr_in serv_addr, cli_addr; static struct option long_options[] = { {"port", required_argument, 0, 'p'}, {"worker-thread-count", required_argument, 0, 'w'}, {"queue-size", optional_argument, 0, 'q'}, {0, 0, 0, 0} }; opt = 0; portno = -1; worker_threads = -1; queue_size = 10; /* default queue size */ long_index = 0; while((opt = getopt_long(argc, argv,"p:w:q:", long_options, &long_index)) != -1){ switch(opt){ case 'p': portno = atoi(optarg); break; case 'w': worker_threads = atoi(optarg); break; case 'q': queue_size = atoi(optarg); break; default: print_usage(); exit(EXIT_FAILURE); } } if(portno == -1 || worker_threads == -1){ print_usage(); exit(EXIT_FAILURE); } //ObjLib_Init(); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { printf("Error opening socket\n"); exit(1); } bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(portno); serv_addr.sin_addr.s_addr = INADDR_ANY; if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ printf("error in binding\n"); exit(1); } pool = threadpool_create(worker_threads, queue_size, 0); assert(pool!=NULL); listen(sockfd, 5); clilen = sizeof(cli_addr); while(1){ int err; newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) &clilen); if(newsockfd < 0) exit(1); if((err = threadpool_add(pool, &thread_function, (void *)&newsockfd, 0)) != 0){ printf("Error %d while adding job to the job queue\n", err); exit(1); } } //ObjLib_Exit(); close(sockfd); return 0; }
int main(int argc, char* argv[]) { int rc; int opt = 0; int options_index = 0; char *conf_file = CONF; /* * parse argv * more detail visit: http://www.gnu.org/software/libc/manual/html_node/Getopt.html */ if (argc == 1) { usage(); return 0; } while ((opt=getopt_long(argc, argv,"Vc:?h",long_options,&options_index)) != EOF) { switch (opt) { case 0 : break; case 'c': conf_file = optarg; break; case 'V': printf(PROGRAM_VERSION"\n"); return 0; case ':': case 'h': case '?': usage(); return 0; } } debug("conffile = %s", conf_file); if (optind < argc) { log_err("non-option ARGV-elements: "); while (optind < argc) log_err("%s ", argv[optind++]); return 0; } /* * read confile file */ char conf_buf[BUFLEN]; zv_conf_t cf; rc = read_conf(conf_file, &cf, conf_buf, BUFLEN); check(rc == ZV_CONF_OK, "read conf err"); /* * install signal handle for SIGPIPE * when a fd is closed by remote, writing to this fd will cause system send * SIGPIPE to this process, which exit the program */ struct sigaction sa; memset(&sa, '\0', sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction(SIGPIPE,&sa,NULL)) { log_err("install sigal handler for SIGPIPI failed"); return 0; } /* * initialize listening socket */ int listenfd; struct sockaddr_in clientaddr; // initialize clientaddr and inlen to solve "accept Invalid argument" bug socklen_t inlen = 1; memset(&clientaddr, 0, sizeof(struct sockaddr_in)); listenfd = open_listenfd(cf.port); rc = make_socket_non_blocking(listenfd); check(rc == 0, "make_socket_non_blocking"); /* * create epoll and add listenfd to ep */ int epfd = zv_epoll_create(0); struct epoll_event event; zv_http_request_t *request = (zv_http_request_t *)malloc(sizeof(zv_http_request_t)); zv_init_request_t(request, listenfd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zv_epoll_add(epfd, listenfd, &event); /* create thread pool */ zv_threadpool_t *tp = threadpool_init(cf.thread_num); /* epoll_wait loop */ while (1) { int n; n = zv_epoll_wait(epfd, events, MAXEVENTS, -1); int i, fd; for (i=0; i<n; i++) { zv_http_request_t *r = (zv_http_request_t *)events[i].data.ptr; fd = r->fd; if (listenfd == fd) { /* we hava one or more incoming connections */ while(1) { debug("## ready to accept"); int infd = accept(listenfd, (struct sockaddr *)&clientaddr, &inlen); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* we have processed all incoming connections */ break; } else { log_err("accept"); break; } } rc = make_socket_non_blocking(infd); check(rc == 0, "make_socket_non_blocking"); debug("new connection fd %d", infd); zv_http_request_t *request = (zv_http_request_t *)malloc(sizeof(zv_http_request_t)); if (request == NULL) { log_err("malloc(sizeof(zv_http_request_t))"); break; } zv_init_request_t(request, infd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zv_epoll_add(epfd, infd, &event); } // end of while of accept debug("## end accept"); } else { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { log_err("epoll error fd: %d", r->fd); close(fd); continue; } /* do_request(infd); close(infd); */ log_info("new task from fd %d", fd); rc = threadpool_add(tp, do_request, events[i].data.ptr); check(rc == 0, "threadpool_add"); } } //end of for } // end of while(1) return 0; }
int main(int argc, char *argv[]) { printf("%s", "start\n"); int listen_fd; int rcode; struct epoll_event *events; if (argc != 2) { fprintf(stderr, "usage: %s [port]\n", argv[0]); exit(EXIT_FAILURE); } struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction(SIGPIPE, &sa, NULL)) { printf("ignore SIGPIPE\n"); } struct sockaddr_in client_addr; socklen_t client_len = 1; memset(&client_addr, 0, sizeof(struct sockaddr_in)); /* create and bind the port, and then set the socket to non blocking mode */ listen_fd = open_listenfd(atoi(argv[1])); debug("listen fd = %d", listen_fd); rcode = make_socket_non_blocking(listen_fd); if (rcode == -1) { log_err("error when making socket non blocking"); abort(); } /* create epoll event */ int efd = epoll_create1(0); if (efd == -1) { log_err("epoll_create"); abort(); } struct epoll_event event; events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * MAXEVENTS); http_request_t *request = (http_request_t *)malloc(sizeof(http_request_t)); http_request_init(request, listen_fd); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; /* register the listen event */ rcode = epoll_ctl(efd, EPOLL_CTL_ADD, listen_fd, &event); if (rcode == -1) { perror("epoll_ctl"); abort(); } threadpool_t *tp = threadpool_init(NUM_OF_THREADS); /* event loop */ while (1) { int n = epoll_wait(efd, events, MAXEVENTS, -1); /* process each incoming IO event */ int i; for (i = 0; i < n; i++) { http_request_t *r = (http_request_t *)events[i].data.ptr; int fd = r->fd; debug("event fd = %d", fd); if (fd == listen_fd) { /* incoming connection event */ while (1) { int client_fd; debug("waiting for accept"); client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // we have already processed the incoming connection debug("incoming connection processed\n"); break; } else { log_err("error occured when accepting connection\n"); break; } } rcode = make_socket_non_blocking(client_fd); if (rcode == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) // we have already processed the incoming connection break; log_err("fail to accept the connection\n"); break; } debug("new connection fd %d", client_fd); http_request_t *request = (http_request_t *)malloc(sizeof(http_request_t)); http_request_init(request, client_fd); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; /* add the new event into epoll */ rcode = epoll_ctl(efd, EPOLL_CTL_ADD, client_fd, &event); if (rcode == - 1) { log_err("fail in epoll_ctl in epoll_wait"); abort(); } } debug("end accept"); } else if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { /* an error has occured on this fd, or the socket is not ready for reading */ log_err("error events: %d", events[i].events); if (events[i].events & EPOLLERR) log_err("EPOLLERR"); if (events[i].events & EPOLLHUP) log_err("EPOLLHUP"); if (!(events[i].events & EPOLLIN)) log_err("EPOLLIN"); close(fd); continue; } else { /* incoming data read event */ /* add the event to the thread pool list */ threadpool_add(tp, handle_http, events[i].data.ptr); debug("thread count: %d", tp->thread_count); debug("thread queue size: %d", tp->queue_size); } } } threadpool_destroy(tp); return 0; }
int main(int argc, char* argv[]) { int rc; char *conf_file = CONF; /* * read confile file */ char conf_buf[BUFLEN]; zxc_conf_t cf; rc = read_conf(conf_file, &cf, conf_buf, BUFLEN); /* * initialize listening socket */ int listenfd; struct sockaddr_in clientaddr; socklen_t inlen = 1; memset(&clientaddr, 0, sizeof(struct sockaddr_in)); listenfd = open_listenfd(cf.port); rc = make_socket_non_blocking(listenfd); /* * create epoll and add listenfd to ep */ int epfd = zxc_epoll_create(0); struct epoll_event event; zxc_http_request_t *request = (zxc_http_request_t *)malloc(sizeof(zxc_http_request_t)); zxc_init_request_t(request, listenfd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zxc_epoll_add(epfd, listenfd, &event); /* create thread pool */ zxc_threadpool_t *tp = threadpool_init(cf.thread_num); /* epoll_wait loop */ while (1) { int n; n = zxc_epoll_wait(epfd, events, MAXEVENTS, -1); int i, fd; for (i=0; i<n; i++) { zxc_http_request_t *r = (zxc_http_request_t *)events[i].data.ptr; fd = r->fd; if (listenfd == fd) { /* we hava one or more incoming connections */ while(1) { int infd = accept(listenfd, (struct sockaddr *)&clientaddr, &inlen); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* we have processed all incoming connections */ break; } else { break; } } rc = make_socket_non_blocking(infd); zxc_http_request_t *request = (zxc_http_request_t *)malloc(sizeof(zxc_http_request_t)); if (request == NULL) { break; } zxc_init_request_t(request, infd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zxc_epoll_add(epfd, infd, &event); } // end of while of accept } else { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { close(fd); continue; } /* do_request(infd); close(infd); */ rc = threadpool_add(tp, do_request, events[i].data.ptr); } } //end of for } // end of while(1) threadpool_destroy(tp, 1); return 0; }