int main(int argc,char* argv[]) { signal(17,signal_handle); int sfd_listen=socket_init(); int sfd_client; struct sockaddr_in client_addr; char buf_client_addr[16]=""; int client_port; while(1) { memset(&client_addr,0,sizeof(struct sockaddr)); sfd_client=socket_accept(sfd_listen,(struct sockaddr*)&client_addr); if(fork()==0) { close(sfd_listen); strcpy(buf_client_addr,inet_ntoa(client_addr.sin_addr)); client_port=ntohs(client_addr.sin_port); printf("client %s:%d connect!\n",buf_client_addr,client_port); child_main(sfd_client); printf("client %s:%d disconnect!\n",buf_client_addr,client_port); close(sfd_client); exit(0); } close(sfd_client); } return 0; }
int main (int argc, char *argv[]) { int exitstatus; if (!(argc > 1 && strcmp (argv[1], "-child") == 0)) { /* This is the parent process. */ signal (SIGINT, cleanup_then_die); signal (SIGTERM, cleanup_then_die); #ifdef SIGHUP signal (SIGHUP, cleanup_then_die); #endif exitstatus = parent_main (); } else { /* This is the child process. */ exitstatus = child_main (); } unlink (DATA_FILENAME); return exitstatus; }
int main (int argc, char *argv[]) { int ret; #ifndef G_OS_WIN32 sigset_t sig_mask, old_mask; sigemptyset (&sig_mask); sigaddset (&sig_mask, SIGUSR1); if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0) { if (sigismember (&old_mask, SIGUSR1)) g_message ("SIGUSR1 was blocked, unblocking it"); } #endif dir = g_get_current_dir (); filename = g_build_filename (dir, "maptest", NULL); displayname = g_filename_display_name (filename); childname = g_build_filename (dir, "mapchild", NULL); if (argc > 1) ret = child_main (argc, argv); else ret = parent_main (argc, argv); g_free (childname); g_free (filename); g_free (displayname); g_free (dir); return ret; }
int start_child(int fd) { int pid; int pfd[2]; if (pipe(pfd) < 0) { ci_debug_printf(1, "Error creating pipe for communication with child\n"); return -1; } if (fcntl(pfd[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(pfd[1], F_SETFL, O_NONBLOCK) < 0) { ci_debug_printf(1, "Error making the child pipe non-blocking\n"); close(pfd[0]); close(pfd[1]); } if ((pid = fork()) == 0) { //A Child ....... MY_PROC_PID = getpid(); attach_childs_queue(childs_queue); child_data = register_child(childs_queue, getpid(), CONF.THREADS_PER_CHILD, pfd[1]); close(pfd[1]); child_main(fd, pfd[0]); exit(0); } else { close(pfd[0]); announce_child(childs_queue, pid); return pid; } }
void make_chl(pCHLD arr,int cnt){ int index; for(index=0;index<cnt;index++){ pid_t pid; int fds1[2],fds2[2];//fds1 pr fds2 pw pipe(fds1); pipe(fds2); pid=fork(); if(pid==0){ close(fds1[0]); close(fds2[1]); dup2(fds1[1],1); dup2(fds2[0],0); close(fds1[1]); close(fds2[0]); child_main(); } else{ close(fds1[1]); close(fds2[0]); arr[index].s_pid=pid; arr[index].s_status=S_IDLE; arr[index].s_read=fds1[0]; arr[index].s_write=fds2[1]; arr[index].s_cnt=0; } } }
int father_main(){ int fd; int ret; int on = 1; int optval = 1; int recv_buf[100]; int client_fd; int pid = fork(); if(pid == 0){ usleep(100); child_main(); exit(0); } else{ if((fd = socket(AF_INET6,SOCK_DCCP,IPPROTO_DCCP)) == -1){ perror("socket"); return -1; } struct sockaddr_in6 addr , remout_addr; memset(&addr,0,sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_port = htons(DCCP_PORT); addr.sin6_addr = in6addr_any; ret = setsockopt(fd, SOL_DCCP, SO_REUSEADDR, (const char *) &on, sizeof(on)); if(ret == -1){ perror("setsockopt 1"); } ret = setsockopt(fd,SOL_IPV6,IPV6_RECVPKTINFO,&optval,sizeof(optval)); if(ret == -1){ perror("setsockopt 2"); } ret = bind(fd,(struct sockaddr *)&addr,sizeof(addr)); if(ret == -1){ perror("bind"); } ret = listen(fd,2); if(ret == -1){ perror("listen"); } client_fd = accept(fd,(struct sockaddr *)&remout_addr,0);//fack addr close(fd); } waitpid(pid,NULL,0); return 0; }
//main int main(int argc , char *argv[]) { int fd_listen, fd_client; MY_ASSERT((fd_listen = socket(AF_INET, SOCK_STREAM , 0)) != -1 ,"listen socket init");//socket struct sockaddr_in seraddr, clientaddr; socklen_t sock_len = sizeof(clientaddr); memset(&seraddr, 0, sizeof(seraddr)); seraddr.sin_family = AF_INET; seraddr.sin_port = htons(atoi(argv[2]) ); seraddr.sin_addr.s_addr = inet_addr(argv[1]); MY_ASSERT(bind(fd_listen, (struct sockaddr*)&seraddr, sizeof(seraddr))==0, "bind");//bind MY_ASSERT(listen(fd_listen, 5) == 0, "listen");//listen while(1) { sock_len =sizeof(clientaddr); memset(&clientaddr, 0, sock_len); fd_client = accept(fd_listen ,(struct sockaddr*)&clientaddr, &sock_len ); printf("client %s:%d connect!\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); if(fork() == 0) { close(fd_listen); child_main(fd_client); close(fd_client); exit(1); } close(fd_client); } }
int make_child(pCHLD arr,int cnt){ int index; int max_read=0x80000000; for(index=0;index<cnt;index++){ pid_t pid; int fds1[2],fds2[2]; pipe(fds1);//0 pr 1 cw pipe(fds2);//0 cr 1 pw pid=fork(); if(pid==0){ close(fds1[0]); close(fds2[1]); dup2(fds2[0],0); dup2(fds1[1],1); child_main(); exit(0); } else{ close(fds1[1]); close(fds2[0]); arr[index].s_pid=pid; arr[index].s_flag=S_IDLE; if(fds1[0]>max_read){ max_read=fds1[0]; } arr[index].s_write=fds2[1]; } } return max_read; }
int main(int argc, char *argv[]) { int fd_listen; fd_listen = socket(AF_INET,SOCK_STREAM,0); signal(17,child_handle); iferr(fd_listen); struct sockaddr_in my,clientaddr; socklen_t sock_len = sizeof(clientaddr); init(&my); iferr(bind(fd_listen,(struct sockaddr*)&my,16)); iferr(listen(fd_listen,5)); int client; printf("start\n"); while(1){ sock_len = sizeof(clientaddr); memset(&clientaddr,0,(int)sock_len); client = accept(fd_listen,(struct sockaddr*)&clientaddr,&sock_len); printf("client : %s:%d connrct!\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port)) ; if(fork()==0) { close(fd_listen); child_main(client); close(client); exit(1); } close(client); } printf("end\n"); wait(NULL); }
/* This function forks a child which calls child_main(). First, * however, it has to establish communication paths to and from the * newborn child. It creates two socket pairs -- one for writing to * the child (from the parent) and one for reading from the child * (writing to the parent). Since that's four socket ends, each * process has to close the two ends it doesn't need. The remaining * two socket ends are retained for reading and writing. In the * child, the STDIN and STDOUT file descriptors refer to these * sockets. In the parent, the function arguments f_in and f_out are * set to refer to these sockets. */ pid_t local_child(int argc, char **argv, int *f_in, int *f_out, int (*child_main)(int, char*[])) { pid_t pid; int to_child_pipe[2]; int from_child_pipe[2]; /* The parent process is always the sender for a local rsync. */ assert(am_sender); if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { rsyserr(FERROR, errno, "pipe"); exit_cleanup(RERR_IPC); } pid = do_fork(); if (pid == -1) { rsyserr(FERROR, errno, "fork"); exit_cleanup(RERR_IPC); } if (pid == 0) { am_sender = 0; am_server = 1; filesfrom_fd = -1; chmod_modes = NULL; /* Let the sending side handle this. */ if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || close(to_child_pipe[1]) < 0 || close(from_child_pipe[0]) < 0 || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { rsyserr(FERROR, errno, "Failed to dup/close"); exit_cleanup(RERR_IPC); } if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); child_main(argc, argv); } /* Let the client side handle this. */ if (logfile_name) { logfile_name = NULL; logfile_close(); } if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { rsyserr(FERROR, errno, "Failed to close"); exit_cleanup(RERR_IPC); } *f_in = from_child_pipe[0]; *f_out = to_child_pipe[1]; return pid; }
int start_server(int fd){ int child_indx,pid,i,status; int childs,freeservers,used,maxrequests; ci_proc_mutex_init(&accept_mutex); if(!create_childs_queue(&childs_queue,MAX_CHILDS)){ debug_printf(1,"Can't init shared memory.Fatal error, exiting!\n"); exit(0); } pid=1; #ifdef MULTICHILD for(i=0;i< START_CHILDS; i++){ if(pid) pid=start_child(fd); } if(pid!=0){ main_signals(); while(1){ sleep(1); /*Must be replaced by nanosleep. */ childs_queue_stats(&childs_queue,&childs,&freeservers, &used, &maxrequests); debug_printf(10,"Server stats: \n\t Childs:%d\n\t Free servers:%d\n\tUsed servers:%d\n\tRequests served:%d\n", childs, freeservers,used, maxrequests); if( (freeservers<=MIN_FREE_SERVERS && childs<MAX_CHILDS) ||childs<START_CHILDS){ debug_printf(8,"Going to start a child .....\n"); pid=start_child(fd); } else if(freeservers>=MAX_FREE_SERVERS&& childs>START_CHILDS){ child_indx=find_an_idle_child(&childs_queue); childs_queue.childs[child_indx].to_be_killed=GRACEFULLY; kill(childs_queue.childs[child_indx].pid,SIGINT); debug_printf(8,"Going to stop child %d .....\n",childs_queue.childs[child_indx].pid); //kill a server ... } } for(i=0;i<START_CHILDS;i++){ pid=wait(&status); debug_printf(5,"The child %d died with status %d\n",pid,status); } } #else child_data=(child_shared_data_t *)malloc(sizeof(child_shared_data_t)); child_data->pid=0; child_data->freeservers=START_SERVERS; child_data->usedservers=0; child_data->requests=0; child_data->connections=0; child_data->to_be_killed=0; child_data->idle=1; child_main(fd); #endif }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; void child_main(int, int, int); if ( (pid = fork()) > 0) return(pid); /* parent */ child_main(i, listenfd, addrlen); /* never returns */ }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; void child_main(int, int, int); if((pid = Fork()) > 0) { return pid; } child_main(i, listenfd, addrlen); }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; if ( (pid = fork()) > 0) return (pid); /* in parent, return child's pid */ // in child child_main(i, listenfd, addrlen); /* never returns */ }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; void child_main(int, int, int); if ((pid = fork()) > 0) return (pid); child_main(i, listenfd, addrlen); }
int start_child(int fd){ int pid; if((pid=fork())==0){//A Child ....... attach_childs_queue(&childs_queue); child_data=register_child(&childs_queue,getpid(),START_SERVERS); child_main(fd); exit(0); } else return pid; }
int child_make(int i, int iListenFd, socklen_t iSockLen) { pid_t pid; if ( (pid = fork()) > 0 ) { return pid; } child_main(i, iListenFd, iSockLen); return 0; }
void ACEApp::parent_hatch(pid_t* child_pids, int child_proc_num) { for (int i = 0; i < child_proc_num; i++) { if (child_pids[i] != 0) { continue; } pid_t child_pid = ACE_OS::fork(); if (child_pid == -1) { ACE_DEBUG((LM_ERROR, "[%D] fork %d child failed, (%d)%s\n", i, errno, ACE_OS::strerror(errno))); ACE_OS::sleep(10); continue; } else if (child_pid == 0) { // child is_child_ = true; is_quit_ = false; ACE_Reactor::instance()->reset_reactor_event_loop(); ACE_OS::signal (SIGINT, SIG_IGN); ACE_OS::signal (SIGCHLD, SIG_IGN); if (init_log(i) == 0) { ACE_DEBUG((LM_INFO, "[%D] enter child main pid %d: %d\n", i, ACE_OS::getpid())); child_main(); ACE_DEBUG((LM_INFO, "[%D] leave child main pid %d: %d\n", i, ACE_OS::getpid())); } quit_app(); ACE_OS::exit(0); } else { // parent ACE_DEBUG((LM_ERROR, "[%D] child forked, pid %d: %d\n", i, child_pid)); child_pids[i] = child_pid; } } }
static int new_conn(int fd) { pid_t pid; pid = fork(); if (!pid) child_main(fd); close(fd); if (pid > 0) pid = 0; else perror("lscgid: fork() failed"); return pid; }
pid_t local_child(int argc, char **argv,int *f_in,int *f_out, int (*child_main)(int, char*[])) { pid_t pid; int to_child_pipe[2]; int from_child_pipe[2]; extern int read_batch; /* dw */ if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { rprintf(FERROR,"pipe: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } pid = do_fork(); if (pid == -1) { rprintf(FERROR,"fork: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } if (pid == 0) { extern int am_sender; extern int am_server; am_sender = read_batch ? 0 : !am_sender; am_server = 1; if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || close(to_child_pipe[1]) < 0 || close(from_child_pipe[0]) < 0 || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); child_main(argc, argv); } if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { rprintf(FERROR,"Failed to close : %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } *f_in = from_child_pipe[0]; *f_out = to_child_pipe[1]; return pid; }
void test(pid_t *pid) { *pid = fork(); if(*pid == 0){ printf("child = %d\n", getpid()); child_main(); }else if(*pid > 0){ printf("parent = %d\n", getpid()); }else printf("failed to fork"); }
int main (int argc, char *argv[]) { dir = g_get_current_dir (); filename = g_build_filename (dir, "maptest", NULL); displayname = g_filename_display_name (filename); childname = g_build_filename (dir, "mapchild", NULL); if (argc > 1) return child_main (argc, argv); else return parent_main (argc, argv); }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; void child_main(int, int, int); if ( (pid = fork()) < 0) { perror("fork error"); exit(1); } else if (pid > 0) { return(pid); /* parent */ } child_main(i, listenfd, addrlen); /* never returns */ }
pid_t child_make(int i, int listenfd, int addrlen) { pid_t pid; void child_main(int, int, int); pid = fork(); if(pid < 0) { err_sys("fork"); } else if ( pid > 0) return(pid); /* parent */ child_main(i, listenfd, addrlen); /* never returns */ }
int main(int argc,char *argv[]) { //1-soclet int fd_listen = socket (AF_INET,SOCK_STREAM,0); printf("listen\n"); per(fd_listen); //2-bind struct sockaddr_in seraddr; memset(&seraddr,0,sizeof(seraddr)); seraddr.sin_family = AF_INET; seraddr.sin_port = htons(SERVER_PORT); seraddr.sin_addr.s_addr = inet_addr(SERVER_IP); int judge = bind(fd_listen,(const struct sockaddr*)&seraddr,sizeof(seraddr)); printf("bind\n"); per(judge); //3-listen judge = listen(fd_listen,5); per(judge); printf("listen finish\n"); //4-int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); it's a block fun int fd_client; while(1){ struct sockaddr_in clientaddr; socklen_t len = sizeof(clientaddr); memset(&clientaddr,0,sizeof(clientaddr)); fd_client = accept(fd_listen,\ (struct sockaddr*)&clientaddr,&len); printf("fdclient = %d \n",fd_client); per(fd_client); printf("%s: %d connect!\n",\ inet_ntoa(clientaddr.sin_addr),\ ntohs(clientaddr.sin_port)); if(fork()==0) { close(fd_listen); child_main(fd_client); exit(0); } } //5- print the server info //6-recv close(fd_client); }
int make_child(pchild_t pchild,int nchild){ int fds[2]; int index=0; socketpair(AF_LOCAL,SOCK_STREAM,0fds); while(nchild>0){ if(!fork()){ close(fds[1]); child_main(fsd[0]); exit(0); } pchild[index].ch_fd=fds[1]; pchild[index].ch_busy=0; nchild--; index++; } }
int start_child(int fd) { int pid; int pfd[2]; int children_num, free_servers, used_servers, max_requests; if (pipe(pfd) < 0) { ci_debug_printf(1, "Error creating pipe for communication with child\n"); return -1; } if (fcntl(pfd[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(pfd[1], F_SETFL, O_NONBLOCK) < 0) { ci_debug_printf(1, "Error making the child pipe non-blocking\n"); close(pfd[0]); close(pfd[1]); } if ((pid = fork()) == 0) { //A Child ....... MY_PROC_PID = getpid(); if (!attach_childs_queue(childs_queue)) { ci_debug_printf(1, "Can not access shared memory for %d child\n", (int)MY_PROC_PID); exit(-2); } child_data = register_child(childs_queue, getpid(), CI_CONF.THREADS_PER_CHILD, pfd[1]); if (!child_data) { childs_queue_stats(childs_queue, &children_num, &free_servers, &used_servers, &max_requests); ci_debug_printf(1, "Not available slot in shared memory for %d child (Number of slots: %d, Running children: %d, Free servers: %d, Used servers: %d)!\n", (int)MY_PROC_PID, childs_queue->size, children_num, free_servers, used_servers ); exit(-3); } close(pfd[1]); child_main(fd, pfd[0]); exit(0); } else { close(pfd[0]); announce_child(childs_queue, pid); return pid; } }
/* * Fork a child "child" (or in our case a process) and then start up the * child_main() function. */ static pid_t child_make (struct child_s *ptr) { pid_t pid; if ((pid = fork ()) > 0) return pid; /* parent */ /* * Reset the SIGNALS so that the child can be reaped. */ set_signal_handler (SIGCHLD, SIG_DFL); set_signal_handler (SIGTERM, SIG_DFL); set_signal_handler (SIGHUP, child_sighup_handler); child_main (ptr); /* never returns */ return -1; }
int main(int argc, const char *argv[]) { struct sockaddr_storage addr; struct sockaddr_un *paddr; sigset_t set; pid_t pid; int error, status; const char *path; if (argc < 2) return (1); path = argv[1]; paddr = (struct sockaddr_un *)&addr; paddr->sun_family = AF_LOCAL; strcpy(paddr->sun_path, path); paddr->sun_len = SUN_LEN(paddr); if (sigfillset(&set) == -1) return (2); if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) return (3); pid = fork(); switch (pid) { case -1: return (4); case 0: return (child_main((struct sockaddr *)paddr)); default: break; } error = parent_main((struct sockaddr *)paddr, pid); if (wait4(pid, &status, 0, NULL) == -1) return (5); if (!WIFEXITED(status)) return (6); if (WEXITSTATUS(status) != 0) return (64 + WEXITSTATUS(status)); return (error == 0 ? 0 : (32 + error)); }
int main() { common_initialize(); pid_t child_pid = fork(); if (child_pid == 0) { // In child return child_main(); } // In parent int ret = parent_main(); int status, wait_ret = waitpid(child_pid, &status, 0); if (wait_ret != child_pid || !WIFEXITED(status)) { return 1 | ret; } return ret; }