warm_up_thread_id=(pthread_t *)malloc(warm_up_threads*sizeof(pthread_t)); range=(struct table_range *)malloc(warm_up_threads*sizeof(struct table_range));  if (init_cache(num_items, sname, uname, auth) != OK) { printf("init_odbc environment failed\n"); return -1; }   if (sem_init(&sem_warm_up, 0, 0) != 0) { perror("sem_init"); return -1; } range[0].author_start = 0; range[0].author_end = num_items / 10 - 1; range[0].title_start = 0; range[0].title_end = num_items / 5 - 1; for (i=0; i<warm_up_threads; i++) { sem_post(&sem_warm_up); if (pthread_create (&warm_up_thread_id[i], NULL, warm_up_cache, (void *)&range[i]) != 0) { printf("warm_up thread create failed"); return -1; }  }   do { sleep(2); sem_getvalue(&sem_warm_up, &sem_val); } while (sem_val > 0);  printf("Cache is warm.\n");  if (sanity_check == 1) { printf("Data sanity check.\n"); printf("Results for a_lname search:\n"); printf("a_id items i_r1 i_t1 i_r2 i_t2 i_r3 i_t3 i_r4 i_t4 i_r5 i_t5\n");   for (i = 0; i < num_items / 10; i++) { printf("%4d %5d", i + 1, author_results_table[i].record_number); for(j = 0; j < 5; j++) { printf(" %4lld %4lld", author_results_table[i].i_related[j], author_results_table[i].i_thumbnail[j]); } printf("\n"); }   printf("Results for i_title search:\n"); printf("i_id items i_r1 i_t1 i_r2 i_t2 i_r3 i_t3 i_r4 i_t4 i_r5 i_t5\n"); for (i = 0; i < num_items / 10; i++) { printf("%4d %5d", i + 1, title_results_table[i].record_number); for(j = 0; j < 5; j++) { printf(" %4lld %4lld", title_results_table[i].i_related[j], title_results_table[i].i_thumbnail[j]); } printf("\n"); } printf("Data sanity check complete.\n"); }
int main(int argc, char *argv[]){	int mastersock, workersock;	struct sockaddr_in socketaddr;	char sname[32], uname[32], auth[32];	pthread_t *warm_up_thread_id;	pthread_t connect_thread;	struct table_range *range;	int port, db_thread, num_items, connectioncount;	int addrlen;	int i, j, rec;	struct sigaction sa;	int c;	int warm_up_threads = 1;	int sem_val;	setlinebuf(stdout);	memset(&sa, 0, sizeof(sa));	sa.sa_handler = &sighandler;	if (sigaction(SIGUSR1, &sa, NULL) == -1)	{		printf("can not register signal handler to SIGUSR1\n");		return -1;	}	if (sigaction(SIGUSR2, &sa, NULL) == -1)	{		printf("can not register signal handler to SIGUSR2\n");		return -1;	}	if (sigaction(SIGSEGV, &sa, NULL) == -1)	{		printf("can not register signal handler to SIGSEGV\n");		return -1;	}	if (argc < 13)	{		printf("usage: %s -d <dbnodename> -u <username> -p <password> -l <port> -c <db_connection> -i <items>\n", argv[0]);		return -1;	}	opterr = 0;	while ((c = getopt(argc, argv, "c:d:i:l:p:u:")) != -1)	{		switch (c)		{			case 'c':				db_thread=atoi(optarg);				break;			case 'd':				strcpy(sname, optarg);				break;			case 'i':				num_items=atoi(optarg);				item_count=num_items;				break;			case 'l':				port = atoi(optarg);				break;			case 'p':				strcpy(auth, optarg);				break;			case 'u':				strcpy(uname, optarg);				break;			case '?':				if (isprint(optopt))				{					fprintf(stderr, "Unknown option `-%c'.\n", optopt);				}				else				{					fprintf (stderr, "Unknown option character `\\x%x'.\n",						optopt);				}				return 1;			default:				return -1;		}	}	init_common();		warm_up_thread_id=(pthread_t *)malloc(warm_up_threads*sizeof(pthread_t));	range=(struct table_range *)malloc(warm_up_threads*sizeof(struct table_range));	if (init_cache(num_items, sname, uname, auth) != OK)	{		printf("init_odbc environment failed\n");		return -1;	}		/* create threads to fill out the results tables */	if (sem_init(&sem_warm_up, 0, 0) != 0)	{		perror("sem_init");		return -1;	}	range[0].author_start = 0;	range[0].author_end = num_items / 10 - 1;	range[0].title_start = 0;	range[0].title_end = num_items / 5 - 1;	for (i=0; i<warm_up_threads; i++)	{		sem_post(&sem_warm_up);		if (pthread_create (&warm_up_thread_id[i], NULL, warm_up_cache, (void *)&range[i]) != 0)		{			printf("warm_up thread create failed");			return -1;		}	}	/* wait for all threads to finish */	do	{		sleep(2);		sem_getvalue(&sem_warm_up, &sem_val);	} while (sem_val > 0);	printf("Cache is warm.\n");	if (sanity_check == 1)	{		printf("Data sanity check.\n");		printf("Results for a_lname search:\n");		printf("a_id items i_r1 i_t1 i_r2 i_t2 i_r3 i_t3 i_r4 i_t4 i_r5 i_t5\n");		/* Check results for an a_lname search. */		for (i = 0; i < num_items / 10; i++)		{			printf("%4d %5d", i + 1, author_results_table[i].record_number);			for(j = 0; j < 5; j++)			{				printf(" %4lld %4lld", author_results_table[i].i_related[j],					author_results_table[i].i_thumbnail[j]);			}			printf("\n");		}		/* Check results for an i_title search. */		printf("Results for i_title search:\n");		printf("i_id items i_r1 i_t1 i_r2 i_t2 i_r3 i_t3 i_r4 i_t4 i_r5 i_t5\n");		for (i = 0; i < num_items / 10; i++)		{			printf("%4d %5d", i + 1, title_results_table[i].record_number);			for(j = 0; j < 5; j++)			{				printf(" %4lld %4lld", title_results_table[i].i_related[j],					title_results_table[i].i_thumbnail[j]);			}			printf("\n");		}		printf("Data sanity check complete.\n");	}	mastersock = _server_init_socket(port);	if (mastersock < 0)	{		printf("init cache server master socket failed\n");		return -1;	}	printf("The cache server is active...\n");	connectioncount=0;	while (1) {		addrlen = sizeof(socketaddr);		pthread_mutex_lock(&mutex_cache_server);		workersock = accept(mastersock, (struct sockaddr *)&socketaddr, (socklen_t *)&addrlen);		if (workersock < 0) {			LOG_ERROR_MESSAGE("accept couldn't open worker socket, errno %d", errno);		}		connectioncount++;		printf("got %d connection\n", connectioncount);		if ((rec=pthread_create(&connect_thread, NULL, cache_thread, &workersock)) != 0)    		{			LOG_ERROR_MESSAGE("pthread_create failed, rec=%d", rec);			return -1;		}	}	printf("Shutting down.\n");	close(mastersock); 	return 0;	}