Sunteți pe pagina 1din 63

Dr. M.G.R.

UNIVERSITY
CHENNAI 600 095

Name :

:
Register No.

Course : M.Tech., CSE

Semester : I

Lab Name : OS & DATA STRUCTURE LAB

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


Dr. M.G.R. UNIVERSITY
CHENNAI - 600 095

Bonafide Certificate

Certified that this is a bonafide record of work done

by _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 1st Semester

Computer Science and Engineering in OS AND DATA STRUCTURES LAB -

MCS691 during the period June 2009 to December 2009.

Staff In-Charge Head of the Department Rector

Submitted for the Practical Examination held on ………………………

Internal Examiner External Examiner


TABLE OF CONTENTS

Ex. Page
Date Name of the Experiment Signature
No. No.

Data Structures Program

1 Stack Implementation Using Array 1

2 Stack Implementation Using Linked List 5

3 Queue Implementation Using Array 9

4 Queue Implementation Using Linked List 14

5 Implementation of Bubble Sort 18

6 Implementation of Merge Sort 21

7 Implementation of Heap Sort 25

8 Implementation of Quick Sort 29

9 Implementation of Tree Traversal 33

10 Implementation of Binary Search 38

Operating Systems Programs

11 Shortest Job First Scheduling 41

12 Round Robin Scheduling 45

13 Producer Consumer Problem 49

14 Priority Scheduling 53

15 Thread Concept 57
Ex. No. : 1 STACK IMPLEMENTATION USING ARRAY
Date :

Aim

To create a data structure for stack using array and perform insertion, deletion of an

item and display content of stack.

Algorithm

¾ Enter either 1 or 2 or 3 to push or pop or display respectively.

¾ To push an element on tot the stack the element is assigned a value and the stack

top is incremented.

¾ To pop an element from the stack, the top most element is displayed and the stack

top is decremented.

¾ The stack elements are displayed when required.

¾ To print and element from the stack the stack top most element is displayed.

1
Program
# include<stdio.h>
# include<conio.h>
# define MAX 5
int top;
int stack[MAX];
void push();
void pop();
void display();
void main()
{
int choice;
top=-1;
do
{
clrscr();
printf("\n\t\t STACK IMPLEMENTATION ");
printf("\n \t 1.Push \n\t 2. Pop \n\t 3. Display\n\t 4. Exit");
printf("\n Enter your choice ..... ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
getch();
break;
case 2:
pop();
getch();
break;
case 3:
display();
getch();
break;
case 4:
return;
default :
printf("\n\n Wrong Choice.... !");
getch();
break;
}

}while(choice != 4);
}
void push()
{
int data,i;

2
if(top==MAX-1)
{
printf("\n\n\t Stack Overflow.....!");
return;
}
else
{
for(i=1;i<=MAX;i++)
{
printf("\n Enter the element.... ");
scanf("%d",&data);
top++;
stack[top]=data;
}
}
}

void pop()
{
int p;
if(top==-1)
{
printf("\n\n Stack Underflow");
return;
}
else
{
p=stack[top];
top--;
printf("\n %d is deleted....",p);
}
}

void display()
{
int i;
if(top==-1)
{
printf("\n\n Stack Underflow...");
return;
}
else
printf("\n\t TOP=%d",top);
for(i=top;i>=0;i--)
printf("\n\t %d",stack[i]);
}

3
Output
STACK IMPLEMENTATION
1.Push
2. Pop
3. Display
4. Exit
Enter your choice ..... 1
Enter the element.... 5
Enter the element.... 4
Enter the element.... 3
Enter the element.... 2
Enter the element.... 1
STACK IMPLEMENTATION
1.Push
2. Pop
3. Display
4. Exit
Enter your choice ..... 3
TOP=4
1
2
3
4
5
STACK IMPLEMENTATION
1.Push
2. Pop
3. Display
4. Exit
Enter your choice ..... 4

Result :

Thus the output of stack implementation was executed successfully using array.

4
Ex. No. : 2 STACK IMPLEMENTATION USING LINKED LIST

Date :

Aim

To implement linked list using dynamic memory allocation.

Algorithm

¾ Start the program

