Honhar Engineer

Honhar Engineer
Thanks for visiting,stay connected for more updates !
Showing posts with label C Program. Show all posts
Showing posts with label C Program. Show all posts

Wednesday, January 8, 2014

C Program to Print India's Map

#include<stdio.h>
int main()
{
int a,b,c;
int count = 1;
for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )
for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
getch();
return 0;
}



NOTE: Above program may have some error if any then comment.....!!!

Thursday, October 24, 2013

C program to implement LRU page replacement algorithm

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6pVD0StzyQiEpErzg_QbbPJoYu-HHYOngSjZBKXz8OovaD5FtdjCfG1NW_0dT9FO-e020PsKo78-bdFST1nT1L7AQcrshihEaAMeaZMxiJ62B2v9aKIVuJ72DxGNHnBvfNphlLIsiTgrg/s1600/c+and+c%252B%252B+meansofmine.jpg
IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

AIM:
To write a c program to implement LRU page replacement algorithm
ALGORITHM :
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8.  Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
            scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
            {
                        c1=0;
                        for(j=0;j<f;j++)
                        {
                                    if(p[i]!=q[j])
                                    c1++;
                        }
                        if(c1==f)
                        {
                                    c++;
                                    if(k<f)
                                    {
                                                q[k]=p[i];
                                                k++;
                                                for(j=0;j<k;j++)
                                                printf("\t%d",q[j]);
                                                printf("\n");
                                    }
                                    else
                                    {
                                                for(r=0;r<f;r++)
                                                {
                                                            c2[r]=0;
                                                            for(j=i-1;j<n;j--)
                                                            {
                                                            if(q[r]!=p[j])
                                                            c2[r]++;
                                                            else
                                                            break;
                                                }
                                    }
                                    for(r=0;r<f;r++)
                                     b[r]=c2[r];
                                    for(r=0;r<f;r++)
                                    {
                                                for(j=r;j<f;j++)
                                                {
                                                            if(b[r]<b[j])
                                                            {
                                                                        t=b[r];
                                                                        b[r]=b[j];
                                                                        b[j]=t;
                                                            }
                                                }
                                    }
                                    for(r=0;r<f;r++)
                                    {
                                                if(c2[r]==b[0])
                                                q[r]=p[i];
                                                printf("\t%d",q[r]);
                                    }
                                    printf("\n");
                        }
            }
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
        7
        7       5
        7       5       9
        4       5       9
        4       3       9
        4       3       7
        9       3       7
        9       6       7
        9       6       2
        1       6       2
The no of page faults is 10
Any problem?? Drop a comment!

Program for FIFO Page Replacement Algorithm in C



https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6pVD0StzyQiEpErzg_QbbPJoYu-HHYOngSjZBKXz8OovaD5FtdjCfG1NW_0dT9FO-e020PsKo78-bdFST1nT1L7AQcrshihEaAMeaZMxiJ62B2v9aKIVuJ72DxGNHnBvfNphlLIsiTgrg/s1600/c+and+c%252B%252B+meansofmine.jpg
IMPLEMENTATION OF FIFO PAGE REPLACEMENT ALGORITHM
AIM
To write a c program to implement FIFO page replacement algorithm
ALGORITHM
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
            printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
            printf("\n ENTER THE PAGE NUMBER :\n");
            for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
            printf("\n ENTER THE NUMBER OF FRAMES :");
            scanf("%d",&no);
for(i=0;i<no;i++)
            frame[i]= -1;
                        j=0;
                        printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
                        {
                                    printf("%d\t\t",a[i]);
                                    avail=0;
                                    for(k=0;k<no;k++)
if(frame[k]==a[i])
                                            {
                                               avail=1;
                                               count++;
                                            }
                                    if (avail==0)
                                    {
                                                frame[j]=a[i];
                                                j=(j+1)%no; 
                                                for(k=0;k<no;k++)
                                                printf("%d\t",frame[k]);
}
                                    printf("\n");
}
                        printf("Page Fault Is %d",count);
                        return 0;
}
OUTPUT:
ENTER THE NUMBER OF PAGES:  20
ENTER THE PAGE NUMBER :       7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES :3
      ref string       page frames
7               7       -1      -1
0               7       0       -1
1               7       0       1
2               2       0       1
0
3               2       3       1
0               2       3       0
4               4       3       0
2               4       2       0
3               4       2       3
0               0       2       3
3
2
1               0       1       3
2               0       1       2
0
1
7               7       1       2
0               7       0       2
1               7       0       1
Page Fault Is 5
Any problem?? Drop a comment!

Thursday, October 3, 2013

Program of comparison between FCFS,SJF,PRIORTY & ROUND-ROBIN CPU Scheduling Algorithm

#include<stdio.h>
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
//fcfs
int temp=0,i,PR3[4],tq,B1[4],TAT1[4],B2[4],B3[4],B4[4],W1[4],SWT1=0,STT1=0;
float AWT1=0.0,ATT1=0.0;
char P1[4];

