Re: create a daemon problems

From: Steve (none_at_none.com)
Date: 05/12/03


Date: Mon, 12 May 2003 20:23:33 +0100

Craig wrote:

> I'm trying to create a daemon, the problem im having is it
> going down to the bottom and doing the
> printf("This is by the exit zero at bottom"); line.
>
> It never going to the for loop.
>
> Ive been racking my head for the past 3 hours and cant figure it. please
> help.
> (I have also tried it withought the if statement block)
> --------------------------------------------
> if( fork() == 0 ) {
>
>
> if (test_mode != 1){
> fclose( stdin );
> fclose( stdout );
> }
> while( 1 ) {
> //////////////////////////////
>
> printf("Test");
>
> ////////////////////////////
> exit ( 0 );
>
> }
> }
>
> else {
> printf("This is by the exit zero at bottom");
> exit( 0 );
> return ( 0 );
>
>
> } /* End if( fork() ) */
>
> exit( 0 );
> return( 0 );
>
> } /* End main() */

Craig wrote:

> Sorry I meant while loop
>
>> You have no for loop in this code.

No it is going into the 'while' loop. Assuming test_mode != 1, the output
looks like this:

This is by the exit zero at bottomTest

Notice it says, 'Test' on the end, from the printf in the while loop.

A fork creates a new process in addition to the original one, and then both
run concurrently. So both branches of the 'if (fork()...)' will
execute: the child process executes the 'if' clause, and the original
process continues running and executes the 'else' clause.



Relevant Pages

  • Re: Difference between "while" and "if" (pretty basic)
    ... Would it be appropriate to use while statements or if statements within the innermost loop? ... When the second line of this piece of code executes, MATLAB checks if a is equal to 1. ... so the WHILE loop body executes and increases the value of b by 1. ...
    (comp.soft-sys.matlab)
  • Re: Difference between "while" and "if" (pretty basic)
    ... > Ok so I tested my own theoretical loops, and the "while" loops seems to> have gone into some infinite loop while the "if" loop did exactly what I> wanted. ... When the second line of this piece of code executes, MATLAB checks if a is equal to 1. ... so the WHILE loop body executes and increases the value of b by 1. ...
    (comp.soft-sys.matlab)
  • Re: Help - Multithreading Question
    ... The problem I am having is not with the while loop. ... > Everything works well when the timer fires or when while loop executes ... >> will end when the timer fires. ... >> unmanaged code. ...
    (microsoft.public.dotnet.framework)
  • Re: Re-instantiation of an application
    ... Now at each point of execution where there is a reboot ... I am writing a test utility that runs multiple tests. ... The tests are executed within a method having a double loop. ... > The outer loop executes a "group" of tests. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Help - Multithreading Question
    ... The problem I am having is not with the while loop. ... Everything works well when the timer fires or when while loop executes ... >> public void OnTimer ...
    (microsoft.public.dotnet.framework)