6. pthread attribute - (2)

System Program/pthread 2013. 7. 12. 18:58

우선 순위 조정을 통하여 pthread 의 동작성을 확인해 본다

 

#include <stdio.h>
#include <string.h>
#include <pthread.h>

static int count = 0;
pthread_attr_t attr[2];

void *threadFunc1(void *arg)
{
	char *s = (char *)arg;
	struct sched_param sched;
	while(1)
	{
		count++;
		printf("%s [%d]\n", s, count);
		pthread_attr_getschedparam(&attr[0], &sched);
		printf("threadFunc1 priority = %d\n", sched.sched_priority); 
		//printf("pthread_self value = %d\n", pthread_self());
		//usleep(100);
	}

	return (void *)strlen(s);
}

void *threadFunc2(void *arg)
{
	char *s = (char *)arg;
	struct sched_param sched;
	while(1)
	{
		count--;
		if(count < 0)
		count = 0;

		printf("%s [%d]\n", s, count);
		pthread_attr_getschedparam(&attr[1], &sched);
		printf("threadFunc2 priority = %d\n", sched.sched_priority);
		//printf("pthread_self value = %d\n", pthread_self());
		//usleep(100);
	}

	return (void *)strlen(s);
}

int main(int argc, char *argv[])
{
	printf("pthread test start !!\n");
	struct sched_param sched;

	pthread_t tid[2];
	void *res;
	int retval;

	pthread_attr_init(&attr[0]);
	pthread_attr_init(&attr[1]);

	pthread_attr_setinheritsched(&attr[0], PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setinheritsched(&attr[1], PTHREAD_EXPLICIT_SCHED);


	printf("Message from main() \n");
	pthread_attr_getschedparam(&attr[0], &sched);
	printf("threadFunc1 priority = %d\n", sched.sched_priority);
	sched.sched_priority = 1;
	pthread_attr_setschedparam(&attr[0], &sched);
	printf("threadFunc1 priority = %d\n", sched.sched_priority);

	pthread_attr_getschedparam(&attr[1], &sched);
	printf("threadFunc2 priority = %d\n", sched.sched_priority);
	sched.sched_priority = 90;
	pthread_attr_setschedparam(&attr[1], &sched);
	printf("threadFunc2 priority = %d\n", sched.sched_priority);

	retval = pthread_create(&tid[0], &attr[0], threadFunc1, "threadFunc1");
	if(retval != 0)
	{
		printf("pthread_create error\n");
	}

	retval = pthread_create(&tid[1], &attr[1], threadFunc2, "threadFunc2");
	if(retval != 0)
	{
		printf("pthread_create error\n");
	}

	retval = pthread_join(tid[0], &res);
	if(retval != 0)
	{
		printf("pthread_join error\n");
	} 

	retval = pthread_join(tid[1], &res);
	if(retval != 0)
	{
		printf("pthread_join error\n");
	} 

	printf("tid[0] = %d Thread returned %ld\n", tid[0], (long) res);
	printf("tid[1] = %d Thread returned %ld\n", tid[1], (long) res);

	return 0;
}


 

'System Program > pthread' 카테고리의 다른 글

3. 쓰레드 종료 및 연관 함수  (0) 2014.03.07
2. pthread 생성  (0) 2014.03.07
8. 쓰레드 클린업 핸들러(쓰레드 취소와 연관)  (0) 2013.07.03
5. pthread attributes  (0) 2013.07.02
1. pthread 개요  (0) 2013.05.27
: