Beispiel #1
0
void
silly_run(struct silly_config *config)
{
	int i;
	int err;
	pthread_t pid[3];
	R.run = 1;
	R.exit = 0;
	pthread_mutex_init(&R.mutex, NULL); 
	pthread_cond_init(&R.cond, NULL);
	if (config->daemon)
		silly_daemon(config);
	signal_init();
	silly_timer_init();
	err = silly_socket_init();
	if (err < 0) {
		fprintf(stderr, "%s socket init fail:%d\n", config->selfname, err);
		exit(-1);
	}
	silly_worker_init();
	srand(time(NULL));
	thread_create(&pid[0], thread_socket, NULL);
	thread_create(&pid[1], thread_timer, NULL);
	thread_create(&pid[2], thread_worker, config);
	fprintf(stdout, "%s is running ...\n", config->selfname);
	for (i = 0; i < 3; i++)
		pthread_join(pid[i], NULL);
	pthread_mutex_destroy(&R.mutex);
	pthread_cond_destroy(&R.cond);
	silly_worker_exit();
	silly_timer_exit();
	silly_socket_exit();
	fprintf(stdout, "%s has already exit...\n", config->selfname);
	return ;
}
Beispiel #2
0
int silly_run(struct silly_config *config)
{
        int i, j;
        int err;
        int tcnt;

        if (config->daemon && silly_daemon(1, 0) == -1) {
                fprintf(stderr, "daemon error:%d\n", errno);
                exit(0);
        }

        signal(SIGPIPE, SIG_IGN);
        signal(SIGHUP, _sig_term);
        signal(SIGINT, _sig_term);
        signal(SIGTERM, _sig_term);

        timer_init();
        silly_socket_init();
        silly_server_init();

        for (i = 0; i < config->listen_count; i++) {
                int n;
                char ip[32];
                char port[32];
                char backlog[32];
                
                uint16_t        nport;
                int             nbacklog;

                backlog[0] = '\0';
                n = sscanf(config->listen[i].addr, "%[0-9.]:%[0-9]:%[0-9]", ip, port, backlog);
                if (n < 2) {
                        fprintf(stderr, "Invalid listen of %s\n", config->listen[i].name);
                        return -1;
                }
                nport = (uint16_t)strtoul(port, NULL, 0);
                nbacklog = (int)strtol(backlog, NULL, 0);
                if (nbacklog == 0)
                        nbacklog = 10;

                err = silly_socket_listen(ip, nport, nbacklog, -1);
                if (err == -1) {
                        fprintf(stderr, "listen :%s(%s) error\n", config->listen[i].addr, config->listen[i].name);
                        return -1;
                }

                snprintf(port, sizeof(port) / sizeof(port[0]), "%d", err);
                silly_env_set(config->listen[i].name, port);
        }
        
        srand(time(NULL));

        //start
        int     workid[config->worker_count];
        for (i = 0; i < config->worker_count; i++) {
                workid[i] = silly_server_open();
                silly_server_start(workid[i], config);
        }
 
        tcnt = 2;
        if (config->debug > 0)
                ++tcnt;
        tcnt += config->worker_count;
        pthread_t pid[tcnt];
        
        i = 0;
        pthread_create(&pid[i++], NULL, _socket, NULL);
        pthread_create(&pid[i++], NULL, _timer, NULL);
        if (config->debug > 0)
                pthread_create(&pid[i++], NULL, _debug, NULL);

        for (j = 0; j < config->worker_count; i++, j++)
                pthread_create(&pid[i], NULL, _worker, &workid[j]);

        fprintf(stderr, "Silly is running...\n");

        for (i = 0; i < tcnt; i++)
                pthread_join(pid[i], NULL);

        silly_server_exit();
        silly_socket_exit();
        timer_exit();

        fprintf(stderr, "silly has already exit...\n");

        return 0;
}