-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathpthread_scheduling.c
More file actions
46 lines (36 loc) · 1.13 KB
/
Copy pathpthread_scheduling.c
File metadata and controls
46 lines (36 loc) · 1.13 KB
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
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *runner(void *param);
int main(int argc, char *argv[])
{
int i, scope;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
/* get the default attributes */
pthread_attr_init(&attr);
/* first inquire on the current scope */
if(pthread_attr_getscope(&attr, &scope) != 0){
fprintf(stderr, "Unable to get scheduling scope\n");
}else{
if(scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS\n");
else if(scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM\n");
else fprintf(stderr, "Illegal scope value\n");
}
/* set the scheduling algorithm to PCS or SCS */
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* create the threads */
for(i = 0;i < NUM_THREADS; i++){
pthread_create(&tid[i], &attr, runner, NULL);
}
/* now join on each thread */
for(i = 0; i < NUM_THREADS; i++){
pthread_join(tid[i], NULL);
}
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
printf("Inside runner\n");
pthread_exit(0);
}