CITS3002 Computer Networks  
prev
next CITS3002 help3002 CITS3002 schedule  

Iterative Servers - managing one process per client

In this second model, a separate operating system process is spawned to handle each new client. There is thus no waiting required if any clients are slow or long-running, but (significant?) additional load is added to an operating system.

Case 2 - wll service subsequent clients quicker, but cannot scale indefinitely:

//  Open a socket, listen, bind an address
    ....
    bool keep_going = true;

    while(keep_going) {
        int new_client   = accept(mysocket, ....);    

        if(new_client == -1) {
            perror("accept");
            keep_going = false;
        }
        else {
            switch ( fork() ) {
            case -1:
                keep_going = false;
                perror("fork");
                break;

            case  0: {
                extern bool service(int sd);

                close(mysocket);
                (void)service(new_client);

                shutdown(new_client, SHUT_RDWR);
                close(new_client);
                exit(EXIT_SUCCESS);
                break;
            }
            default:
                shutdown(new_client, SHUT_RDWR);
                close(new_client);
                break;
            }
        }
    }
    shutdown(mysocket, SHUT_RDWR);
    close(mysocket);

Note that while this example will work, that it is not complete. In particular, the parent process will need to (eventually) wait for the completion of its child processes, else many "zombie" processes will persist.


CITS3002 Computer Networks, Lecture 9, Client/server design, p9, 1st May 2024.