¾ Enter choice for creation, insertion, deletion of node in a linked list.

¾ In Creation of a node in a liked list the function Malloc() is used to allocate the

memory.

¾ Enter the insertion element and say whether it is to be inserted at the front, middle

or end. Display the list content.

¾ Enter the element which is going to be deleted at front, middle or end and display

the list.

¾ The last pointer of list is always pointing a null value.

¾ Repeat the above process.

¾ Stop the program.

5
Program

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void push();
void pop();
void display();
struct node
{
int info;
struct node*link;
};
struct node *top,*ptr,*p;
void main()
{
int choice,item;
top=NULL;
clrscr();
printf("\n\t\t STACK USING LINKED LIST\n");
do
{
printf("\n1.push 2.pop 3.display 4.end");
printf("\nEnter your option:");
scanf("%d",&choice);
if (choice==1)
{
printf("\nEnter the element:");
scanf("%d",&item);
ptr=malloc(sizeof (ptr));
ptr->info=item;
ptr->link=NULL;
push();
}
if(choice==2)
pop();
if(choice==3)
display();
}

6
while (choice < 4);
}
void push()
{
if(top==0)
top=ptr;
else
{
ptr->link=top;
top=ptr;
}
}

void pop()
{
int m;
if(top==NULL)
printf("\nStack is Underflow\n");
else
{
m=top->info;
top=top->link;
printf("\nThe deleted element is %d\n",m);
}
}

void display()
{
if(top==NULL)
printf("\nStack is Empty\n");
p=top;
while(p!=NULL)
{

printf("\n\t\t%d",p->info);
p=p->link;
}
}

7
Output
STACK USING LINKED LIST
1.Push 2.Pop 3.Peep 4.End
Enter your choice :1
Enter the element:10
1.Push 2.Pop 3.Peep 4.End
Enter your choice :1
Enter the element:20
1.Push 2.Pop 3.Peep 4.End
Enter your choice :3
The element are
20
10
1.Push 2.Pop 3.Peep 4.End
Enter your choice :2
The element deleted is 20
1.Push 2.Pop 3.Peep 4.End
Enter your choice :3
The element are
10
1.Push 2.Pop 3.Peep 4.End
Enter your choice :2
The element deleted is 10
1.Push 2.Pop 3.Peep 4.End
Enter your choice :2
Stack is Underflow
1.Push 2.Pop 3.Peep 4.End
Enter your choice :3
Stack is empty
1.Push 2.Pop 3.Peep 4.End
Enter your choice :4

Result

Thus the output of linked list was executed successfully and output was obtained.

8
Ex. No. : 3 QUEUE IMPLEMENTATION USING ARRAY
Date :

Aim

To create a data structure for queue using array and perform insertion, deletion of an

item and display the content of queue.

Algorithm

¾ Declare a structure queue with data types item, front, rear.

¾ Declare the function empty, insert, delete display globally.

¾ Initialize front to 0and rear to -1.

¾ Select the choice 1 for insert and 2 for delete.

¾ If the choice is 1 get the number of elements to insert

¾ If the choice is 2, call the delete function and display the queue.

¾ Repeat the steps until you want.

9
Program
# include<stdio.h>
# include<conio.h>
# define MAX 5
int rear, front;
int queue[MAX];
void insert();
void del();
void display();
void main()
{
int choice;
rear=-1;
front=0;

do
{
clrscr();
printf("\n \t QUEUE IMPLEMENTATION :");
printf("\n\n\t 1. Insert \n\t 2. Delete \n\t 3. Display \n\t 4. Exit");
printf("\n Enter Your Choice....");
scanf("%d",&choice);
switch(choice)
{
case 1 :
insert();
getch();
break;
case 2 :
del();
getch();
break;
case 3 :
display();

10
getch();
break;
case 4 :
return;
default :
printf("\n\t Wrong Choice ....");
return;
}
} while(choice != 4);
}
void insert()
{
int data,p;
if(rear==MAX-1)
{
printf("\n\n Queue Overflow.....!");
return;
}
else
{
printf("\n Enter the element...");
scanf("%d",&data);
rear++;
p=data;
queue[rear]=p;
printf("\n %d is inserted...",p);
}
}

void del()
{
int p;

if(rear < front)

11
{
printf("\n Queue Underflow...");
return;
}
else
{
p=queue[front];
front++;
rear--;
printf("\n %d is deleted...",p);
}
}
void display()
{
int i;
if(rear<front)
{
printf("\n \t Rear : %d \t Front : %d",rear,front);
for(i=front;i<=rear;i++)
printf("\n\t %d",queue[i]);
printf("\n Queue Underflow.....!");
return;
}
else
{
printf("\n \t Rear : %d \t Front : %d",rear,front);
for(i=front;i<=rear;i++)
printf("\n\t %d",queue[i]);
}
}

12
Output

QUEUE IMPLEMENTATION :
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice....1
Enter the element...5
5 is inserted...
QUEUE IMPLEMENTATION :
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice....1
Enter the element...6
6 is inserted...
QUEUE IMPLEMENTATION :
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice....1
Enter the element...7
7 is inserted...
QUEUE IMPLEMENTATION :
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice....3

Rear : 2 Front : 0
5
6
7

Result

Thus the implementation of queue using array was executed successfully.

13
Ex. No. : 4 QUEUE IMPLEMENTATION USING LINKED LIST
Date :

Aim

To create a data structure for queue using dynamic memory allocation and perform

insertion, deletion of an item and display the content of queue.

Algorithm

¾ Declare a structure queue with data types item, front, rear.

¾ Declare the function empty, insert, delete display globally.

¾ Initialize front to 0and rear to -1.

¾ Select the choice 1 for insert and 2 for delete.

¾ If the choice is 1 get the number of elements to insert

¾ If the choice is 2, call the delete function and display the queue.

¾ Repeat the steps until you want.

14
Program

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
struct qnode
{
int data;
struct qnode *next;
};

struct qnode *front,*rear;


void insertQ();
void delQ();
void display();

void main()
{
int ch=0;
front=rear=NULL;
while(ch!=4)
{
clrscr();
printf("\t\t\t Queue Operations\n");
printf("\n\t1.Insert");
printf("\n\t2.Delete");
printf("\n\t3.display");
printf("\n\t4.exit");
printf("\n\tEnter u r choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insertQ();break;
case 2: delQ(); break;
case 3: display(); break;
case 4: break;
default: printf("Enter valid choice..!!!"); break;
}
getch();
}
}

void insertQ()
{
struct qnode *t;
t=(struct qnode*)malloc(sizeof(struct qnode));

15
printf("Enter the Q new element:");
scanf("%d",&t->data);
t->next=NULL;
if (front==NULL)
front=t;
else
rear->next=t;
rear=t;
}

void delQ()
{
struct qnode *t;
if (front == NULL)
printf(" Q is empty......!!!! ");
else
{
t=front;
printf("element %d is deleted from Q",t->data);
front=t->next;
if (front==NULL) rear=NULL;
free(t);
}
}

void display()
{
struct qnode *t;
char msg[100];
if(front==NULL)
printf("Q is empty......!");
else
{
t=front;
strcpy(msg," F ");
printf("\n\n\t\t");
while(t!=NULL)
{
printf("%3d",t->data);
t=t->next;
strcat(msg," ");
}
strcat(msg,"\b\b\b\bR");
printf("\n\t\t%s",msg);
}
}

16
Output

Queue Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter u r choice:1
Enter the Q new element:4
Enter the Q new element:5
Enter the Q new element:6
Enter the Q new element:7

Queue Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter u r choice:3
4 5 6 7
F R

Queue Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter u r choice:2
element 4 is deleted from Q

Queue Operations
1.Insert
2.Delete
3.Display
4.Exit
Enter u r choice:3
5 6 7

Result

Thus the output of Queue Using Linked List was verified successfully.

17
Ex. No. : 5 IMPLEMENTATION OF BUBBLE SORT

Date :

Aim

To write a C program to implement bubble sort.

Algorithm

¾ Start

¾ Declare the required variables to perform bubble sort.

¾ Declare the required functions

¾ Get the number of items from the user to be sorted

¾ Enter the number of items using input()

¾ Call the sort () function to perform sorting by swapping the numbers

¾ Call the display () function to display the sorted list

¾ End

18
Program

#include<stdio.h>
int noofitems,x[100],i,j;
void main()
{
void sort(),display(),input();
int i;
printf("\n enter how many elements do you want to feed \n");
scanf("%d",&noofitems);
printf("\n enter %d elements",noofitems);
for(i=0;i<noofitems;++i)
scanf("%d",&x[i]);
sort();
display();
}
void input()
{
printf("\n elements are:\n");
for(i=0;i<noofitems;++i)
printf("%5d",x[i]);
}
void display()
{
printf("\n sorted elements are:\n");
for(i=0;i<noofitems;++i)
printf("%5d",x[i]);
}
void sort()
{
int swap=1,temp;
for(i=0;i<noofitems&&swap==1;++i)
{
swap=0;
for(j=0;j<noofitems-(i+1);++j)
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
swap=1;
}
}
}

19
Output

Enter how many elements you want to feed: 5

Enter 5 elements: 45 23 67 33 5

Sorted elements are: 5 23 33 45 67

Result

Thus the output of bubble sort process was executed successfully.

20
Ex. No. : 6 IMPLEMENTATION OF MERGE SORT
Date :

Aim

To write a data structure program to perform merge sort.

Algorithm

¾ start the program

¾ Read the variables witch are declared in the program

¾ Call the function merging

¾ Check the cognition that the initial value is generated the assumed one.

¾ print the variable with the suitable pace

¾ terminate the program

21
Program
# include<stdio.h>
# include<conio.h>
int i,j,temp;
int a[20],b[20],c[20],t1,m,n,s,s2;
void main()
{
void input();
void divide();
void mrg();
clrscr();
printf("\n Merge sort");
input();
divide();
printf("\n sorted element");
for(i=0;i<s;i++)
{
printf("%d\no",c[i]);
}
getch();
}

void input()
{
printf("\n enter the limit of array");
scanf("%d",&s);
printf("\n Enter the elements of array");
for(i=0;i<s;i++)
scanf("%d",&c[i]);
}

void divide()
{
if(s%2==0)
m=n=s2;
else
{
m=(s+1)/2;
n=m-1;
}
for(i=0;i<m;i++)
a[i]=c[i];
t1=0;
for(i=0;i<s;i++)
{
b[t1]=c[i];
t1++;

22
}
for(i=0;i<m-1;i++)
for(j=i+1;j<m;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("\n the first sorted array");
for(i=0;i<m;i++)
printf("%d\n",a[i]);
for(i=0;i<m-1;i++)
for(j=i+1;j<m;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
printf("\n the sorted second array");
for(i=0;i<n;i++)
{
printf("%d\n",b[i]);
}
}
void mrg()
{
for(i=0;i<m;i++)
c[i]=a[j];
t1=0;
for(i=m;i<s;i++)
{
c[i]=b[t1];
t1++;
}
for(i=0;i<s-1;i++)
for(j=i+1;j<s;j++)
if(c[i]< c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}

23
Output
Merge sort

Enter the limit of array 5


Enter the elements of array
3
5
2
7
1
the sorted first array
2
3
5
the sorted second array
1
7
sorted element
1
2
3
5
7

Result

Thus the output of merge sort was verified.

24
Ex. No. : 7 IMPLEMENTATION OF HEAP SORT
Date :

Aim

To write a program to sort the element in the given array using heap sort.

Algorithm

¾ Start the program.

¾ Accept the array element.

¾ Initialize P=1,Y=N div 2+1

¾ Repeat the following while (Y>2) repeat Y=Y+1,sort the value at the

• end of loop

¾ Y=N+1

¾ While(Y=2) repeat Y=Y+1 Store the values Root is replaced by bottom left node.

¾ Print the array in the sorted form.

¾ Stop the program.

25
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
int a[size];
void main()
{
int i,n,val;
int getdata();
void heap(int,int);
void sort(int);
clrscr();
printf("\n Program for heap sort");
n=getdata();
for(i=1;i<n;i++)
{
val=a[i];
heap(i,val);
}
printf("\n Heap is created as:");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
sort(n);
printf("\n After sorting by heap sort:");
for(i=0;i<n;i++)
printf("\n\t%d",a[i]);
getch();
}
int getdata()
{
int i,n;
printf("\n How many elements you want to sort:");
scanf("%d",&n);
if(n>size)
{
printf("\n Move element than array size");
return n;
}
printf("\n Enter the elements:");
for(i=0;i<n;i++)

26
scanf("%d",&a[i]);
return n;
}
void heap(int i,int val)
{
int s,parent;
s=i;
parent=(s-1)/2;
while(s>0&&a[parent]<val)
{
a[s]=a[parent];
s=parent;
parent=(parent-1)/2;
}
a[s]=val;
}
void sort(int n)
{
int i,node,parent,pos;
for(i=n-1;i>0;i--)
{
pos=a[i];
a[i]=a[0]; parent=0;
if(i==1)
node=-1;
else
node=1;
if( i>2&&a[2]>a[i])
node=2;
while(node>=0&&a[node])
{
a[parent]=a[node];
parent=node;
node=2*parent+1;
if(node+1<=i-1&&a[node]<a[node+1])
node++;
if(node>i-1)
node=-1;
}
a[parent]=pos;
}
}
27
Output

Program for heap sort

How many elements you want to sort:3

Enter the elements:6

Heap is created as:

After sorting by heap sort:

Result

Thus the program has been executed for heap sort and the output is verified.

28
Ex. No. : 8 IMPLEMENTATION OF QUICK SORT
Date :

Aim

To find a small number using quick sorting program.

Algorithm

¾ Start the program

¾ Read the total number of array

¾ Enter the size of array

¾ Enter the elements of the array

¾ Set a loop until the array

¾ Print before sorting and using equivalent condition

¾ Print after sorting an set a start up to the position you enter.

¾ Check the condition

¾ Print the sorted values

¾ Stop the program

29
Program
# include<stdio.h>
# include<stdlib.h>
# include<conio.h>
# define max 10
void main()
{
int n,I,x[max];
void quick(in tint x[],int,int);
clrscr();
printf(“\n Enter how many elements …”);
scanf(%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n Enter the elements :”);
scanf(“%d”,&x[i]);
}
quick(x,0,n-1);
printf(“\n The sorted Elements are…. “);
for(i=0;i<n;i++)
printf(“%d”,&x[i]);
getch();
}
void quick(int x[max], int lb, int ub)
{
int new_pivote;
int partition(int x[max], int lb, int ub);
if(lb < ub)
{
new_pivote=partition(x,lb,ub);
quick(x,lb,new_pivote-1)
quick(x,lb,new_pivote+1,ub);
}
}

30
int partition((int x[max], int lb, int ub)
{
int q,pivote,I,lower;
void interchange(int x[max]int a,int b);
interchange(x,lb,(lb+ub)/2);
pivot=x[lb];
lower=lb;
for(i=lb+1;i<=ub;i++)
{
if(x[i]<pivot)
{
lower=lower+1;
interchange(x,lower,i);
}
}
interchange(x,lb,lower);
q=lower;
return(q);
}
void interchange(int x[max],int a,int b)
{
int temp;
temp=x[a];
x[a]=x[b];
x[b]=temp;
}

31
Output
Enter how many elements…… 5
Enter the element : 9
Enter the element : 1
Enter the element : 6
Enter the element : 3
Enter the element : 0

The Sorted elements


0
1
3
6
9

Result

Thus the quick sort program was successfully executed.

32
Ex. No. : 9 IMPLEMENTATION OF TREE TRAVERSAL
Date :

Aim

To write a program in C for implementing an expression tree to produce it’s different tree
traversals.

Algorithm

¾ Start the Program.

¾ Declare the variables as needed.

¾ Declare the In order, Pre order and Post order operations.

¾ Allocate the memory space for new node.

¾ Define the different tree traversal techniques.

¾ Give the expression to traverse and enter the choice to traverse.

¾ Stop the program.

33
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef struct tree*node;
node insert(int, node T);
void inorder(node T);
void preorder(node T);
void postorder(node T);
struct tree
{
int data;
struct tree*right,*left;
}*root;

void main()
{
node T=NULL;
int data,ch,i=0,n; clrscr();
printf("\n enter the number of element in the tree :");
scanf("%d",&n);
printf("\n enter the elements :");
while(i<n)
{
scanf("%d",&data);
T=insert(data,T);
i++;
}
printf("\n 1.INORDER \t 2.PREORDER \t 3.POSTORDER \t 4.EXIT");
do
{
printf("\n Enter your choice :");
scanf("%d",&ch);

34
switch(ch)
{
case 1:printf("\n Inorder traversal of the given tree");
inorder(T); break;
case 2:printf("\n Preorder traversal of the given tree");
preorder(T); break;
case 3:printf("\n Postorder traversal of the given tree");
postorder(T); break;
case 4:printf("Exit");
exit(0);
}
}
while(ch<4);
getch();
}
node insert(int X,node T)
{
struct tree *newnode;
newnode=(struct tree *)malloc(sizeof(struct tree));
if(newnode==NULL)
printf("\n out of space \n");
else
{
if(T==NULL)
{
newnode->data=X;
newnode->left=NULL;
newnode->right=NULL;
T=newnode;
}
else
{
if(X<T->data)
T->left=insert(X,T->left);

35
else
T->right=insert(X,T->right);
}
}
return T;
}
void inorder(node T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d \t",T->data);
inorder(T->right);
}
}
void preorder(node T)
{
if(T!=NULL)
{
printf("%d \t",T->data);
preorder(T->left); preorder(T->right);
}
}
void postorder(node T)
{
if(T!=NULL)
{
postorder(T->left); postorder(T->right);
printf("%d \t",T->data);
}
}

36
Output
Enter the number of element in the tree: 3

Enter the elements: 11


10
23

1. INORDER 2.PREORDER 3.POSTORDER 4.EXIT


Enter your choice: 1

In order traversal of the given tree 10 11 23


Enter your choice: 2

Preorder traversal of the given tree 11 10 23


Enter your choice: 3

Post order traversal of the given tree10 23 11


Enter your choice: 4

Result

Thus the tree traversal program was executed successfully.

37
Ex. No. : 10 IMPLEMENTATION OF BINARY SEARCH
Date :

Aim

To find a number using Binary search method.

Algorithm

¾ Start the program

¾ Enter the size of array

¾ Enter the elements of the array

¾ Set a loop until the array

¾ Enter the search element

¾ To Find out the given search element using loop

¾ Check the condition

¾ If the element found print it location otherwise print element is not found

¾ Stop the program

38
Program
#include<stdio.h>
void main()
{
int a[100],I,n,low,high,mid,term,flag=1;
clrscr();
printf("Enter The number of Element in Array");
scanf("%d",&n);
printf("\nEnter The Element in ascending order");
for(I=0;I<n;I++)
scanf("%d",&a[I]);
printf("Enter the element to be searched");
scanf("%d",&term);
printf("%d",term);
low=0; high=n-1;
while(low<=high)
{
mid=low+high/2;
if(term<a[mid])
high=mid-1;
else
if(term>a[mid])
low=mid+1;
else
if(term==a[mid])
{
printf("%d found Successfully at location %d",term,mid+1);
flag=0;
break;
}
}
if(flag)
printf("Search unsuccessful");
}

39
Output
Enter The number of Element in Array
4
Enter The Element in ascending order
45
56
78
80
Enter the element to be searched
45
45 is founded at the location 1

Result

Thus the quick sort program was successfully executed.

40
Ex. No. : 11 SHORTEST JOB FIRST SCHEDULING
Date :

Aim

To implement the shortest job first scheduling algorithm

Algorithm

¾ Start the process

¾ Declare the array size

¾ Get the number of elements to be inserted

¾ Select the process which have shortest burst will execute first

¾ If two process have same burst length then FCFS scheduling algorithm used

¾ Make the average waiting the length of next process

¾ Start with the first process from it’s selection as above and let other process to be in queue

¾ Calculate the total number of burst time

¾ Display the values

¾ Stop the process

41
Program
#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;

42
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf("\n\t process \t waiting time \t turn around time \n");
for(i=0;i<n;i++)
printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]);
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
return 1;
}

43
Output

enter the number of process


5
enter the burst times
24568

process waiting time turn around time


p[0] 0 2
p[1] 2 6
p[2] 6 11
p[3] 11 17
p[4] 17 25
the average waiting time is 7.200000
the average turn around time is 12.200000

Result

Thus the output of shortest job first scheduling process was executed successfully.

44
Ex. No. : 12 ROUND ROBIN SCHEDULING
Date :

Aim
To write a program for round robin scheduling process

Algorithm

¾ Start the program

¾ Get the process and time slice

¾ Run the process at regular intervals [cyclic] until all process finished

¾ Calculate average waiting time

¾ Calculate average turn around time

¾ Stop the program

45
Program
#include<stdio.h>
int p[10],w[10],s[10],n,i,qt;
float tw=0,aw;
main()
{
int j;
int check();
void allocate();
clrscr();
printf("\nEnter the no. of processes:");
scanf("%d",&n);
printf("\n Enter the processing time for:");
for(j=0;j<n;j++)
{
printf("\n Process ID %d :",j+1);
scanf("%d",&p[j]);
}
printf("\n Enter the Quantum Time of CPU:");
scanf("%d",&qt);
for(j=0;j<n;j++)
{
s[j]=0;
}
while(check()!=1)
{
for(i=0;i<n;i++)
{
if(s[i]!=1)
{
allocate();
}
}
}
printf("\n Waiting Time :");
for(j=0;j<n;j++)
{
printf("\n The process %d is %d",j+1,w[j]);
tw+=w[j];
}
printf("\n The total waiting time is :%6.2f",tw);
aw=tw/n;
printf("\n The average waiting time is :%6.2f",aw);
}

46
void allocate()
{
void cal_w_time(int);
if(p[i]>=qt)
{
p[i]=p[i]-qt;
cal_w_time(qt);
if(p[i]==0)
{
s[i]=1;
}
}
else
{
cal_w_time(p[i]);
p[i]=0;
s[i]=1;
}
}
void cal_w_time(int m)
{
int j;
for(j=0;j<n;j++)
{
if(s[j]!=1 && j!=i)
{
w[j]+=m;
}
}
}
int check()
{
int j;
for(j=0;j<n;j++)
{
if(s[j]==0)
{
return(0);
}
}
return(1);
}

47
Output

Enter the no of processes:3

Enter the processing time for:

Process ID 1 :9

Process ID 2 :2

Process ID 3 :5

Enter the Quantum Time of CPU:2

Waiting Time :
The process 1 is 7
The process 2 is 2
The process 3 is 8
The total waiting time is : 17.00
The average waiting time is : 5.67

Result

Thus the output of round robin scheduling program was executed successfully.

48
Ex. No. : 13 PRODUCER CONSUMER PROBLEM
Date :

Aim

To write a C shell program to implement producer consumer concept

Algorithm

¾ Start the program

¾ Initialize the values for loop producer the item into buffer

¾ Print the full and empty value for each item in the buffer

¾ Using the condition we have to empty the buffer

¾ Using for loop each item is entered one by one from buffer

¾ Stop the program

49
Program

#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf(“\nProducer Consumber Problem”);
printf("\n\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}

50
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}

51
Output
1.PRODUCER
2.CONSUMER
3.EXIT

ENTER YOUR CHOICE


1

producer produces the item1


ENTER YOUR CHOICE
1

producer produces the item2


ENTER YOUR CHOICE
1

producer produces the item3


ENTER YOUR CHOICE
1
BUFFER IS FULL
ENTER YOUR CHOICE
2

consumer consumes item3


ENTER YOUR CHOICE
2

consumer consumes item2


ENTER YOUR CHOICE
2

consumer consumes item1


ENTER YOUR CHOICE
2
BUFFER IS EMPTY
ENTER YOUR CHOICE
3

Result

Thus the output of producer consumer problem was verified successfully

52
Ex. No. : 14 PRIORITY SCHEDULING
Date :

Aim

To write a C program to implement priority scheduling concept

Algorithm

¾ Start the program

¾ Generate the schedule

¾ Create priority based on the weight age

