Question: 1. Complete the codes for (6), (7), and (8). You need to implement one scheduling policy in each of the three slots. 2. Complete the
1. Complete the codes for (6), (7), and (8). You need to implement one scheduling policy in each of the three slots.
2. Complete the codes for (9). When SCHED_FIFO and SCHED_RR are used as the scheduling policies, you can assign different scheduling priorities for each thread you create. For example, you can assign priority 1 to thread 0, 2 for thread 1, and so and so forth.
At the beginning of the program, you can use #define to select which scheduling policy you are going to use for the whole application.
Comment two #define statements and keep only one #define statement at one time for one compilation.
Also, you can define how many threads you want to create by changing the number after NUM_THRADS. Every time you change the codes, you have to re-compile the codes to make changes effective.
You are required to create 4, 8, and 16 threads. For each number of thread, you need to try every three scheduling policies. In other words, there are totally 9 scenarios and you need to run the application for 9 times.
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
//#define SCHEDULE_FIFO
//#define SCHEDULE_RR
#define SCHEDULE_OTHER
// number of threads you want to create
#define NUM_THREADS 16
// (1) tid:
// (2) thread_num:
typedef struct {
pthread_t tid;
int thread_num;
} thread_info_t;
// (3)
void *runner(void *param)
{
thread_info_t *thread_info = param;
#ifndef SCHEDULE_OTHER
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
sched_setaffinity(0, sizeof(cpuset), &cpuset);
// delay
int d = 0;
while (d < 1000000) d++;
#endif
struct sched_param sched_param_s;
sched_getparam(0, &sched_param_s);
printf("I am thread %2d (priority: %2d) on CPU core %2d. ",
thread_info->thread_num,
sched_param_s.sched_priority,
sched_getcpu());
pthread_exit(0);
}
int main(int argc, char *argv[])
{
thread_info_t *thread_info;
int i;
pthread_attr_t attr;
pthread_attr_init(&attr);
int ret;
// (4)
ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
if (0 != ret) {
perror("setinheritsched error.");
return 0;
}
// (5)
ret = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
if (0 != ret) {
if (ENOTSUP == ret) {
printf("OS does not support this scope. ");
}
perror("setscope error.");
return 0;
}
int scope;
ret = pthread_attr_getscope(&attr, &scope);
if (0 != ret) {
perror("getscope error.");
return 0;
}
switch (scope) {
case PTHREAD_SCOPE_SYSTEM:
printf("Current scheduling scope: PTHREAD_SCOPE_SYSTEM ");
break;
case PTHREAD_SCOPE_PROCESS:
printf("Current scheduling scope: PTHREAD_SCOPE_PROCESS ");
break;
default:
printf("Current scheduling scope: No Matched Scope ");
break;
}
#ifdef SCHEDULE_FIFO
// (6) implements SCHED_FIFO and assign ret with the function return value
#endif
#ifdef SCHEDULE_RR
// (7) implements SCHED_RR and assign ret with the function return value
#endif
#ifdef SCHEDULE_OTHER
// (8) implements SCHED_OTHER and assign ret with the function return value
#endif
if (0 != ret) {
if (ENOTSUP == ret) {
printf("OS does not support this scheduling policy. ");
}
perror("setschedpolicy error.");
return 0;
}
int sched_policy;
ret = pthread_attr_getschedpolicy(&attr, &sched_policy);
if (0 != ret) {
perror("getschedpolicy error.");
return 0;
}
switch (sched_policy) {
case SCHED_FIFO:
printf("Current scheduling policy: SCHED_FIFO. ");
break;
case SCHED_RR:
printf("Current scheduling policy: SCHED_RR. ");
break;
case SCHED_OTHER:
printf("Current scheduling policy: SCHED_OTHER. ");
break;
case SCHED_BATCH:
printf("Current scheduling policy: SCHED_BATCH. ");
break;
case SCHED_IDLE:
printf("Current scheduling policy: SCHED_IDLE. ");
break;
default:
printf("Current scheduling policy: No Matched One. ");
break;
}
thread_info = calloc(NUM_THREADS, sizeof(thread_info_t));
if (!thread_info) {
perror("calloc error.");
return 0;
}
for (i = 0; i < NUM_THREADS; i++) {
thread_info[i].thread_num = i;
#ifndef SCHEDULE_OTHER
// (9) apply different priorities to each thread you will create under
// SCHED_FIFO and SCHED_RR policies. Assign ret with the function
// return value
if (0 != ret) {
perror("setchedparam error.");
free(thread_info);
return 0;
}
#endif
// (10)
ret = pthread_create(&thread_info[i].tid, &attr, runner, &thread_info[i]);
if (0 != ret) {
perror("pthread create error.");
free(thread_info);
return 0;
}
}
// (11)
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(thread_info[i].tid, NULL);
}
free(thread_info);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
