int main(int argc, char **argv) 
{
    int listenfd, *connfd, port, clientlen, i;
    struct sockaddr_in clientaddr;
    pthread_t tid;

    port_queue prod_var;
	
    /* Check command line args */
    if (argc != 2) {
	fprintf(stderr, "usage: %s <port>\n", argv[0]);
	exit(1);
    }
    port = atoi(argv[1]);

    connfdqp = queueInit ();

    prod_var.port = port;
    prod_var.q = connfdqp;

    Pthread_create(&tid, NULL, producer, &prod_var);
    printf("Producer thread created %u\n", (unsigned int) tid);
    Pthread_detach(tid);
    printf ("Producer thread detached\n");

    for (i = 0; i < MAXTHREAD; i++) {
    	Pthread_create(&tid, NULL, consumer, connfdqp);
    	printf("Consumer thread created %u\n", (unsigned int) tid);
    	Pthread_detach(tid);
    	printf("Consumer thread detached\n");
    }

    printf("Main thread exited\n");
    Pthread_exit(NULL);
}
Exemplo n.º 2
0
template<bool check_depleted> void abc_para_test() {
    concat<char> str
    ,   *dep1 = new concat<char>
    ,   *dep2 = new concat<char>
    ;
    assert(!str.ready());
    assert(!dep1->closed());
    assert(!dep2->closed());
    pthread_t thread[2];
    Pthread_create(&thread[0], NULL, abc, dep1);
    Pthread_detach(thread[0]);
    Pthread_create(&thread[1], NULL, abc, dep2);
    Pthread_detach(thread[1]);
    str.give(dep1);
    str.give(dep2);
    str.close();
    for (int i = 0; i < 2; i++) {
        for (char let = 'a'; let <= 'z'; let++) {
            if (check_depleted) {
                assert(!str.depleted());
                assert(str.ready());
            }
            assert(str.get() == let);
        }
    }
    assert(!str.ready());
    assert(str.depleted());
    assert(str.closed());
}
Exemplo n.º 3
0
static void * time_echo(void *sockfd)
{	
	int fd,maxfdp;
	fd_set rset,wset;
	Pthread_detach(pthread_self());
	char buf[MAXLINE];
	time_t ticks;
	struct timeval tv;

	fd=(int)sockfd;
	FD_ZERO(&rset);
	FD_ZERO(&wset);
	for( ; ; ){

		FD_SET(fd, &rset);
		maxfdp = fd + 1;
		tv.tv_sec=5;
		tv.tv_usec=0;
		ticks = time(NULL);
		snprintf(buf, sizeof(buf), "%.24s\r\n",ctime(&ticks));
		Write(fd, buf, strlen(buf));
		Select(maxfdp, &rset, NULL, NULL, &tv);

		if(FD_ISSET(fd, &rset)){
			printf("time client termination: EPIPE error detected\n");
			FD_CLR(fd,&rset);		
			Close(fd);
			return NULL;
		
		}

	}
	Close(fd);
	return (NULL);
}
	static void *echosel(void *arg)/* Echo Thread*/
    {
        int     connfd,wr,fileflags;
		ssize_t n;
        char    buf[MAXLINE];
        connfd = *((int *) arg);
        free(arg);
		
        Pthread_detach(pthread_self());/* Detach Thread*/
         

		back:
		while ( (n = readline(connfd, buf, MAXLINE)) > 0)/*Read data from Client*/
		{ 
			wrback:
		  wr= writen(connfd, buf, n);/*write back data from Client*/
		}	
		  if(wr<0&& errno == EPIPE)		/*Handling Write Error due to EPIPE */	
		{
			Fputs(" Echo Client termination:EPIPE error detected\n", stdout); 
			Close(connfd);
			return;
		}
		else if (wr<0 && errno == EINTR)/*Handling Write Error due to EINTR */	
		{ 
			goto wrback;
		} 
		else if (wr<0)/*Handling Write Error */	
		{
			Fputs("Echo Client termination:Write error detected,error occured due to\n", stdout); 
			Fputs(strerror(errno),stdout);  
			Fputs("\n",stdout);
			Close(connfd);
			return;
		}
				
						
        if (n < 0 && errno == EINTR)/*Handling read Error due to EINTR */	
        { 
			goto back;
	    }
		else if (n < 0)
        {   /*Handling read Error */
			Fputs(" Echo Client termination:socket read returned with value -1, error occured due to\n", stdout);
            Fputs(strerror(errno),stdout);
            Fputs("\n",stdout);   			
			Close(connfd);
			return;
		}
		else	 /*Handling client Termination */	
		{
			Fputs(" Echo Client termination:socket read returned with value 0\n", stdout); 
			Close(connfd);
			return;
		}
          
			Close(connfd);               
			return (NULL);
    
    }
