CPipedCMD::CPipedCMD(const char *cmd) : m_szCommand(cmd), m_ChildPID(0), m_bChEOF(false) { Pipe(m_iPipeFD); m_ChildPID = Fork(); if (m_ChildPID == 0) // Child { if (setsid() == -1) _exit(127); // Redirect to stdout dup2(m_iPipeFD[1], STDOUT_FILENO); close(m_iPipeFD[0]); close(m_iPipeFD[1]); execlp("/bin/sh", "sh", "-c", cmd, NULL); _exit(127); } else if (m_ChildPID > 0) // Parent { ::Close(m_iPipeFD[1]); m_PollData.fd = m_iPipeFD[0]; m_PollData.events = POLLIN | POLLHUP; } }
/** * make and then "./tcpserv01 [mode]" to run * mode = 1: reverse string * mode = 2: reverse string and save to file * mode = 3: take number input and return */ int main(int argc, char **argv) { printf("TCP Server!\n"); int listenfd, connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof (servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(listenfd, (SA *) & servaddr, sizeof (servaddr)); Listen(listenfd, LISTENQ); while (1) { clilen = sizeof (cliaddr); connfd = Accept(listenfd, (SA *) & cliaddr, &clilen); if ((childpid = Fork()) == 0) { Close(listenfd); // int mode = atoi(argv[1]); int mode = 1; // process request from client process_request(connfd, mode); exit(0); } Close(connfd); } }
int main() { SpaceId kid; int joinval; char *args[2]; prints("PARENT exists\n", ConsoleOutput); args[0] = "kid"; args[1] = (char *)0; kid = Fork(); if (kid != 0) { prints("PARENT after fork; kid pid is ", ConsoleOutput); printd((int)kid, ConsoleOutput); prints("\n", ConsoleOutput); joinval = Join(kid); prints("PARENT off Join with value of ", ConsoleOutput); printd(joinval, ConsoleOutput); prints("\n", ConsoleOutput); Halt(); /* not reached */ } else Exec("kid", args); }
int main(int argc, char *arg[]) { char *pToInputLine = NULL; /*This MUST be initialized, otherwise getline might think the memory pointed to by garbage is good to use or re-allocate with realloc!*/ int bufsize=0; /*We MUST use a non-misleading, and even better, informative name for the bufsize variable. My original "nread" was WRONG*/ int nret; printf("Hello. This is an interactive program.\n"); if(signal(SIGTSTP,handler)==SIG_ERR||signal(SIGINT,handler) == SIG_ERR) unix_error("signal error"); //pause(); nret = getline( &pToInputLine, &bufsize, stdin); printf("Thanks for line %s lengths: bufsize=%d nret=%d,", pToInputLine, bufsize, nret); printf("\nGive me another command:"); nret = getline( & pToInputLine, &bufsize, stdin ); printf("Thanks for line %s Lengths: bufsize=%d nret=%d,", pToInputLine, bufsize, nret); if( strcmp(pToInputLine, "FirstCommand\n")==0) { printf("Thanks for that FirstCommand!\n"); } else if( strcmp(pToInputLine, "Fork\n") == 0 ) { int fret; printf("hey man, I see you really want to try a Fork. Here goes\n"); fret = Fork(); printf("Fork() returned %d\n", fret); } }
int main(int argc, char **argv) { int pid; sigset_t mask; Signal(SIGCHLD, handler); initjobs(); /* Initialize the job list */ while (1) { Sigemptyset(&mask); Sigaddset(&mask, SIGCHLD); Sigprocmask(SIG_BLOCK, &mask, NULL); /* Block SIGCHLD */ /* Child process */ if ((pid = Fork()) == 0) { Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */ Execve("/bin/ls", argv, NULL); } /* Parent process */ addjob(pid); /* Add the child to the job list */ Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */ } exit(0); }
int main(int argc, char **argv) { sigset_t mask, prev; Signal(SIGCHLD, sigchld_handler); Signal(SIGINT, sigint_handler); Sigemptyset(&mask); Sigaddset(&mask, SIGCHLD); while (1) { Sigprocmask(SIG_BLOCK, &mask, &prev); /* Block SIGCHLD */ if (Fork() == 0) /* Child */ exit(0); /* Wait for SIGCHLD to be received */ pid = 0; while (!pid) Sigsuspend(&prev); /* Optionally unblock SIGCHLD */ Sigprocmask(SIG_SETMASK, &prev, NULL); /* Do some work after receiving SIGCHLD */ printf("."); } exit(0); }
int main(int argc, char *argv[]) { pid_t pid; TELL_WAIT(); pid = Fork(); if (pid == 0) { while ( n < 1000) { WAIT_PARENT(); printf(" c%d ", n); n += 2; TELL_PARENT(getppid()); //sleep(1); } } else if(pid > 0) { while (n < 1000) { printf(" p%d ", n); n += 2; TELL_CHILD(pid); // sleep(1); WAIT_CHILD(); } } printf("Hello, world\n"); return 0; }
void RshWatcher(int ninvoke,int port){ /* echo dynamically allocated PORT number */ /* fprintf(stdout,"%d\n",port); */ /* if( foreground ) */ if( ninvoke == 0 ){ CStr(host,MaxHostNameLen); getpeerNAME(0,AVStr(host)); proc_title("delegated/rsh/%s",host); if( Fork("RshWatcher") == 0 ){ serverControl(stdin,stdout); _Finish(0); } }else{ fprintf(stderr,"\r\nRESTARTED\r\n> "); fflush(stderr); } /* fclose(stdin); fclose(stdout); fclose(stderr); */ }
/* increment counter in shared memory segment; mmap /dev/zero version */ int main(void) { int fd, i, counter; pid_t pid; void *area; if ((fd = open("/dev/zero", O_RDWR)) < 0) err_sys("open error"); if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) err_sys("mmap error"); close(fd); /* can close /dev/zero now that it is mapped */ TELL_WAIT(); if ((pid = Fork()) > 0) { /* parent */ for (i = 0; i < NLOOPS; i += 2) { if ((counter = update((long *)area)) != i) err_quit("parent: expected %d, got %d", i, counter); TELL_CHILD(pid); WAIT_CHILD(); } } else { /* child */ for (i = 1; i < NLOOPS + 1; i += 2) { WAIT_PARENT(); if ((counter = update((long *)area)) != i) err_quit("child: expected %d, got %d", i, counter); TELL_PARENT(); } } exit(0); }
int connectToSftp(const char *host,int port,const char *user,int fdc,int fdv[]){ int socks[2]; socks[1] = CC_connect("sftp",host,port,user); if( 0 <= socks[1] ){ DEBUG("---- SFTPCC HIT[%d] %s@%s:%d\n",socks[1],user,host,port); if( !sftpIsAlive(socks[1]) ){ close(socks[1]); }else return socks[1]; } DEBUG("---- SFTPCC MISS %s@%s:%d\n",user,host,port); Socketpair(socks); if( Fork("SftpGW") == 0 ){ int fi; for( fi = 0; fi < fdc; fi++ ) close(fdv[fi]); close(socks[1]); closeServPorts(); signal(SIGINT,sigTERM); signal(SIGTERM,sigTERM); SftpGW(host,port,socks[0]); _exit(0); } close(socks[0]); return socks[1]; }
int main(int argc, char **argv) { int i, nloop, pipe1[2], pipe2[2]; char c; pid_t childpid; if (argc != 2) err_quit("usage: lat_pipe <#loops>"); nloop = atoi(argv[1]); Pipe(pipe1); Pipe(pipe2); if ( (childpid = Fork()) == 0) { for ( ; ; ) { /* child */ if (Read(pipe1[0], &c, 1) != 1) err_quit("read error"); Write(pipe2[1], &c, 1); } exit(0); } /* 4parent */ doit(pipe2[0], pipe1[1]); Start_time(); for (i = 0; i < nloop; i++) doit(pipe2[0], pipe1[1]); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); exit(0); }
int my_open(const char *pathname, int mode) { int fd, sockfd[2], status; pid_t childpid; char c, argsockfd[10], argmode[10]; Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); //child process if ((childpid = Fork()) == 0) { Close(sockfd[0]); snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]); /* what is in sockfd[1] */ snprintf(argmode, sizeof(argmode), "%d", mode); execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *)NULL); err_sys("execl error"); } //parent process Close(sockfd[1]); Waitpid(childpid, &status, 0); if (WIFEXITED(status) == 0) err_quit("child did not terminate"); if ((status = WEXITSTATUS(status)) == 0) read_fd(sockfd[0], &c, 1, &fd); else { errno = status; fd = -1; } Close(sockfd[0]); return (fd); }
int main(int argc, char *argv[]) { int fd; pid_t pid; /*Create a file and write two bytes to it*/ fd = Open("templock", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE) ; if(write(fd, "ab", 2) != 2) err_sys("write error"); TELL_WAIT(); pid = Fork(); if(pid == 0) { lockabyte("child", fd, 0); TELL_PARENT(getppid()); WAIT_PARENT(); lockabyte("child", fd, 1); }else { lockabyte("parent", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0); } return 0; }
void myFunction2() { int a1; int b1; int c1=200; int arr[40]; a1=123; b1=77; Write("\nHALT 1: myFunction2\n",21,ConsoleOutput); Fork(add); if(a1+b1 == c1) { Write("\nHALT 1: myFunction2: SumIsCorrect\n",35,ConsoleOutput); } else { Write("\nHALT 1: myFunction2: SumIs Not Correct\n",40,ConsoleOutput); } Exit(0); }
int main(void) { int status, i; pid_t pid; /* Parent creates N child processes */ for (i = 0; i < N; i ++) { if ((pid = Fork()) == 0) exit(100 + i); } /* Parent reaps N child processes */ while ((pid = waitpid(-1, &status, 0)) > 0) { if (WIFEXITED(status)) { printf("Child %d terminated normally with exit status = %d\n", pid, WEXITSTATUS(status)); } else printf("Child %d terminated abnormally\n", pid); } /* The only normal termination is if there are no more children */ if (errno != ECHILD) unix_error("waitpid error"); exit(EXIT_SUCCESS); }
void main() { OpenFileId OutFid, InFid; int ret =335; int size = 9; int stub =3; char buffer[20]; Create("out"); Create("out1"); OutFid = Open("out"); Write("Test: First write!\n", 21, ConsoleOutput); Read(buffer, size, ConsoleInput); Write(buffer, size, ConsoleOutput); Yield(); Write("Test: Second write!\n", 22, OutFid); Close(OutFid); Fork(test_function1); Exec("../test/test1"); }
//=============================main function============================ int main(int argc,char **argv) { int listenfd,connfd; pid_t childpid; socklen_t clilen,addrlen; struct sockaddr *cliaddr; if (argc == 2) listenfd = Tcp_listen(NULL,argv[1],&addrlen); else if (argc == 3) listenfd = Tcp_listen(argv[1],argv[2],&addrlen); else err_quit("usage: server [<host>] <port#>"); cliaddr = Malloc(addrlen); Signal(SIGCHLD,sig_chld); Signal(SIGINT,sig_int); for (;;) { clilen = addrlen; if ((connfd = accept(listenfd,cliaddr,&clilen))<0) { if (errno == EINTR) continue; else err_sys("accept error"); } if ((childpid == Fork()) == 0) { Close(listenfd); web_child(connfd); exit(0); } Close(connfd); } }
int main(int argc, char **argv) { int listenfd, connfd; pid_t childpid; void sig_chld(int), sig_int(int), web_child(int); socklen_t addrlen; struct netbuf cliaddr; if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], &addrlen); else if (argc == 3) listenfd = Tcp_listen(argv[1], argv[2], &addrlen); else err_quit("usage: serv01 [ <host> ] <port#>"); cliaddr.buf = Malloc(addrlen); cliaddr.maxlen = addrlen; Signal(SIGCHLD, sig_chld); Signal(SIGINT, sig_int); for ( ; ; ) { connfd = Xti_accept(listenfd, &cliaddr, 1); printf("connection from %s\n", Xti_ntop(&cliaddr)); if ( (childpid = Fork()) == 0) { /* child process */ Close(listenfd); /* close listening socket */ web_child(connfd); /* process the request */ exit(0); } Close(connfd); /* parent closes connected socket */ } }
int main() { int i, n; char buf[MAXBUF]; if (signal(SIGCHLD, handler1) == SIG_ERR) unix_error("signal error"); /* parent creates children */ for (i = 0; i < 3; i++) { if (Fork() == 0) { printf("Hello from child %d\n", (int)getpid()); Sleep(1); exit(0); } } /* parent waits for terminal input and then processes it */ if ((n = read(STDIN_FILENO, buf, sizeof(buf))) < 0) unix_error("read"); printf("Parent processing input\n"); while (1) ; exit(0); }
int main(int argc,char **argv) { int listenfd,connfd; pid_t childpid; struct sockaddr_in servaddr,cliaddr; socklen_t clilen; listenfd = Socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(WBING_PORT); Bind(listenfd,(SA *) &servaddr,sizeof(servaddr)); Listen(listenfd,10); signal(SIGCHLD,sig_chld); for(;;) { clilen = sizeof(cliaddr); connfd = Accept(listenfd,(SA*) &cliaddr,&clilen); if((childpid=Fork())==0){ Close(listenfd); do_it(connfd); exit(0); } Close(connfd); } }
int main(int argc, char **argv) { int sendfd, recvfd; const int on = 1; socklen_t salen; struct sockaddr *sasend, *sarecv; if (argc != 3) err_quit("usage: sendrecv <IP-multicast-address> <port#>"); sendfd = Udp_client(argv[1], argv[2], (void **) &sasend, &salen); recvfd = Socket(sasend->sa_family, SOCK_DGRAM, 0); Setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recvfd, sarecv, salen); Mcast_join(recvfd, sasend, salen, NULL, 0); Mcast_set_loop(sendfd, 0); if (Fork() == 0) recv_all(recvfd, salen); /* child -> receives */ send_all(sendfd, sasend, salen); /* parent -> sends */ }
int main(int argc, char **argv) { int i, nloop, contpipe[2], datapipe[2]; pid_t childpid; if (argc != 4) err_quit("usage: bw_pipe <#loops> <#mbytes> <#bytes/write>"); nloop = atoi(argv[1]); totalnbytes = atoi(argv[2]) * 1024 * 1024; xfersize = atoi(argv[3]); buf = Valloc(xfersize); Touch(buf, xfersize); Pipe(contpipe); Pipe(datapipe); if ( (childpid = Fork()) == 0) { writer(contpipe[0], datapipe[1]); /* child */ exit(0); } /* 4parent */ Start_time(); for (i = 0; i < nloop; i++) reader(contpipe[1], datapipe[0], totalnbytes); printf("bandwidth: %.3f MB/sec\n", totalnbytes / Stop_time() * nloop); kill(childpid, SIGTERM); exit(0); }
/* eval - evaluate a command line */ void eval(char *cmdline, char *alias_list, char *env[]) { char *argv[MAXARGS]; /* argv for execve() */ char buf[MAXLINE]; /* holds modified command line */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ strcpy(buf, cmdline); bg = parseline(buf, argv, alias_list, env); if (argv[0] == NULL) return; /* ignore empty lines */ if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } } /* parent waits for foreground job to terminate */ if (!bg) { int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else printf("%d %s", pid, cmdline); } return; }
output_t* run(char* path, char** args, int nargs) { output_t* output = (output_t*) Malloc(sizeof(*output)); int pfd[2]; Pipe(pfd); pid_t pid = Fork(); if(!pid) { Close(pfd[0]); char** argv; int argc = nargs + 1; argv = (char**) Malloc(sizeof(char*) * (argc + 1)); for(int i = 0; i < argc; i++) { argv[i] = (char*) Malloc(ARG_LEN); } argv[argc] = NULL; strcpy(argv[0], path); for(int i = 1; i < argc; i++) { assert(args[i - 1] != NULL); strcpy(argv[i], args[i - 1]); } Close(STDOUT); dup2(pfd[1], STDOUT); execv(path, argv); perror("execv"); } Close(pfd[1]); waitpid(pid, &output->retstat, 0); output->nbytes = read(pfd[0], output->out, BUF_SIZE); return output; }
unsigned ReqFile_run_cmd( void ) { file_run_cmd_ret *ret; #if defined(__WINDOWS__) ret = GetOutPtr( 0 ); ret->err = 0; #else bool chk; char buff[64]; file_run_cmd_req *acc; unsigned len; tiny_ret_t rc; acc = GetInPtr( 0 ); len = GetTotalSize() - sizeof( *acc ); ret = GetOutPtr( 0 ); chk = CheckPointMem( acc->chk_size, buff ); rc = Fork( (char *)GetInPtr( sizeof(*acc) ), len ); ret->err = TINY_ERROR( rc ) ? TINY_INFO( rc ) : 0; if( chk ) CheckPointRestore(); #endif return( sizeof( *ret ) ); }
int main() { int status, i; pid_t pid; /* Parent creates N children */ for (i = 0; i < N; i++) //line:ecf:waitpid1:for if ((pid = Fork()) == 0) /* Child */ //line:ecf:waitpid1:fork exit(100+i); //line:ecf:waitpid1:exit /* Parent reaps N children in no particular order */ while ((pid = waitpid(-1, &status, 0)) > 0) { //line:ecf:waitpid1:waitpid if (WIFEXITED(status)) //line:ecf:waitpid1:wifexited printf("child %d terminated normally with exit status=%d\n", pid, WEXITSTATUS(status)); //line:ecf:waitpid1:wexitstatus else printf("child %d terminated abnormally\n", pid); } /* The only normal termination is if there are no more children */ if (errno != ECHILD) //line:ecf:waitpid1:errno unix_error("waitpid error"); exit(0); }
int main(int argc, char **argv) { int listenfd, connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; void sig_chld(int); listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); Signal(SIGCHLD, sig_chld); for ( ; ; ) { clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); if ( (childpid = Fork()) == 0) { /* child process */ Close(listenfd); /* close listening socket */ str_echo(connfd); /* process the request */ exit(0); } Close(connfd); /* parent closes connected socket */ } }
int main(int argc, char **argv) { int listenfd, connfd, port; socklen_t clientlen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); Signal(SIGCHLD, sigchld_handler); listenfd = Open_listenfd(port); while (1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if (Fork() == 0) { Close(listenfd); echo(connfd); hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("server connected to %s (%s)\n", hp->h_name, haddrp); Close(connfd); exit(0); } Close(connfd); } exit(0); }
int my_open(const char *pathname, int mode) { int fd, sockfd[2], status; pid_t childpid; char c, argsockfd[10], argmode[10]; Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); if ( (childpid = Fork()) == 0) { /* child process */ Close(sockfd[0]); snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]); snprintf(argmode, sizeof(argmode), "%d", mode); execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *) NULL); err_sys("execl error"); } /* parent process - wait for the child to terminate */ Close(sockfd[1]); /* close the end we don't use */ Waitpid(childpid, &status, 0); if (WIFEXITED(status) == 0) err_quit("child did not terminate"); if ( (status = WEXITSTATUS(status)) == 0) Read_fd(sockfd[0], &c, 1, &fd); else { errno = status; /* set errno value from child's status */ fd = -1; } Close(sockfd[0]); return(fd); }
int main() { int status, i; pid_t pid[N], retpid; /* Parent creates N children */ for (i = 0; i < N; i++) if ((pid[i] = Fork()) == 0) /* Child */ //line:ecf:waitpid2:fork exit(100+i); /* Parent reaps N children in order */ i = 0; while ((retpid = waitpid(pid[i++], &status, 0)) > 0) { //line:ecf:waitpid2:waitpid if (WIFEXITED(status)) printf("child %d terminated normally with exit status=%d\n", retpid, WEXITSTATUS(status)); else printf("child %d terminated abnormally\n", retpid); } /* The only normal termination is if there are no more children */ if (errno != ECHILD) unix_error("waitpid error"); exit(0); }