Está en la página 1de 13

Scheduling algorithm

Scheduling algorithm is the method by which threads, processes or data flows are
given access to system resources (e.g. processor time, communications bandwidth).
This is usually done to load balance a system effectively or achieve a target quality of
service. The need for a scheduling algorithm arises from the requirement for most
modern systems to perform multitasking(execute more than one process at a time)
and multiplexing (transmit multiple flows simultaneously).

In Computing And Multitasking


The algorithm used may be as simple as round-robin in which each process is given
equal time in a cycling list. So, process A executes for 1 ms, then process B, then
process C, then back to process A. More advanced algorithms take into account
process priority, or the importance of the process. This allows some processes to use
more time than other processes. It should be noted that the kernel always uses
whatever resources it needs to ensure proper functioning of the system, and so can be
said to have infinite priority. In SMP (symmetric multiprocessing) systems, processor
affinity is considered to increase overall system performance, even if it may cause a
process itself to run more slowly. This generally improves performance by
reducing cache thrashing.

In Computer Networks And Multiplexing


In computer networks and multiplexing, the notion of a scheduling algorithm is used
as an alternative to first-come first-served queuing of data packets. In advanced packet
radio wireless networks such as HSDPA (High-Speed Downlink Packet
Access ) 3.5G cellular system, channel-dependent scheduling may be used to take
advantage of favourable channel conditions to increase the throughput and system
spectral efficiency. The simplest best-effort scheduling algorithms are round-robin, fair
queuing (a max-min fair scheduling algorithm), proportionally fair scheduling
and maximum throughput. If differentiated or guaranteed quality of service is offered, as
opposed to best-effort communication, weighted fair queuing may be utilized. .The
notion of a scheduling algorithm is used as an alternative to first-come first-served
queuing of data packets. LIFO and FIFO refer to how requests or intermediate results
are stored and dealt with. If requests are handled on a first-come-first-served basis, the
date store is called a 'queue'. If requests are handled Last-In-First-Out, the structure is
called a 'stack'. Different operations require different data handling.

WHY SCHEDULING ALGORITHMS?????


Scheduling algorithms are used to decide when and for how long each process runs
and also make choices about Preemptibility, Priority, Running time, Run-time-to-
completion, fairness.

FIFO
In computer science this term refers to the way data stored in a queue is processed as
first in first out(FIFO). Each item in the queue is stored in a queue (simpliciter) data
structure. The first data to be added to the queue will be the first data to be removed,
then processing proceeds sequentially in the same order. This is typical behavior for a
queue.
Disk controllers can use the FIFO as a disk scheduling algorithm to determine the order
to service disk I/O requests.

In hardware FIFO is used for synchronization purposes. It is often implemented as a


circular queue, and thus has two pointers:

1. Read Pointer/Read Address Register


2. Write Pointer/Write Address Register

Read and write addresses are initially both at the first memory location and the FIFO
queue is Empty.
FIFO EmptyWhen read address register reaches to write address register, the FIFO
triggers the Empty signal.FIFO FULLWhen write address register reaches to read
address register, the FIFO triggers the FULL signal.

FIFO is used in queues. And a queue is a particular kind of collection in which the
entities in the collection are kept in order and the principal (or only) operations on the
collection are the addition of entities to the rear terminal position and removal of entities
from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data
structure. In a FIFO data structure, the first element added to the queue will be the first
one to be removed. This is equivalent to the requirement that whenever an element is
added, all elements that were added before have to be removed before the new element can
be invoked. A queue is an example of a linear data structure.
Queues provide services in computer science, transport and operations research where various
entities such as data, objects, persons, or events are stored and held to be processed later. In
these contexts, the queue performs the function of a buffer.

Queues are common in computer programs, where they are implemented as data structures
coupled with access routines, as an abstract data structure or in object-oriented languages as
classes. Common implementations are circular buffers and linked lists.

Rear: A variable stores the index number in the array at which the new data will be
added (in the queue).

Front: It is a variable storing the index number in the array where the data will be
retrieved.
Algorithum for queues

This algo inserts an element ITEM into a queue.

INSERTION(QUEUE, N, FRONT, REAR, ITEM)

1. If FRONT=1 and REAR=N, or if FRONT=REAR+1, then:


Write: OVERFLOW, and Return.

2.If FRONT:=NULL, then:


Set FRONT:=1 and REAR:=1
Else if REAR=N, then:
Set REAR:=1
Else:
Set REAR:=REAR+1
[End of If structure]

3.Set QUEUE[REAR]:=ITEM

4.Return

This algo deletes an element from queue and assigns it to the variable ITEM.

DELETE(QUEUE, N, FRONT, REAR, ITEM)


1. If FRONT=NULL then: Write : UNDERFLOW, and Return.

2.Set ITEM:=QUEUE[FRONT]

3. If FRONT= REAR, then


Set FRONT:=NULL and REAR:=NULL
Else if FRONT=N , then:
Set FRONT:=1
Else:
Set FRONT:=FRONT+1
[End of if structure]

4. Return
SOURCE CODE FOR NSERTION AND DELETION IN QUEUES

#include<stdio.h>
#include<conio.h>
#incldue<alloc.h>
Typedef struct node

