Wednesday, September 9, 2009

Scheduling

Linux

Since version 2.5 of the kernel, Linux has used a multilevel feedback queue with priority levels ranging from 0-140. 0-99 are reserved for real-time tasks and 100-140 are considered nice task levels. For real-time tasks, the time quantum for switching processes is approximately 200 ms and 10 ms for nice tasks. The scheduler will run through the queue of all ready processes, letting the highest ones go first and run through their time slice, and afterwards they will be placed in an expired queue. Then when the active queue is empty the expired queue will then be the active and vice versa. From versions 2.6 to 2.6.23, the kernel used an O(1) scheduler. In version 2.6.23, they replaced this method with the Completely Fair Scheduler that uses Red Black trees instead of queues.

sched_setscheduler, sched_getscheduler


NAME

       sched_setscheduler, sched_getscheduler - set and get scheduling policy/parame-
ters

SYNOPSIS

       #include 

int sched_setscheduler(pid_t pid, int policy,
const struct sched_param *param);

int sched_getscheduler(pid_t pid);

struct sched_param {
...
int sched_priority;
...
};

DESCRIPTION top

       sched_setscheduler() sets both the scheduling policy and the associated
parameters for the process whose ID is specified in pid. If pid equals zero,
the scheduling policy and parameters of the calling process will be set. The
interpretation of the argument param depends on the selected policy.
Currently, Linux supports the following "normal" (i.e., non-real-time)
scheduling policies:

SCHED_OTHER the standard round-robin time-sharing policy;

SCHED_BATCH for "batch" style execution of processes; and

SCHED_IDLE for running very low priority background jobs.

The following "real-time" policies are also supported, for special time-
critical applications that need precise control over the way in which runnable
processes are selected for execution:

SCHED_FIFO a first-in, first-out policy; and

SCHED_RR a round-robin policy.

The semantics of each of these policies are detailed below.

sched_getscheduler() queries the scheduling policy currently applied to the
process identified by pid. If pid equals zero, the policy of the calling
process will be retrieved.

Scheduling Policies

       The scheduler is the kernel component that decides which runnable process will
be executed by the CPU next. Each process has an associated scheduling policy
and a static scheduling priority, sched_priority; these are the settings that
are modified by sched_setscheduler(). The scheduler makes it decisions based
on knowledge of the scheduling policy and static priority of all processes on
the system.

For processes scheduled under one of the normal scheduling policies
(SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used in
scheduling decisions (it must be specified as 0).

Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR)
have a sched_priority value in the range 1 (low) to 99 (high). (As the
numbers imply, real-time processes always have higher priority than normal
processes.) Note well: POSIX.1-2001 only requires an implementation to
support a minimum 32 distinct priority levels for the real-time policies, and
some systems supply just this minimum. Portable programs should use
sched_get_priority_min(2) and sched_get_priority_max(2) to find the range of
priorities supported for a particular policy.

Conceptually, the scheduler maintains a list of runnable processes for each
possible sched_priority value. In order to determine which process runs next,
the scheduler looks for the non-empty list with the highest static priority
and selects the process at the head of this list.

A process's scheduling policy determines where it will be inserted into the
list of processes with equal static priority and how it will move inside this
list.

All scheduling is preemptive: if a process with a higher static priority
becomes ready to run, the currently running process will be preempted and
returned to the wait list for its static priority level. The scheduling
policy only determines the ordering within the list of runnable processes with
equal static priority.

SCHED_FIFO: First In-First Out scheduling

       SCHED_FIFO can only be used with static priorities higher than 0, which means
that when a SCHED_FIFO processes becomes runnable, it will always immediately
preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE process.
SCHED_FIFO is a simple scheduling algorithm without time slicing. For
processes scheduled under the SCHED_FIFO policy, the following rules apply:

* A SCHED_FIFO process that has been preempted by another process of higher
priority will stay at the head of the list for its priority and will resume
execution as soon as all processes of higher priority are blocked again.