Exemplo n.º 5
0
static void *time_serv(void *arg) {
    
    printf("This is the new window for time service\n"); 

    Pthread_detach(pthread_self());
    
    int sockfd=*((int*)arg);
    free(arg);
    time_t ticks;
    char buff[MAXLINE];

    while(1) {

        if(Readable_timeo(sockfd, 5)==0) {

	    ticks=time(NULL);        
            snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
        
            Write(sockfd, buff, strlen(buff));

	    printf("Current time is sent to client\n");
	}
	else break;
    }
    
    printf("This time service is terminated\n");

    Close(sockfd);
    return NULL;
}
Exemplo n.º 6
0
void *serv_thread(void *vargp)
{
    int res;
    char buf[SHORT_BUF];
    int connfd;

    Pthread_mutex_lock(&psyc);
    connfd = *(int *)vargp;
    Pthread_mutex_unlock(&psyc);

    Pthread_detach(pthread_self());
#ifdef DEBUG
    printf("one service thread is created...\n");
#endif
    for(;;){
        res = recv(connfd, buf, SHORT_BUF, 0);
        if (res == -1) {
            delay_sys("fail to recv\n");
            break;
        } else if (res == 0) {
            delay_sys("the connect broken\n"); 
            break;
        }
        buf[res] = 0;
        printf("Have receve the commind : %s\n", buf);

        if( tasks(buf, connfd) < 0) 
          break;
    }

    printf("cilent %d is left...\n", connfd);
    close(connfd);
    return NULL;
}
Exemplo n.º 7
0
void *thread(void *vargp) /* thread routine */
{
    Pthread_detach(pthread_self());
    printf("Hello, world!\n"); 
    while(1);
    return NULL;
}
Exemplo n.º 8
0
static void* doit(void* arg)
{
	Pthread_detach(pthread_self());
	str_echo((int)arg);
	Close((int)arg);
	return NULL;
}
Exemplo n.º 9
0
/*
* thread function
*/
void *thread_func(void *vargp) {
    Pthread_detach(pthread_self());
    while (1) {
        int connfd = sbuf_remove(&sbuf);
        doit(connfd);
        Close(connfd);
    }
}
Exemplo n.º 10
0
void *doit_thread(void *vargp){
	int connfd = *((int *)vargp);
	Pthread_detach(pthread_self());
	Free(vargp);
	doit(connfd);
	Close(connfd);
	return NULL;
}
Exemplo n.º 11
0
/* 
 * thread - using for process request from client
 * (please see slides of Lecture: Concurrency page 46 for more detail)
 */
void *thread(void *vargp) {
    int connfd = *((int *)vargp); 
    Pthread_detach(pthread_self()); // detach! important
    Free(vargp);
    doit(connfd); // see tiny.c for more info
    Close(connfd);
    return NULL; 
}
Exemplo n.º 12
0
static void *
doit(void *arg)
{
	Pthread_detach(pthread_self());
	str_echo((int) arg);	/* same function as before */
	Close((int) arg);		/* done with connected socket */
	return(NULL);
}
Exemplo n.º 13
0
void* doit(void* arg)
{
    void web_child(int);

    Pthread_detach(pthread_self());
    web_child((int)arg);
    Close((int)arg);
    return (NULL);
}
Exemplo n.º 14
0
void *thread(void *vargp) 
{  
    Pthread_detach(pthread_self()); 
    while (1) { 
	int connfd = sbuf_remove(&sbuf); /* Remove connfd from buffer */
	echo_cnt(connfd);                /* Service client */
	Close(connfd);
    }
}
Exemplo n.º 15
0
/* Thread routine */
void *thread(void *vargp) 
{  
    int connfd = *((int *)vargp);
    Pthread_detach(pthread_self()); //line:conc:echoservert:detach
    Free(vargp);                    //line:conc:echoservert:free
    echo(connfd);
    Close(connfd);
    return NULL;
}
Exemplo n.º 16
0
void *handle_request_threaded(void *vargp)
{
	int connfd = *((int *) vargp);
	Pthread_detach(pthread_self());
	Free(vargp);
	handle_request(connfd);
	Close(connfd);
	return NULL;
}
Exemplo n.º 17
0
void *thread(void *vargp)
{
    int connfd = *(int *)vargp;
    Pthread_detach(pthread_self());  /* 分离线程 */
    Free(vargp);
    echo(connfd);
    Close(connfd);
    return NULL;
}
Exemplo n.º 18
0
Arquivo: spread.cpp Projeto: olyd/st
Spread::Spread(SpreadNode &spreadNode):mSpreadNode(spreadNode)
{
	mSpreadNode.isFinished = false;
	pthread_mutex_init(&mMutex,NULL);
	pthread_cond_init(&mCond,NULL);

	Pthread_create(&mPid, NULL, &StartSpreadThread, this);
	Pthread_detach(mPid);
}
Exemplo n.º 19
0
void *thread(void *vargp)
{
	Pthread_detach(Pthread_self());
	while (1)
	{
		int connfd = sbuf_remove(&sbuf);
		echo_cnt(connfd);
		Close(connfd);
	}
}
Exemplo n.º 20
0
/* 
 * worker_thread - thread that initialized first, then invoke when 
 * reqeust comming in 
 */
