int main (void) { struct sigaction usr_action; sigset_t block_mask; pid_t child_id; /* Establish the signal handler. */ sigfillset (&block_mask); usr_action.sa_handler = synch_signal; usr_action.sa_mask = block_mask; usr_action.sa_flags = 0; sigaction (SIGUSR1, &usr_action, NULL); /* Create the child process. */ child_id = fork (); if (child_id == 0) child_function (); /* Does not return. */ /*@group*/ /* Busy wait for the child to send a signal. */ while (!usr_interrupt) ; /*@end group*/ /* Now continue execution. */ puts ("That's all, folks!"); return 0; }
// ========== Connection Handling ========== void handle_connection() { pid_t pid; int cfd; char c; // Wait to accept connection cfd = accept(sfd, NULL, NULL); if (cfd == -1) { crit_error("Could not accept connections"); } pid = fork(); if (pid == -1) error("Fork"); if (pid == 0) { // we are the child. child_function(cfd); printf("Spawned child exiting.\n"); while(read(cfd, &c, 1) > 0); std::cout << "Connection reading..." << std::endl; close(cfd); std::cout << "Connection closed." << std::endl; exit(0); } else { // we are the parent close(cfd); // the child handles this } }
int main() { pid_t child_id; signal(SIGUSR1, sig_usr); child_id = fork(); if(child_id == 0) child_function(); while(!usr_interrupt); puts("That's all."); return 0; }
int start_server() { int r; int pid; r = initialize_server(); if (r < 0) { LOG("Initialazing failed ... code %d\n",r); return -1; } //we start the 6 processes: // 4 listen on udp, one on TLS and one is a timer for (r=0;r<MAX_PROCESSES;r++) { pid = fork(); if (pid<0) { LOG("start_server:cannot fork\n"); return -2; } else if (pid == 0) //child { child_function(r+1); } else //dad { //LOG("Child %d started[%d]\n",r+1,pid); pids[r] = pid; } } if (dont_fork == 1) { for(;;) pause(); } else { sleep(10);//to be sure cleanup(1); while(wait(0)>0); return 0; } //LOG("starting server ... ok\n"); return 0; }
int main (void) { #ifdef MULTIPROCESS pid_t child; child = fork (); if (child == -1) return 1; #endif mypid = getpid (); #ifdef MULTIPROCESS if (child != 0) parent_function (child); else #endif child_function (); /* Not reached. */ abort (); }
int main(int argc, char *argv[]) { char file[80]; printf("Please specify a file to copy to /tmp?"); scanf("%80s", file); pid_t child = fork(); if (child == -1) { perror("Could not fork"); } else if (child == 0) { //Parent return child_function(file); } while (waitpid(child, NULL, WNOHANG) == 0) { printf("What is your name\n"); char name[80]; char color[80]; scanf("%s", name); printf("What is your favorite color?\n"); scanf("%s", color); } return 0; }