* When a SCHED_FIFO process becomes runnable, it will be inserted at the end
of the list for its priority.

* A call to sched_setscheduler() or sched_setparam(2) will put the SCHED_FIFO
(or SCHED_RR) process identified by pid at the start of the list if it was
runnable. As a consequence, it may preempt the currently running process
if it has the same priority. (POSIX.1-2001 specifies that the process
should go to the end of the list.)

* A process calling sched_yield(2) will be put at the end of the list.

No other events will move a process scheduled under the SCHED_FIFO policy in
the wait list of runnable processes with equal static priority.

A SCHED_FIFO process runs until either it is blocked by an I/O request, it is
preempted by a higher priority process, or it calls sched_yield(2).

SCHED_RR: Round Robin scheduling

       SCHED_RR is a simple enhancement of SCHED_FIFO.  Everything described above
for SCHED_FIFO also applies to SCHED_RR, except that each process is only
allowed to run for a maximum time quantum. If a SCHED_RR process has been
running for a time period equal to or longer than the time quantum, it will be
put at the end of the list for its priority. A SCHED_RR process that has been
preempted by a higher priority process and subsequently resumes execution as a
running process will complete the unexpired portion of its round robin time
quantum. The length of the time quantum can be retrieved using
sched_rr_get_interval(2).

SCHED_OTHER: Default Linux time-sharing scheduling

       SCHED_OTHER can only be used at static priority 0.  SCHED_OTHER is the
standard Linux time-sharing scheduler that is intended for all processes that
do not require the special real-time mechanisms. The process to run is chosen
from the static priority 0 list based on a dynamic priority that is determined
only inside this list. The dynamic priority is based on the nice value (set
by nice(2) or setpriority(2)) and increased for each time quantum the process
is ready to run, but denied to run by the scheduler. This ensures fair
progress among all SCHED_OTHER processes.

SCHED_BATCH: Scheduling batch processes

       (Since Linux 2.6.16.)  SCHED_BATCH can only be used at static priority 0.
This policy is similar to SCHED_OTHER in that it schedules the process
according to its dynamic priority (based on the nice value). The difference
is that this policy will cause the scheduler to always assume that the process
is CPU-intensive. Consequently, the scheduler will apply a small scheduling
penalty with respect to wakeup behaviour, so that this process is mildly
disfavored in scheduling decisions.

This policy is useful for workloads that are non-interactive, but do not want
to lower their nice value, and for workloads that want a deterministic
scheduling policy without interactivity causing extra preemptions (between the
workload's tasks).

SCHED_IDLE: Scheduling very low priority jobs

       (Since Linux 2.6.23.)  SCHED_IDLE can only be used at static priority 0; the
process nice value has no influence for this policy.

This policy is intended for running jobs at extremely low priority (lower even
than a +19 nice value with the SCHED_OTHER or SCHED_BATCH policies).

Privileges and resource limits

       In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) processes can
set a non-zero static priority (i.e., set a real-time scheduling policy). The
only change that an unprivileged process can make is to set the SCHED_OTHER
policy, and this can only be done if the effective user ID of the caller of
sched_setscheduler() matches the real or effective user ID of the target
process (i.e., the process specified by pid) whose policy is being changed.

Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling on an
unprivileged process's static priority for the SCHED_RR and SCHED_FIFO
policies. The rules for changing scheduling policy and priority are as
follows:

* If an unprivileged process has a non-zero RLIMIT_RTPRIO soft limit, then it
can change its scheduling policy and priority, subject to the restriction
that the priority cannot be set to a value higher than the maximum of its
current priority and its RLIMIT_RTPRIO soft limit.

* If the RLIMIT_RTPRIO soft limit is 0, then the only permitted changes are to
lower the priority, or to switch to a non-real-time policy.

* Subject to the same rules, another unprivileged process can also make these
changes, as long as the effective user ID of the process making the change
matches the real or effective user ID of the target process.

