Documente Academic
Documente Profesional
Documente Cultură
UNIVERSITY
CHENNAI 600 095
Name :
:
Register No.
Semester : I
Bonafide Certificate
by _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 1st Semester
Ex. Page
Date Name of the Experiment Signature
No. No.
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
Algorithm
¾ 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.
¾ 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
Algorithm
¾ 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
¾ Enter the element which is going to be deleted at front, middle or end and display
the list.
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
Algorithm
¾ If the choice is 2, call the delete function and display the queue.
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;
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
13
Ex. No. : 4 QUEUE IMPLEMENTATION USING LINKED LIST
Date :
Aim
To create a data structure for queue using dynamic memory allocation and perform
Algorithm
¾ If the choice is 2, call the delete function and display the queue.
14
Program
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
struct qnode
{
int data;
struct qnode *next;
};
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
Algorithm
¾ Start
¾ 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 5 elements: 45 23 67 33 5
Result
20
Ex. No. : 6 IMPLEMENTATION OF MERGE SORT
Date :
Aim
Algorithm
¾ Check the cognition that the initial value is generated the assumed one.
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
Result
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
¾ 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.
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
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
Algorithm
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
Result
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
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
Result
37
Ex. No. : 10 IMPLEMENTATION OF BINARY SEARCH
Date :
Aim
Algorithm
¾ If the element found print it location otherwise print element is not found
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
40
Ex. No. : 11 SHORTEST JOB FIRST SCHEDULING
Date :
Aim
Algorithm
¾ Select the process which have shortest burst will execute first
¾ If two process have same burst length then FCFS scheduling algorithm used
¾ Start with the first process from it’s selection as above and let other process to be in queue
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
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
¾ Run the process at regular intervals [cyclic] until all process finished
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
Process ID 1 :9
Process ID 2 :2
Process ID 3 :5
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
Algorithm
¾ Initialize the values for loop producer the item into buffer
¾ Print the full and empty value for each item in the buffer
¾ Using for loop each item is entered one by one from buffer
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
Result
52
Ex. No. : 14 PRIORITY SCHEDULING
Date :
Aim
Algorithm
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
Result
56
Ex. No. : 15 THREAD CONCEPT
Date :
Aim
Algorithm
¾ Read the input from input reader method from the I/O class.
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
OUTPUT:
7
10
15
22
Result:
60