int start_worker(int i) { int fd[2]; pid_t pid; if(socketpair( AF_UNIX, SOCK_STREAM, 0, fd) < 0) { perror( "socketpair()" ); return L_HTTP_FAIL; } if(set_nonblocking(fd[0]) == L_HTTP_FAIL || set_nonblocking(fd[1]) == L_HTTP_FAIL) return L_HTTP_FAIL; if ((pid = fork ()) == 0) { printf("fork success: %d\n", getpid()); worker_main(fd); exit(1); } else if(pid < 0){ syslog (LOG_INFO, "fork error"); printf("fork error"); return L_HTTP_FAIL; } workers[i].pid = pid; workers[i].slot = i; workers[i].fd[0] = fd[0]; workers[i].fd[1] = fd[1]; close(fd[1]); return L_HTTP_SUCCESS; }
void loop_run(int nthread) { int i, ret; pthread_attr_t attr; pthread_attr_init(&attr); // pthread_t pthread_t用于声明线程ID。 typedef unsigned long int pthread_t; pthread_t* tids = malloc(sizeof(pthread_t) * nthread); for (i=0; i<nthread - 1; i++) { // pthread_create是类Unix操作系统(Unix、Linux、Mac OS X等)的创建线程的函数。 // 第一个参数为指向线程标识符的指针。 // 第二个参数用来设置线程属性。 // 第三个参数是线程运行函数的起始地址。 if ((ret = pthread_create(tids + i, &attr, worker_main, NULL)) != 0) { fprintf(stderr, "Can't create thread: %s\n", strerror(ret)); exit(1); } } worker_main(NULL); // wait workers to stop for (i=0; i<nthread - 1; i++) { // http://flyingv.iteye.com/blog/776476 (void) pthread_join(tids[i], NULL); pthread_detach(tids[i]); } free(tids); aeApiFree(&loop); }
pid_t start_worker(int passive_sock) { pid_t pid; if (!(pid = fork())) { exit(worker_main(passive_sock)); } return pid; }
// // FUNCTION: CSampleService::ServiceWorkerThread(void) // // PURPOSE: The method performs the main function of the service. It runs // on a thread pool worker thread. // void CSampleService::ServiceWorkerThread(void) { // Periodically check if the service is stopping. while (!m_fStopping) { // Perform main service function here... worker_main(); ::Sleep(2000); // Simulate some lengthy operations. } // Signal the stopped event. SetEvent(m_hStoppedEvent); }
int main(int argc, char *argv[]) { const char *host = NULL; int port = MPI_QUEUE_DEFAULT_PORT; char addr[LINK_ADDRESS_MAX]; char c; int w, rank; signal(SIGTERM, handle_abort); signal(SIGQUIT, handle_abort); signal(SIGINT, handle_abort); MPI_Init(&argc, &argv); debug_config(argv[0]); while((c = getopt(argc, argv, "d:ho:t:w:v")) != (char) -1) { switch (c) { case 'd': debug_flags_set(optarg); break; case 't': idle_timeout = string_time_parse(optarg); break; case 'o': debug_config_file(optarg); break; case 'v': show_version(argv[0]); return 0; case 'w': w = string_metric_parse(optarg); link_window_set(w, w); break; case 'h': default: show_help(argv[0]); return 1; } } if ((argc - optind) != 2) { show_help(argv[0]); return 1; } host = argv[optind]; port = atoi(argv[optind + 1]); if(!domain_name_cache_lookup(host, addr)) { fprintf(stderr, "couldn't lookup address of host %s\n", host); exit(1); } MPI_Comm_rank(MPI_COMM_WORLD, &rank); if(rank) { return worker_main(); } else { return master_main(host, port, addr); } }