Monday, 21 October 2013

reverse

echo "Enter the string"
read str
reverse=`echo $str | rev`

if test $str = $reverse
then
echo "palindrome"
else
echo "Non-palindrome"
fi

prime

set -vx
echo "enter no"
read no

flag=0

n=` expr $no - 1 `

while [ $n -ne 1 ]
do
  
 res=` expr $no % $n`
 
   if [ $res -eq 0 ]
then  
        flag=1
     break
   fi

  n=` expr $n -1 `

done

if [ $flag -ne 1 ]
then
  echo "prime"
else
echo "not prime"
fi
 

exp

#!/bin/sh


a=10 b=20

echo `expr $a + $b`

echo `expr $a - $b`

echo `expr $a \* $b`

echo `expr $a / $b`

echo `expr $a % $b`

fibo

#!/bin/sh
echo "Enter how many no.s you want?"
read n
echo "Fibonacci Series is::"
pr=1
ne=0
cu=0
echo $ne
echo $pr

i=2
while [ $i -lt $n ]
do
ne=`expr $pr + $cu`
echo $ne
cu=$pr
pr=$ne
i=`expr $i + 1`
done

fact

set  -vx
echo "enter no"
read  n
fact=1

if [ $n -eq 0 ]
then
 fact=0
fi

while [ $n -gt 0 ]
do
fact=`expr $fact \* $n `
n=`expr $n - 1 `
done

echo "facto:$fact"

banker_banker

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

typedef struct proc
{
    int rm[10];
    int ra[10];
    int rn[10];
    int pf;
    int pid;
}proc;

proc p[25];
int ar[10];
int np,nr;

int banker(proc p1[25],int ar1[10]);
void display();
void display1(proc p1[25]);
void request1();
void main()
{
    int i,j,val;
    proc p1[25];
    int ar1[10];
    printf("\n Enter No of proocesses : ");
    scanf("%d",&np);
    printf("\n Enter No of Resources : ");
    scanf("%d",&nr);
   
    printf("\n Enter Process Details.....");
    for(i=0;i<np;i++)
    {
        printf("\n Enter Process ID : ");
        scanf("%d",&p[i].pid);
        p[i].pf=0;
        printf("\n Enter Maximum Resources Needed : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&p[i].rm[j]);
        }
   
        printf("\n Enter Resources Allocated : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&p[i].ra[j]);
            p[i].rn[j]=p[i].rm[j]-p[i].ra[j];
        }
    }   
           
        printf("\n Enter Resources Available : ");   
        for(j=0;j<nr;j++)
        {
            printf("\n R%d=",j);
            scanf("%d",&ar[j]);
        }


        display();


    for(i=0;i<np;i++)
    {
        p1[i]=p[i];
    }   
       
    display1(p1);

    printf("\n Available Resources : ");
    for(i=0;i<nr;i++)
    {
        ar1[i]=ar[i];       
        printf("\t%d",ar1[i]);
    }
   

        val=banker(p1,ar1);
        request1();
       
}


void display()
{
    int i,j;
    system("clear");
    printf("\n PID     Max_Need_Resources      Allocated_Resources");
   
    for(i=0;i<np;i++)
    {
        printf("\n %d",p[i].pid);
        printf("     ");
        for(j=0;j<nr;j++)
        {
            printf("    %d",p[i].rm[j]);       
        }   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p[i].ra[j]);       
        }
    printf("\n");   
       
    }
   

}


int banker(proc p1[25],int ar1[10])
{
    int i,id,j,k=0,flg=0;
//    int ar1[10];
//    proc p1[25];
   
    printf("\n Sequence Of Execution : ");
    while(k<np)
    {
        for(i=0;i<np;i++)
        {
            if(p[i].pf==0)
            {
                for(j=0;j<nr;j++)
                {
                    if(p1[i].rn[j]<=ar1[j])
                    {
                        flg=0;
                    }
                    else
                    {
                        flg=1;
                        break;
                    }
                }   
           
                if(flg==0)
                {
                    for(j=0;j<nr;j++)
                    {
                        ar1[j]=ar1[j]+p1[i].ra[j];
                        p[i].pf=1;           
                    }
                    printf("=====>>%d",p[i].pid);
                    k++;
                    break;
                }
            }
        }//end for
       
        if(flg==1)
        {
            printf("\n System is in Unsafe state.....");
            return(0);       
        }
   
       
    }//end while   
    if(k==np)
    {
        printf("\n System is in Safe State......");
        return(1);       
    }
    printf("\n");
   
           
}

void request1()
{

    int i,id,r[10],j,val;
    proc p2[25];
    int ar2[10];
    //========requesting for resources========
       
   
    for(i=0;i<np;i++)
    {
        p2[i]=p[i];
    }   
       
    display1(p2);

    printf("\n Available Resources : ");
    for(i=0;i<nr;i++)
    {
        ar2[i]=ar[i];       
        printf("\t%d",ar2[i]);
    }
   
    printf("\n Enter Process Id : ");
    scanf("%d",&id);
   
    printf("\n Enter Requested resources : ");
    for(i=0;i<nr;i++)
    {
        scanf("%d",&r[i]);
        p2[id].ra[i]=p2[id].ra[i]+r[i];
        ar2[i]=ar2[i]-r[i];
        printf("Resource %d",i);
    }
    printf("\n caall to banker.....");
    val=banker(p2,ar2);
    if(val==1)
    {
        printf("\n Requested Resources Are Allocated.....");
    }
    else
    {
        printf("\n Requested Resources Are Not Allocated.....");
    }
}


void display1(proc p1[25])
{
    int i,j;
//    system("clear");
    printf("\n PID     Max_Need_Resources      Allocated      Cur_Need");
   
    for(i=0;i<np;i++)
    {
        printf("\n %d",p1[i].pid);
        printf("     ");
        for(j=0;j<nr;j++)
        {
            printf("    %d",p1[i].rm[j]);       
        }   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p1[i].ra[j]);       
        }
   
        printf("     ");
        for(j=0;j<nr;j++)
        {   
            printf("    %d",p1[i].rn[j]);       
        }
    printf("\n");   
       
    }
   

}




















awk

BEGIN{
        main();
        fact=1;
         rev=0;
    }
function main()
{
    do
    {
        printf("\n1.Factorial\n2.Prime Number\n3.Biggest of 3 no's\n4.Palindrome\
            \n5.Number Palindrome\n6.Exit");
        printf("\nEnter Choice  :  ");
        getline ch;
        if(ch==1)
        {
            printf("\n Enter The Number  :  ");
            getline num;
            fact=1;
            for(i=num;i>0;i--)
                {
                    fact=fact*i;
                }
            printf("\nFactorial of %d is %d\n\n",num,fact);
        }
        if(ch==2)
        {
            printf("\n Enter The Number:");
            getline num;
            if(num==1 || num==2)
               
            for(i=2;i>num;i++)
                   {
                    if(num%i==0)
                    {
                                                f=1;
                        break;
                                        }
                    else
                        f=0;
                }
            if(f==1)
                printf("%d is not a prime number",num);
            else
                printf("%d is a prime number",num);               
        }

        if(ch==3)
        {
            printf("\n Enter First Number");
            getline num1;
            printf("\n Enter Second Number");
            getline num2;
            printf("\n Enter Third Number");
            getline num3;
            if(num1>num2 && num1>num3)
                printf("%d is the greatest",num1);
            else if(num2>num3)
                printf("%d is the greatest",num2);
            else
                printf("%d is the greatest",num3);
        }
        if(ch==4)
        {
            printf("\nEnter The String:");
            getline str;
            len=length(str)
            for(i=len;i>0;i--)
            {
                rev=substr(str,i,1);
                str1=str1 rev
            }
            if(str1==str)
                printf("\nString is Palindrome");
            else
                printf("\nString is not a Palindrome");
        }
        if(ch==5)
        {
            printf("\n Enter The Number:");
            getline num;
            rev=0;
            num1=num
            while(num>=10)
              {
                rem=num%10;
                num=int(num/10);
                rev=rev+rem;
                rev=rev*10;
            }
            rev=rev+num;
            if(rev==num1)
                printf("\nNumber is Palindrome");
            else
                printf("\nNumber is not a Palindrome");
        }

    }while(ch!=6)
}

-----------------------------------------------------------

{

printf "Enter string :: ";
getline name;
l=length(name);
for(i=l;i>0;i--)
{
    rev=substr(name,i,1);
    str=str rev;

}
if(str==name)
{
printf "String is palindrome";

}
else
{

printf "String is not palindrome";
}
}
END{
}

----------------------------------------------------------
BEGIN{
FS=":"
main();
}
function create()
{
    printf("\nEnter Number of Records:");
    getline num;
    for(i=0;i<num;i++)
    {
        printf("\nEnter Roll No :");
        getline rollno;
        printf("\nEnter Name :");
        getline name;
        printf("\nEnter Marks of Subject M1 :");
        getline m1;
        printf("\nEnter Marks of Subject M2 :");
        getline m2;
        printf("\nEnter Marks of Subject M3 :");
        getline m3;
        printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"stud.txt";
    }
}
function display()
{
    printf("ROLLNO\tNAME\tSUB1\tSUB2\tSUB3");
    while(getline <"stud.txt"==1)
    {
        printf("\n%d\t%s\t%d\t%d\t%d",$1,$2,$3,$4,$5);
    }
}
function modify()
{
    ff=0;
    printf("\nEnter Rollno of Student to modify:");
    getline rollno
    while(getline<"stud.txt"==1)
            {
        if(rollno==$1)
        {
                      name=$2
            ff=1;
            printf("Enter New Marks for M1,M2,M3:");
            getline m1;
             getline m2;
                              getline m3;
                printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"new.txt";
        }
        else
        {
           print>"new.txt"
        }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function delet()
{
    f=0;
    printf("\nEnter Roll No you want to delete:");
    getline rollno;
    while(getline<"stud.txt"==1)
    {
      if(rollno!=$1)
              {
        print>"new.txt"
      }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function search()
{
    printf("\nEnter Roll No of Student to Search:");
    getline rollno;
    f=0;
    while(getline<"stud.txt"==1)
    {       
      if(rollno==$1)
      {
        printf("\nRecord is present and Name is:%s",$2);
        f=1;
        break;
      }
    }
    if(f==0)
        printf("\nRecord is not present");
}
function main()
{
    printf("\n1.Create DataBase\n2.Delete a Record\n3.Modify a Record\n4.Search a   Record\n5.DisplayDataBase");           
    printf("\nEnter your choice: ");
           getline ch
      if (ch==1)
         create();
      if (ch==2)
         delet();
        if (ch==3)
         modify();
      if (ch==4)
         search();
      if (ch==5)
          display();
      printf("\n\nDo you want to continue(1:yes,0:no):");
       getline ans
      if (ans==1)
        system("awk -f ASSN2.awk")
}

mem

#include<stdio.h>
void accept(int n,int nh,int holes[20],int p[20]);
void firstfit(int n,int nh,int holes[20],int p[20]);
void bestfit(int n,int nh,int holes[20],int p[20]);
void worstfit(int n,int nh,int holes1[20],int p[20]);
void nextfit(int n,int nh,int holes1[20],int p[20]);
void main()
{
    int ch,n,nh,holes[20],p[20];
    printf("\nEnter no. of processes: ");
    scanf("%d",&n);
    printf("\nEnter no. of holes: ");
    scanf("%d",&nh);
    accept(n,nh,holes,p);
    do
    {
        printf("\nMENU:\n\n1.First fit\n2.Best Fit\n3.Worst Fit\n4.Next fit\n5.Re-enter processes and holes\n6.Exit\n\n\tEnter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: firstfit(n,nh,holes,p);
                break;
            case 2: bestfit(n,nh,holes,p);
                break;           
            case 3: worstfit(n,nh,holes,p);
                break;           
            case 4: nextfit(n,nh,holes,p);
                break;           
            case 5: printf("\nEnter no. of processes: ");
                scanf("%d",&n);
                printf("\nEnter no. of holes: ");
                scanf("%d",&nh);
                accept(n,nh,holes,p);           
            case 6: break;
            default:printf("\nWrong choice entered\n");
                break;
        }
    }while(ch!=6);
}
//Here, we accept the size of the processes and the size of the holes
void accept(int n,int nh,int holes[20],int p[20])
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("\nEnter size of process %d: ",i+1);
        scanf("%d",&p[i]);
    }
    for(i=0;i<nh;i++)
    {
        printf("\nEnter size of hole %d: ",i+1);
        scanf("%d",&holes[i]);
    }
}
void firstfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,holes[20];
//We transfer the hole sizes to a new array so that they don't get changed in the main function.
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<nh;j++)
        {
            if(p[i]<=holes[j])    //We find the first hole that fits, and place the process in it.
            {
                printf("\nProcess %d fits into hole %d",i+1,j+1);               
                holes[j]=holes[j]-p[i];
                break;
            }
        }
        if(j==nh)
            printf("\nProcess %d does not fit into any hole",i+1);   
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}
//This is a variation on first fit. In next fit, we resume the search from the last hole that was filled. Eg., if process 1 filled hole 2, then process 2 will begin searching for a hole that fits, starting from hole 2.
void nextfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j=0,holes[20],x;
//Variable x is used to hold a value such that when j reaches x-1, it has traversed the whole array. Since the first process has to traverse the array from 0 to nh-1, we initialize x with nh
    x=nh;
//We transfer the hole sizes to a new array so that they don't get changed in the main function.   
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        for(;j<nh;)
        {
            if(p[i]<=holes[j])
            {
                printf("\nProcess %d fits into hole %d",i+1,j+1);               
                holes[j]=holes[j]-p[i];
                x=j;
                break;           
            }
//If following condition is satisfied, whole array has been traversed.
            else if(j==x-1)
            {
                j=nh;
                break;
            }
            else j=(j+1)%nh;
        }
        if(j==nh)
            printf("\nProcess %d does not fit into any hole",i+1);   
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}


//In bestfit, we find the smallest hole that can hold the given process.
void bestfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,index[20],cnt,min,holes[20];
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
        cnt=0;
        for(j=0;j<nh;j++)
        {
            if(p[i]<=holes[j])
            {
                index[cnt++]=j;    //We store the index of all holes that the process can fit it.
            }
        }
        if(cnt==0)
        {
            printf("\nProcess %d does not fit into any holes",i+1);       
            continue;
        }
//Using the following, we find the smallest hole that can hold the process, and place the process in that hole
        min=index[0];
        for(j=1;j<cnt;j++)
        {
            if(holes[index[j]]<holes[min])
                min=index[j];
        }       
        printf("\nProcess %d fits into hole %d",i+1,min+1);               
        holes[min]=holes[min]-p[i];
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}





//In worstfit, we find the largest hole that can fit the process
void worstfit(int n,int nh,int holes1[20],int p[20])
{
    int i,j,index[20],max,holes[20];
    for(i=0;i<nh;i++)
        holes[i]=holes1[i];
    for(i=0;i<n;i++)
    {
//Using the following, we find the largest hole
        max=0;
        for(j=1;j<nh;j++)
        {
            if(holes[j]>holes[max])
                max=j;
        }
//Here, we check whether the largest hole is big enough to hold the process. If yes, we allocate it. If not, the process does not fit in any holes.
        if(holes[max]>p[i])
        {
            holes[max]=holes[max]-p[i];
            printf("\nProcess %d fits into hole %d",i+1,max+1);
        }
        else printf("\nProcess %d does not fit into any holes",i+1);
    }
    printf("\n\nHoles at the end of fitting all processes: ");
    for(i=0;i<nh;i++)
        printf("\nHole %d: %d",i+1,holes[i]);
}

shell

#!/bin/sh
echo "1.PALINDROME 2.CONCATE 3.REVERSE"
echo "\n Enter the choice :: "
read ch
case "$ch" in
    1)
echo "\nEnter the string ::"
read str
temp=$str
l=${#str}
rev=""
                                   
 while test $l -ne 0
do
k=`echo $str|cut -c $l `
rev=$rev$k
l=` expr $l - 1 `
                                   
done
                                   
if [ $temp != $rev ]
then
echo "string not palindrom"
else
echo " pailndrom"
fi

;;

2)
echo "\nEnter the string ::"
read str1
echo "\nEnter the string ::"
read str2
car=$str1$str2
echo "CONCATENATED :: $car"

;;

3)
echo "\nEnter the string ::"
read str
temp=$str
l=${#str}
rev=""
                                   
 while test $l -ne 0
do
k=`echo $str|cut -c $l `
rev=$rev$k
l=` expr $l - 1 `
                                   
done
echo "Reverse string is ::$rev";;

esac
---------------------------------------------------------

echo "         Menu\n
     1.even odd\n 2.pallindrome\n 3.reverse\n 4.prime no "
read choice
case "$choice" in
      1)echo "enter no"
        read no
        c=` expr $no % 2 `
    if [ $c -eq 0 ]
    then
    echo "$no is even"
    else
    echo "$no is odd"
    fi  ;;
      2)echo " Enter the number of digit of number you want to reverse "
read n
echo " Enter the no."
read no1
no=$no1

while test $n -ne 0

do
x=` expr $no % 10 `
no=` expr $no / 10 `
rev=` expr $rev + $x `
rev=` expr $rev \* 10 `
n=` expr $n - 1 `
done

rev=` expr $rev / 10 `
#echo " the reverse no is:$rev"
if [ $no1 != $rev ]
then
echo "sting is not palindrom"
else
echo "string is pailndrom"
fi;;
     3)#!/bin/sh
echo " Enter the number of digit of number you want to reverse "
read n
echo " Enter the no."
read no1
no=$no1

while test $n -ne 0

do
x=` expr $no % 10 `
no=` expr $no / 10 `
rev=` expr $rev + $x `
rev=` expr $rev \* 10 `
n=` expr $n - 1 `
done

rev=` expr $rev / 10 `
echo " the reverse no is:$rev";;

4)
echo "Enter the no :: "
read no
flag=0
n=` expr $no - 1 `
while [ $n -ne 1 ]
do
res=` expr $no % $n`

if [ $res -eq 0 ]
then

    flag=1
    break

fi

n=` expr $n - 1 `

done

if [ $flag -ne 1 ]
then

echo " prime"

else

echo " not prime"

fi


esac



pipe

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
#include<string.h>
#define sz 50

pthread_t p,c;
sem_t mut,datachk;
char rbuf[sz];
int fd[2],nw;

void * producer()
  {
    char buf[sz];
    while(1)
      {
        printf("\n\nENTER DATA :  ");
        gets(buf);
        sem_wait(&mut);   
        nw=write(fd[1],buf,strlen(buf));
        printf("\n\n%d BYTE DATA IS WRITTEN.",nw);
        sem_post(&mut);
        sem_post(&datachk);
        sleep(1);
      }
  }

void * consumer()
  {
    int i,nr;
    char rbuf[sz];
    while(1)
      {
    sem_wait(&datachk);
        sem_wait(&mut);
        nr=read(fd[0],rbuf,nw);
        printf("\n\n%d BYTE DATA IS READ.",nr);
        printf("\n\n%s DATA IS READ.",rbuf);
        sem_post(&mut);
        for(i=0;i<sz;i++)
          rbuf[i]=0;
      }
  }

void main()
  {
    int i;
    sem_init(&mut,0,1);
    sem_init(&datachk,0,0);
    if(pipe(fd)==0)
      {
      if(pthread_create(&p,NULL,producer,(void *)&i) !=0)
          printf("\n\nERROR CREATION");       
    if(pthread_create(&c,NULL,consumer,(void *)&i) !=0)
          printf("\n\nERROR CREATION");  
        pthread_join(p,NULL);
        pthread_join(c,NULL);
     }
  }

scheduling

#include<stdio.h>
//Definition of structure for process
struct process
{
    char name[20];
    int flag,arrival,burst,initburst,prio;
};
//Queue Definition along with queue functions
struct queue
{
    struct process data[20];
    int r,f;
};
void init(struct queue *q)
{
    q->r=q->f=-1;
}
int empty(struct queue q)
{
    if(q.r==-1)
        return(1);
    return(0);
}
//Normal enqueue(used in FCFS and Round Robin)
void enqueue(struct queue *q,struct process d)
{
    if(empty(*q))
        q->r=q->f=0;
    else q->r=q->r+1;
    q->data[q->r]=d;
}
/*Enqueue for sjf scheduling. In this, we maintain a priority based queue
where priority is given to the shortest job, i.e, the job with min. burst time is
always at the front of the queue*/
void sjfenqueue(struct queue *q, struct process d)
{
    int x,y;
    if(empty(*q))
    {
        q->r=q->f=0;
        q->data[q->r]=d;
    }
    else
    {
        x=q->f;
        y=q->r;
        //Go through the queue to find position of new process d
        while(q->data[x].burst<=d.burst&&x<=y)
            x++;
        //Shift elements one place to the right to make space for d
        while(y>=x)
        {
            q->data[y+1]=q->data[y];
            y--;
        }
        q->data[x]=d;
        q->r=q->r+1;
    }
}
/*Enqueue for priority scheduling. Same as above except the priority is now
using process priority instead of burst time*/
void prioenqueue(struct queue *q, struct process d)
{
        int x,y;
        if(empty(*q))
        {
                q->r=q->f=0;
                q->data[q->r]=d;
        }
        else
        {
                x=q->f;
                y=q->r;
                //Go through the queue to find position of new process d
                while(q->data[x].prio<=d.prio&&x<=y)
                        x++;
                //Shift elements one place to the right to make space for d
                while(y>=x)
                {
                        q->data[y+1]=q->data[y];
                        y--;
        }
        q->data[x]=d;
        q->r=q->r+1;
    }
}
struct process dequeue(struct queue *q)
{
    struct process d;
    d=q->data[q->f];
    if(q->f==q->r)
        q->f=q->r=-1;
    else q->f=q->f+1;
    return(d);
}
void accept(struct process p[20],int n);
void fcfs(struct process p[20],int n);
void sjf(struct process p[20],int n);
void prio(struct process p[20],int n);
void sjfpre(struct process p[20],int n);
void priopre(struct process p[20],int n);
void rr(struct process p[20],int n);
void main()
{
    int ch,n,i;
    struct process p[20];
    printf("\nEnter number of processes: ");
    scanf("%d",&n);   
    accept(p,n);
    do
    {
    for(i=0;i<n;i++)
        p[i].flag=0;
    printf("\nMENU:\n1.FCFS\n2.SJF\n3.SJF-preemptive\n4.Priority\n5.Priority-preemptive\n6.Round Robin\n7.Re-enter processes\n8.Exit\n\n\tEnter your choice: ");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1: fcfs(p,n);
            break;
        case 2: sjf(p,n);
            break;
        case 3: sjfpre(p,n);
            break;
        case 4: prio(p,n);
            break;
        case 5: priopre(p,n);
            break;
        case 6: rr(p,n);
            break;       
        case 7: printf("\nEnter number of processes: ");
            scanf("%d",&n);
            accept(p,n);
            break;
        case 8: break;       
        default:printf("\nWrong choice entered\n");
            break;
    }
    }while(ch!=8);
}
void accept(struct process p[20],int n)
{
    int i;
    char s[2];   
    for(i=0;i<n;i++)
    {
    printf("\nProcess %d: ",i+1);
//We use sprintf to give thee processes names like 'P1','P2',etc.
    sprintf(p[i].name,"P%d",i+1);
    printf("\nEnter arrival time: ");
    scanf("%d",&p[i].arrival);
    printf("\nEnter burst time: ");
    scanf("%d",&p[i].initburst);
    //initburst is the initial burst time, and 'burst' is current burst time
    p[i].burst=p[i].initburst;
    }
}
void fcfs(struct process p[20],int n)
{
    int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                enqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
                flag1=1;
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
/*Decrement burst time to indicate completion of one burst */
        pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as FCFS, except we use sjfenqueue function instead of enqueue function
void sjf(struct process p[20],int n)
{
    int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when     i=1. The loop will run until completed processes(comp) equals no. of proceses
    */
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                sjfenqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
                flag1=1;
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
/*Decrement burst time to indicate completion of one burst */
        pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as sjf,but with preemption
void sjfpre(struct process p[20],int n)
{
    int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when     i=1. The loop will run until completed processes(comp) equals no. of proceses
    */
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                sjfenqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
                flag1=1;
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
//The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has smaller burst time than the current process, then current process is enqueued and the new process is dequeued into pc
        else if(q.f!=-1 && pc.burst > q.data[q.f].burst)
        {
            printf("%s|",pc.name);
            pretime[cnt++]=i;
            sjfenqueue(&q,pc);
            pc=dequeue(&q);
        }
/*Decrement burst time to indicate completion of one burst */
        pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}


//Exactly the same as FCFS, except we use prioenqueue function instead of enqueue function
void prio(struct process p[20],int n)
{
    int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
    //Accept the priority for the processes
    for(i=0;i<n;i++)
    {   
        printf("\nEnter priority for process %d: ",i+1);
        scanf("%d",&p[i].prio);
    }   
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when     i=1. The loop will run until completed processes(comp) equals no. of proceses
    */
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                prioenqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
                flag1=1;
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
/*Decrement burst time to indicate completion of one burst */
        pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Exactly the same as prio,but with preemption
void priopre(struct process p[20],int n)
{
    int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
    //Accept the priority for the processes
    for(i=0;i<n;i++)
    {   
        printf("\nEnter priority for process %d: ",i+1);
        scanf("%d",&p[i].prio);
    }   
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when     i=1. The loop will run until completed processes(comp) equals no. of proceses
    */
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                prioenqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
                flag1=1;
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
    //The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has higher priority than the current process, then current process is enqueued and the new process is dequeued into pc
        else if(q.f!=-1 && pc.prio > q.data[q.f].prio)
        {
            printf("%s|",pc.name);
            pretime[cnt++]=i;
            prioenqueue(&q,pc);
            pc=dequeue(&q);
        }
/*Decrement burst time to indicate completion of one burst */
        pc.burst--;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}

//Procedure for Round Robin
void rr(struct process p[20],int n)
{
    int i,j,qt,tr=0,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
    float avgta=0,avgwt=0;   
    struct queue q;
    init(&q);
    struct process pc;
    /* pc is the current process. Since initially, there is no process in
    pc, we set its flag to -1*/
    pc.flag=-1;
//Obtain quantum time for Round Robin
    printf("\nEnter quantum time: ");
    scanf("%d",&qt);
    printf("\nGANTT CHART\n\n\t|");
    /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
//Slight modification in the loop for RR. Since the same process will run for qt moments, we increment i by qt instead of by 1   
    for(i=0;comp<n;i++)
    {
/*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
*/
        for(j=0;j<n;j++)
        {
            if(p[j].arrival<=i && p[j].flag==0)
            {
                p[j].flag=1;
                enqueue(&q,p[j]);
            }
        }
/*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
        if(pc.flag==-1)
        {
/*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
            if(empty(q) && flag1==0)
            {
                flag1=1;
                continue;
            }
            else if(empty(q) && flag1==1)
                continue;
            else if(!empty(q) && flag1==1)
            {
                printf("  |");
/*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */               
                pretime[cnt++]=i;
                flag1=0;
                pc=dequeue(&q);
            }
            else pc=dequeue(&q);
        }
/*If queue is not empty and the current process has already run for qt moments, then we replace the current process by the new process*/
        else if(!empty(q) && tr==qt)
        {
            pretime[cnt++]=i;
            printf("%s|",pc.name);
            enqueue(&q,pc);
            pc=dequeue(&q);
            tr=0;    //Reset time-run.
        }
//Decrement burst time
        pc.burst--;
//tr is time-run. It is required to keep track of whether a process has run for the given quantum time.
        tr++;
/* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
        if(pc.burst==0)
        {
            turnaround=(i+1)-pc.arrival;
            waiting=turnaround-pc.initburst;
            avgta+=turnaround;
            avgwt+=waiting;
            pretime[cnt++]=i+1;
            comp++;
            printf("%s|",pc.name);
            pc.flag=-1;
            tr=0;    //Reset time-run
        }
    }
//Display the times below the Gantt Chart
    printf("\n\t0 ");
    for(i=0;i<cnt;i++)
    {   
        if(pretime[i]>9)
            printf("%d ",pretime[i]);
        else    printf (" %d ",pretime[i]);
    }
//Calculate avg. TA and WT
    avgta=avgta/n;
    avgwt=avgwt/n;
    printf("\n\nAvg. TA time: %.2f",avgta);
    printf("\n\nAvg. WT time: %.2f",avgwt);
}

pagereplacement

#include<stdio.h>
void lru(int pa[20],int pt[10],int n,int np);
void optimal(int pa[20],int pt[10],int n,int np);
void mfu(int pa[20],int pt[10],int n,int np);
void main()
{
    int i,n,np,pa[20],pt[10],ch;
    printf("\nEnter no. of slots in the page table: ");
    scanf("%d",&n);
    printf("\nEnter no. of pages to be accessed: ");
    scanf("%d",&np);
    printf("\nEnter the sequence in which the pages are to be accessed:\n");
    for(i=0;i<np;i++)
    {
        scanf("%d",&pa[i]);
    }   
    do
    {
        printf("\nMENU:\n1.LRU\n2.Optimal\n3.MFU\n4.Exit\n\tEnter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: lru(pa,pt,n,np);
                break;
            case 2: optimal(pa,pt,n,np);
                break;
            case 3: mfu(pa,pt,n,np);
                break;
            case 4: break;
            default:printf("\nWrong choice entered");
                break;
        }
    }while(ch!=4);
}
void mfu(int pa[20],int pt[10],int n,int np)
{
    int i,flag2,flag[10],j,k,cnt=0,pf=0,x;
        for(i=0;i<np;i++)
        {       
        for(j=0;j<n;j++)
            flag[j]=0;
                flag2=0;
                for(j=0;j<cnt;j++)
                {
                        if(pt[j]==pa[i])
                                flag2=1;
                }
                if(!flag2)
                {
                        if(cnt<n)
                        {
                                pt[cnt]=pa[i];
                                cnt++;
                        }
            else
            {
                for(j=0;j<n;j++)
                {
                    x=pt[j];
                    for(k=0;k<i;k++)
                    {
                        if(pa[k]==x)
                            flag[j]++;
                    }
                }
                x=0;
                                for(j=1;j<n;j++)
                                {
                                        if(flag[j]>flag[x])
                                                x=j;
                                }
                                pt[x]=pa[i];
            }
            pf++;
        }
        printf("\n\nPage Table %d:\n",i+1);
                for(j=0;j<cnt;j++)
                        printf("%d\n",pt[j]);
                printf("\nPage faults: %d\n",pf);
    }
}
void optimal(int pa[20],int pt[10],int n,int np)
{
    int i,flag2,flag[10],j,k,cnt=0,pf=0,x;
    for(i=0;i<np;i++)
    {
        flag2=0;
        for(j=0;j<cnt;j++)
        {
            if(pt[j]==pa[i])// check if page already exists in frame
                flag2=1;//no page fault   
        }
        if(!flag2)
        {
            if(cnt<n)//initially ,when frame is empty or partially empty
            {   
                pt[cnt]=pa[i];
                cnt++;
            }
            else
            {
                for(j=0;j<n;j++)
                {
                    x=pt[j];
                    for(k=i+1;k<np;k++)
                    {
                        if(pa[k]==x)
                        {
                            flag[j]=k;
                            break;
                        }
                    }
                    if(k==np)
   
                    flag[j]=np;
                }
                x=0;
                for(j=1;j<n;j++)
                {
                    if(flag[j]>flag[x])
                        x=j;
                }
                pt[x]=pa[i];               
            }       
            pf++;// page fault occurs
        }   
        printf("\n\nPage Table %d:\n",i+1);
        for(j=0;j<cnt;j++)
            printf("%d\n",pt[j]);
        printf("\nPage faults: %d\n",pf);
    }
}
void lru(int pa[20],int pt[10],int n,int np)
{
    int i,j,cnt=0,flag[10],x,k,flag2,pf=0;
    for(i=0;i<np;i++)
    {
        flag2=0;
        for(j=0;j<cnt;j++)
        {
            if(pt[j]==pa[i])
                flag2=1;   
        }
        if(!flag2)
        {
            if(cnt<n)
            {   
                pt[cnt]=pa[i];
                cnt++;
            }   
            else
            {
                for(j=0;j<n;j++)
                {
                    x=pt[j];
                    for(k=0;k<i;k++)
                    {
                        if(pa[k]==x)
                            flag[j]=k;
                    }   
                }
                x=0;
                for(j=1;j<n;j++)
                {
                    if(flag[j]<flag[x])
                        x=j;
                }
                pt[x]=pa[i];
            }
            pf++;
        }
        printf("\n\nPage Table %d:\n",i+1);
        for(j=0;j<cnt;j++)
            printf("%d\n",pt[j]);
        printf("\nPage faults: %d\n",pf);
    }   
}