for(i=0;i<3;i++)
{
cout<<"Enter process names with their Burst Time and Priority(Ashish)\n";
cin>>P1[i]>>B1[i]>>PR3[i];
B2[i]=B1[i];
B3[i]=B1[i];
B4[i]=B1[i];
}
cout<<"\nEnter time slice for round robin\n";
cin>>tq;

W1[0]=0;
for(i=1;i<3;i++)
W1[i]=W1[i-1]+B1[i-1];

for(i=0;i<3;i++)
TAT1[i]=W1[i]+B1[i];

for(i=0;i<3;i++)
{
SWT1=SWT1+W1[i];
STT1=STT1+TAT1[i];
}
AWT1=(float)SWT1/3;
ATT1=(float)STT1/3;

//sjf
int W2[4],TAT2[4],SWT2=0,STT2=0;
float AWT2=0.0,ATT2=0.0;

for(int j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(B2[i]>B2[i+1])
{temp=B2[i];
B2[i]=B2[i+1];
B2[i+1]=temp;}
}
}
W2[0]=0;
for(i=1;i<3;i++)
W2[i]=W2[i-1]+B2[i-1];

for(i=0;i<3;i++)
TAT2[i]=W2[i]+B2[i];

for(i=0;i<3;i++)
{
SWT2=SWT2+W2[i];
STT2=STT2+TAT2[i];
}
AWT2=(float)SWT2/3;
ATT2=(float)STT2/3;

//priority

int W3[4],TAT3[4],SWT3=0,STT3=0;
float AWT3=0.0,ATT3=0.0;
for(j=0;j<3;j++)
{
for(i=0;i<2-j;i++)
{
if(PR3[i]>PR3[i+1])
{temp=PR3[i];
PR3[i]=PR3[i+1];
PR3[i+1]=temp;
temp=B3[i];
B3[i]=B3[i+1];
B3[i+1]=temp;
}
}
}
W3[0]=0;
for(i=1;i<3;i++)
W3[i]=W3[i-1]+B3[i-1];

for(i=0;i<3;i++)
TAT3[i]=W3[i]+B3[i];

for(i=0;i<3;i++)
{
SWT3=SWT3+W3[i];
STT3=STT3+TAT3[i];
}
AWT3=(float)SWT3/3;
ATT3=(float)STT3/3;

//round
int st[10],wt[10], tat[10];
int  count=0, swt=0, stat=0, sq=0,n=3;
float AWT4=0.0, ATT4=0.0;
for(i=0;i<3;i++)
st[i]=B4[i];


while(1)
{
for(i=0, count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
{
st[i]=st[i]-tq;
}
else
if (st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-B4[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
AWT4=(float)swt/n;
ATT4=(float)stat/n;

//calculation

cout<<"FCFS- Average waiting time is "<<AWT1<<" and Average turn around time is "<<ATT1<<"\n";
cout<<"SJF- Average waiting time is "<<AWT2<<" and Average turn around time is "<<ATT2<<"\n";
cout<<"PRIORITY- Average waiting time is "<<AWT3<<" and Average turn around time is "<<ATT3<<"\n";
cout<<"ROUND ROBIN- Average waiting time is "<<AWT4<<" and Average turn around time is "<<ATT4<<"\n";

if(AWT1<AWT2 && AWT1<AWT3 && AWT1<AWT4)
{
cout<<"FCFS Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT1<<" and Average Turn Around time= "<<ATT1;
}
else if(AWT2<AWT1 && AWT2<AWT3 && AWT2<AWT4)
{
cout<<"SJF Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT2<<" and Average Turn Around time= "<<ATT2;

}
else if(AWT3<AWT1 && AWT3<AWT2 && AWT3<AWT4)
{
cout<<"Priority Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT3<<" and Average Turn Around time= "<<ATT3;

}
else
{
cout<<"Round Robin Scheduling has least average waiting time and turn around time\n";
cout<<"Average Waiting time= "<<AWT4<<" and Average Turn Around time= "<<ATT4;

}
getch();
}

Sunday, August 11, 2013

Round-Robin Scheduling Algorithm

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer,count,pt[10],rt,i,j,totwt=0,t,n=3,found=0,m;
float avgwt;
clrscr();
for(i=0;i<n;i++)
{
printf("enter the process name : ");
scanf("%s",&p[i]);
printf("enter the processing time : ");
scanf("%d",&pt[i]);
}
printf("enter the time quantum : ");
scanf("%d",&timer);
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}
while(i<n);
count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{
 wt[i]=wt[found]-(count*timer);
count=0; found=0;
}
}
for(i=0;i<m;i++)
{
totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\ntotal waiting time %d\n",totwt);
printf("total avgtime %f",avgwt);
getch();
}