do'nt get priority and policy of thread using POSIX thread lib...

hi guys, this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>

#define THREAD_ID pthread_self()
#define PRIORITY1 4
#define PRIORITY2 3
int 			count = 0;
int 			policy;
pthread_t 		thread1, thread2;
pthread_attr_t 		attr1, attr2, attr;
struct sched_param 	param1, param2;
struct sched_param 	param;
pthread_mutex_t 	mutex;

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fnThread
 *  Description:  
 * =====================================================================================
 */
	void*
fnThread()
{
	while(count < 10) {
		pthread_mutex_lock(&mutex);
		
		pthread_getschedparam(THREAD_ID, &policy, &param);
		
		printf("thread:  %lu, priority: %d, policy: %d, count: %d\n",
			(unsigned long)THREAD_ID, param.sched_priority, policy, count++);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
}		/* -----  end of function fnThread  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
	int
main ( int argc, char *argv[] )
{

	pthread_mutex_init(&mutex, NULL);
	//init attributes
	pthread_attr_init(&attr);
	pthread_attr_init(&attr2);
	//set policy
	pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
	pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);
	//set priority
	param1.sched_priority = PRIORITY1;	
	pthread_attr_setschedparam(&attr1, &param1);
	
	param2.sched_priority = PRIORITY2;	
	pthread_attr_setschedparam(&attr2, &param2);
	//create thread
	pthread_create(&thread1, &attr1, &fnThread, NULL);
	pthread_create(&thread2, &attr2, &fnThread, NULL);
	//join
	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);

	pthread_attr_destroy(&attr1);
	pthread_attr_destroy(&attr2);
	pthread_mutex_destroy(&mutex);
	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */

main function contains 2 threads, all of threads execute only function fnThread. Task of function is printing thread id, priority and policy of thread onto screen (10 times).
and result:
1
2
3
4
5
6
7
8
9
10
thread:  3078544240, priority: 0, policy: 0, count: 0
thread:  3070151536, priority: 0, policy: 0, count: 1
thread:  3078544240, priority: 0, policy: 0, count: 2
thread:  3070151536, priority: 0, policy: 0, count: 3
thread:  3078544240, priority: 0, policy: 0, count: 4
thread:  3070151536, priority: 0, policy: 0, count: 5
thread:  3078544240, priority: 0, policy: 0, count: 6
thread:  3070151536, priority: 0, policy: 0, count: 7
thread:  3070151536, priority: 0, policy: 0, count: 8
thread:  3078544240, priority: 0, policy: 0, count: 9

this is unexpected result. Instead displays priority as 3 in thread1 (or 4 in thread2), and display policy as 1 (SCHED_FIFO is defined as 1),but all equal 0. Why don't get exactly priority and policy of thread? Please help me, thanks so much!
Topic archived. No new replies allowed.