Showing posts with label LAB C PROGRAM. Show all posts
Showing posts with label LAB C PROGRAM. Show all posts

Friday, 10 October 2014

Concurrent checkpoint

/*CSEMATTER.BLOGSPOT.IN
Program-to implement concurrent checkpoint*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int main()
{
    int pr[10],p[10],n,i,j,count,temp;

    printf("Enter the no. of processes: ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",i+1);
        scanf("%d",&pr[i]);
    }

    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        count=0;
        for(j=0; j<n; j++)
        {
            if(i!=j)
                printf("P%d is sending request to P%d \n",p[i],p[j]);
             else
                 continue;
        }
        for(j=0; j<n; j++)
        {
            if(i!=j)
            {
                printf("P%d is sending ack to P%d \n",p[j],p[i]);
                count++;
            }
            else
                continue;
        }
        if(count==n-1)
        {
            printf("Ack from all the processes has been received \n");
            printf("P%d is forming a Consistent Checkpoint point \n",p[i]);
             delay(10000);
            for(j=0; j<n; j++)
            {
                if(i!=j)
                    printf("P%d sending release msg to P%d \n",p[i],p[j]);
                else
                    continue;
            }
            printf("\n \n");
        }
        else
            printf("Ack from all the processes not received hence Process P%d cann't form a consistent checkpoint \n \n",p[i]);
    }
    getch();

}

/* OUTPUT:

Enter the no. of processes: 3
Enter the no. of processes: 3
Enter the priority of processes
Priority of process P1 :
3
Priority of process P2 :
2
Priority of process P3 :
1
P3 is sending request to P2
P3 is sending request to P1
P2 is sending ack to P3
P1 is sending ack to P3
Ack from all the processes has been received
P3 is forming a Consistent Checkpoint point
P3 sending release msg to P2
P3 sending release msg to P1


P2 is sending request to P3
P2 is sending request to P1
P3 is sending ack to P2
P1 is sending ack to P2
Ack from all the processes has been received
P2 is forming a Consistent Checkpoint point
P2 sending release msg to P3
P2 sending release msg to P1


P1 is sending request to P3
P1 is sending request to P2
P3 is sending ack to P1
P2 is sending ack to P1
Ack from all the processes has been received
P1 is forming a Consistent Checkpoint point
P1 sending release msg to P3
P1 sending release msg to P2    */

Thursday, 9 October 2014

bayzentine program

/*CSEMATTER.BLOGSPOT.IN
Program-implement bayzentine program in c*/

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>


int main()
{
int i,j,p[20][20],n,nf,cnt0=0,cnt1=1;
printf("Enter the no. of processes:");
scanf("%d",&n);
printf("Enter the process no. of faulty process:");
scanf("%d",&nf);
for(i=0;i<n;i++)
{
if(i==nf)
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=random(2);
else
p[i][j]=NULL;}
}
else
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=1;
else
p[i][j]=NULL;
}}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[i][j]==1)
++cnt1;
else if(p[i][j]==0)
++cnt0;
else
continue;
}
if(cnt1>cnt0)
continue;
else
{
printf("Processes are not synchronised \n \n");

}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",p[i][j]);
}
printf("\n");
}
getch();
}

/*OUTPUT:
Enter the no. of processes:3
Enter the process no. of faulty process:2
Processes are not synchronised

    1   1
1       1
1   0          */

Tuesday, 7 October 2014

MAEKAWA ALGORITHM DISTRIBUTED SYSTEM

