Ejemplo n.º 1
0
void runserver(int numthreads, unsigned short serverport) {
    //////////////////////////////////////////////////

    // create your pool of threads here

    //////////////////////////////////////////////////
    
    
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            fprintf(stderr, "Got connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));

           ////////////////////////////////////////////////////////
           /* You got a new connection.  Hand the connection off
            * to one of the threads in the pool to process the
            * request.
            *
            * Don't forget to close the socket (in the worker thread)
            * when you're done.
            */
           ////////////////////////////////////////////////////////


        }
    }
    fprintf(stderr, "Server shutting down.\n");
        
    close(main_socket);
}
Ejemplo n.º 2
0
void runserver(int numthreads, unsigned short serverport) {
///////////////////////////////////////////////////////////////
	int i=0;
	for(;i<numthreads;i++){
		pthread_t thread;
		pthread_create(&thread,NULL, &worker_start, NULL); 
	}
    	printf("made %d threads\n", numthreads);
//////////////////////////////////////////////////////////////   
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            time_t now = time(NULL);
            char* ip = inet_ntoa(client_address.sin_addr);
            int port = ntohs(client_address.sin_port);
            fprintf(stderr, "Got connection from %s:%d at %s\n", ip, port, ctime(&now));
/////////////////////////////////////////////////////////////////////
           queue_add_head(new_sock, ip, port);
           printf("added to queue\n");
           pthread_cond_signal(&cond);
/////////////////////////////////////////////////////////////////////
        }
    }
    pthread_cond_broadcast(&cond);
    fprintf(stderr, "Server shutting down.\n");
        
    close(main_socket);
}
Ejemplo n.º 3
0
/* Worker thread that waits for JVM migration */
static void JNICALL
worker2(jvmtiEnv* jvmti, JNIEnv* jni, void *p) {
    
    char buffer[256];
    struct sockaddr_in cli_addr;
    socklen_t clilen = sizeof(cli_addr);
    pid_t pid = getpid();
    
    int sockfd = prepare_server_socket();
    if(sockfd < 0) {
        fprintf(log, "ERROR: unable to create proxy server... exiting.\n");
        fflush(log);
        return;
    }
    
    while(true) {       
        coord_sock = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (coord_sock < 0) {
            fprintf(log, "ERROR: connection accept failed.\n");
            return;
        }   

        // Write PID    
        if (write(coord_sock, &pid, sizeof(pid_t)) < 0) {
            fprintf(log, "ERROR: unable to send pid (%u)\n", pid);
        }
        
        // Read Command
        if(read(coord_sock, buffer, 256) < 0) {
            fprintf(log, "ERROR: failed to read from socket.\n");
        }
        else if(!strncmp(buffer, PREPARE_MIGRATION, sizeof(PREPARE_MIGRATION))) {
            fprintf(log, "Prepare Migration\n");
            agent_sock = prepare_client_socket(PROXY_SOCK_PORT);  // closed at finish
            prepare_marking = 1;
            jvmti->PrepareMigration(0);
            fprintf(log, "Going to sleep\n");
            sem_wait(&semph);
            fprintf(log, "Comming from sleep\n");
            prepare_migration = 1;
            jvmti->PrepareMigration(min_migration_bandwidth);
        }
        else {
            fprintf(log, "ERROR: received unknown message. Ignoring...\n");
        }
        fflush(log);
    }
}
Ejemplo n.º 4
0
void runserver(int numthreads, unsigned short serverport) {
	//setting up args//
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t worker = PTHREAD_COND_INITIALIZER;
    pthread_cond_t producer = PTHREAD_COND_INITIALIZER;
    int nextin = 0;
    int nextout = 0;
    int requests = 0;
    int sockets[numthreads];//stores the socket numbers, how the threads will know what socket to send a file to
    struct sockaddr_in addrs[numthreads]; //socket addresses, this with the sockets array will tell the workers where to send info
    threadargs temparg;//dummy arg
    temparg.mutex = &mutex;
    temparg.worker = &worker;
    temparg.producer = &producer;
	//nextin, nextout, and requests will all be pointers to an in b/c they will be shared amongst all threads to help w/ concurrency
    temparg.nextin = &nextin;
    temparg.nextout = &nextout;
    temparg.requests = &requests;
    temparg.numthreads = numthreads;
    struct sockaddr_in holdaddr;
    int i = 0;
    for(;i<numthreads;i++){//initialze lists
        sockets[i] = 0;
        addrs[i] = holdaddr;               
    }
    temparg.socketnumber = &sockets;//use a pointer to these lists to enable sharing
    temparg.saddr = &addrs;
	//whew.. all done w/ that, now lets make some threads
	pthread_t workers[numthreads];
    i = 0;
    for(; i<numthreads;i++){
        pthread_create(&workers[i], NULL, dowork, (void*)&temparg);//make the lists
    }
    // create your pool of threads here

    //////////////////////////////////////////////////
    
    
    int main_socket = prepare_server_socket(serverport);
    //set up constant part of threadarguments//
    
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            fprintf(stderr, "Got connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
            
           
            ////////////////////////////////////////////////////////
            pthread_mutex_lock(&mutex);            
            while (requests == numthreads){//dont give us too much work
                pthread_cond_wait(&producer, &mutex);
            }
            sockets[nextin] = new_sock;//update the socket info
            addrs[nextin] = client_address;//and the address
            nextin = (nextin +1) % numthreads;//overwrite last used bin
            requests++;//one more task
            pthread_cond_signal(&worker);
            pthread_mutex_unlock(&mutex);
           ////////////////////////////////////////////////////////


        }
    }
    //closeing threads -- eventually will be refactored into a function
    for(;i<requests;i++){
        pthread_cond_signal(&worker);//clear out the rest of the requests
    } 
    sockets[nextout] = -9999;//alert threads that we're closing
    i = 0;
    requests = numthreads+1;
    for(; i <numthreads;i++){//all threads should have exited
        pthread_cond_signal(&worker);
    }
    i = 0;
    for(; i <numthreads;i++){//pick up the scraps
        pthread_join(workers[i], NULL);
    }
    
    
    
    
    fprintf(stderr, "Server shutting down.\n");
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&worker);
	pthread_cond_destroy(&producer);
    close(main_socket);
}
Ejemplo n.º 5
0
void runserver(int numthreads, unsigned short serverport) {
    //////////////////////////////////////////////////
    // create your pool of threads here
	//pthread_t p1;
	//
	pthread_t threadarray [numthreads];
	int x = 0;
	for(; x < numthreads; x++)
	{
		pthread_create(&(threadarray[x]),NULL, worker_function, NULL);
	}


    //////////////////////////////////////////////////
    
    
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
	//printf("%s", "loop");
	//fflush(stdout);
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            time_t now = time(NULL);
            fprintf(stderr, "Got connection from %s:%d at %s\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port), ctime(&now));

           ////////////////////////////////////////////////////////
           /* You got a new connection.  Hand the connection off
            * to one of the threads in the pool to process the
            * request.
            *
            * Don't forget to close the socket (in the worker thread)
            * when you're done.
            */
           ////////////////////////////////////////////////////////
		struct work_queue_item * newnode = (struct work_queue_item *) malloc(sizeof(struct work_queue_item));
		newnode->sock = new_sock;
		pthread_mutex_lock(&work_mutex);
		if (head != NULL) {
			head->previous = newnode;
		}
		else {
			tail = newnode;	
		}
		newnode->next = head;
		head = newnode;
		newnode->previous = NULL;
		newnode->port = ntohs(client_address.sin_port);
		newnode->ip =inet_ntoa(client_address.sin_addr);
		queue_count++;
		pthread_cond_signal(&work_cond);
		pthread_mutex_unlock(&work_mutex);
		senddata(new_sock, HTTP_200, strlen(HTTP_200));
        }
    }

    
    while(queue_count > 0); //spin wait for threads to finish
    queue_count = -1; //when signal all threads should break and be free!
    pthread_cond_broadcast(&work_cond);
    x = 0;
    for(; x < numthreads; x++)
    {
		
		pthread_join(threadarray[x], NULL);		
    }
    fprintf(stderr, "Server shutting down.\n");
    close(main_socket);
}
Ejemplo n.º 6
0
void runserver(int numthreads, unsigned short serverport) {
    //////////////////////////////////////////////////

    // create your pool of threads here

    //////////////////////////////////////////////////
    
	
	//Initialize socket queue 
	socket_pool_t *mysockets = malloc(sizeof(socket_pool_t));
    	socket_pool_init(mysockets);
	
	struct thread_args *myargs = malloc(sizeof(struct thread_args));
	myargs->sockets = mysockets;
	//myargs->weblog = fopen(NULL,"weblog.txt");

	pthread_t threadpool[numthreads];
	
	int i=0;
    //for loop to initialize threads in threadpool 
    for (; i<numthreads; i++){
        if (0 > pthread_create(&threadpool[i], NULL, (void *)consumer, myargs)){
           fprintf(stderr, "Error creating thread: %s\n", strerror(errno));
		}
	}
  
    
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            fprintf(stderr, "Got connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));

           ////////////////////////////////////////////////////////
           /* You got a new connection.  Hand the connection off
            * to one of the threads in the pool to process the
            * request.
            *
            * Don't forget to close the socket (in the worker thread)
            * when you're done.
            */
           ////////////////////////////////////////////////////////
		   pthread_mutex_lock(&mysockets->socket_pool_lock);
		   add_socket(mysockets, new_sock);
		   pthread_mutex_unlock(&mysockets->socket_pool_lock);
		   get_new_socket(mysockets);
		   close(new_sock);

        }
    }
    fprintf(stderr, "Server shutting down.\n");
        
    close(main_socket);
}
Ejemplo n.º 7
0
void runserver(int num_threads, unsigned short serverport) {
    struct node **sock_list[2]; //to contain pointers to head and tail for passing to threads
	struct node *head = NULL;
	struct node *tail = NULL;
	sock_list[0] = &head;
	sock_list[1] = &tail; //to pass to new threads

	pthread_t threads[num_threads];
    int i = 0;

    // start up the threads; they'll start trying to consume immeidately
    for (i = 0; i < num_threads; i++) {
        if (0 > pthread_create(&threads[i], NULL, worker, (void*)&sock_list)) {
            fprintf(stderr, "Error creating thread: %s\n", strerror(errno));
        }
    }
    
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0){
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);


	//thread pool set up and a-okay by now
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {            
            fprintf(stderr, "Got connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));

			produce_cnct(new_sock, client_address, &head, &tail); //inserting socket at end of linked list. Only executed on main thread
        }
    }
    fprintf(stderr, "Server shutting down.\n");

	// threads are done doing work    
    // wait for workers to complete
	pthread_cond_broadcast(&consumer); //wakes up all the consumers, which will then escape and stop running
	for (i = 0; i < num_threads; i++) {
        pthread_join(threads[i], NULL);
    }
	printf("threads woken up");

    close(main_socket);
}
Ejemplo n.º 8
0
void runserver(int numthreads, unsigned short serverport) {
    //////////////////////////////////////////////////

	linkedlist* socks = malloc(sizeof(linkedlist));
	socks-> head = malloc(sizeof(struct node));
	(socks->head)->socket = -1;
	socks->tail = socks->head;
	socks->listlock = malloc(sizeof(pthread_mutex_t));
	pthread_mutex_init(socks->listlock, NULL);
	socks->listempty = malloc(sizeof(pthread_cond_t));
	pthread_cond_init(socks->listempty,NULL);
    // create your pool of threads here
	if(numthreads<1){
		numthreads = 1;
	}
	pthread_t** threads = malloc(sizeof(pthread_t*)*numthreads);
	int i = 0;
	for(; i < numthreads; i++){
		threads[i] = malloc(sizeof(pthread_t));
		pthread_create(threads[i], NULL, sock_consume, (void*)socks);
	}

    //////////////////////////////////////////////////
    
    
    int main_socket = prepare_server_socket(serverport);
    if (main_socket < 0) {
        exit(-1);
    }
    signal(SIGINT, signal_handler);

    struct sockaddr_in client_address;
    socklen_t addr_len;

    fprintf(stderr, "Server listening on port %d.  Going into request loop.\n", serverport);
    while (still_running) {
        struct pollfd pfd = {main_socket, POLLIN};
        int prv = poll(&pfd, 1, 10000);

        if (prv == 0) {
            continue;
        } else if (prv < 0) {
            PRINT_ERROR("poll");
            still_running = FALSE;
            continue;
        }
        
        addr_len = sizeof(client_address);
        memset(&client_address, 0, addr_len);

        int new_sock = accept(main_socket, (struct sockaddr *)&client_address, &addr_len);
        if (new_sock > 0) {
            
            fprintf(stderr, "Got connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));

           ////////////////////////////////////////////////////////
           /* You got a new connection.  Hand the connection off
            * to one of the threads in the pool to process the
            * request.
            *
            * Don't forget to close the socket (in the worker thread)
            * when you're done.
            */
           ////////////////////////////////////////////////////////

			pthread_mutex_lock(socks-> listlock);
			list_insert(new_sock, &socks->tail);
			(socks->tail)-> ip = strdup(inet_ntoa(client_address.sin_addr)); //malloc! The free is taken care of in list_remove.
			(socks->tail)-> port = ntohs(client_address.sin_port);
			pthread_cond_broadcast(socks->listempty);
			pthread_mutex_unlock(socks->listlock);

        }
    }

	i = 0;
	for(; i < numthreads; i++){
		pthread_join(*(threads[i]), NULL);
		free(threads[i]);	
	}
	free(threads);
	
	free(socks->listlock);
	free(socks->listempty);
	free(socks);
	
    fprintf(stderr, "Server shutting down.\n");
        
    close(main_socket);
}