Пример #1
0
int main( int argc, char *argv[] ) {


    if ( (argc-1) % 2 != 0 ) {
        println("Malformed flags.");
        return EXIT_FAILURE;
    } else { // overwrite defaults

        int i;
        char flag[ SMALL_BUFFER ];
        for ( i = 1; i < argc; i++ ) {
            strcpy( flag, argv[ i ] );
            if ( strEqual(flag, "-s") )
                service_time = atoi( argv[ ++i ] );
            else if ( strEqual(flag, "-b") )
                break_time = atoi( argv[ ++i ] );
            else if ( strEqual(flag, "-h") )
                shared_id = atoi( argv[ ++i ] );
        }
    }

    shared = attachSharedMem( shared_id );
    initSems();
    int i = 0;

    do {

        p_sem_wait( &shared->client_present );

        if ( !clientsPresent() ) {

            println( "No clients present. Cashier taking break." );
            sleep(  break_time );

        } else {

            signalClient();
            serviceClient();

        }

        i++;

    } while ( i < MAX_NUM_CLIENTS );

    log( "A cashier detaching from shared memory." );
    detachSharedMem( shared );

    return 0;

}
//void ioworker(int *client, int sockfd, int nready, int maxi, fd_set rset, fd_set allset) {
void* ioworker(void *newClient) {

	size_t i = 0;
	Sel_Struct *data;
	ClientData *thisClient = (ClientData *)newClient;
	int *client = thisClient->client;
	int sockfd = thisClient->sockfd;
	int nready = thisClient->nready;
	int maxi = thisClient->maxi;
	fd_set rset = thisClient->rset;
	fd_set allset = thisClient->allset;

	char* clientAddr = thisClient->ipAddr;
	int clientPort = thisClient->port;

	for (i = 0; i <= maxi; i++) {
		if ((sockfd = client[i]) < 0) continue;

		if (FD_ISSET(sockfd, &rset)) {
			data = malloc(sizeof(Sel_Struct));
			data->client = client;
			data->sockfd = sockfd;
			data->i		 = i;
			data->allset = allset;
			data->ipAddr = clientAddr;
			data->port 	 = clientPort;
			data->numOfRequests = 0;
			data->numOfBytesRecv = 0;

			serviceClient((void *)data);

			if (--nready <= 0) break;
		}
	}
	 return NULL;	
}