Saturday, 1 July 2017

Priority based cpu scheduling in c program - Priority based cpu scheduling in operating system

Aim:To  implement Priority based cpu scheduling algorithm.

Program:

 #include<stdio.h>
#include<conio.h>
void main()
{
                int bt[10],wt[10],tt[10],pt[10],i,j,n,stt,swt=0,temp1,temp2;
                float awt,att;
                clrscr();
                printf("enter no of processes \n");
                scanf("%d",&n);
                printf("enter %d process times \n",n);
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                printf("enter %d process priorites \n",n);
                for(i=0;i<n;i++)
                                scanf("%d",&pt[i]);
                printf("\n CPU time and priorities before sorting");
                for(i=0;i<n;i++)
                                printf("%d \t %d \n",bt[i],pt[i]);
                for(i=0;i<n;i++)
                {
                                for(j=i;j<=n-1;j++)
                                {
                                                if(pt[i]>pt[j])
                                                {
                                                                temp1=bt[i];
                                                                bt[i]=bt[j];
                                                                bt[j]=temp1;
                                                                temp2=pt[i];
                                                                pt[i]=pt[j];
                                                                pt[j]=temp2;
                                                }
                                }
                }
                printf("\n CPU times and priorities after sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t %d \n",bt[i],pt[i]);
                tt[0]=bt[0];
                wt[0]=0;
                stt=0;
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                stt+=tt[i];
                                swt+=wt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("\n CPU time \t priority \t wtime \t ttime \n");
                for(i=0;i<n;i++)
                                printf("%d \t %d \t %d \t %d \n",bt[i],pt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f",awt);
                printf("\n average turn around time is %f",att);
                getch();
}

Output:




Shortest Job First in c program - SJF in operating system

Aim: To Implement Shortest Job First cpu scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
                int n,bt[10],wt[10],tt[10],stt,swt=0,i,j,temp;
                float awt,att;
                clrscr();
                printf("enter no of process \n");
                scanf("%d",&n);
                printf("enter process cpu time \n");
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                printf("\n cpu time before sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                for(i=0;i<n;i++)
                                for(j=i;j<=n-1;j++)
                                {
                                                if(bt[i]>bt[j])
                                                {
                                                                temp=bt[i];
                                                                bt[i]=bt[j];
                                                                bt[j]=temp;
                                                }
                                }
                printf("\n cpu time after sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                wt[0]=0;
                tt[0]=stt=bt[0];
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                swt+=wt[i];
                                stt+=tt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("cpu time \t waiting \t turn around \n");
                for(i=0;i<n;i++)
                                printf("%d\t\t%d\t\t%d\n",bt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f turn around time is %f \n",awt,att);
                getch();
}

Output:





First Come First Serve in c program - First Come First Serve(FCFS) in operating systems

AIM:To implement First Come First Serve process scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
                int n,bt[10],wt[10],tt[10],stt,swt=0,i;
                float awt,att;
                clrscr();
                printf("enter no of process \n");
                scanf("%d",&n);
                printf("enter process cpu time \n");
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                wt[0]=0;
                tt[0]=stt=bt[0];
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                swt+=wt[i];
                                stt+=tt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("cpu time \t waiting \t turn around \n");
                for(i=0;i<n;i++)
                                printf("%d\t\t%d\t\t%d\n",bt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f turn around time is %f \n",awt,att);
                getch();
}

Output:





Friday, 13 May 2016

BruteForce Pattern Matching Algorithm C program

Aim : To implement Pattern Matching Technique using Brute Force Algorithm

Program:

#include<stdio.h>

#include<string.h>

char t[100],p[50];

void main()

{

int pos;

clrscr();

printf("Enter the Source String ");

scanf("%s",t);

printf("Enter the pattern ");

scanf("%s",p);

pos=brute_force();

if(pos==-1)

printf("%s pattern not found in text",p);

else

printf("%s pattern found at index %d",p,pos);

getch();

}

int brute_force()

{

int n,j,m,i;

n=strlen(t);

m=strlen(p);

for(i=0;i<n;i++)

{

j=0;

while(j<m && t[i+j]==p[j])

{

j++;

if(j==m)

return i+1;  //pattern found

}

}

return -1;  //pattern not found

}

Output :



Thursday, 5 May 2016

Boyre Moore Pattern Matching Algorithm in C

Aim : To implement Pattern Matching Technique using Boyre Moore Algorithm

Program:

#include<string.h>

#include<stdio.h>

char t[20],p[20]; // t and p for to store text and pattern

int l[256]={-1}; //to store number occurence of each letter

void main()

{

int pos,i;

clrscr();

printf("Enter the Source String ");

scanf("%s",t);

printf("Enter the Pattern ");

scanf("%s",p);

for(i=0;p[i]!='\0';i++)

l[p[i]]=i;

pos=boyer_pattern();

if(pos==-1)

printf("%s pattern is found in the text ",p);

else

printf("%s pattern found at index %d",p,pos);

getch();

}

int boyer_pattern()

{

int n,m,i,j;

n=strlen(t);//length of text

m=strlen(p);//length of pattern

i=m-1;

j=m-1;

while(i<n)//until all characters in text are finished

{

   if(t[i]==p[j])

   {

if(j==0)

return i+1;

else

{

i--;

j--;

}

   }

   else

   {

i=i+m-min(j,1+l[t[i]]);

j=m-1;

   }

}

return -1;

}

int min(int x, int y)

{

if(x<y)

return x;

else

return y;

}

Output:



Thursday, 13 November 2014

Krushkals Minimum cost spanning tree

Aim:  To implement krushkals algorithm to generate minimum spanning tree

Program:

#include<stdio.h>

int cost[10][10],i,j,k,n,a,b,ne,min=999,set[10],mincost=0,u,v;

void main()

{

clrscr();

printf("minimum cost spanning tree with krushkals");

printf("\nenter no of vertices");

scanf("%d",&n);

printf("\nenter cost matrix");

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

{

for(j=1;j<=n;j++)

{

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

}

}

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

set[i]=i;

ne=0;

while(ne<n-1)

{

min=999;

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

{

for(j=1;j<=n;j++)

{

if(cost[i][j]<min)

{

min=cost[i][j];

a=i;

b=j;

}

}

}

u=find(a);

v=find(b);

if(u!=v)

{

unin(u,v);

ne++;

printf("%d edges(%d,%d) is cost is %d\n",ne,a,b,min);

mincost=mincost+min;

}

cost[a][b]=cost[b][a]=999;

}

printf("\n total min cost spanning tree is %d",mincost);

getch();

}

int find(int x)

{

return set[x];

}

int unin(int i,int j)

{

int k;

for(k=1;k<=n;k++)

{

if(set[k]==j)

set[k]=i;

}

}


Input:

   the input is given in adjacent matrix  from for below graph




Output: