int main(void) { int sock[2]; if(socketpair(PF_UNIX, SOCK_STREAM, 0, sock)) { perror("socketpair"); exit(1); } else { printf("Established socket pair: (%d, %d)\n", sock[0], sock[1]); } switch(fork()) { case 0: close(sock[0]); child_process(sock[1]); break; case -1: perror("fork"); exit(1); default: close(sock[1]); parent_process(sock[0]); wait(NULL); break; } return(0); }
void start(void) { pid_t pid; daemon(1, 0); chdir("/tmp/"); while (1) { pid = fork(); if (pid == 0) { child_process(); break; } else if (pid > 0) { parent_process(pid); } else { print_log("fork error"); exit(1); } } exit(0); }
int main() { if(-1 == tell_wait()) { printf("fail to tell_wait\n"); return 0; } pid_t pid = fork(); if(0 > pid) { printf("fail to fork, errno: %d, errmsg: %s\n", errno, strerror(errno)); return 0; } else if(0 == pid) { child_process(); _exit(0); } parent_process(); int status; if(-1 == wait(&status)) { printf("[P]fail to wait for child process, errno: %d, errmsg: %s\n", errno, strerror(errno)); } return 0; }
void execute_program(char *program, char *argv[], char *envp[], int input_count, char *input[], char **output) { int fd_stdin[2]; int fd_stdout[2]; if(-1 == pipe(fd_stdout)) { FLOCK_TRACE("Error: Failed to create pipe for stdout \n"); exit(1); } if(-1 == pipe(fd_stdin)) { FLOCK_TRACE("Error: Failed to create pipe for stdin\n"); exit(1); } pid_t pid = fork(); if(pid== -1) { FLOCK_TRACE("Error: Failed to create a process\n"); exit(1); } else if(pid == 0) { //Child process gets pid as zero FLOCK_TRACE("Child process created"); child_process(fd_stdin, fd_stdout, program, argv, envp); } else if(pid > 0) { // parent process gets child_pid FLOCK_TRACE("Parent process"); parent_process(pid, fd_stdin, fd_stdout, input_count, input, output); } }
void main() { pid_t pid; pid = fork(); if(pid == 0) { child_process(); } else { parent_process(); } }
int main(int argc, char const *argv[]) { int newpid; printf("Before: pid is %d\n", getpid()); if ((newpid = fork()) == 0) { child_process(DELAY); } else { parent_process(newpid); } return 0; }
int main() { char mqname[NAMESIZE]; pid_t pid; int to_parent[2]; int to_child[2]; int rval; struct sigaction sa; sa.sa_handler = SIG_IGN; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sigaction(SIGCHLD, &sa, NULL); sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); rval = pipe(to_parent); if (rval == -1) { perror(ERROR_PREFIX "fd[0]"); return PTS_UNRESOLVED; } rval = pipe(to_child); if (rval == -1) { perror(ERROR_PREFIX "fd[1]"); return PTS_UNRESOLVED; } pid = fork(); if (pid == -1) { perror(ERROR_PREFIX "fork"); return PTS_UNRESOLVED; } if (pid == 0) { //child process close(to_parent[PIPE_READ]); close(to_child[PIPE_WRITE]); return child_process(mqname, to_child[PIPE_READ], to_parent[PIPE_WRITE]); } else { //parent process close(to_parent[PIPE_WRITE]); close(to_child[PIPE_READ]); return parent_process(mqname, to_parent[PIPE_READ], to_child[PIPE_WRITE], pid); } }
int main(int argc, char *argv[]) { /* Programma che genera un processo figlio e che poi termina prima del figlio stesso, rendendolo orfano; attenzione, non si sta parlando di zombie, lo scopo e' di far adottare l'orfano da init. */ pid_t pid; pid = fork(); if (pid == -1) { fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno)); exit(EXIT_FAILURE); } if (pid == 0) child_process(pid); else parent_process(pid); return(EXIT_SUCCESS); }
int main(int argc , char **argv[]) { printf("\nmain()\n"); int id; id = fork(); if(id == 0) { //printf("\nchild process : %d\n" , getpid()); //fork(); child_process(); } else if (id > 0) { //printf("\nparent process : %d\n" , getpid()); //wait(); parent_process(); } printf("\nend of main : %d\n" , getpid()); return 0; }
static int clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel, struct clk_init_data *init_data) { const char **parent_names = NULL; u32 parent_count = 0; u32 *parent_sel; /* * If a peripheral clock has multiple parents, the value * used by the hardware to select that parent is represented * by the parent clock's position in the "clocks" list. Some * values don't have defined or supported clocks; these will * have BAD_CLK_NAME entries in the parents[] array. The * list is terminated by a NULL entry. * * We need to supply (only) the names of defined parent * clocks when registering a clock though, so we use an * array of parent selector values to map between the * indexes the common clock code uses and the selector * values we need. */ parent_sel = parent_process(clocks, &parent_count, &parent_names); if (IS_ERR(parent_sel)) { int ret = PTR_ERR(parent_sel); pr_err("%s: error processing parent clocks for %s (%d)\n", __func__, init_data->name, ret); return ret; } init_data->parent_names = parent_names; init_data->num_parents = parent_count; sel->parent_count = parent_count; sel->parent_sel = parent_sel; return 0; }
void sample_start() { pid_t pid; int pipefd[2]; /* create pipe first */ if (pipe(pipefd) < 0) { perror("pipe"); return; } /* then fork child process */ pid = fork(); if (pid < 0) { perror("fork"); return; } if (pid > 0) child_process(pipefd); else parent_process(pipefd); }
int main(void) { pid_t result_pid; struct termios oldtio, newtio; char buf[255]; fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); if (fd < 0) { perror(MODEMDEVICE); return (-1); } tcgetattr(fd, &oldtio); memset(&newtio, 0, sizeof(newtio)); // ready serial port serial_init(fd); //fork result_pid = fork(); if (result_pid == -1) { fprintf(stderr, "fork failed.\n"); return COULDNOTFORK; } if (result_pid == 0) { child_process(); } else { fprintf(stderr, "fork completed"); parent_process(result_pid); } STOP = TRUE; return 0; }
/*** * main_template总是在main函数中调用,通常如下一行代码即可: * int main(int argc, char* argv[]) * { * return main_template(argc, argv); * } */ int main_template(IMainHelper* main_helper, int argc, char* argv[]) { // 退出代码,由子进程决定 int exit_code = 1; // 忽略掉PIPE信号 if (main_helper->ignore_pipe_signal()) { if (SIG_ERR == signal(SIGPIPE, SIG_IGN)) { fprintf(stderr, "Ignored SIGPIPE error: %s.\n", sys::CUtils::get_last_error_message().c_str()); return 1; } } while (true) { pid_t pid = self_restart(main_helper)? fork(): 0; if (-1 == pid) { // fork失败 fprintf(stderr, "fork error: %s.\n", sys::CUtils::get_last_error_message().c_str()); break; } else if (0 == pid) { child_process(main_helper, argc, argv); } else if (!parent_process(main_helper, pid, exit_code)) { break; } } return exit_code; }
static void RunCrashHandler( const CrashData *crash ) { if( g_pCrashHandlerArgv0 == NULL ) { safe_print( fileno(stderr), "Crash handler failed: CrashHandlerHandleArgs was not called\n", NULL ); _exit( 1 ); } /* Block SIGPIPE, so we get EPIPE. */ struct sigaction sa; memset( &sa, 0, sizeof(sa) ); sa.sa_handler = SIG_IGN; if( sigaction( SIGPIPE, &sa, NULL ) != 0 ) { safe_print( fileno(stderr), "sigaction() failed: %s", strerror(errno), NULL ); /* non-fatal */ } static int received = 0; static int active = 0; if( active ) { /* We've received a second signal. This may mean that another thread * crashed before we stopped it, or it may mean that the crash handler * crashed. */ switch( crash->type ) { case CrashData::SIGNAL: safe_print( fileno(stderr), "Fatal signal (", SignalName(crash->signal), ")", NULL ); break; case CrashData::FORCE_CRASH: safe_print( fileno(stderr), "Crash handler failed: \"", crash->reason, "\"", NULL ); break; default: safe_print( fileno(stderr), "Unexpected RunCrashHandler call (", itoa(crash->type), ")", NULL ); break; } if( active == 1 ) safe_print( fileno(stderr), " while still in the crash handler\n", NULL); else if( active == 2 ) safe_print( fileno(stderr), " while in the crash handler child\n", NULL); _exit( 1 ); } active = 1; received = getpid(); /* Stop other threads. XXX: This prints a spurious ptrace error if any threads * are already suspended, which happens in ForceCrashHandlerDeadlock(). */ RageThread::HaltAllThreads(); /* We need to be very careful, since we're under crash conditions. Let's fork * a process and exec ourself to get a clean environment to work in. */ int fds[2]; if( pipe(fds) != 0 ) { safe_print( fileno(stderr), "Crash handler pipe() failed: ", strerror(errno), "\n", NULL ); exit( 1 ); } pid_t childpid = fork(); if( childpid == -1 ) { safe_print( fileno(stderr), "Crash handler fork() failed: ", strerror(errno), "\n", NULL ); _exit( 1 ); } if( childpid == 0 ) { active = 2; close( fds[1] ); spawn_child_process( fds[0] ); } else { close( fds[0] ); parent_process( fds[1], crash ); close( fds[1] ); int status = 0; #if !defined(MACOSX) waitpid( childpid, &status, 0 ); #endif /* We need to resume threads before continuing, or we may deadlock on _exit(). */ /* XXX: race condition: If two threads are deadlocked on a pair of mutexes, there's * a chance that they'll both try to lock the other's mutex at the same time. If * that happens, then they'll both time out the lock at about the same time. One * will trigger the deadlock crash first, and the other will be suspended. However, * once we resume it here, it'll continue, and * trigger the deadlock crash again. * It can result in unrecoverable deadlocks. */ RageThread::ResumeAllThreads(); if( WIFSIGNALED(status) ) safe_print( fileno(stderr), "Crash handler child exited with signal ", itoa(WTERMSIG(status)), "\n", NULL ); } }