* Special rules apply for the SCHED_IDLE: an unprivileged process operating
under this policy cannot change its policy, regardless of the value of its
RLIMIT_RTPRIO resource limit.

Privileged (CAP_SYS_NICE) processes ignore the RLIMIT_RTPRIO limit; as with
older kernels, they can make arbitrary changes to scheduling policy and
priority. See getrlimit(2) for further information on RLIMIT_RTPRIO.

Response time

       A blocked high priority process waiting for the I/O has a certain response
time before it is scheduled again. The device driver writer can greatly
reduce this response time by using a "slow interrupt" interrupt handler.

Miscellaneous

       Child processes inherit the scheduling policy and parameters across a fork(2).
The scheduling policy and parameters are preserved across execve(2).

Memory locking is usually needed for real-time processes to avoid paging
delays; this can be done with mlock(2) or mlockall(2).

Since a non-blocking infinite loop in a process scheduled under SCHED_FIFO or
SCHED_RR will block all processes with lower priority forever, a software
developer should always keep available on the console a shell scheduled under
a higher static priority than the tested application. This will allow an
emergency kill of tested real-time applications that do not block or terminate
as expected. See also the description of the RLIMIT_RTTIME resource limit in
getrlimit(2).

POSIX systems on which sched_setscheduler() and sched_getscheduler() are
available define _POSIX_PRIORITY_SCHEDULING in .

RETURN VALUE top

       On success, sched_setscheduler() returns zero.  On success,
sched_getscheduler() returns the policy for the process (a non-negative
integer). On error, -1 is returned, and errno is set appropriately.

ERRORS top

       EINVAL The scheduling policy is not one of the recognized policies, or param
does not make sense for the policy.

EPERM The calling process does not have appropriate privileges.

ESRCH The process whose ID is pid could not be found.

CONFORMING TO

       POSIX.1-2001 (but see BUGS below).  The SCHED_BATCH and SCHED_IDLE policies
are Linux-specific.

NOTES

       POSIX.1 does not detail the permissions that an unprivileged process requires
in order to call sched_setscheduler(), and details vary across systems. For
example, the Solaris 7 manual page says that the real or effective user ID of
the calling process must match the real user ID or the save set-user-ID of the
target process.

Originally, Standard Linux was intended as a general-purpose operating system
being able to handle background processes, interactive applications, and less
demanding real-time applications (applications that need to usually meet
timing deadlines). Although the Linux kernel 2.6 allowed for kernel
preemption and the newly introduced O(1) scheduler ensures that the time
needed to schedule is fixed and deterministic irrespective of the number of
active tasks, true real-time computing was not possible up to kernel version
2.6.17.

Real-time features in the mainline Linux kernel

       From kernel version 2.6.18 onwards, however, Linux is gradually becoming
equipped with real-time capabilities, most of which are derived from the
former realtime-preempt patches developed by Ingo Molnar, Thomas Gleixner,
Steven Rostedt, and others. Until the patches have been completely merged
into the mainline kernel (this is expected to be around kernel version
2.6.30), they must be installed to achieve the best real-time performance.
These patches are named:

patch-kernelversion-rtpatchversion

and can be downloaded from
http://www.kernel.org/pub/linux/kernel/projects/rt/.

Without the patches and prior to their full inclusion into the mainline
kernel, the kernel configuration offers only the three preemption classes
CONFIG_PREEMPT_NONE, CONFIG_PREEMPT_VOLUNTARY, and CONFIG_PREEMPT_DESKTOP
which respectively provide no, some, and considerable reduction of the worst-
case scheduling latency.

With the patches applied or after their full inclusion into the mainline
kernel, the additional configuration item CONFIG_PREEMPT_RT becomes available.
If this is selected, Linux is transformed into a regular real-time operating
system. The FIFO and RR scheduling policies that can be selected using
sched_setscheduler() are then used to run a process with true real-time
priority and a minimum worst-case scheduling latency.

