Example #1
0
int
main(int argc, char **argv)
{
	pthread_t	tid_produce, tid_consume;

	if (argc != 2)
		err_quit("usage: mycat2 <pathname>");

	fd = Open(argv[1], O_RDONLY);

		/* 4initialize three semaphores */
	Sem_init(&shared.mutex, 0, 1);
	Sem_init(&shared.nempty, 0, NBUFF);
	Sem_init(&shared.nstored, 0, 0);

		/* 4one producer thread, one consumer thread */
	Set_concurrency(2);
	Pthread_create(&tid_produce, NULL, produce, NULL);	/* reader thread */
	Pthread_create(&tid_consume, NULL, consume, NULL);	/* writer thread */

	Pthread_join(tid_produce, NULL);
	Pthread_join(tid_consume, NULL);

	Sem_destroy(&shared.mutex);
	Sem_destroy(&shared.nempty);
	Sem_destroy(&shared.nstored);
	exit(0);
}
Example #2
0
int main(int argc,char **argv)
{

	pthread_t produce_tid,consume_tid; 


	if(argc!=2)
		err_quit("usage: mycat <pathname> ");

	if((fd=open(argv[1],O_RDONLY))==-1)
		err_sys("open error");

	Sem_init(&shared.mutex,0,1);
	Sem_init(&shared.nempty,0,NBUFF);
	Sem_init(&shared.nstored,0,0);


	Set_concurrency(2);

	Pthread_create(&produce_tid,NULL,produce,NULL);
	Pthread_create(&consume_tid,NULL,consume,NULL);

	Pthread_join(produce_tid,NULL);
	Pthread_join(consume_tid,NULL);

	Sem_destroy(&shared.mutex);
	Sem_destroy(&shared.nempty);
	Sem_destroy(&shared.nstored);
	exit(0);
}
Example #3
0
int main(int argc, char **argv)
{
	pthread_t tid_produce, tid_consume;

	if (argc != 2)
		err_quit("Usage: buffer <pathname>");

	fd = Open(argv[1], O_RDONLY);

	Sem_init(&shared.mutex, 0, 1);
	Sem_init(&shared.nempty, 0, NBUFF);
	Sem_init(&shared.nstored, 0, 0);

	Pthread_setconcurrency(2);
	Pthread_create(&tid_produce, NULL, produce, NULL);
	Pthread_create(&tid_consume, NULL, consume, NULL);

	Pthread_join(tid_produce, NULL);
	Pthread_join(tid_consume, NULL);

	Sem_destroy(&shared.mutex);
	Sem_destroy(&shared.nempty);
	Sem_destroy(&shared.nstored);

	exit(0);
}
Example #4
0
void log_close(log_t *log)
{
	Sem_wait(&log->sem);
	Sem_destroy(&log->sem);
	Close(log->fd);
	free(log);
	return;
} /* end log_close */
Example #5
0
/* include main */
int
main(int argc, char **argv)
{
	int		i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];
	pthread_t	tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS];

	if (argc != 4)
		err_quit("usage: prodcons4 <#items> <#producers> <#consumers>");
	nitems = atoi(argv[1]);
	nproducers = min(atoi(argv[2]), MAXNTHREADS);
	nconsumers = min(atoi(argv[3]), MAXNTHREADS);

		/* 4initialize three semaphores */
	Sem_init(&shared.mutex, 0, 1);
	Sem_init(&shared.nempty, 0, NBUFF);
	Sem_init(&shared.nstored, 0, 0);

		/* 4create all producers and all consumers */
	Set_concurrency(nproducers + nconsumers);
	for (i = 0; i < nproducers; i++) {
		prodcount[i] = 0;
		Pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]);
	}
	for (i = 0; i < nconsumers; i++) {
		conscount[i] = 0;
		Pthread_create(&tid_consume[i], NULL, consume, &conscount[i]);
	}

		/* 4wait for all producers and all consumers */
	for (i = 0; i < nproducers; i++) {
		Pthread_join(tid_produce[i], NULL);
		printf("producer count[%d] = %d\n", i, prodcount[i]);	
	}
	for (i = 0; i < nconsumers; i++) {
		Pthread_join(tid_consume[i], NULL);
		printf("consumer count[%d] = %d\n", i, conscount[i]);	
	}

	Sem_destroy(&shared.mutex);
	Sem_destroy(&shared.nempty);
	Sem_destroy(&shared.nstored);
	exit(0);
}