Exemple #1
0
int
main(int argc, char **argv)
{
	int n, scope;
	pthread_t t;
	pthread_attr_t attr;
	struct sched_param param;
	unsigned seed = 1;

	Pthread_attr_init(&attr);
	if (argc == 1) {
		printf("PTHREAD_SCOPE_PROCESS\n");
		Pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
	}
	else {
		printf("PTHREAD_SCOPE_SYSTEM\n");
		Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
	}

	Pthread_attr_getschedparam(&attr, &param);
	printf("sched_priority %d\n", param.sched_priority);

	Pthread_mutex_lock(&m);

	for (n = 0; n < 20; ++n) {
		param.sched_priority = rand_r(&seed) % 128;

		printf("creating thread %02d prio %02d\n", 
			n, param.sched_priority);
		Pthread_attr_setschedparam(&attr, &param);
     		Pthread_create(&t, &attr, Thread, (void *)n);
	}

	sleep(1);
	Pthread_mutex_unlock(&m);

	pthread_exit(NULL);
}
Exemple #2
0
void
my_create(door_info_t *iptr)
{
    pthread_t	tid;
    pthread_attr_t	attr;

    printf("tserver1: iptr = %p\n", iptr);
    Pthread_attr_init(&attr);
    Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    Pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    Pthread_create(&tid, &attr, my_thread, (void *) iptr->di_proc);
    Pthread_attr_destroy(&attr);
    printf("created server thread %ld\n", pr_thread_id(&tid));
}
Exemple #3
0
int
main(int argc, char **argv)
{
	int n;
	pthread_t t, t1, t2;
	pthread_attr_t attr;

	Pthread_attr_init(&attr);

	Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	Pthread_create(&t, &attr, SumThread, NULL);
	Pthread_create(&t1, &attr, InsThread, NULL);
	Pthread_create(&t2, &attr, InsThread, NULL);

	Pthread_join(t1, NULL);
	Pthread_join(t2, NULL);
	return 0;
}
Exemple #4
0
int
main(int argc, char **argv)
{
	int n, e;
	unsigned prod_sum, cons_sum;
	struct thread_data *data;
	pthread_attr_t attr;
	int canceled = 0;
	pthread_t tid;

 	if (argc > 1 && !strcmp("-", argv[1]))
 		Error("usage: %s num_producers num_consumers "
			"num_items maxbuflen busy-loops", argv[0]);

	if (argc > 1) {
		num_producers = atoi(argv[1]);
		if (num_producers < 0)
			num_producers = 1;
		if (num_producers > MAXPRODUCERS)
			num_producers = MAXPRODUCERS;
	}
	if (argc > 2) {
		num_consumers = atoi(argv[2]);
		if (num_consumers < 0)
			num_consumers = 1;
		if (num_consumers > MAXCONSUMERS)
			num_consumers = MAXCONSUMERS;
	}
	if (argc > 3) {
		num_items = atoi(argv[3]);
		if (num_items < 0)
			num_items = 0;
	}
	if (argc > 4) {
		maxbuflen = atoi(argv[4]);
		if (maxbuflen < 0)
			maxbuflen = 1;
		if (maxbuflen > MAXBUFLEN)
			maxbuflen = MAXBUFLEN;
	}
	if (argc > 5)
		busy_loops = atoi(argv[5]);
	printf("num_producers %d num_consumers %d num_items %d "
		"maxbuflen %d busy-loops %d\n",
		num_producers, num_consumers, num_items, maxbuflen, busy_loops);
	num_items /= num_producers; /* items/producer */

	sigemptyset(&sigmask);
	sigaddset(&sigmask, SIGINT);
	Pthread_sigmask(SIG_BLOCK, &sigmask, NULL);

	Pthread_attr_init(&attr);
	Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	/* Starte die Producer */
	for (n = 0; n < num_producers; ++n) {
		data = malloc(sizeof(struct thread_data));
		if (!data)
			Error("malloc");
		data->seed = n;
		data->no = n;
		producer_data[n] = data;
     		Pthread_create(&producers[n], &attr, Producer, data);
	}

	/* Starte die Consumer */
	for (n = 0; n < num_consumers; ++n) {
		data = malloc(sizeof(struct thread_data));
		if (!data)
			Error("malloc");
		data->no = n;
     		Pthread_create(&consumers[n], &attr, Consumer, data);
	}

     	Pthread_create(&tid, &attr, SigThread, NULL);

	/* Warte auf alle Producer */
	prod_sum = 0;
	for (n = 0; n < num_producers; ++n) {
		Pthread_join(producers[n], (void **)&data);
		if (data == PTHREAD_CANCELED)
			canceled = 1;

		prod_sum += producer_data[n]->sum;
	}
	if (canceled)
		printf("*** program was cancelled\n");

	/* setze eof und wecke consumer auf */
	Pthread_mutex_lock(&buf_mutex);
	eof = 1;
	Pthread_mutex_unlock(&buf_mutex);
	Pthread_cond_broadcast(&empty_cond);

	/* Warte auf alle Consumer */
	cons_sum = 0;
	for (n = 0; n < num_consumers; ++n) {
		Pthread_join(consumers[n], (void **)&data);

		cons_sum += data->sum;
	}

	printf("prot_sum %u cons_sum %u\n", prod_sum, cons_sum);
	if (cons_sum != prod_sum)
		printf("ERROR\n");

	return 0;
}
/*************************************************
  * Function:		Pthread_attr_set()
  * Description:    设置线程属性包裹函数 
  *					默认线程属性:
  *					1.不继承创建线程的调度策略和参数,使用设置的参数
  *					2.系统范围内争抢CPU
  *					3.线程采用SCHED_RR调度算法
  *					4.线程分离属性
  *					5.线程优先级:在调度策略的优先级minPriority~maxPriority范围内
  *					6.线程栈大小属性:最小为16k
  * Input:          *attr---线程属性结构
  *					priority---线程优先级
  *					stacksize---线程栈大小 
  * Output:         *attr---线程属性结构
  * Return:         0/error 
*************************************************/
int Pthread_attr_set(pthread_attr_t *attr, int priority, size_t stacksize)
{
	int rval;
	
	if(attr == AII_NULL)
	{
		return -1;	
	}

	/*初始化线程属性结构*/
	rval = Pthread_attr_init(attr);
	if (rval != 0)
	{
		return rval;
	}

	/*不继承创建线程的调度策略和参数*/
	rval = Pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	/*系统范围内争抢CPU*/
	rval = Pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	/*线程采用SCHED_RR调度算法*/
	rval = Pthread_attr_setschedpolicy(attr, SCHED_RR);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	/*设置线程分离属性:线程结束后,由系统自动回收资源*/
	rval = Pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	/*设置线程优先级属性*/
	rval = Pthread_attr_setschedpriority(attr, priority);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	/*设置线程栈大小属性*/
	rval = Pthread_attr_setstacksize(attr, stacksize);
	if (rval != 0)
	{
		Pthread_attr_destroy(attr);
		return rval;
	}

	return 0;
}
Exemple #6
0
int
main(int argc, char **argv)
{
	int n, k, e;
	unsigned prod_sum, cons_sum;
	pthread_attr_t attr;
	struct packet *p;
	int packets_per_producer;

 	if (argc > 1 && !strcmp("-", argv[1]))
 		Error("usage: %s num_producers num_consumers "
			"num_items buflen busy-loops", argv[0]);

	if (argc > 1) {
		num_producers = atoi(argv[1]);
		if (num_producers < 0)
			num_producers = 1;
		if (num_producers > MAXPRODUCERS)
			num_producers = MAXPRODUCERS;
	}
	if (argc > 2) {
		num_consumers = atoi(argv[2]);
		if (num_consumers < 0)
			num_consumers = 1;
		if (num_consumers > MAXCONSUMERS)
			num_consumers = MAXCONSUMERS;
	}
	if (argc > 3) {
		num_items = atoi(argv[3]);
		if (num_items < 0)
			num_items = 0;
	}
	if (argc > 4) {
		buflen = atoi(argv[4]);
		if (buflen < 1)
			buflen = 1;
	}
	if (argc > 5)
		busy_loops = atoi(argv[5]);
	printf("num_producers %d num_consumers %d num_items %d "
		"buflen %d busy-loops %d\n",
		num_producers, num_consumers, num_items, buflen, busy_loops);
	num_items /= num_producers; /* items/producer */

	Pthread_attr_init(&attr);

	Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	/* Producer Datenstruktur */
	packets_per_producer = (num_producers + num_consumers) * 2;
	for (n = 0; n < num_producers; ++n) {
		Pthread_mutex_init(&producers[n].queue_mutex, NULL);
		Pthread_cond_init(&producers[n].empty_cond, NULL);
		producers[n].seed = n;
		producers[n].no = n;
		producers[n].emptyPacketQueue = NULL;
		for (k = 0; k < packets_per_producer; ++k) {
			p = malloc(sizeof(struct packet) + 
				(buflen - 1) * sizeof(unsigned));
			if (!p)
				Error("malloc");

			p->owner = n;
			p->next = producers[n].emptyPacketQueue;
			producers[n].emptyPacketQueue = p;
		}
	}

	/* Consumer Datenstruktur */
	for (n = 0; n < num_consumers; ++n) {
		Pthread_mutex_init(&consumers[n].queue_mutex, NULL);
		Pthread_cond_init(&consumers[n].empty_cond, NULL);
		consumers[n].no = n;
		consumers[n].eof = 0;
		consumers[n].fullPacketQueue = NULL;
	}

	/* Starte die Producer */
	for (n = 0; n < num_producers; ++n)
     		Pthread_create(&producers[n].tid, &attr, 
						Producer, &producers[n]);

	/* Starte die Consumer */
	for (n = 0; n < num_consumers; ++n)
     		Pthread_create(&consumers[n].tid, &attr, 
						Consumer, &consumers[n]);

	/* Warte auf alle Producer */
	prod_sum = 0;
	for (n = 0; n < num_producers; ++n) {
		Pthread_join(producers[n].tid, NULL);

		prod_sum += producers[n].sum;
	}

	/* setze eof und wecke consumer auf */
	for (n = 0; n < num_consumers; ++n) {
		Pthread_mutex_lock(&consumers[n].queue_mutex);
		consumers[n].eof = 1;
		Pthread_mutex_unlock(&consumers[n].queue_mutex);
		Pthread_cond_broadcast(&consumers[n].empty_cond);
	}

	/* Warte auf alle Consumer */
	cons_sum = 0;
	for (n = 0; n < num_consumers; ++n) {
		Pthread_join(consumers[n].tid, NULL);

		cons_sum += consumers[n].sum;
	}

	printf("prot_sum %u cons_sum %u\n", prod_sum, cons_sum);
	if (cons_sum != prod_sum)
		printf("ERROR\n");

	return 0;
}