Пример #1
0
int main(int argc, char *argv[]){
	pthread_t tidProducer, tidConsumer;
	int i;
	int err;

	sbuf_init(NUMS);

	err = pthread_create(&tidProducer, NULL, producer, (void*)(&sb));
	if (err != 0){
		fprintf(stderr, "pthread_create error\n");
		exit(EXIT_FAILURE);
	}
	
	err = pthread_create(&tidConsumer, NULL, consumer, (void*)(&sb));
	if(err != 0){
		fprintf(stderr, "pthread_create error\n");
		exit(EXIT_FAILURE);
	}

	sleep(10);
	pthread_kill(tidProducer, 0);
	pthread_kill(tidConsumer, 0);
	
	sbuf_deinit(sp);
	return 0;
}
Пример #2
0
Файл: tiny.c Проект: akxxsb/tiny
int main(int argc, char **argv)
{
    int listenfd, connfd;
    char hostname[MAXLINE], port[MAXLINE];
    socklen_t clientlen;
    struct sockaddr_storage clientaddr;
    pthread_t tid;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(1);
    }

    listenfd = Open_listenfd(argv[1]);
    sbuf_init(&sbuf, bufferSize);           //初始化结构
    
    int i;
    for(i = 0; i < MAXThreads; ++i) Pthread_create(&tid,NULL,thread,NULL);
    while (1) {
        clientlen = sizeof(clientaddr);
        connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
            Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
                    port, MAXLINE, 0);
        printf("Accepted connection from (%s, %s)\n", hostname, port);
        sbuf_insert(&sbuf, connfd);         //生产者加入描述符
    }
    sbuf_deinit(&sbuf);                     //释放buffer
    return 0;
}
Пример #3
0
static const char* eval_expr_list(Node* expr_list, bool first_comma)
{
  assert(expr_list->type == EXPR_LIST);
  StringBuf sb_temp;
  sbuf_init(&sb_temp, "");
  for (uint i = 0; i < expr_list->children.size; i++){
    if (i == 0 and not first_comma){
      sbuf_printf(&sb_temp, 
                  "%s", 
                  eval_Value(node_get_child(expr_list, i)));
    } else {
      sbuf_printf(&sb_temp,
                  ", %s",
                  eval_Value(node_get_child(expr_list, i)));
    }
  }
  const char* result = mem_strdup(sb_temp.str);
  sbuf_deinit(&sb_temp);
  return result;
}
Пример #4
0
void *do_reg(void *arg)
{
    int sfd, confd;
    struct sockaddr_in cli_addr;		
    arg_t *rarg; 
    socklen_t len = sizeof(struct sockaddr_in); 
    pthread_t tid;

    Pthread_mutex_init(&psyc, NULL);
    sbuf_init(&sbuf, SHORT_BUF);

    rarg = (arg_t *)arg;
    sfd = init_tcp_sock(rarg->port, rarg->serv_ip);
    if(sfd == -1) {
        err_sys("failed to init tcp socket...\n");
    }

#ifdef DEBUG		
    printf("init the tcp sock\n"); /* for debuging... */
#endif
    while(1){
        Pthread_mutex_lock(&psyc);
        confd = Accept(sfd, (SA *)&cli_addr, &len);
        Pthread_mutex_unlock(&psyc);
        Pthread_create(&tid, NULL, serv_thread, &confd);
#ifdef DEBUG		
        printf("one client come to server: %s\n",
                inet_ntoa(cli_addr.sin_addr));
#endif

    }

    Pthread_mutex_destroy(&psyc);
    sbuf_deinit(&sbuf);
    close(sfd);
    return NULL;
}