int main(int argc, char **argv) { int this_id=0, n_procs=0, n_workers=0; int status=0, ierr=0; // dummy ierr=MPI_Init(&argc, &argv); ierr=MPI_Comm_rank(MPI_COMM_WORLD, &this_id); ierr=MPI_Comm_size(MPI_COMM_WORLD, &n_procs); n_workers = n_procs-1; if (n_procs > 1) { if (this_id == MASTER) { ierr = run_master(n_workers); fprintf(stderr,"master: running MPI finalize\n"); } else { ierr = run_worker(this_id); } } else { status=EXIT_FAILURE; fprintf(stderr,"need > 1 processes (master+workers)\n"); } ierr=MPI_Finalize(); if (this_id == MASTER) { fprintf(stderr,"master: MPI finalized\n"); } if (ierr != 0) { status=EXIT_FAILURE; fprintf(stderr,"Encountered error: %d\n", ierr); } return status; }
int main(int argc, char *argv[]) { coreid_t mycore = disp_get_core_id(); debug_printf("This is mem_bench_3\n"); if (argc < 2) { return run_worker(mycore); } else { return run_master(mycore, argc, argv); } }
int start_worker(struct sockaddr_in addr, int sv) { pid_t pid=fork(); if(pid<0) { perror("worker fork()"); return -1; } if(pid>0) { return (int)pid; } run_worker(addr, sv); return 0; }
int main(int argc, char *argv[]) { char buf[BUFSIZE] = "localhost"; unsigned port = CPORT; int c; int level = 1; bool try_local_router = false; bool try_self_route = true; while ((c = getopt(argc, argv, "hv:H:P:nr")) != -1) { switch (c) { case 'h': usage(argv[0]); break; case 'H': strncpy(buf, optarg, BUFSIZE-1); buf[BUFSIZE-1] = '\0'; break; case 'v': level = atoi(optarg); break; case 'P': port = atoi(optarg); break; case 'n': try_self_route = false; break; case 'r': try_local_router = true; break; default: printf("Unknown option '%c'\n", c); usage(argv[0]); break; } } set_verblevel(level); init(buf, port, try_self_route, try_local_router); if (signal(SIGTERM, sigterm_handler) == SIG_ERR) err(false, "Couldn't install signal handler"); run_worker(); finish(); mem_status(stdout); chunk_status(stdout); return 0; }
int main(int argc, const char * argv[]) { // Too few arguments specified. if (argc < 2) { print_usage(); return 0; } // One argument specified, should only be the interactive mode. if (argc == 2) { if (!strcmp("-i", argv[1])) { run_interactive(); return 0; } print_usage(); return 0; } // Two arguments. The first should either be the demonstration or worker flag. // The second is assumed to be a file path; errors with that are handled further down. if (argc == 3) { if (!strcmp("-d", argv[1])) { run_multiprocess(argv); return 0; } if (!strcmp("-w", argv[1])) { run_worker(argv); return 0; } if (!strcmp("-t", argv[1])) { run_threaded(argv[2]); return 0; } print_usage(); return 0; } // Anything more and it was used wrong. print_usage(); return 0; }
int main(int argc, char *argv[], char *envp[]) { mog_upgrade_prepare(argc, argv, envp); /* hack for older gcov + gcc, see nostd/setproctitle.h */ spt_init(argc, argv, envp); set_program_name(argv[0]); mog_intr_disable(); setup(argc, argv); /* this daemonizes */ mog_process_init(worker_processes); if (worker_processes == 0) run_worker(0); else run_master(); return 0; }
int start_worker(struct worker *w, struct options *opt) { int fds_r[2]; int fds_w[2]; int cpid; w->next = NULL; w->status = STATUS_READY; if(pipe(fds_r) || pipe(fds_w)) { perror("pipe"); return EXIT_FAILURE; } cpid = fork(); if(cpid == -1) { perror("fork"); return EXIT_FAILURE; } if(cpid == 0) { struct worker_data wd = {}; wd.pipe_r = fds_w[0]; wd.pipe_w = fds_r[1]; wd.debug = opt->debug; wd.timeout = opt->timeout; wd.dns_servers = opt->dns_servers; wd.ai_family = opt->ai_family; close(fds_r[0]); close(fds_w[1]); sigaction(SIGINT, &sigign, NULL); sigaction(SIGTERM, &sigdfl, NULL); _exit(run_worker(&wd)); } else { w->pipe_r = fds_r[0]; w->pipe_w = fds_w[1]; w->url = NULL; close(fds_r[1]); close(fds_w[0]); w->pid = cpid; return 0; } return 0; }
int main(int argc, char *argv[]) { int me, nprocs; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Barrier(MPI_COMM_WORLD); my_id = me; if (me == MASTER) { run_master(me, nprocs, argc, argv); } else { run_worker(me, nprocs); } MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); }
static void fork_worker(unsigned worker_id) { pid_t pid; pid_t parent = getpid(); /* not using getppid() since it's racy */ pid = fork(); if (pid > 0) { mog_process_register(pid, worker_id); } else if (pid == 0) { /* workers have no workers of their own */ worker_processes = 0; mog_process_reset(); /* worker will call mog_intr_enable() later in notify loop */ run_worker(parent); exit(EXIT_SUCCESS); } else { syslog(LOG_ERR, "fork() failed: %m, sleeping for 10s"); mog_sleep(10); } }
int main(int argc, char **argv) { char ch; char path[PATHLENGTH], buf[MAXWORD], d_paths[MAXPATHS][PATHLENGTH]; FreqRecord master_array[MAXRECORDS]; char *startdir = "."; pid_t pid; int i, j, k, num = 0; while((ch = getopt(argc, argv, "d:")) != -1) { switch (ch) { case 'd': startdir = optarg; break; default: fprintf(stderr, "Usage: query [-d DIRECTORY_NAME]\n"); exit(1); } } // Open the directory provided by the user (or current working directory) DIR *dirp; if((dirp = opendir(startdir)) == NULL) { perror("opendir"); exit(1); } /* For each entry in the directory, eliminate . and .., and check * to make sure that the entry is a directory, then call run_worker * to process the index file contained in the directory. * Note that this implementation of the query engine iterates * sequentially through the directories, and will expect to read * a word from standard input for each index it checks. */ struct dirent *dp; while((dp = readdir(dirp)) != NULL) { if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0 || strcmp(dp->d_name, ".svn") == 0) { continue; } strncpy(path, startdir, PATHLENGTH); strncat(path, "/", PATHLENGTH - strlen(path) - 1); strncat(path, dp->d_name, PATHLENGTH - strlen(path) - 1); struct stat sbuf; if(stat(path, &sbuf) == -1) { //This should only fail if we got the path wrong // or we don't have permissions on this entry. perror("stat"); exit(1); } if(S_ISDIR(sbuf.st_mode)) { //copy the path of each subdirectory to the d_paths array strncpy(d_paths[num], path, PATHLENGTH); //count the number of subdirectories num++; } } //create a buffer that will read the FreqRecord from a worker FreqRecord *buffer = malloc(sizeof(FreqRecord)); if (buffer == 0) { perror("malloc"); exit(1); } //two 2D array of pipes to stores all the necessary pipes int p1[num][2], p2[num][2]; for (j = 0; j < num; j++) { //create pipes before forking so that master and worker have access to the same pipe if (pipe(p1[j]) == -1) { perror("pipe"); exit(1); } if (pipe(p2[j]) == -1) { perror("pipe"); exit(1); } } for (j = 0; j < num; j++) { pid = fork(); switch (pid) { //fork fails case -1: perror("fork"); exit(1); //child case 0: //close the writing end of pipe 1 if (close(p1[j][1]) != 0) { perror("close"); exit(1); } //close the reading end of pipe 2 if (close(p2[j][0]) != 0) { perror("close"); exit(1); } for (k = 0; k < j; k++) { //close the writing end of pipe 1 if (close(p1[k][1]) != 0) { perror("close"); exit(1); } //close the reading end of pipe 2 if (close(p2[k][0]) != 0) { perror("close"); exit(1); } } //run worker for each child //reads from reading end of pipe1, writes to writing of pipe2 run_worker(d_paths[j], p1[j][0], p2[j][1]); exit(0); //parent default: //close the reading end of pipe 1 if (close(p1[j][0]) != 0) { perror("close"); exit(1); } //close the writing end of pipe 2 if (close(p2[j][1]) != 0) { perror("close"); exit(1); } break; } } while (1) { printf("Enter the word:\n"); int r, s = 0; //if user enters Ctrl-D, break out of the infinite loop if ((r = read(STDIN_FILENO, buf, MAXWORD)) == 0) { printf("Exiting\n"); break; } //otherwise continue the algorithm else { buf[r-1] = '\0'; for (i = 0; i < MAXRECORDS; i++) { master_array[i].freq = 0; strncpy(master_array[i].filename, "", PATHLENGTH); } for (i = 0; i < num; i++) { //write the word to all workers if (write(p1[i][1], buf, MAXWORD) < 0) { perror("write"); exit(1); } } //initialize the master frequency array for (i = 0; i < num; i++) { //read one FreqRecord from each worker if (read(p2[i][0], buffer, sizeof(FreqRecord)) < 0) { perror("read"); exit(1); } if(buffer->freq == 0) continue; master_array[s].freq = buffer->freq; strncpy(master_array[s].filename, buffer->filename, PATHLENGTH); s++; //while workers still have data while (buffer->freq != 0) { master_array[s].freq = buffer->freq; strncpy(master_array[s].filename, buffer->filename, PATHLENGTH); if (read(p2[i][0], buffer, sizeof(FreqRecord)) < 0) { perror("read"); exit(1); } s++; } } print_freq_records(master_array); } } //close all pipes before exiting for (i = 0; i < num; i++) { if (close(p1[i][1]) != 0) exit(1); if (close(p2[i][0]) != 0) exit(1); if (close(p2[i][1]) != 0) exit(1); if (close(p1[i][0]) != 0) exit(1); } for (i = 0; i < num; i++) { if (wait(NULL) == -1) { perror("wait"); exit(1); } } free(buffer); return 0; }
int main(int argc, char **argv) { int forks = 1; array_t(char*) addr_set; array_init(addr_set); char *keyfile = NULL; const char *config = NULL; char *keyfile_buf = NULL; /* Long options. */ int c = 0, li = 0, ret = 0; struct option opts[] = { {"addr", required_argument, 0, 'a'}, {"config", required_argument, 0, 'c'}, {"keyfile",required_argument, 0, 'k'}, {"forks",required_argument, 0, 'f'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ((c = getopt_long(argc, argv, "a:c:f:k:vVh", opts, &li)) != -1) { switch (c) { case 'a': array_push(addr_set, optarg); break; case 'c': config = optarg; break; case 'f': g_interactive = 0; forks = atoi(optarg); if (forks == 0) { kr_log_error("[system] error '-f' requires number, not '%s'\n", optarg); return EXIT_FAILURE; } #if (!defined(UV_VERSION_HEX)) || (!defined(SO_REUSEPORT)) if (forks > 1) { kr_log_error("[system] libuv 1.7+ is required for SO_REUSEPORT support, multiple forks not supported\n"); return EXIT_FAILURE; } #endif break; case 'k': keyfile_buf = malloc(PATH_MAX); assert(keyfile_buf); /* Check if the path is absolute */ if (optarg[0] == '/') { keyfile = strdup(optarg); } else { /* Construct absolute path, the file may not exist */ keyfile = realpath(".", keyfile_buf); if (keyfile) { int len = strlen(keyfile); int namelen = strlen(optarg); if (len + namelen < PATH_MAX - 1) { keyfile[len] = '/'; memcpy(keyfile + len + 1, optarg, namelen + 1); keyfile = strdup(keyfile); /* Duplicate */ } else { keyfile = NULL; /* Invalidate */ } } } free(keyfile_buf); if (!keyfile) { kr_log_error("[system] keyfile '%s': not writeable\n", optarg); return EXIT_FAILURE; } break; case 'v': kr_debug_set(true); break; case 'V': kr_log_info("%s, version %s\n", "Knot DNS Resolver", PACKAGE_VERSION); return EXIT_SUCCESS; case 'h': case '?': help(argc, argv); return EXIT_SUCCESS; default: help(argc, argv); return EXIT_FAILURE; } } /* Switch to rundir. */ if (optind < argc) { const char *rundir = argv[optind]; if (access(rundir, W_OK) != 0) { kr_log_error("[system] rundir '%s': %s\n", rundir, strerror(errno)); return EXIT_FAILURE; } ret = chdir(rundir); if (ret != 0) { kr_log_error("[system] rundir '%s': %s\n", rundir, strerror(errno)); return EXIT_FAILURE; } if(config && access(config, R_OK) != 0) { kr_log_error("[system] rundir '%s'\n", rundir); kr_log_error("[system] config '%s': %s\n", config, strerror(errno)); return EXIT_FAILURE; } } kr_crypto_init(); /* Fork subprocesses if requested */ int fork_count = forks; while (--forks > 0) { int pid = fork(); if (pid < 0) { perror("[system] fork"); return EXIT_FAILURE; } /* Forked process */ if (pid == 0) { kr_crypto_reinit(); break; } } /* Block signals. */ uv_loop_t *loop = uv_default_loop(); uv_signal_t sigint, sigterm; uv_signal_init(loop, &sigint); uv_signal_init(loop, &sigterm); uv_signal_start(&sigint, signal_handler, SIGINT); uv_signal_start(&sigterm, signal_handler, SIGTERM); /* Create a server engine. */ knot_mm_t pool = { .ctx = mp_new (4096), .alloc = (knot_mm_alloc_t) mp_alloc }; struct engine engine; ret = engine_init(&engine, &pool); if (ret != 0) { kr_log_error("[system] failed to initialize engine: %s\n", kr_strerror(ret)); return EXIT_FAILURE; } /* Create worker */ struct worker_ctx *worker = init_worker(loop, &engine, &pool, forks, fork_count); if (!worker) { kr_log_error("[system] not enough memory\n"); return EXIT_FAILURE; } /* Bind to sockets and run */ for (size_t i = 0; i < addr_set.len; ++i) { int port = 53; const char *addr = set_addr(addr_set.at[i], &port); ret = network_listen(&engine.net, addr, (uint16_t)port, NET_UDP|NET_TCP); if (ret != 0) { kr_log_error("[system] bind to '%s#%d' %s\n", addr, port, knot_strerror(ret)); ret = EXIT_FAILURE; } } /* Start the scripting engine */ if (ret == 0) { ret = engine_start(&engine, config ? config : "config"); if (ret == 0) { if (keyfile) { auto_free char *cmd = afmt("trust_anchors.file = '%s'", keyfile); if (!cmd) { kr_log_error("[system] not enough memory\n"); return EXIT_FAILURE; } engine_cmd(&engine, cmd); lua_settop(engine.L, 0); } /* Run the event loop */ ret = run_worker(loop, &engine); } } /* Cleanup. */ array_clear(addr_set); engine_deinit(&engine); worker_reclaim(worker); mp_delete(pool.ctx); if (ret != 0) { ret = EXIT_FAILURE; } kr_crypto_cleanup(); return ret; }