¾ Base on the priority schedule will be conducted

¾ Implement priority scheduling

¾ Stop the program

53
Program

#include<malloc.h>
#include<stdio.h>
void line(int i)
{
int j;
for(j=1;j<=i;j++)
printf("_");
printf("\n");
}
struct process
{
int p_id,priority;
int etime,wtime,tatime;
};
struct process *p,temp;
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
{
clrscr();
printf("priority scheduling\n");
line(29);
printf("enter the number of processes:");
scanf("%d",&n);
p=(struct process *) calloc(n+1,sizeof(struct process));
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
{
printf("enter the execution time of process%d:",i-1);
scanf("%d",&p[i].etime);
printf("enter the priority of process%d:",i-1);
scanf("%d",&p[i].priority);

54
p[i].p_id=i;
}
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
if(p[i].priority > p[j+1].priority)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime+p[i].etime;
awtime+=p[i].wtime;
atatime+=p[i].tatime;
}
awtime=awtime/n;
atatime=atatime/n;
printf("\n schedule\n");
line(60);
printf("process\t\t execution\t waiting\tturnaround\n");
printf("idno\t\t time\t\t time\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d\n",p[i].p_id,p[i].etime,p[i].wtime,p[i].tatime);
line(60);
printf("average waiting time:\t%2f\n",awtime);
printf("average turn around time:\t%2f\n",atatime);
line(60);
getch();
return 0;
}

55
Output

[btech@localhost IT]$ cc prior.c


[btech@localhost IT]$ ./a.out
priority scheduling
_____________________________
enter the number of processes:5
enter the execution time of process0:5
enter the priority of process0:3
enter the execution time of process1:4
enter the priority of process1:2
enter the execution time of process2:2
enter the priority of process2:1
enter the execution time of process3:5
enter the priority of process3:3
enter the execution time of process4:1
enter the priority of process4:1
schedule
____________________________________________________________
process execution waiting turnaround
idno time time time
____________________________________________________________
3 2 0 2
2 4 2 6
1 5 6 11
5 1 11 12
4 5 12 17
____________________________________________________________
average waiting time: 6.200000
average turn around time: 9.600000
____________________________________________________________

Result

Thus the output of priority scheduling concept was verified successfully

56
Ex. No. : 15 THREAD CONCEPT
Date :

Aim

To write a java program for developing a thread application

Algorithm

¾ Start the program

¾ Create a thread with the help of super class using thread

¾ Read the input from input reader method from the I/O class.

¾ Compute the value of matrix

¾ Stop the program

57
Program for Multithreaded Matrix Multiplication

import java.lang.*;
import java.io.*;
class MatMulti extends Thread
{
static int in1[][];
static int in2[][];
static int out[][];
static int n=2;
int row;
MatMulti(int i)
{
row=i;
this.start();
}
public void run()
{
int i,j;
for(i=0;i<n;i++)
{
out[row][i]=0;
for(j=0;j<n;j++)
out[row][i]=out[row][i]+in1[row][j]*in2[j][i];
}
}
public static void main(String args[])
{
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the order of Matrix : ");
try
{
n=Integer.parseInt(br.readLine());
}
catch(Exception e){}
in1=new int[n][n];
in2=new int[n][n];
out=new int[n][n];

58
System.out.println("Enter the First Matrix : ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
try
{
in1[i][j]=Integer.parseInt(br.readLine());
}catch(Exception e){}
}
}
System.out.println("Enter the Second Matrix : ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
try
{
in2[i][j]=Integer.parseInt(br.readLine());
}catch(Exception e){}
}
}
MatMulti mat[]=new MatMulti[n];
for(i=0;i<n;i++)
mat[i]=new MatMulti(i);
try
{
for(i=0;i<n;i++)
mat[i].join();
}catch(Exception e){}
System.out.println("OUTPUT :");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
System.out.println(out[i][j]);
}
}

59
Output

Enter the order of the Matrix: 2

Enter the first Matrix:


1
2
3
4

Enter the second Matrix:


1
2
3
4

OUTPUT:
7
10
15
22

Result:

Thus the program is executed and output also verified.

60

S-ar putea să vă placă și