Wednesday, August 14, 2013

Producer-Consumer and Sleeping Barber problem in Operating System




//Producer-Consumer programming in Operating system
#include<stdio.h>
#include<conio.h>
int scustomer_entry(lastcust,dly)

{
delay(dly);
printf("customer %d just enter the queue \n",++lastcust);
return 0;
}
int teller_delay( int i){
    int k,p=i;
    switch(p)
    {
case 1:
    k=500;
    break;
case 2:
        k=2000;
        break;
case 3:
        k=1500;
        break;
default :
      k=0;
}
return k;
}
int  main()
{
    int lastcust=0,i,k,k1,k2,teller[3],last_served_cust=0;
    teller[1]=0;
    teller[2]=0;
    teller[3]=0;
    printf("Press any key to start \t\tand x to quit.\n");
    while(1)
    {
        k1=random(1000);
        customer_entry(++lastcust,k1);
        if(lastcust>=last_served_cust)
        {
            for(i=1;i<=3;i++)
            {
                k=random(2)+1;
                if(teller[k]==0)
                {
                    teller[k]=1;
                    printf("\nteller %d is free so customer %d entered \n",k,++last_served_cust);
                    if(k1<teller_delay(k))
                    {
                        k1=randim(1000);
                        customer_entry(++lastcust,k1);
                        break;
                    }
                if(k1<200)
                {
                    k1=random(1000);
                    continue;

                }
                teller[k]=0;
                break;
                }
            }
        }
        else
        {
            printf("There are no more customers in queue,tellers are sleeping .");

        }
    }
    return 0;
}




// Sleeping Barber problem
#include<stdio.h>
#include<conio.h>
int scustomer_entry(lastcust,dly)
{
delay(dly);
printf("customer %d just enter the queue \n",++lastcust);
return 0;
}
int teller_delay(i){
    int k,p=i;
    switch(p)
    {
case 1:
    k=500;
    break;
case 2:
        k=2000;
        break;
case 3:
        k=1500;
        break;
default :
      k=0;
}
return k;
}
void main()
{
    int lastcust=0,i,k,k1,k2,teller[3],last_served_cust=0;
    teller[1]=0;
    teller[2]=0;
    teller[3]=0;
    printf("Press any key to start \t\tand x to quit.\n");
    while(1)
    {
        k1=random(1000);
        customer_entry(++lastcust,k1);
        if(lastcust>=last_served_cust)
        {
            for(i=1;i<=3;i++)
            {
                k=random(2)+1;
                if(teller[k]==0)
                {
                    teller[k]=1;
                    printf("\nteller %d is free so customer %d entered \n",k,++last_served_cust);
                    if(k1<teller_delay(k))
                    {
                        k1=randim(1000);
                        customer_entry(++lastcust,k1);
                        break;
                    }
                if(k1<200)
                {
                    k1=random(1000);
                    continue;

                }
                teller[k]=0;
                break;
                }
            }
        }
        else
        {
            printf("There are no more customers in queue,tellers are sleeping .");

        }
    }
    return 0;
}


No comments:

Post a Comment