int main(int argc, char ** argv) { /* Create variables corresponding to the number of processes that have * been requested as well as this processes particular rank. */ int n_processes, rank; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &n_processes); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Split the tasks up according to whether they are the worker processes * or the master process. */ switch (rank) { case MASTER_PROCESS: master_process(); TERMINATE_ALL(); printf("Master process completed.\n"); break; case SCRIBE_PROCESS: scribe_process(); printf("Scribe process completed.\n"); break; default: worker_process(); printf("Worker process completed.\n"); break; } /* That's all folks! */ MPI_Finalize(); }
int main(int argc, char* argv[]){ //useage:./epollserver port worker_process_num if(argc < 3){ printf("useage:./epollserver port worker_process_num\n"); exit(0); } int port = atoi(argv[1]); int worker_num = atoi(argv[2]); //master start work master_process(port, worker_num); }
int master_main(char **argv, int waitattach, int dontfork) { int fd[2] = {-1, -1}; int s; pid_t pid; /* Use a default redraw method if one hasn't been specified yet. */ if (redraw_method == REDRAW_UNSPEC) redraw_method = REDRAW_CTRL_L; /* Create the unix domain socket. */ s = create_socket(sockname); if (s < 0 && errno == ENAMETOOLONG) { char *slash = strrchr(sockname, '/'); /* Try to shorten the socket's path name by using chdir. */ if (slash) { int dirfd = open(".", O_RDONLY); if (dirfd >= 0) { *slash = '\0'; if (chdir(sockname) >= 0) { s = create_socket(slash + 1); fchdir(dirfd); } *slash = '/'; close(dirfd); } } } if (s < 0) { printf("%s: %s: %s\n", progname, sockname, strerror(errno)); return 1; } #if defined(F_SETFD) && defined(FD_CLOEXEC) fcntl(s, F_SETFD, FD_CLOEXEC); /* If FD_CLOEXEC works, create a pipe and use it to report any errors ** that occur while trying to execute the program. */ if (dontfork) { fd[1] = dup(2); if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0) { close(fd[1]); fd[1] = -1; } } else if (pipe(fd) >= 0) { if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0) { close(fd[0]); close(fd[1]); fd[0] = fd[1] = -1; } } #endif if (dontfork) { master_process(s, argv, waitattach, fd[1]); return 0; } /* Fork off so we can daemonize and such */ pid = fork(); if (pid < 0) { printf("%s: fork: %s\n", progname, strerror(errno)); unlink_socket(); return 1; } else if (pid == 0) { /* Child - this becomes the master */ if (fd[0] != -1) close(fd[0]); master_process(s, argv, waitattach, fd[1]); return 0; } /* Parent - just return. */ #if defined(F_SETFD) && defined(FD_CLOEXEC) /* Check if an error occurred while trying to execute the program. */ if (fd[0] != -1) { char buf[1024]; ssize_t len; close(fd[1]); len = read(fd[0], buf, sizeof(buf)); if (len > 0) { write(2, buf, len); kill(pid, SIGTERM); return 1; } close(fd[0]); } #endif close(s); return 0; }