int main(int argc, char **argv) {
	int nthreads = 1;
	int i;
	char *cachedir = "locals.txt";
	char option_char;

	mqd_t request_mq, response_mq;
	struct mq_attr attr;

	while ((option_char = getopt_long(argc, argv, "t:c:h", gLongOptions, NULL)) != -1) {
		switch (option_char) {
			case 't': // thread-count
				nthreads = atoi(optarg);
				break;   
			case 'c': //cache directory
				cachedir = optarg;
				break;
			case 'h': // help
				Usage();
				exit(0);
				break;    
			default:
				Usage();
				exit(1);
		}
	}

	if (signal(SIGINT, _sig_handler) == SIG_ERR){
		fprintf(stderr,"Can't catch SIGINT...exiting.\n");
		exit(EXIT_FAILURE);
	}

	if (signal(SIGTERM, _sig_handler) == SIG_ERR){
		fprintf(stderr,"Can't catch SIGTERM...exiting.\n");
		exit(EXIT_FAILURE);
	}

	/* Initializing the cache */
	simplecache_init(cachedir);

	//Your code here...

	/* initialize the queue attributes */
	attr.mq_flags = 0;
	attr.mq_maxmsg = 10;
	attr.mq_msgsize = 10240;
	attr.mq_curmsgs = 0;
	//open a message queue for receiving request
	request_mq = mq_open("mq_request", O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &attr);
	if (request_mq < 0) {
		perror("In mq_open()");
		exit(1);
	}
	response_mq = mq_open("mq_response", O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &attr);
	if (response_mq < 0) {
		perror("In mq_open()");
		exit(1);
	}
}
示例#2
0
int main(int argc, char **argv)
{
	int nthreads = 1;
	char *cachedir = "locals.txt";
	char option_char;

	while ((option_char = getopt_long(argc, argv, "t:c:h", gLongOptions, NULL)) != -1)
	{
		switch (option_char)
		{
			case 't': // thread-count
				nthreads = atoi(optarg);
				break;
			case 'c': //cache directory
				cachedir = optarg;
				break;
			case 'h': // help
				Usage();
				exit(0);
				break;
			default:
				Usage();
				exit(1);
		}
	}

	if (signal(SIGINT, _sig_handler) == SIG_ERR)
	{
		fprintf(stderr,"Can't catch SIGINT...exiting.\n");
		exit(EXIT_FAILURE);
	}

	if (signal(SIGTERM, _sig_handler) == SIG_ERR)
	{
		fprintf(stderr,"Can't catch SIGTERM...exiting.\n");
		exit(EXIT_FAILURE);
	}

	// Initialize cache
	simplecache_init(cachedir);

    // Initializing thread constructs
	InitializeThreadConstructs();
	InitializeThreadPool(nthreads);

	// Initializing synchronization queues
	InitializeSynchronizationQueues();

    HandleIncomingRequests();

    CleanupThreads();

	return 0;
}
示例#3
0
/* main (Boss thread) */
int main(int argc, char **argv) {
    int nthreads = 1;
    char *cachedir = "locals.txt";
    char option_char;

    while ((option_char = getopt_long(argc, argv, "t:c:h", gLongOptions, NULL)) != -1) {
        switch (option_char) {
            case 't': // thread-count
                nthreads = atoi(optarg);
                break;   
            case 'c': //cache directory
                cachedir = optarg;
                break;
            case 'h': // help
                Usage();
                exit(0);
                break;    
            default:
                Usage();
                exit(1);
        }
    }

    if (signal(SIGINT, _sig_handler) == SIG_ERR){
        fprintf(stderr,"Can't catch SIGINT...exiting.\n");
        exit(EXIT_FAILURE);
    }

    if (signal(SIGTERM, _sig_handler) == SIG_ERR){
        fprintf(stderr,"Can't catch SIGTERM...exiting.\n");
        exit(EXIT_FAILURE);
    }

    /* Initializing the cache */
    simplecache_init(cachedir);

    /* Read messages from webproxy */   
    /* Open the message queue */
    while((mqd = mq_open(mq_name, O_RDONLY)) == -1) {
        /* Spin until message queue is open */
    }

    /* Allocate worker threads */
    th_workers = (pthread_t *) malloc (nthreads * sizeof(pthread_t));

    /* Array to hold thread ids; necessary to prevent race condition when assigning ids */
    long *th_ids = (long *) malloc(nthreads * sizeof(long)); 

    /* Spawn the threads */
    int idx = 0;
    long th_retval = 0;
    for (idx = 0; idx < nthreads; ++idx) {
        th_ids[idx] = idx;
        th_retval = pthread_create(&th_workers[idx], NULL, worker_func, (void*) &th_ids[idx]);
        if (th_retval > 0) {
            strerr_exit("simplecached, pthread_create", th_retval);
        }
    }

    /* Wait for the threads to join */
    for (idx = 0; idx < nthreads; idx++) {
        th_retval = pthread_join(th_workers[idx], NULL);
    }

    /* threads Ids are assigned; we no longer need array of Ids */
    free(th_ids);

    /* Exit pthreads */
    pthread_exit(NULL);
    free(th_workers);

    return 0;
}