Scheduling Policies

These policies either require superuser privilege (ie. run as root) or realtime capabilities for unprivileged users in the form of a PAM module. They include SCHED_FIFO and SCHED_RR.


REALTIME POLICIES


SCHED_FIFO

These processes schedule according to their realtime priority which is unrelated to the nice value. The highest priority process runs indefinitely, never releasing the cpu except to an even higher priority realtime task or voluntarily. Only proper realtime code should ever use this policy as the potential for hardlocking a machine is high if the process runs away. Audio applications for professional performance such as jack use this policy.


SCHED_RR


These run similar to SCHED_FIFO except that if more than one process has the same realtime priority, they will run for short periods each and share the cpu.

NON REALTIME POLICIES

These policies do not require special privileges to use and include SCHED_NORMAL and SCHED_BATCH in mainline and -ck. -ck also includes two extra unprivileged policies, SCHED_ISO and SCHED_IDLEPRIO.

SCHED_NORMAL

This is how most normal applications are run. The amount of cpu each process consumes and the latency it will get is mostly determined by the 'nice' value. They run for short periods and share cpu amongst all other processes running with the same policy, across all nice values. Known as SCHED_OTHER in most of the rest of the world, including glibc headers as per POSIX.1.

SCHED_BATCH


Similar to SCHED_NORMAL in every way except that specifying a task as batch means you are telling the kernel that this process should not ever be considered an interactive task. This means that you want these tasks to get the same share of cpu time as the same nice level SCHED_NORMAL tasks would have, but you do not care what latency they have.


SCHED_ISO

Unique to -ck this is a scheduling policy designed for pseudo-realttime scheduling without requiring superuser privileges (unlike SCHED_RR and SCHED_FIFO). When scheduled SCHED_ISO, a task can receive very low latency scheduling, and can take the full cpu like SCHED_RR, but unlike the realtime tasks they cannot starve the machine as an upper limit to their cpu usage is specified in a tunable (see below). It is designed for realtime like behaviour without risk to hanging for programs not really coded safely enough to be run realtime such as ordinary audio and video playback software. SCHED_ISO does not take a realtime priority, but nice levels like other normal tasks, although the nice value is largely ignored except when the task uses more than its cpu limit.

SCHED_IDLEPRIO


Also unique to -ck this is a scheduling policy designed for tasks to purely use idle time. This means that if anything else is running, these tasks will not even progress at all. This policy is useful for performing prolonged computing tasks such as distributed computing clients (setiathome, foldingathome, etc) and prevents these tasks from stealing away any cpu time from other applications. Also it can be useful for conducting large compilations of software without affecting other tasks. These tasks are also flagged in -ck for less aggressive memory usage and disk read bandwidth, but these affects are not potent, and if the task uses a lot of memory and disk it will be noticeable. SCHED_IDLEPRIO takes a nice value. However this nice value only determines the cpu distribution between all idleprio tasks, allowing many idleprio tasks to be running with different nice values. This might be desirable, for example, when using a distributed computing client at nice 19 and compiling software at nice 0 when both are SCHED_IDLEPRIO.


Monday, September 7, 2009

Dynamically initialize 2D array in ruby

#!/usr/bin/ruby

class Array2D
def initialize(width, height)
@data = Array.new(width) { Array.new(height) }
end
def [](x, y)
@data[x][y]
end
def []=(x, y, value)
@data[x][y] = value
end
end

puts("please enter the size of square matrix")
$num=gets.chomp.to_i
arr = Array2D.new($num, $num)

for j in 0..$num-1

for k in 0..$num-1
arr[j, k] = 0
end
arr[j, k]= 0
end

Friday, September 4, 2009

Why SystemC instead of C++ ?

