NEXT UP previous
Next: My Home Page

Answers

  1. This is actually the arrangement suggested in the previous section, which gives clues as to the required answer. What you do is to make the process into a daemon set up the socket and then go into a loop accepting client connections. Each time you get a client connection you fork a child process to deal with it while the parent loops back to accept another connection.

    The pseudo code could be:

    	Setup current process as a daemon (fork, setsid, fork etc.)
    	Create a socket on a well known port
    	while (true)
    		Get a client connection
    		if (fork()==0)
    			Close parent's copy of client connection 
    		else
    			In child, deal with client request
    			exit child process 
    		endif
    		endwhile
    
    You will see essentially this solution used in the Tiny Daemon case study in the next tutorial.

  2. The solution to this problem just requires you to set up your launch command as a daemon to make the process run as a daemon and then use exec() to change the program in the daemon process to the one given on the command line. This can be done as follows:

    	main(int argc, char **argv) 
    	{
    		Setup process as a daenon (fork, setsid, fork etc.) 
    
    		execvp(argv [1], &argv[1]);
    	}
    

NEXT UP previous
Next: My Home Page