本文整理汇总了C++中Fork函数的典型用法代码示例。如果您正苦于以下问题:C++ Fork函数的具体用法?C++ Fork怎么用?C++ Fork使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fork函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
}
开发者ID:BackupTheBerlios,项目名称:nixstaller-svn,代码行数:26,代码来源:utils.cpp
示例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);
}
}
开发者ID:duybkict,项目名称:BattleShip,代码行数:39,代码来源:tcpserv01.c
示例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);
}
开发者ID:jeche,项目名称:OPS,代码行数:28,代码来源:parent_child.c
示例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);
}
}
开发者ID:jiuningzhong,项目名称:proj01,代码行数:34,代码来源:interact.c
示例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);
}
开发者ID:09zwcbupt,项目名称:csapp,代码行数:25,代码来源:procmask.c
示例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);
}
开发者ID:Nigelzhf,项目名称:csappCodeHomeWork,代码行数:27,代码来源:sigsuspend.c
示例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;
}
开发者ID:jack-lijing,项目名称:unix,代码行数:35,代码来源:signal.c
示例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);
*/
}
开发者ID:2dot4,项目名称:Psiphon3-for-Linux,代码行数:25,代码来源:remote.c
示例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);
}
开发者ID:tcharding,项目名称:self_learning,代码行数:33,代码来源:counter-mmapdev0.c
示例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];
}
开发者ID:2dot4,项目名称:Psiphon3-for-Linux,代码行数:27,代码来源:sftp.c
示例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);
}
开发者ID:piaoyimq,项目名称:CppSpace,代码行数:33,代码来源:lat_pipe.c
示例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);
}
开发者ID:CanuxCheng,项目名称:UNP1,代码行数:35,代码来源:mycat.c
示例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;
}
开发者ID:jack-lijing,项目名称:unix,代码行数:29,代码来源:lock.c
示例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);
}
开发者ID:rmayur,项目名称:nachos-project-4-submitted,代码行数:26,代码来源:halt.c
示例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);
}
开发者ID:weiang,项目名称:C-codes,代码行数:26,代码来源:waitpid.c
示例16: main
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");
}
开发者ID:VasishtaShastry,项目名称:CS170,代码行数:28,代码来源:test.c
示例17: main
//=============================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);
}
}
开发者ID:nightfly19,项目名称:renyang-learn,代码行数:36,代码来源:server.c
示例18: main
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 */
}
}
开发者ID:rkks,项目名称:refer,代码行数:33,代码来源:serv01.c
示例19: main
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);
}
开发者ID:09zwcbupt,项目名称:csapp,代码行数:27,代码来源:signal1.c
示例20: main
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);
}
}
开发者ID:wbing-xu,项目名称:wbing_telnet,代码行数:32,代码来源:server.c
注:本文中的Fork函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论