The C++ language is based on sequential programming. Consequently it is not suited for the modeling of concurrent activities. Furthermore most system and hardware models require a notion of delays, clocks or time. features which are not present in C++ as a software programming language. As a result, complex and detailed systems cannot be easily described in C++ alone. Additionally communication mechanisms used in hardware models, such as signals and ports, are very different from those used in software programming. Lastly, the data types found in C++ are too remote from the actual hardware implementation.
Ultimately, new dedicated data types and communication mechanisms have to be provided.

Wednesday, September 2, 2009

SystemC

Why systemC?

SystemC is replacing the specially designed HDL's like Verilog and VHDL in many situations. This does not mean that these HDL's are obsolete now, instead, systemC supports a new approach to design a system.

The systemC born because of the necessities of the current electronic industry: Electronic gadgets are incorporating greater and greater functionality today, but not compromising with the time to produce and market the gadgets. For example, you want your mobile handset to have internet facility but you are not ready to wait for one year for that facility to come. It is easy for you to demand, but it is not so easy for electronic design engineers who design the system. The greater complexity of the future systems are making the situation still worst. Previously, the C (or C++) was used to write the software part of the design. For hardware part any of the existing HDL's were used to design the hardware. It was very difficult to setup a testbench which is common for both, since they are entirely different languages. The introduction of systemC solved many of these problems.

The systemC is nothing but a C++ class library specially designed for system design. This is an open source ware, maintained by OSCI (Open source SystemC Initiative). (visit http://systemc.org for more details.)


The advantages of using systemC are:

1. It inherits all the features of C++, which is a stable programming language accepted all over the world. It has got large language constructs, which makes easier to write the program with less efforts.

2. Rich in data types: along with the types supported by C++, systemC supports the use of special data types which are often used by the hardware engineers.

3. It comes with a strong simulation kernel to enable the designers to write good test benches easily, and to simulate it. This is so important because the functional verification at the system level saves a lot of money and time.

4. It introduces the notion of time to C++, to simulate synchronous hardware designs. This is common in most of the HDL's.

5. While most of the HDL's support the RTL level of design, systemC supports the design at an higher abstraction level. This enables large systems to be modeled easily without worrying the implementation of it. It also supports RTL design, and this subset is usually called as systemC RTL.

6. Concurrency: To simulate the concurrent behavior of the digital hardware, the simulation kernel is is so designed that all the 'processes' are executed concurrently, irrespective of the order in which they are called.

Monday, August 31, 2009

How to create doubly linklist using template in c++

First create list.h file and put this code in list.h

#include "list.h"
#include
using namespace std;

template<> class Lnode
{
public:
T data;
Lnode<> *next;
Lnode<> *prev;

};


template<> class List
{
public:
void add( T data );
void display();
T remove();
Lnode<>* head;
List()
{
head = new Lnode<>();
head->next = NULL;
head->prev = NULL;
}
};

template<> void List<>::add( T data )
{
Lnode<> *p;
p = new Lnode<>();
p->data = data;
p->next = head->next;
p->prev = head->prev;
head->next = p;
}

template<> T List<>::remove()
{
T data;
Lnode<> *node;

if (head->next == NULL)
{
cout << "ERROR: `remove' called with empty list.\n"; exit(1); } node = head->next;
data = node->data;

head->next = node->next;
head->prev = node->prev;
delete node;

return data;
}

template<> void List<>::display()
{
T data;
Lnode<> *node;

while(head->next!= NULL)
{

head=head->next;
cout <<>data;
cout << "\n"; } }

Next create linklist_template.cpp and put this code in linklist_template.cpp.

#include "list.h"
#include
using namespace std;

int main()
{
List<> list1;
List<> list2;

list1.add( 5 );
list1.add( 25 );
list1.add( 35 );
list1.add( 45 );
list2.add( 2.7 );

list1.display();

cout << list1.remove();
cout << "\n";
cout << list1.remove();
cout << "\n";
cout << list1.remove();
cout << "\n";

l

return 0;
}