ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 2 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 28 Apr 2017 My Price 11.00

Operating Systems Home Assignment IV Problem I

How do I implement SCHED_RR in C?  The assignment is attached. Problem 1 is the problem I am having. The code we were provided will also be attached.

 

 

Operating Systems Home Assignment IV Problem I (Thread Scheduling with Pthreads, 30 Pts)
The file pth.c implements the thread creation and scheduling within Linux by using the Pthreads
library. You are required to:
1. Understand the codes, and fill the comments from (1) to (11) except (6) to (9).
2. Complete the codes for (6), (7), and (8). You need to implement one scheduling policy in
each of the three slots.
3. 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.
In order to compile the program, you can use: gcc –o pth pth.c –lpthread
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.
Document the results (what the program outputs) and explain your observations. Try to use ./pth
to run the program. If this does not work, try sudo ./pth and explain the reason. The following resource may help you with this homework:
http://man7.org/linux/man-pages/man3/pthread_attr_setschedpolicy.3.html
http://man7.org/linux/man-pages/man3/pthread_attr_setschedparam.3.html
In Linux, you can also use chrt –m to check available scheduling policies and range of priorities in
your current OS. Problem 2 (Scheduling, optimization 15 pts) Discuss how the following pairs of scheduling criteria conflict in certain settings. 1. CPU utilization and response time (5 pt) 2. Average turnaround time and maximum waiting time (5 pt) 3. I/O device utilization and CPU utilization (5 pt) Problem 3 (Scheduling, computation 30 pts) Consider the following set of processes, with the length of the CPU-burst time given in
milliseconds: Process Burst Time Priority P1 10 3 P2 1 1 P3 2 3 P4 1 4 P5 5 2 The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. 1- Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a
non preemptive priority (a smaller priority number implies a higher priority), and RR
(quantum = 1) scheduling. (12 Pt) 2- What is the turnaround time of each process for each of the scheduling algorithms in part
1? (8 Pt) 3- What is the waiting time of each process for each of the scheduling algorithms in part 1?
(4 Pt) 4- Which of the schedules in part a results in the minimal average waiting time (over all
processes)? (4 Pt) Problem 4 (Scheduling, Variable priorities 15 pts) Consider a preemptive priority scheduling algorithm based on dynamically changing priorities.
Larger priority numbers imply higher priority. When a process is waiting for the CPU (in the ready queue, but not running), its priority changes
at a rate α; when it is running, its priority changes at a rate β. All processes are given a priority of 0 when they enter the ready queue. The parameters α and β can be set to give many different
scheduling algorithms. 1. What is the algorithm that results from β > α > 0? (7 Pt) 2. What is the algorithm that results from α < β < 0? (7 Pt)

 

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
//#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.\n",
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.\n");
}
perror("setscope error.");
return 0;
}
int scope;
ret = pthread_attr_getscope(&attr,
if (0 != ret) {
perror("getscope error.");
return 0;
}
switch (scope) {
case PTHREAD_SCOPE_SYSTEM:
printf("Current scheduling
break;
case PTHREAD_SCOPE_PROCESS:
printf("Current scheduling
break;
default:
printf("Current scheduling
break;
} &scope); scope: PTHREAD_SCOPE_SYSTEM\n");
scope: PTHREAD_SCOPE_PROCESS\n");
scope: No Matched Scope\n"); #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.\n");
}
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.\n");
break;
case SCHED_RR:
printf("Current scheduling policy: SCHED_RR.\n");
break;
case SCHED_OTHER:
printf("Current scheduling policy: SCHED_OTHER.\n");
break;
case SCHED_BATCH:
printf("Current scheduling policy: SCHED_BATCH.\n");
break;
case SCHED_IDLE:
printf("Current scheduling policy: SCHED_IDLE.\n"); break;
default:
printf("Current scheduling policy: No Matched One.\n");
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 #endif if (0 != ret) {
perror("setchedparam error.");
free(thread_info);
return 0;
} // (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;
}

Attachments:

Answers

(11)
Status NEW Posted 28 Apr 2017 02:04 AM My Price 11.00

-----------

Not Rated(0)