Int data;

Struct node *next;

Node;

Void main()

Node *head *p;

Clrscr();

Int n;

Printf(“enter no. of elements”);

Scanf(“%d”&n);

Head=(node*)malloc(sizeof(node));

Head->next=NULL;

Printf(“enter the elements”);

Scanf(“%d”,&(head->data));

P=head;

For(int i=1;i<n;i++)

p->next=(node*)malloc(sizeof(node));

p=p->next;
p->next=NULL;

scanf(“%d”,&p->data);

Int ch=0;

While(ch!=4)

Printf(“enter your choice \n 1. insert”);

Printf(:\n 2. delete\n 3. display \n 4. quit);

Scanf(“%d”,&ch);

Switch(ch)

Case1: p->next=(node*)malloc(sizeof(node));

P=p->next=NULL;

Printf(“enter elements to insert”);

Scanf(“%d”,&(p->data));

Break;

Case2:

Printf(“enter elements to delete %d”,head->data);

Head=head->next;

Break;

Case3:

P=head;

Printf(“\n”)

While(n!=Null)

{
Printf(“%d”,p->data);

P=p->next;

Break;

Case4:

Printf(“\n quit”);

Break;

Getch();

LIFO
In computer science, a stack is a last in, first out (LIFO) abstract data type and data
structure. A stack can have any abstract data type as an element, but is characterized
by only two fundamental operations: push and pop. The push operation adds to the top
of the list, hiding any items already on the stack, or initializing the stack if it is empty.
The pop operation removes an item from the top of the list, and returns this value to the
caller. A pop either reveals previously concealed items, or results in an empty list.
A stack is a restricted data structure, because only a small number of operations are
performed on it. The nature of the pop and push operations also means that stack
elements have a natural order. Elements are removed from the stack in the reverse
order to the order of their addition: therefore, the lower elements are typically those that
have been in the list the longest. Operations used in stack that is push and pop are:

 a push operation, in which a data item is placed at the location pointed to by the
stack pointer, and the address in the stack pointer is adjusted by the size of the data
item;
 a pop or pull operation: a data item at the current location pointed to by the stack
pointer is removed, and the stack pointer is adjusted by the size of the data item.
Algorithum for traversing a stack is as follows:

Algo(traversing linear array)here LA is a linear array with lower bound LB and upper
bound UB.this algo traverses LA applying an operation. PROCESS to each element of
LA
1.[initialize counter] set k=LB

2.Repeat Steps3 and 4 while k<=UB

3.[visit element] Apply PROCESS to LA[k]

4.[increase counter] Set k=k+1


[END of Step2 Loop]

5.Exit

Source code for traversing of lifo::

#include<iostream.h>
#include<conio.h>
Void main()
{
Int n ,t[10];
Cout<<”Enter the number of elements want to enter: ”;
Cin>>n;
Cout<<”Enter the elements :”;
For(int i=0;i<n;i++)
{
Cin>>t[i];
}

Cout<<”Values entered are: ”;


For(int j=0;j<n;j++)
{
Cout<<t[j];
}
Getch();
}

PUSH(STACK, TOP, MAXSTK, ITEM)


This procedure pushes an ITEM into a stack

1.If TOP=MAXSTK, then: Print OVERFLOW, and Return

2.Set TOP:=TOP+1

3.Set STACK[TOP]:=ITEM

4.EXIT.

POP(STACK, TOP, MAXSTK, ITEM)


This procedure deletes the top element of STACK and assigns it to the variable ITEM

1. If TOP=0, then: Print UNDERFLOW, and Return

2. Set ITEM:=STACK[TOP]

3. TOP:=TOP-1

4. EXIT.

SOURCE CODE FOR PUSH AND POP FUNCTION

#include<stdio.h>

#include<malloc.h>

#include<conio.h>
typedef struct node

int data;

struct node *next;

node;

void main()

node *head,*p;

clrscr();

int n;

printf("enter the number of elements in stack:");

scanf("%d",&n);

head=(node*)malloc(sizeof(node));

head->next=NULL;

printf("enter the elements:");

scanf("%d",&(head->data));

p=head;

for(int i=1;i<n;i++)

p->next=(node*)malloc(sizeof(node));

p=p->next;

p->next=NULL;

scanf("%d",&(p->data));
}

int ch;

printf("enter ur choice 1 push 2 pop 3 display 4 quit :”);

scanf("%d",&ch);

if(ch==1)

node *newn;

newn=(node*)malloc(sizeof(node));

newn->next=NULL;

printf("enter the info of elementto b inserted: ");

scanf("%d",&(newn->data));

newn->next=head;

head=newn;

else if(ch==2)

int item;

item=head->data;

head=head->next;

printf("item is deleted,so poped element is %d",&item);

else if(ch==3)

p=head;

printf("displaying");
while(p!=NULL)

printf("%d",p->data);

p=p->next;

else if(ch==4)

printf("quit");

getch();

else

printf("wrong choice");

goto n;

}
REFERENCES
http://learning-computer-programming.blogspot.com/2007/06/data-structures-
introduction-to-stacks.html

http://www.cs.princeton.edu/introcs/43stack/