void *worker_thread(void *vargp) {  
    #ifdef DEBUG
    printf("enter worker_thread\n");
    #endif
    Pthread_detach(pthread_self()); 
    while (1) { 
	    int client_fd = sbuf_remove(&sbuf);
        request_handler(client_fd);
    }    
}
Exemplo n.º 21
0
/*
 * connection handler as a thread for each connection
 * */
void *conn_handler (void *vargp){
	unsigned int thread_id = pthread_self();
	printf("NET*** [ 0x%x ] *** New connection \n" , thread_id);
    int connfd = *((int *)vargp);
    Pthread_detach(pthread_self()); //line:conc:echoservert:detach
    Free(vargp);                    //free allocated descriptor in main thread to prevent memory leak
    echo(connfd, thread_id);
    Close(connfd);
    return NULL;
}
Exemplo n.º 22
0
Arquivo: tiny.c Projeto: akxxsb/tiny
void * thread(void * vargp)
{
    Pthread_detach(pthread_self());         //分离当前线程
    while(1) {
        int connfd = sbuf_remove(&sbuf);    //从buffer取出描述符
        doit(connfd);
        Close(connfd);                      //关闭连接
    }
    return NULL;
}
Exemplo n.º 23
0
/* $begin thread */
void *thread(void *vargp) {
    int connfd = *((int *)vargp);
    V(&mut_fd);
    Pthread_detach(pthread_self());
    Free(vargp);
    doit(connfd);
    printf("connfd: %d is closed\n",connfd);
    close(connfd);
    return NULL;
}
Exemplo n.º 24
0
/* thread handler: do proxy*/
void *proxy_thread(void *argp)
{
    int connfd = *((int *)argp);
    Pthread_detach(pthread_self());
    Free(argp);
    //connfd = pop_fd_q();
    //printf("### proxy_thread: %d, tid: %d\n", connfd, gettid());
    run_proxy(connfd);

    return NULL;
}
Exemplo n.º 25
0
/** @brief Handles each incoming connection.
 *  Detaches the thread from the parent, removes a single connection
 *  descriptor from the shared buffer, and handles its request.
 *  @param vargp NULL pointer.
 *  @return NULL.
 */
void *conn_handler(void *vargp)
{
	int conn_fd;	

	Pthread_detach(pthread_self());

	while(1) {
		conn_fd = sbuf_remove(&sbuf);
		handle_request(conn_fd);
		close(conn_fd);
	}
}
Exemplo n.º 26
0
/* thread routine */
void *thread(void *vargp) 
{  
    int connfd = *((int *)vargp);
    printf("New thread created! fd = %d >>>>>>>>>>>>>>>>>>\n", connfd);
    Pthread_detach(pthread_self()); 
    Free(vargp);
    Signal(SIGPIPE, sigpipe_handler);
    proxy(connfd);
    Close(connfd);
    printf("Thread is closing! fd = %d <<<<<<<<<<<<<<<<<<<<<<<<\n", connfd);
    return NULL;
}
Exemplo n.º 27
0
static void *
do_echo(void *arg)
{
	int connfd;

	connfd=*((int *)arg);
	free(arg);

	Pthread_detach(pthread_self());
	str_echo(connfd);
	Close(connfd);
	return(NULL);
}
Exemplo n.º 28
0
void *thread (void *vargp) {
	thread_args args;
	args = *((thread_args *) vargp);
	Pthread_detach(pthread_self());
	// handle segment fault: it is sometimes weird
	Signal(SIGSEGV, sigsegv_handler);
	// Valar Dohaeris
	serve(args.fd);
	// Valar Morghulis
	Close(args.fd);
	Free(vargp);
	return NULL;
}
Exemplo n.º 29
0
static void *
doit(void *arg)
{
	int		connfd;

	connfd = *((int *) arg);
	free(arg);

	Pthread_detach(pthread_self());
	str_echo(connfd);		/* same function as before */
	Close(connfd);			/* we are done with connected socket */
	return(NULL);
}
Exemplo n.º 30
0
void * ServeThread(void * vargp)
{
	Pthread_detach(pthread_self());

	while (1)
	{
		int connfd = sbuf_remove(&ClientPool);
		fprintf(stderr, "Thread %llu handling request %d.\n", (unsigned long long int)pthread_self(), connfd);
		ServeClient(connfd);
		Close(connfd);
		fprintf(stderr, "Thread %llu's request %d ended.\n", (unsigned long long int)pthread_self(), connfd);
	}
}