/*CSEMATTER.BLOGSPOT..IN
Program name-Maekawa algorithm in distributed system*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void process1();
void process2();
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int p[10],pr[10],n,i,j,count=0,ran_pr,temp,max_lmt,min_lmt;

int main()
{
    printf("Enter the no. of processses \n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
      do
       {
           ran_pr=random(n);
       }
       while(ran_pr!=0);
    ran_pr=3;
    printf("Control process is P%d \n",ran_pr);
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",p[i]);
        scanf("%d",&pr[i]);
    }
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n-1; i++)
    {
        if(p[i]<ran_pr)
        {
            max_lmt=ran_pr;
            process1();
        }
        else
        {
            min_lmt=ran_pr+1;
            process2();
        }
    }

    getch();
}

void process1()
{
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");
    if(count==(ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

void process2()
{
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");

    if(count==(n-ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

/*OUTPUT:
Enter the no. of processses 5
Control process is P3
Enter the priority of processes
Priority of process P1 :2
Priority of process P2 :1
Priority of process P3 :5
Priority of process P4 :3
Priority of process P5 :4

Process P2 is sending request to P1
Process P2 is sending request to P3
Process P1 is acknowledging P2
Process P3 is acknowledging P2
Process P2 is entering CS
Process P2 is sending reply to P1
Process P2 is sending reply to P3

Process P1 is sending request to P2
Process P1 is sending request to P3
Process P2 is acknowledging P1
Process P3 is acknowledging P1
Process P1 is entering CS
Process P1 is sending reply to P2
Process P1 is sending reply to P3

Process P4 is sending request to P5
Process P5 is acknowledging P4
Process P4 is entering CS
Process P4 is sending reply to P5

Process P5 is sending request to P4
Process P4 is acknowledging P5
Process P5 is entering CS
Process P5 is sending reply to P4

Process P3 is sending request to P1
Process P3 is sending request to P2
Process P1 is acknowledging P3
Process P2 is acknowledging P3
Process P5 is entering CS
Process P3 is sending reply to P1
Process P3 is sending reply to P2
*/

Wednesday, 4 September 2013

BINARY SEARCH C PROGRAM


/*CSEMATTER.BLOGSPOT.IN

Program: Program in C to perform binary search*/

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],num,count,mid,beg,end,term,found=0;
clrscr();
printf("enter the no. of elements to enter");
scanf("%d",&num);
printf("enter the numbers");
for(count=0;count<num;count++)
{scanf("%d",&arr[count]);}
printf("enter the term to be searched");
scanf("%d",&term);
beg=0;
end=num;
mid=(beg+end)/2;
while(beg<=end)
{
mid=(beg+end)/2;
if(term<arr[mid])
{end=mid-1;
}
if(term>arr[mid])
{
beg=mid+1;
}
if(term==arr[mid])
{
printf("element found at %d position in array",mid+1);
found=1;
break;
}
}
if(found==0)
{
printf("term not found");
}
getch();
}

/*OUTPUT:
enter the no. of elements to enter 6
enter the numbers
10
42
48
56
78
99
enter the term to be searched 48
element found at 3 position in array*/

SEQUENTIAL SEARCH C PROGRAM

ALGORITHM LAB PROGRAMS

/*CSEMATTER.BLOGSPOT.IN
SEQUENTIAL SEARCH PROGRAM

Program:Program in C to perform sequential search*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],num,count,term,found=0;
clrscr();
printf("enter the no. of elements to enter");
scanf("%d",&num);
printf("enter the numbers");
for(count=0;count<num;count++)
{scanf("%d",&arr[count]);}
printf("enter the term to be searched");
scanf("%d",&term);
for(count=0;count<num;count++)
{
if(arr[count]==term)
{printf("term found at %d position in array",count+1);
found=1;
break;
}
}
if(found==0)
printf("term no found");
getch();
}

/*enter the no. of elements to enter 5
enter the numbers
10
94
66
78
12
enter the term to be searched 78
term found at 4 position in array*/



Monday, 2 September 2013

PRIORITY BASED CPU SCHEDULING C PROGRAM



/*CSEMATTERBLOGSPOT.IN

Write a program in C to implement priority based CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
    int gd=DETECT,gm,i,j,k,n,b,count=0,arr[20],prio[20],bt[20],wt[20],tat[20];
    float avg_wt,avg_tat,sum_wt=0.0,sum_tat=0.0;
    initgraph(&gd,&gm,"C:\TC\BIN");
    printf("Enter the no. of processes  ");
    scanf("%d",&n);
    printf("Enter the burst time of processes \n");
    for(i=0;i<n;i++)
        scanf("%d",&bt[i]);
    printf("Enter priority of processes \n");
    for(i=0;i<n;i++)
    {    scanf("%d",&prio[i]);
        arr[i]=prio[i];}
    for(i=0;i<=(n-2);i++)
    {for(j=0;j<=((n-2)-i);j++)
      {   if(arr[j]>arr[j+1])
          {
              b=arr[j];
              arr[j]=arr[j+1];
              arr[j+1]=b;
          }} }
    wt[0]=0;
    tat[0]=0;
    for(i=0;i<n;i++)
    {   for(j=0;j<n;j++)
        {      if(arr[i]!=prio[j])
               count=count+1;
           else
               break;}
        wt[i+1]=bt[count]+wt[i];
        tat[i+1]=bt[count]+tat[i];
        count=0; }
    for(i=0;i<n;i++)
    sum_wt=sum_wt+wt[i];
    for(i=0;i<(n+1);i++)
    sum_tat=sum_tat+tat[i];
    avg_wt=(sum_wt/(float)n);
    avg_tat=(sum_tat/(float)n);
    printf("Process_no. Burst_Time Priority Waiting_Time Turn_Around_Time \n");
    for(i=0;i<n;i++)
    {
    printf("P%d\t\t%d\t%d\t\t%d\t\t%d\n",i,bt[i],prio[i],wt[i],tat[i+1]);}
    printf("Average waiting time is: %f \n",avg_wt);
    printf("Average turn around time is: %f",avg_tat);
    printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
    rectangle(2,300,270,320);
    getch();
    closegraph();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
Enter priority of processes
5 1 2 4 3

Process_no.  Burst_Time  Priority  Waiting_Time  Turn_Around_Time
P1 8    1 0 8
P2 4    2 8 12
P4 10    3 12 22
P3 6    4 22 28
P0 12    5 28 40

Average waiting time is 14.00000
Average turn around time is 22.00000

GANTT CHART
 ||P1|| ||P2||  ||P4||   ||P3||   ||P0||
0      8       12 22 28  40*/

PREEMPTIVE SCHEDULING C PROGRAM


/* CSEMATTER.BLOGSPOT.IN
C Program for preemptive cpu scheduling*/
#include<stdio.h>
#include<conio.h>
int main()
{
    int min(int, int []);
    int temp,time,temp4,c,n,i,j,a1[20],wt[20],pr[20],bt[20],gc[20],arr_ti[20],tat[20],sum_bt=0,a=0,p=0,temp1,count;
    printf("Enter the no. of proceses");
    scanf("%d",&n);
    printf("Enter the burst time of processes");
    for(i=0; i<n; i++)
    {scanf("%d",&bt[i]);
      sum_bt=sum_bt+bt[i];
       a1[i]=bt[i];}
    for(i=0; i<n; i++)
    pr[i]=i+1;
    printf("Enter the arrival time");
    for(i=0; i<n; i++)
        scanf("%d",&arr_ti[i]);
    for(i=0; i<(n-2); i++)
    {  for(j=i; j<((n-2)-i); i++)
        {
            if(arr_ti[j]>arr_ti[j+1])
            {   temp=arr_ti[j];
                arr_ti[j]=arr_ti[j+1];
                arr_ti[j+1]=temp;
                temp=a1[j];
                a1[j]=a1[j+1];
                a1[j+1]=temp;
                temp=pr[j];
                pr[j]=pr[j+1];
                pr[j+1]=temp;
            }}}
    for(i=0; i<n; i++)
    {
        wt[i]=0;
        tat[i]=0;
    }
    for(i=0; i<=sum_bt; i++)
    {
        a++;
        if(a<5)
        {
            c=min(a,a1)+1;
            gc[i]=c;
        }
        else
        {
            c=min(5,a1)+1;
            gc[i]=c;
        }  }
    for(i=0; i<n; i++)
    {
        temp=i+1;
        time=0;
        temp1=0;
        for(j=0; j<sum_bt; j++)
        {time++;
            if(temp==gc[j])
            {
             if(temp1==0)
            {
            wt[i]=wt[i]+time-(i+1);
            temp1=j+2;
            }
            else
            {
            wt[i]=wt[i]+(j+1)-temp1;
            temp1=j+2;
            }}}}

    for(i=0; i<n; i++)
    {
        temp=0;
        for(j=0; j<sum_bt; j++)
        {
            if(i+1==gc[j])
            {
                if(temp==0)
                {
                    tat[i]=tat[i]+bt[i]+j-arr_ti[i];
                    temp=j+1;
                }
                else
                {
                    tat[i]=tat[i]+j-temp;
                    temp=j+1;
                }  }}}
     //printing the output

    printf("DATA IS AS FOLLOWS: \n");
    printf("Process\t Burst Time\t Arrival Time\t Waiting Time\t Turn Around Time\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t %d\t\t\t %d\t\t %d\t\t %d\n",pr[i],bt[i],arr_ti[i],wt[i],tat[i]);
    }
    printf("\n \n");
    printf("GANTT CHART: \n");
    c=gc[0];
    printf("  ||P%d\t",gc[0]);
    for(i=1;i<sum_bt;i++)
    {
        if(gc[i]!=c)
        {printf(" ||P%d|| ",gc[i]);
        c=gc[i];}
    }
    printf("\n0\t");
    c=gc[0];
    for(i=1;i<=sum_bt;i++)
    {    p=p+1;
         if(gc[i]!=c)
        {printf("%d \t",p);
        c=gc[i];
        }
        }
    getch();
}


int min(int a,int a1[20])
{
    int i,count=0;
    int temp=a1[0];
    if(a==1)
    { temp--;}
    else
    {
        for(i=0; i<a; i++)
        {
            if(temp>a1[i])
            {
                temp=a1[i];
                count=i;}}
        temp--;}
    if(temp==0)
    {temp=500;}
    a1[count]=temp;
    return(count);  /* returning the index of array whose value is minimum*/
}

/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5   2   2   3   6
Enter the arrival time
0   1   2   3   4
DATA IS AS FOLLOWS:
Process  Burst Time  ArrivalTime  Waiting Time Turn AroundTime
1           5           0           7               12
2           2           1           0               2
3           2           2           1               3
4           3           3           2               5
5           6           4           8               14
GANTT CHART:
  ||P1||    ||P2||  ||P3||  ||P4||  ||P1||  ||P5||
0        1        3        5       8      12      18*/

SJF CPU SCHEDULING C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
program in C to implement shortest job first CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
int main()
{
int p[10],temp;
int tot=0,wt[10],pt[10],tat[10],i,j,n,temp1,tot1=0;
float avg=0,avg1=0;
printf("enter no of processes:");
scanf("%d",&n);
printf("enter process time");
for(i=0;i<n;i++)
{
scanf("%d",&pt[i]);
p[i]=i;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
temp=p[i];
p[i]=p[j];
p[j]=temp;

}
}
}
wt[0]=0;
for(i=1;i<=n;i++)
{
wt[i]=wt[i-1]+pt[i-1];

tot=tot+wt[i];
}
for(i=0;i<n;i++)
   {tat[i]=wt[i]+pt[i];
    tot1=tot1+tat[i];}
avg=(float)tot/n;
avg1=(float)tot1/n;
printf("p_no.\t P_time\t Waiting_Time\t  Turn_Around_Time\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t %d\n",p[i],pt[i],wt[i],tat[i]);
printf("avg waiting time=%f\n",avg);
printf("avg turn around time=%f",avg1);
printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
        getch();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
P_no.  P_Time  Waiting_Time  Turn_Around_Time
2 4     0 4
3 6     4 10
1 8     10 18
4       10        18 28
0 12     28 40

Average waiting time is 20.000000
Average turn around time is 20.000000

GANTT CHART
P0 P1 P2 P3 P4
0 4 10 18 28*/

ROUND ROBIN OS C PROGRAM

/*CSEMATTERBLOG
PROGARM TO IMPLEMENT ROUND ROBIN ALGO*/
#include<stdio.h>
#include<conio.h>
int main()
{
    int bt[20],gc[20],wt[20],tat[20],bt1[20],st[20],ts,n,i,j,k,count=0,count1,sum_bt=0,tq;
    int swt=0,stat=0,temp,sq=0,c,p=0;
float awt=0.0,atat=0.0;
    printf("Enter the nuo. of processs");
    scanf("%d",&n);
    printf("Enter the burst time");
    for(i=0; i<n; i++)
        {scanf("%d",&bt[i]);
        bt1[i]=bt[i];
        st[i]=bt[i];}
    printf("Enter the time slice");
    scanf("%d",&ts);
    tq=ts;
    for(i=0; i<n; i++)
    sum_bt=sum_bt+bt[i];
        for(k=0; k<n; k++)
    {
       do
        {
            for(i=0; i<n; i++)
            {
                if(bt[i]>=ts)
                {
                    for(j=count; j<(count+ts); j++)
                        gc[j]=i+1;
                    count+=ts;
                    bt[i]=bt[i]-ts;
                }
                else
                {
                    for(j=count; j<=(count+bt[i]); j++)
                        gc[j]=i+1;
                    count+=bt[i];
                    bt[i]=0;
                }
            }
        }while(bt[k]!=0);
    }

 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]-bt1[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d %d %d %d\n",i+1,bt1[i],wt[i],tat[i]);
printf("Avg wait time is %f Avg turn around time is %f",awt,atat);
 printf("\n \n");
    printf("GANTT CHART: \n");
    c=gc[0];
    printf("  ||P%d\t",gc[0]);
    for(i=1;i<sum_bt;i++)
    {
        if(gc[i]!=c)
        {printf(" ||P%d|| ",gc[i]);
        c=gc[i];}
    }
    printf("\n0\t");
    c=gc[0];
    for(i=1;i<=sum_bt;i++)
    {    p=p+1;
         if(gc[i]!=c)
        {printf("%d \t",p);
        c=gc[i];
        }}
   getch();
}
/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5   1   2   2   3
Enter the time slice 2
Process  Burst Time   Waiting Time Turn AroundTime
1           5           8               13
2           1           2               3
3           2           3               5
4           2           5               7
5           3           9               12
GANTT CHART:
  ||P1||    ||P2||  ||P3||  ||P4||  ||P5||  ||P1||  ||P5||  ||P1||
0        2        3        5      7       9       11      12      13*/

PRODUCER CONSUMER C PROGRAM

/*CSEMATTER.BLOGSPOT.IN
C Program to implement producer consumer problem*/
#include<stdio.h>
#include<conio.h>
int main()
{
int producer(int [],int,int);
int consumer(int [],int,int);
int buffer[20],max,n,a=0,a1=0,ch,cons=0;
 printf("\n\n\n\tEnter Stack Size :");
 scanf("%d",&max);
do
 {
   printf("\n\t\tCHOICES\n\t\t\n\t1.Producer\n\t2.Consumer\n\t3.Exit\nEnter your choice :\n ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
  {
   if(a==max & cons==0)
   {printf("STACK FULL...\n");
            break;}
     else if(cons==2)
     {
         printf("Consumer has not consumed all item yet");
         break;
    }
    else
    {a=producer(buffer,max,a);
     break;}
    }
    case 2:
  {if(a!=max)
  {printf("STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE\n");
        break;}
      if(a==max)
     {a1=consumer(buffer,max,a1);
      if(a1==max)
      {a=0;
      cons=0;}
      else
      cons=2;}
     break;}
     case 3:
     break;
    }
 }
while(ch!=3);
getch();
}
 int producer(int buffer[20],int max,int a)
{
int i,n;
int counter=a;
printf("Enter the no. of items to be produced");
scanf("%d",&n);
for(i=a;i<a+n;i++)
{
printf("Enter the item to be produced");
scanf("%d",&buffer[i]);
counter=counter+1;
}
return(counter);
}
int consumer(int buffer[20],int max,int a1)
{
int i,n;
int counter1=a1;
printf("Enter the no. of items to be consumed");
scanf("%d",&n);
for(i=a1;i<a1+n;i++)
{
printf("Consumed item is: %d \n",buffer[i]);
buffer[i]=0;
counter1=counter1+1;
}
return(counter1);}
/*OUTPUT
     Enter Stack Size :5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Enter the no. of items to be produced3
Enter the item to be produced1
Enter the item to be produced2
Enter the item to be produced3
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
STACK NOT FULL YET...PRODUCER TURNS TO PRODUCE
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Enter the no. of items to be produced2
Enter the item to be produced4
Enter the item to be produced5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
STACK FULL...
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
Enter the no. of items to be consumed3
Consumed item is: 1
Consumed item is: 2
Consumed item is: 3
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 1
Consumer has not consumed all item yet
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
 2
Enter the no. of items to be consumed2
Consumed item is: 4
Consumed item is: 5
                CHOICES
        1.Producer
        2.Consumer
        3.Exit
Enter your choice :
3 */

READER WRITER C PROGRAM

/*CSEMATTER.BLOGSPOT.IN
C Program to implement reader writer problem*/
#include<stdio.h>
#include<conio.h>
int ch,write=0,readcount=0;
int main()
{
void writerin();
void writerout();
void readerin();
void readerout();
 do
 {
   printf("\n\t\tCHOICES\n\t\t\n\t1 Writer in\n\t2 Writer out\n\t3.Reader in");
   printf("\n\t3..Reader out\n\t5.exit\nEnter your choice :\n ");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
    {
     writerin();
    }
    case 2: 
    {
       writerout();   
     }
     case3:
 {
         readerin();
 }
     case4:
       {readerout();
       }
     case 5:
     break;
    }
 }
while(ch!=5);
getch();
}
void readerout()
{
if(write==1 && readcount>0)
      {
        readcount--;
 }
 else if(write==1 && readcount==0)
        {
      write=0;
         }
  else
 printf("no reader is reading");
}
void readerin()
{
if(write==0 && readcount==0)
      {
      readcount++;
      write=1;
      }
else if(write==1 && readcount>0)
{
readcount++;
}
else
{
printf("writer is writing");
}
}

void writerin()
{
if(readcount==0)
{
write=1;
printf("writer is writing\n");
}
else
{
printf("%d reader are reading..wait for finish\n",readcount);
}
}
void writerout()
{
if(write==1)
{
write=0;
printf("writer is exiting\n");
}
else
{
printf("no writer is writing\n");}
}
}

FIFO PAGE REPLACEMENT C PROGRAM


/* CSEMATTERBLOG

C PROGRAM TO IMPLEMENT FIFO PAGE REPLACEMENT ALGO*/
#include<stdio.h>
#include<conio.h>
void main()
{
int frame[20],pages[20],n,i,j,k,fr,count=0,avail;
clrscr();
printf("enter the no. of pages");
scanf("%d",&n);
printf("enter the page sequence");
for(i=0;i<n;i++)
scanf("%d",&pages[i]);
printf("enter the no. of frames");
scanf("%d",&fr);
for(i=0;i<fr;i++)
frame[i]=-1;
j=0;
printf("ref string \t page frame\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",pages[i]);
avail=0;
for(k=0;k<fr;k++)
if(frame[k]==pages[i])
avail=1;
if(avail==0)
{
frame[j]=pages[i];
j=(j+1)%fr;
count++;
for(k=0;k<fr;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("page fault is :%d",count);
getch();
}
/*OUTPUT
enter the no. of pages12
enter the page sequence2
3
4
5
2
3
6
2
3
4
5
6
enter the no. of frames3
ref string       page frame
2               2       -1      -1
3               2       3       -1
4               2       3       4
5               5       3       4
2               5       2       4
3               5       2       3
6               6       2       3
2
3
4               6       4       3
5               6       4       5
6
page fault is :9
*/

FCFS DISK SCHEDULING C PROGRAM

/*CSEMATTER.BLOGSPOT.IN

C PROGRAM TO IMPLEMENT FCFS DISK SCHEDULING  ALGO*/
#include<stdio.h>
#include<conio.h>
void main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float aver;
clrscr();
printf("enter the max range of disk");
scanf("%d",&max);
printf("enter the size of queue request");
scanf("%d",&n);
printf("enter the queue");
for(i=1;i<=n;i++)
{scanf("%d",&queue[i]);}
printf("enter the initial head position");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("move is from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("total seek time is%d\n",seek);
aver=seek/(float)n;
printf("avrage seek time is %f\n",aver);
getch();
}
/*OUTPUT:
enter the max range of disk180
enter the size of queue request8
enter the queue87
170
40
150
36
72
66
15
enter the initial head position60
move is from 60 to 87 with seek 27
move is from 87 to 170 with seek 83
move is from 170 to 40 with seek 130
move is from 40 to 150 with seek 110
move is from 150 to 36 with seek 114
move is from 36 to 72 with seek 36
move is from 72 to 66 with seek 6
move is from 66 to 15 with seek 51
total seek time is557
avrage seek time is 69.625000
*/