close
Pthread 優先權的設定範例
pthread priority setting
Use pthread_attr_setschedparam() to set priority. the structure struct sched_param is used by this API. Set the priority for the thread to the member sched_priority in struct sched_param as you want. ** only priority sched_param.sched_priority is supported.
Prototype
int pthread_attr_setschedparam(pthread_attr_t *tattr, const struct sched_param *param);
Example
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
|
/*
errors.h
*/
#ifndef __errors_h
#define __errors_h
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Define a macro that can be used for diagnostic output from
* examples. When compiled -DDEBUG, it results in calling printf
* with the specified argument list. When DEBUG is not defined, it
* expands to nothing.
*/
#ifdef DEBUG
# define DPRINTF(arg) printf arg
#else
# define DPRINTF(arg)
#endif
/*
* NOTE: the "do {" ... "} while (0);" bracketing around the macros
* allows the err_abort and errno_abort macros to be used as if they
* were function calls, even in contexts where a trailing ";" would
* generate a null statement. For example,
*
* if (status != 0)
* err_abort (status, "message");
* else
* return status;
*
* will not compile if err_abort is a macro ending with "}", because
* C does not expect a ";" to follow the "}". Because C does expect
* a ";" following the ")" in the do...while construct, err_abort and
* errno_abort can be used as if they were function calls.
*/
#define err_abort(code,text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (code)); \
abort (); \
} while (0)
#define errno_abort(text) do { \
fprintf (stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror (errno)); \
abort (); \
} while (0)
#endif
|
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
|
/*
priority_setting.c
*/
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include "errors.h"
int main(int argc, char *argv[])
{
pthread_t my_thread_id;
pthread_attr_t my_attr;
struct sched_param my_param, thread_param;
int ret, rr_min_priority, rr_max_priority, thread_policy;
int status;
/* If the priority scheduling option is defined, set various
* scheduling parameters. Note that it is particularly
* important that you remember to set the inheritsched
* attribute to PTHREAD_EXPLICIT_SCHED, or the
* the policy and priority that you've set will be ignored!
* The default behavior is to inherit scheduling
* information from the creating thread.
*/
status = pthread_attr_init(&my_attr);
if(status!=0)
err_abort(status, "Init attr");
status = pthread_attr_getschedpolicy(&my_attr,&thread_policy);
if(status!=0)
err_abort(status, "Get policy");
status = pthread_attr_getschedparam(&my_attr, &thread_param);
if(status!=0)
err_abort(status, "Get sched param");
status = pthread_attr_setschedpolicy(&my_attr, SCHED_RR);
if(status != 0)
printf("Unable to set SCHED_RR policy.n");
else {
rr_min_priority = sched_get_priority_min(SCHED_RR);
if(rr_min_priority == -1)
errno_abort("Get SCHED_RR min priority");
rr_max_priority = sched_get_priority_max(SCHED_RR);
if(rr_max_priority == -1)
errno_abort("Get SCHED_RR max priority");
thread_param.sched_priority = (rr_min_priority+rr_max_priority)/2;
printf("SCHED_RR priority range is %d to %d : using %d\n", rr_min_priority, rr_max_priority, thread_param.sched_priority);
status = pthread_attr_setschedparam(&my_attr, &thread_param);
if(status != 0)
err_abort(status, "Set params");
status = pthread_attr_setinheritsched(&my_attr, PTHREAD_EXPLICIT_SCHED);
if(status != 0)
err_abort(status, "Set inherit");
}/*end of else*/
//...
}/*end of main*/
|
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
|
# Makefile
OS := $(shell uname -s)
CC = gcc
RM = rm
MAKE = make
SOURCE := $(wildcard *.c) $(wildcard *.cc) $(wildcard *.cxx) $(wildcard *.cpp)
#TARGET=$(shell target=""; for i in $(SOURCE); do target+=$(echo $i | awk -F"." '{ printf $1}'); target+=" "; done;)
CFLAGS = -fno-stack-protector -Wall
all:
@TARGET=""; for i in $(SOURCE); do TARGET+=$$(echo $$i | awk -F"." '{ printf $$1}'); TARGET+=" "; done; \
for i in $$TARGET; \
do \
#$(CC) -o "$$i" "$$i".c $(CFLAGS); \
$(MAKE) $$i; \
done;
clean:
${RM} -f *.o *~
ifeq ($(strip $(OS)),Darwin)
${RM} -f $$(file `find .` | grep "bit executable" | awk '{print $$1}' | sed 's/\.*\://')
else ifeq ($(strip $(OS)),Linux)
${RM} -f $$(file `find .` | grep "bit ...* executable" | awk '{print $$1}' | sed 's/\.*\://')
endif
|
Other related APIs:
int sched_get_priority ();
where policy could be PTHREAD_EXPLICIT_SCHED or PTHREAD_INHERIT_SCHED
Reference
全站熱搜
留言列表