c++ - fork() system call within a daemon using _exit() -


there lot of questions on fork() little bit confused in code.i analyzing code in c++ in have got function.

int daemon(int nochdir, int noclose)  {    switch (fork())    {     case 0:  break;     case -1: return -1;     default: _exit(0);          /* exit original process */   }    if (setsid() < 0)               /* shoudn't fail */     return -1;  /* dyke out switch if want acquire control tty in */ /* future -- not advisable daemons */   printf("starting %s                [ ok ]\n",agent_name);    switch (fork())    {     case 0:  break;     case -1: return -1;     default: _exit(0);   }    if (!nochdir)       {     chdir("/");   }    if (!noclose)    {     dup(0);      dup(1);   }  return 0; } 

so fork create exact copy of code fork() has been called. so,

  1. is switch executed twice or once?

  2. if twice in switch if child executes first? break or go other statements?

  3. what if parent executes? main process terminated , child continue?

edit: switch run twice once parent , once child. , behaves on return values.

and final thing is, daemon predefined function , has been redefined , used user created daemon. how create daemon process ,

`if (!nochdir)     {     chdir("/"); }` 

and

if (!noclose)  {     dup(0);      dup(1); } 

i calling function this.

if (daemon(0, 0) < 0)  {     printf("starting %s                [ failed ]\n",agent_name);     exit(2); } 

is switch executed twice or once?

it said fork function called once returns twice, once in each process: once in parent , once in child.

man :

on success, pid of child process returned in parent, , 0 returned in child. on failure, -1 returned in parent, no child process created, , errno set appropriately

it might return once (-1): in parent if child wasn't created. returns in parent ( -1 on error, > 0 on success).

if twice in switch if child executes first? break or go other statements?

it unknown whether child or parent returns first. after fork() memory segments copied child, continues correct value 0 returned fork(). parent continues pid of child. use return value of fork in code determine whether child or parent. maybe more clear if write code way

int daemon( int nochdir, int noclose)  {    pid_t pid;                /* drive logic in code */     if ( ( pid = fork()) < 0) /* fork , remember actual value returned pid */       return -1;    if( pid > 0)     _exit(0);               /* exit original process */    // here pid 0, i.e. child 

what if parent executes? main process terminated , child continue?

what if parent exit() called before child instructions? yes, parent terminate, child on own. both parent , child processes possess same code segments, execute independently of each other (unless added synchronization).

http://linux.die.net/man/2/fork


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -