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; 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) { 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; pool = threadpool_create(THREAD, QUEUE, 0); fprintf(stderr, "Pool started with %d threads and " "queue size of %d\n", THREAD, QUEUE); int *data = malloc(DATASIZE * sizeof(int)); for (int i = 0; i < DATASIZE; i++) data[i] = i + 1; threadpool_map(pool, DATASIZE, is_simple, data, 0); for (int i = 0; i < DATASIZE; i++) printf("%d ", data[i]); printf("%\n"); threadpool_reduce_t reduce; reduce.begin = data; reduce.end = data + DATASIZE; reduce.object_size = sizeof(*data); reduce.self = NULL; reduce.reduce = my_reduce; reduce.reduce_alloc_neutral = my_alloc_neutral; reduce.reduce_finish = my_finish; reduce.reduce_free = my_free; threadpool_reduce(pool, &reduce); return 0; }
void global_init() { int i; char buf[50]; if (access("./LOG", F_OK) != 0) { if (mkdir("./LOG", 0755) < 0) { perror("mkdir LOG failed"); exit (-1); } } sprintf(buf, "./LOG/log_%d.txt", LOGFILE_CNT++); log_fp = fopen(buf, "a"); read_config(); thp = threadpool_create(8, 128, 0); //printf("threadpool inited\n"); handle = dlopen(DLL_FILE_NAME, RTLD_LAZY); if (handle == NULL) { fprintf(stderr, "Failed to open library %s err:%s\n", DLL_FILE_NAME, dlerror()); exit(EXIT_FAILURE); } check_complete = dlsym(handle, "check_complete"); get_path = dlsym(handle, "get_path"); handle_request = dlsym(handle, "handle_request"); }
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; }
//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); }
int main(int argc, char *argv[]) { if ( argc != 3 ) { printf("usage: %s numtasks poolsize\n", argv[0]); exit(0); } int NUMTASKS = atoi(argv[1]); //1600 // 16000 int POOLSIZE = atoi(argv[2]); // 300 //868 struct timespec tspec; long long withpool, nopool, before; static long long waittime = 500; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); struct threadpool * pool = threadpool_create(POOLSIZE); for (int i=0; i < NUMTASKS; ++i) { threadpool_enqueue(pool, busywait, &waittime); } threadpool_destroy(pool); clock_gettime(CLOCK_REALTIME, &tspec); withpool = timespecToMs(tspec) - before; clock_gettime(CLOCK_REALTIME, &tspec); before = timespecToMs(tspec); pthread_t threads[NUMTASKS]; for (int i=0; i < NUMTASKS; ++i) { pthread_create(&threads[i], NULL, busypthread, &waittime); } for(int i=0; i < NUMTASKS; ++i) { if(pthread_join(threads[i], NULL) != 0) { exit(EXIT_FAILURE); } } clock_gettime(CLOCK_REALTIME, &tspec); nopool = timespecToMs(tspec) - before; printf("Pool runtime: %lld\n",withpool); printf("Naked runtime: %lld\n",nopool); 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() { my_count1 = 0; my_count2 = 0; pthread_mutex_init(&lock1, NULL); pthread_mutex_init(&lock2, NULL); threadpool_t *pool = threadpool_create(2); threadpool_add_task(pool, mk_task(f1, NULL)); threadpool_add_task(pool, mk_task(f2, NULL)); threadpool_add_task(pool, mk_task(f1, NULL)); threadpool_destroy(pool, immediate_shutdown); threadpool_add_task(pool, mk_task(f2, NULL)); pthread_mutex_destroy(&lock1); pthread_mutex_destroy(&lock2); printf("my_count1 : %d\n", my_count1); printf("my_count2 : %d\n", my_count2); }
int main(int argc, char **argv) { int err; sigset_t oldmask; pthread_t tid; sigemptyset(&mask); sigaddset(&mask, SIGPIPE); //向信号集中添加SIGPIPE if ((err = pthread_sigmask(SIG_BLOCK, &mask, &oldmask)) != 0) log_exit(err, "SIG_BLOCK error"); err = pthread_create(&tid, NULL, sig_handler, 0); if (err != 0) log_exit(err, "Cannot create thread!"); init_config(); int listenfd = initserver(); set_noblock(listenfd); threadpool_t *tp = threadpool_create(8); main_loop(tp, listenfd); threadpool_join(tp); exit(0); }
int main(int argc, char *argv[]) { long i = 10000; threadpool_t *pool = threadpool_create(10, 100, 0); assert(pool); while (i > 0) { if (i % 3 == 0) { assert(threadpool_add_task(pool, task3, (void*)(long)(pool->threads_num), 1000) == 0); } else if (i % 3 == 1) { assert(threadpool_add_task(pool, task2, (void*)(long)(pool->threads_num), 500) == 0); } else { assert(threadpool_add_task(pool, task1, (void*)(long)(pool->threads_num), 0) == 0); } i--; } while (threadpool_task_over(pool, 1, 3) != 0) {}; threadpool_exit(pool); assert(threadpool_destroy(pool, 1, 3) == 0); exit(0); }
int main() { fp = fopen("log.txt","w+"); long long int cost = 0; struct timeval tv; starttime(&tv); thread_pool_t *pool = threadpool_create(2, 4, 10000); int i; for(i = 0; i < 1000; i++) { dispatch(pool, test_fun, (void *)i); } //sleep(5); dispatch(pool, test_fun, (void *)i, EMG_PRI); threadpool_destroy(pool, 1); cost = stoptime(tv); printf("%lld\n", cost/1000); return 0; }
void do_subprocess_job(int serfd) { int clifd, epollfd, nthread; struct epoll_event ev, *ea; threadpool_t *tpool=NULL; nthread = atoi(walkerconf[THREADNUMBER].value); if (nthread>0) tpool = threadpool_create(nthread, 4, 0); /*epoll routine*/ epollfd = epoll_create(100); ev.data.fd = serfd; ev.events = EPOLLIN; epoll_ctl(epollfd, EPOLL_CTL_ADD, serfd, &ev); ea=(struct epoll_event*)malloc(sizeof(ev)*EPOLLMAX); while(1){ memset(ea, 0, sizeof(ev)*EPOLLMAX); int ret = epoll_wait(epollfd, ea, EPOLLMAX, -1); for (int i=0; i<ret; i++){ if (ea[i].data.fd == serfd && ea[i].events & EPOLLIN){ clifd = user_accept(serfd); if (clifd == -1)/*connected has been established by other process*/ continue; ev.data.fd = clifd; ev.events = EPOLLIN | EPOLLONESHOT; //ev.events = EPOLLIN; epoll_ctl(epollfd, EPOLL_CTL_ADD, clifd, &ev); }else if (ea[i].events & EPOLLIN){ http_handle(ea[i].data.fd, tpool, epollfd); } } } }
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 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; }
int main(int argc, char ** argv) { int listen_fd, epfd; int epoll_events_count; struct epoll_event ev, events[EPOLL_EVENTS_MAX]; struct sockaddr_in clientaddr; socklen_t addrlen; int i, serv_port; SAK_threadpool_t * tp_handler = NULL; #if DEBUG clock_t tstart; #endif if(argc != 2) { perror("usage: ./lotus <port>"); exit(-1); } serv_port = atoi(argv[1]); bzero(&clientaddr, sizeof(clientaddr)); addrlen = sizeof(struct sockaddr_in); /*********************** thread pool ***************************/ tp_handler = threadpool_create(THREADPOOL_THREADS); assert(tp_handler != NULL); printf("thread pool is created\n"); /************************** socket *****************************/ /* make server socket */ ERR2(listen_fd, make_server_socket(serv_port)); printf("web server socket is made\n"); /**************************** epoll ****************************/ /* create epoll */ ERR2(epfd, epoll_create(EPOLL_SIZE)); ev.data.fd = listen_fd; ev.events = EPOLLIN|EPOLLET; ERR(set_nonblock(listen_fd)); /* add listen_fd to epoll */ ERR(epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &ev)); printf("epoll module is created\n"); /* ignore pipe broken signal */ signal(SIGPIPE, SIG_IGN); /************************* main ********************************/ for(;;) { /* wait for changes of fd set */ ERR2(epoll_events_count, epoll_wait(epfd, events, EPOLL_EVENTS_MAX, EPOLL_TIMEOUT)); #if DEBUG tstart = clock(); #endif for(i = 0; i < epoll_events_count; i++) { #if DEBUG printf("events[%d].data.fd == %d\n", i, events[i].data.fd); #endif if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { fprintf(stderr, "ERROR: epoll error\n"); close(events[i].data.fd); continue; } else if(events[i].data.fd == listen_fd) { #if DEBUG printf("Is listen_fd = %d\n", listen_fd); #endif /* a new connection is coming, we accept it and add the new connection fd to epoll set */ while(1) { int connect_fd; char clientaddr_buff[20] = {0}; connect_fd = accept(listen_fd, (struct sockaddr *)&clientaddr, &addrlen); if(connect_fd == -1) { if((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* We have process all incoming connection */ break; } else { perror("accept"); break; } } inet_ntop(AF_INET, &clientaddr.sin_addr, clientaddr_buff, sizeof(clientaddr_buff)), printf("Client [%s : %d] is connected\n", clientaddr_buff, ntohs(clientaddr.sin_port)); ERR(set_nonblock(connect_fd)); ev.data.fd = connect_fd; #if DEBUG printf("Get and put connect_fd = %d into epoll\n", connect_fd); #endif ev.events = EPOLLIN|EPOLLET; ERR(epoll_ctl(epfd, EPOLL_CTL_ADD, connect_fd, &ev)); } continue; } else { /* Can read */ /************************************************/ /* Put the task into thread pool's work queue */ /************************************************/ #if DEBUG printf("Is connect_fd = %d\n", events[i].data.fd); #endif threadpool_put(tp_handler, process_request, (void *)events[i].data.fd); request_count ++; printf("Has handle %d requests\n", request_count); //ERR(epoll_ctl(epfd, EPOLL_CTL_DEL, ev.data.fd, &ev)); } } #if DEBUG printf("Statistics: %d events handled at %2.f seconds(s)\n", epoll_events_count, (double)(clock() - tstart)/CLOCKS_PER_SEC); #endif } close(listen_fd); close(epfd); threadpool_destroy(tp_handler); return 0; }
// The main function int main(int argc, char **argv) { // Read in argc int i; int num_servers; int portno; char *backend_hosts[MAX_SERVERS]; int backend_port[MAX_SERVERS]; int weights[MAX_SERVERS]; // if not enough number of servers or too many servers num_servers = (argc - 2) / 3; if (argc < 4 || num_servers > MAX_SERVERS || (argc - 2) % 3) { fprintf(stderr, "Usage: %s PROXY_PORTNO [BACKEND_ADDR PORTNO WEIGHT]...\n" "MAX server num: %d\n", argv[0], MAX_SERVERS); exit(1); } // Read in configs portno = atoi(argv[1]); for (i = 0; i < num_servers; ++i) { int host_index = i * 3 + 2; backend_hosts[i] = argv[host_index]; backend_port[i] = atoi(argv[host_index + 1]); weights[i] = atoi(argv[host_index + 2]); #ifdef DEBUG printf("[DEBUG] server %s:%d weight %d\n", backend_hosts[i], backend_port[i], weights[i]); #endif } // Start server procedure // setup network, start server int serverfd; server_conf_t server_conf; balancer_t balancer; unsigned backend_rotate; threadpool_t *pool; // Init balancer balancer_init(&balancer, num_servers, weights); // Init thread pool pool = threadpool_create(THREAD_NUM, THREADPOOL_SIZE); // creating server thread pool printf("[INFO] Created server pool with %d threads and %d queue\n", THREAD_NUM, THREADPOOL_SIZE); // Init server config pthread_mutex_init(&(server_conf.lock), NULL); server_conf.total_conn = 0; for (i = 0; i < num_servers; ++i) { server_conf.connections[i] = 0; } // starting server procedure // Starting binding and listening printf("[INFO] Starting server procedure\n"); if ((serverfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("[ERROR] Error creating socket\n"); exit(1); } if (sock_bind_listen(serverfd, portno, MAX_CONNECT) < 0) { perror("[ERROR] Error binding and listening to socket\n"); exit(1); } // Main loop while (1) { // accept new connection int clientfd; // setup server balancer_balance(&balancer, &server_conf); server_conf.index = balancer.index; server_conf.backend_host = backend_hosts[balancer.index]; server_conf.backend_port = backend_port[balancer.index]; #ifdef DEBUG printf("[DEBUG] balancer index %d out of %d servers\n", balancer.index, balancer.server_num); #endif // setup connection number pthread_mutex_lock(&(server_conf.lock)); server_conf.connections[balancer.index]++; server_conf.total_conn++; pthread_mutex_unlock(&(server_conf.lock)); // accept and new thread if ((clientfd = sock_accept(serverfd)) > 0) { #ifdef DEBUG printf("[INFO] Accepting new connection\n"); #endif // hand new request to threads server_conf.clientsockfd = clientfd; threadpool_assign(pool, server_thread, (void *)&server_conf); } } // Gracefully shutting down threadpool_destroy(pool); // DEBUG printf("[INFO] closing procedure\n"); pthread_mutex_destroy(&(server_conf.lock)); return 0; }
K threads_init(K x) { unsigned char bytes[]={0x01,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0x65,0x13}; K s=ktn(KG,sizeof(bytes));memcpy(kG(s),bytes,sizeof(bytes)); if(!okx(s))R krr("serialization");value_fn=d9(s); if(!(threadpool=threadpool_create(NUMTHREADS,QUEUESIZE,0)))R krr("threadpool"); if(pipe(mainloop_pipe)==-1)R krr("pipe");sd1(mainloop_pipe[0],threads_q_callback);R(K)0;}
void *DataReceiver::epollReadData(void *arg) { struct epoll_event ev_read[10]; int nfds = 0; //return the events count ThreadPool *tp = threadpool_create(5, 10240); int recBufferLen = 1024; char *recData = (char*) (malloc(recBufferLen)); int start = 0, end = 0, totleLen = 0; int index = 0; int dataLen; int readLen = 0; SocketPool *sp = NULL; std::map<int, SocketPool*>::iterator it; while (1) { nfds = epoll_wait(*(int*) (arg), ev_read, 10, 300); for (int i = 0; i < nfds; ++i) { start = 0; end = 0; totleLen = 0; readData: readLen = CSocket::anetRead(ev_read[i].data.fd, recData + end, recBufferLen - end); if (readLen <= 0) { pthread_mutex_lock(&mutex); it = socketMap->find(ev_read[i].data.fd); if (it != socketMap->end()) { sp = it->second; } pthread_mutex_unlock(&mutex); if (sp) { sp->closeSocket(ev_read[i].data.fd); sp = NULL; } gaeaLog(GAEA_WARNING, "DataReceiver::anetRead,happened socket error%d,%d\n", end, recBufferLen); continue; } totleLen += readLen; while (end < totleLen) { if (recData[end] == P_END_TAG[index]) { ++index; if (index == sizeof(P_END_TAG)) { dataLen = end - start - sizeof(P_START_TAG); char *retData = (char*) (malloc(dataLen)); memcpy(retData, recData + start + sizeof(P_START_TAG), dataLen); readDataStruct *rds = new readDataStruct; rds->fd = ev_read[i].data.fd; rds->len = dataLen; rds->data = retData; threadpool_add_task(tp, socketReadData, rds); index = 0; ++end; start = end; } } else if (recData[end] == P_END_TAG[0]) { index = 1; } else { index = 0; } ++end; } if (end == totleLen) { if (start == 0 && end >= recBufferLen) { recData = (char*) (realloc(recData, recBufferLen * 2)); recBufferLen = recBufferLen * 2; goto readData; } else if (start < end - 1) { memcpy(recData, recData + start, end - start); end -= start; start = 0; totleLen = end; goto readData; } } } } return NULL; }
int main(int argc, char **argv) { void *mod_routing = NULL; char *conffile = NULL; FILE *file = NULL; int nofork = 1; char str[100]; int c = 0; char b_log = 1; char b_fork = 1; memset(&str, 0, 100*sizeof(char)); log_init("logFile",NULL); log2display(LOG_ALERT); p_threadpool = threadpool_create(5, 8096, 0); init_maps(); init_call_id(NULL); while((c=getopt(argc, argv, "c:vP:fhD:"))!=-1) { switch(c) { case 'c': conffile = optarg; break; case 'v': printf("sip2smpp version: %s\n", VERSION); exit(0); break; case 'P': pid_file = optarg; break; case 'f': nofork = 0; b_fork = 0; break; case 'h': usage(0); break; case 'D': { char log = atoi(optarg); if(log >= 0 && log <= 8) { log2display((Loglevel)log); b_log = 0; } break; } default: abort(); } } if(!conffile) { conffile = (char*)malloc(sizeof(char)*strlen(DEFAULT_CONFIG)+1); strcpy(conffile,DEFAULT_CONFIG); } if((file = fopen(conffile,"r")) != NULL) { fclose(file); } else { ERROR(LOG_FILE | LOG_SCREEN,"The INI file isn't found!"); handler(-1); } if(load_config_file((uint8_t*)conffile, CONFIG_ALL, NULL) == -1) { ERROR(LOG_FILE | LOG_SCREEN,"There are errors in the INI file!"); free_config_file(CONFIG_ALL, NULL); handler(-1); } if(b_log) { log2display((Loglevel)cfg_main->log_level); } if(b_fork) { nofork = !cfg_main->fork; } if(daemonize(nofork) != 0) { ERROR(LOG_FILE | LOG_SCREEN,"Daemoniize failed"); exit(-1); } //Load routing module void* functions[2] = { send_sms_to_smpp, send_sms_to_sip }; void* cfgs[2] = { cfg_smpp, cfg_sip }; if(cfg_main->routing_module) { mod_routing = dlopen(cfg_main->routing_module, RTLD_NOW | RTLD_GLOBAL); if(!mod_routing) { ERROR(LOG_SCREEN | LOG_FILE, "%s", dlerror()); handler(-1); } f_start_routing = dlsym(mod_routing, "start_routing"); f_routing = dlsym(mod_routing, "routing"); f_close_routing = dlsym(mod_routing, "close_routing"); } else { f_start_routing = default_start_routing; f_routing = default_routing; f_close_routing = default_close_routing; } if(db_init() == -1) { ERROR(LOG_FILE | LOG_SCREEN,"There are errors when the DB connection!"); handler(-1); } else { //TODO: send sms saved in db } if(f_start_routing(functions, cfgs) != 0) { ERROR(LOG_FILE | LOG_SCREEN, "Routing loading failed"); handler(-1); } if(cfg_main && cfg_main->launch_msg) { printf("\033[0;36m%s\033[0m\n", cfg_main->launch_msg); } printf("SIP 2 SMPP Version [%s]\n", VERSION); printf("Pid file [%s]\n", pid_file); printf("Config File [%s]\n", conffile); display_config_file(CONFIG_ALL, NULL); start_all_threads_interfaces(); join_all_threads(); threadpool_destroy(p_threadpool, threadpool_graceful); f_close_routing(); if(cfg_main->routing_module) { dlclose(mod_routing); } free_config_file(CONFIG_ALL, NULL); handler(0); return 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; }
threadpool_t *create_threadpool() { return threadpool_create(THREAD_COUNT, QUEUE_SIZE, 0); }