Skip to content

Commit 1d17fad

Browse files
committed
CH5: Multiprocessor and real time scheduling
1 parent c084bcf commit 1d17fad

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

5-Process Scheduling/a.out

8.98 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <pthread.h>
2+
#include <stdio.h>
3+
4+
#define NUM_THREADS 5
5+
6+
void *runner(void *param);
7+
8+
int main(int argc, char *argv[])
9+
{
10+
int i, scope;
11+
pthread_t tid[NUM_THREADS];
12+
pthread_attr_t attr;
13+
14+
/* get the default attributes */
15+
pthread_attr_init(&attr);
16+
17+
/* first inquire on the current scope */
18+
if(pthread_attr_getscope(&attr, &scope) != 0){
19+
fprintf(stderr, "Unable to get scheduling scope\n");
20+
}else{
21+
if(scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS\n");
22+
else if(scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM\n");
23+
else fprintf(stderr, "Illegal scope value\n");
24+
}
25+
26+
/* set the scheduling algorithm to PCS or SCS */
27+
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
28+
29+
/* create the threads */
30+
for(i = 0;i < NUM_THREADS; i++){
31+
pthread_create(&tid[i], &attr, runner, NULL);
32+
}
33+
34+
/* now join on each thread */
35+
for(i = 0; i < NUM_THREADS; i++){
36+
pthread_join(tid[i], NULL);
37+
}
38+
}
39+
40+
/* Each thread will begin control in this function */
41+
void *runner(void *param)
42+
{
43+
printf("Inside runner\n");
44+
45+
pthread_exit(0);
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <pthread.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
5+
#define NUM_THREADS 5
6+
7+
void *runner(void *param);
8+
9+
int main(){
10+
int i, policy;
11+
pthread_t tid[NUM_THREADS];
12+
pthread_attr_t attr;
13+
14+
pthread_attr_init(&attr);
15+
16+
if(pthread_attr_getschedpolicy(&attr, &policy) != 0){
17+
fprintf(stderr, "Unable to get policy\n");
18+
}else{
19+
if(policy == SCHED_OTHER) printf("SCHED_OTHER\n");
20+
else if(policy == SCHED_RR) printf("SCHED_RR\n");
21+
else if(policy == SCHED_FIFO) printf("SCHED_FIFO\n");
22+
}
23+
24+
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0){
25+
fprintf(stderr, "Error setting policy\n");
26+
}
27+
28+
for(i = 0; i < NUM_THREADS; i++){
29+
pthread_create(&tid[i], &attr, runner, NULL);
30+
}
31+
32+
for(i = 0; i < NUM_THREADS; i++){
33+
pthread_join(tid[i], NULL);
34+
}
35+
}
36+
37+
void *runner(void *param){
38+
printf("Inside runner\n");
39+
sleep(3);
40+
printf("Exiting runner\n");
41+
42+
pthread_exit(0);
43+
}

0 commit comments

Comments
 (0)