本文整理汇总了C++中Fork函数的典型用法代码示例。如果您正苦于以下问题:C++ Fork函数的具体用法?C++ Fork怎么用?C++ Fork使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fork函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: m_szCommand
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;
}
}
示例2: main
/**
* 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);
}
}
示例3: main
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);
}
示例4: main
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);
}
}
示例5: main
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);
}
示例6: main
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);
}
示例7: main
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;
}
示例8: RshWatcher
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);
*/
}
示例9: main
/* 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);
}
示例10: connectToSftp
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] %[email protected]%s:%d\n",socks[1],user,host,port);
if( !sftpIsAlive(socks[1]) ){
close(socks[1]);
}else
return socks[1];
}
DEBUG("---- SFTPCC MISS %[email protected]%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];
}
示例11: main
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);
}
示例12: my_open
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);
}
示例13: main
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;
}
示例14: myFunction2
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);
}
示例15: main
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);
}