Friday 26 October 2012

menu driven shell

while [ 1 ]
do
echo " "
echo "1) Factorail no"
echo "2) Greatest of 3 nos"
echo "3) Prime no"
echo "4) Palindrome string"
echo "5) Palindrome no"
echo "6) Exit"
echo "Enter Your choice : "
read ch
case "$ch" in
1)
echo "Program for Factorial no"
echo "Enter number : "
read num
i=1
fact=1
for((i=1;i<=num;i++))
{
    fact=$(($fact*$i))
}
echo "Factorial is : " $fact
;;
2)
echo "Program for greatest no "
echo "Enter num1 : "
read n1
echo "Enter num2 : "
read n2
echo "Enter num3 : "
read n3
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
    echo "$n1 is greatest no"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ]
then
        echo "$n2 is greatest no"
else
    echo "$n3 is greatest no"
fi
;;
3)
echo "Program for prime no : "
echo "Enter num : "
read num
i=2
flag=1
for((i=2;i<=num-1;i++))
{
if [ $(($num % $i)) -eq 0 ]
then
    flag=0
    break
fi
}
if [ $flag -eq 1 ]
then
    echo "Prime no"
else
    echo "Not a Prime no"

fi
;;
4)
echo "Enter string : "
read str
reverse=`echo $str| rev`
if [ $reverse = $str ]
then
    echo "String is palindrome"
else
    echo "String is not palindome"
fi
;;
5)
echo "Enter number : "
read num
org=$num
rev=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    num=$(($num/10))
    rev=$(($rev*10))
    rev=$(($rev+$r))
   
done
if [ $rev -eq $org ]
then
    echo "No is palindrome"
else
    echo "No. is not palindrome"
fi
;;
6)
exit
;;
*)
echo "Invalid Choice"
;;
esac
done

database shell

printf "Enter a File Name : "
read fname
while [ 1 ]
do

echo " "
echo " ***** MENU **** "
echo "1) Create Record"
echo "2) View Record"
echo "3) Insert Record"
echo "4) Delete Record"
echo "5) Search"
echo "6) Exit"
echo "Enter your choice : "
read ch
case "$ch" in
1)
    echo "CREATE"
    echo "Enter student info : \n"
    echo "Roll No. : "
    read rno
    echo "Name : "
    read name
    echo "Year : "
    read year
    echo "Branch : "
    read branch
    echo "Percentage : "
    read per
    printf "%d\t%s\t%s\t%s\t%s\n" $rno $name $year $branch $per >> $fname
;;
2)
    echo "VIEW"
    if [ -e $fname ]
    then
        cat $fname
    else
    echo "File does not exist"
    fi

;;
3)
    echo "INSERT"
        echo "Enter student info : \n"
        echo "Roll No. : "
        read rno
        echo "Name : "
        read name
        echo "Year : "
        read year
        echo "Branch : "
        read branch
        echo "Percentage : "
        read per
        printf "%d\t%s\t%d\t%d\t%d" $rno $name $year $branch $per >> $fname
;;
4)
    echo "DELETE"
    echo "Enter roll no which u want to delete : "
    read rno
    grep "$rno" "$fname"
    ans=$?
    if [ $ans -eq 0 ]
    then
    echo "Record Found"
    grep -v "$rno" "$fname"
    rm "$fname"
    mv temp.dat "$fname"
    echo "Record deleted !!!"
    else
    echo "Record not found"
    fi
;;


5)
    echo "SEARCH"
        echo " "
        echo "Result of a particular record"
        echo "Enter roll no which u want to search: "
        read rno
        grep "$rno" "$fname"
        ans=$?
        if [ $ans -eq 0 ]
        then
        echo "Record Found"
        else
        echo "Record not found"
        fi
;;
6)
exit
;;
*)
    echo "Invalid choice"
esac
done

cmd (print n words, sum of digit)

if [ $# -ne 3 ]
then
echo " Insuffient command line arguments "
else
choice=1
echo "1) Print the word N times"
echo "2) Sum of digit no."
echo "3) Exit"
echo "Enter your choice : "
read choice
case $choice in
1)
count=$1
while [ $count -gt 0 ]
do
 echo "$2"
    count=`expr $count - 1`
done
;;
2)
num=$3
sum=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    sum=$(($sum+$r))
    num=$(($num/10))
done
    echo "Sum of digit of no is : $sum"
;;
3)
exit
;;
*)
echo "Invalid choice"
;;
esac
fi


cmd line (biggest no , reverse)

if [ $# -ne 4 ]
then
echo "Insuffiecient command line arguments"
else
choice=1
while [ $choice -ne 4 ]
do
echo "1) Biggest of 3 nos"
echo "2) Reverse no"
echo "3) Exit"
echo "Enter your choice : "
read choice
case $choice in
1)
    if [ $1 -gt $2 -a $1 -gt $3 ]
    then
        echo "$1 is greatest no"
    elif [ $2 -gt $1 -a $2 -gt $3 ]
        then
            echo "$2 is greatest no"
    else
        echo "$3 is greatest no"
    fi
;;
2)
num=$4
rev=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    rev=$(($rev*10))
    rev=$(($rev+$r))
    num=$(($num/10))
done
    echo "Reverse no. : $rev"
;;
3)
exit
;;
*)
    echo "Invalid choice"
;;
esac
done
fi


awk menu

BEGIN{
 menu()
}

{
 if(ch==1)
 factorial()
 if(ch==2)
 greater()
 if(ch==3)
 prime()
 if(ch==4)
 numpal()
 if(ch==5)
 strpal()
 if(ch==6)
 exit
}

#functions

function menu()
{
 system("clear")

 printf("\n\nenter your choice:")
 getline ch<"-"
}

function factorial()
{
 printf("enter the number:")
 getline a<"-"
 f=1
 if(a==0)
 print 1
 for(i=1;i<=a;i++)
 f=f*i
 printf("\nfactorial of %d is :%d",a,f)
 getline z"<"
 menu()

}

function greater()
{
 printf("enter the value of a:")
 getline a<"-"
 printf("enter the value of b:")
 getline b<"-"
 printf("enter the value of c:")
 getline c<"-"
 if(a>b && a>c)
 printf("\n%d is greatest",a)
 else if(b>a && b>c)
 printf("\n%d is greatest",b)
 else
 printf("\n%d is greatest",c)
 getline z<"-"
 menu()
}

function prime()
{
 printf("enter the number:")
 getline a<"-"
 i=2
 flag=1
 while(i<a)
 {
  if(a%i==0)
  {
   flag=0
   break
  }
  i++
 }
 if(flag==1)
 printf("\nit is a prime number")
 else
 printf("\nnot a prime number")
 getline z<"-"
 menu()
}

function strpal()
{
 printf("enter the string:")
 getline str<"-"
 i=1
 flag=0
 len=length(str)
 while(i<len)
 {
  ch1=substr(str,i,1)
  ch2=substr(str,len,1)
  if(ch1!=ch2)
  {
   flag=1
   break
  }
  i++
  len--
 }
 if(flag==0)
 printf("\nstring is palindrome")
 else
 printf("\nnot a palindrome")
 getline z<"-"
 menu()
}

function numpal()
{
 printf("\nenter a number:")
 getline num<"-"
 rev=0
 org=num
 while(num>0)
 {
  r=int(num%10)
  num=int(num/10)
  rev=int(rev*10+r)
 }
 if(rev==org)
 printf("\nthe number is a palindrome")
 else
 printf("\nthe number is not a palindrome")
 getline z<"-"
 menu()
}

awk database

#Title: Write a  awk program to handle student data base with options given below,
#    a) Create data base. b) View Data Base. c) Insert a record.
#    d) Delete a record. e) Modify a record. f) Result of a particular
#       student. g) Exit.

BEGIN{
main();
}
function main()
{
   
        printf("\n1. Insert")
        printf("\n2. View")
        printf("\n3. Search")
        printf("\n4. Modify")
        printf("\n5. Delete")
        printf("\n6. Result Analysis")
        printf("\n7. Exit")
        printf("\nPlease enter your choice\t")
        getline choice
       
        if(choice==1)
        {
            printf("\nEnter your roll number\t")
            getline roll_no
            printf("\nEnter the name\t")
            getline name
            printf("\nEnter the marks")
            printf("\nDBMS : ")
            getline dbms
            printf("SE : ")
            getline se
            printf("CNT : ")
            getline cnt
            printf("OS : ")
            getline os
            printf("TOC : ")
            getline toc
            printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"student.txt"
            #printf("\n%d|%s",rollno,name)>>"stud1.txt";
        }
       
        else if(choice==2)
        {
            printf("\nRoll No\tName\tDBMS\tSE\tCNT\tOS\tTOC")
            printf("\n=======================================================================")
            while(getline < "student.txt"==1)
            {
                printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d",$1,$2,$3,$4,$5,$6,$7)
            }
        }

        else if(choice==3)
        {
            printf("\nEnter the roll number to be searched\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)
                    flag=1
                    break
                }
            }
            if(flag==0)
                printf("\nRecord not found\n")
        }

        else if(choice==4)
        {
            printf("\nEnter the roll number to be modified\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nEnter new roll number\t")
                    getline rno
                    printf("\nEnter the new name\t")
                    getline name
                    printf("\nEnter the new marks")
                    printf("\nDBMS : ")
                    getline dbms
                    printf("SE : ")
                    getline se
                    printf("CNT : ")
                    getline cnt
                    printf("OS : ")
                    getline os
                    printf("TOC : ")
                    getline toc
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"temp.txt"
                    flag=1
                }
                else
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
            }
            "mv temp.txt student.txt"|getline
            #"rm temp.txt"|getline

            if(flag==1)
                printf("\nRecord modified successfully\n")
            else
                printf("\nRecord not found\n")
        }
       
        else if(choice==5)
        {
            printf("\nEnter the roll number to be deleted\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1!=roll_no)
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
                else
                    flag=1
            }
            "mv temp.txt student.txt"|getline
       
            if(flag==0)
                printf("\nRecord not found\n")
            else
                printf("\nRecord deleted sucessfully\n")
        }
       
        else if(choice==6)
        {
            printf("\nEnter the roll number\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nTotal = %d",($3+$4+$5+$6+$7))
                printf("\nPercentage = %2.2f ",($3+$4+$5+$6+$7)/5)
                    flag=1
                    break
                }
            }
           
            if(flag==0)
                printf("\nRecord not found")
        }

        else if(choice==7)
        {
            exit 0
        }
        printf("\n")
}       

sjf p


#include<stdio.h>
#include<malloc.h>
typedef struct process
{
    int p_name;
    int cpu_time,arrival_time,finish_time,turn_time;
    int waiting_time,start_time,cpu_temp,flag;
}process;

int main()
{
    int no,i,total_cpu_time=0,j,processes,next_proc,current,x;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7];
    process small;
    int waiting_proc=0,minus;
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time and CPU time\n");
        scanf("%d%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
        obj[i].cpu_temp=obj[i].cpu_time;
        total_cpu_time=total_cpu_time+obj[i].cpu_time;
    }
    for(i=0;i<no;i++)
    {
        obj[i].flag=0;
    }
//Sort according to CPU time
    processes=1;
    current=0;
    next_proc=1;
    small=obj[0];
    i=1;
    printf("\n\tRunning Time\tProcess No\n");

    while(i<=total_cpu_time)
    {
        if(obj[current].cpu_temp == obj[current].cpu_time)
        {
            obj[current].start_time=i-1;
        }
        minus=i-1;
        printf("\n\t%d--%d\t\tp%d",minus,i,current);
        obj[current].cpu_temp=obj[current].cpu_temp - 1;
       
        if(obj[current].cpu_temp==0 && obj[current].flag==0)
        {
            obj[current].flag=1;
            obj[current].finish_time=i;
            printf("\np%d terminates here...",obj[current].p_name);
            printf("\nFinish Time=%d",obj[current].finish_time);
            waiting_proc=waiting_proc-1;
        }
       
        if(obj[next_proc].arrival_time==i && next_proc!=no)
        {
            next_proc=next_proc+1;
            processes=processes+1;
            waiting_proc=waiting_proc+1;
        }

        small=obj[current];
        for(x=0;x<processes;x++)
        {
            if((small.cpu_temp>obj[x].cpu_temp) && (obj[x].flag==0))
                {
                small=obj[x];
                current=x;
                }
                if((small.cpu_temp<obj[x].cpu_temp) && (small.flag==1))
                    {
                    small=obj[x];
                current=x;
                }
        }

            /*  if(waiting_proc==0 && processes==no)
           {
            for(x=0;x<processes;x++)
            {
                if(obj[x].flag==0)
                {
                    small=obj[x];       
                    current=x;
                }
             }
            }
        */
       i++;           
    }

//calculates time for each process

    for(i=0;i<no;i++)
    {
        obj[i].turn_time=obj[i].finish_time-obj[i].arrival_time;
        obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
       
        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }

//Display all times
printf("\n=======================================================================================================");
    printf("\nP_ID\t   Arrival_Time\t   CPU_time\t   Start_Time\t   Finish_Time\t   Turn_Time\t   Waiting_Time");
printf("\n=======================================================================================================");
    for(i=0;i<no;i++)
    {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);
    }
    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;
   
    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");

return 0;
}

sjf np

#include<stdio.h>
#include<malloc.h>
typedef struct process
{
    char p_name[2];
    int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time;
}process;

int main()
{
    int no,i,total_cpu_time=0,j;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7];
    process small;
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time and CPU time\n");
        scanf("%s%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
        total_cpu_time=total_cpu_time+obj[i].cpu_time;
    }

//Sort according to CPU time
    for(i=0;i<no;i++)
    {
        for(j=i+1;j<no;j++)
        {
            if(obj[i].cpu_time>obj[j].cpu_time)
            {
                small=obj[i];
                obj[i]=obj[j];
                obj[j]=small;
            }

        }
    }

    for(i=0;i<no;i++)
    {
        printf("\n%d",obj[i].cpu_time);
    }

//First process time

    obj[0].start_time=1;
    obj[0].finish_time=obj[0].cpu_time;
    obj[0].turn_time=obj[0].cpu_time;
    obj[0].waiting_time=0;

    avg_turn_time=obj[0].turn_time;
    avg_waiting_time=0;
//calculates time for each process

    for(i=1;i<no;i++)
    {
        obj[i].start_time=obj[i-1].finish_time+1;
        obj[i].finish_time=obj[i-1].finish_time+obj[i].cpu_time;
        obj[i].turn_time=obj[i].finish_time;
        obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;

        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }

//Display all times
printf("\n============================================================================================");
printf("\nP_ID\tArrival_Time   CPU_time     Start_Time   Finish_Time    Turn_Time     Waiting_Time\n");
printf("============================================================================================\n");
    for(i=0;i<no;i++)
    {
printf("\n%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);


    }
    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;

    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");

}

RR


#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
    char p_name[2];
    int cpu_time,arrival_time,finish_time,turn_time;
    int waiting_time,start_time,cpu_temp,flag;
}process;

int main()
{
    int no,i,total_cpu_time=0,j,processes,running_time,process_no=0,current,x,time;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7];
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time and CPU time\n");
        scanf("%s%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
        obj[i].cpu_temp=obj[i].cpu_time;
        obj[i].flag=0;
        total_cpu_time=total_cpu_time+obj[i].cpu_time;
    }

    printf("\nEnter time quantam for each process");
    scanf("%d",&time);

    i=0;
    x=0;
    processes=no;
    running_time=0;
    printf("\nRunning Time  Executig Process\n");
    while(running_time<total_cpu_time)
    {
        for(j=0;j<time;j++)
        {
            if(obj[i].cpu_temp==obj[i].cpu_time)
            {
                obj[i].start_time=running_time+1;
            }
            if(obj[i].cpu_temp!=0)
            {
                obj[i].cpu_temp=obj[i].cpu_temp-1;
                printf("\n\t%d\t%s",running_time,obj[i].p_name);
                running_time=running_time+1;
            }
            if(obj[i].cpu_temp==0 && obj[i].flag!=1)
            {
                obj[i].flag=1;
                obj[i].finish_time=running_time;
                printf("\n%d",obj[i].finish_time);
            }
        }

        i++;
        if(i==no)
        {
            i=0;
        }
    }
//calculates time for each process
    for(i=0;i<no;i++)
    {
        obj[i].turn_time=obj[i].finish_time;
        obj[i].waiting_time=obj[i].turn_time - obj[i].cpu_time;

        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }
//Display all times
    printf("\n----------------------------------------------------------");
    printf("\nP_ID\tArrival_Time  CPU_time  Start_Time  Finish_Time  Turn_Time   Waiting_Time\n");
    printf("\n----------------------------------------------------------");
   
    for(i=0;i<no;i++)
    {
printf("\n%s\t%d\t%d\t%d\t%d\t%d\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);
    }

    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;
   
    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");
}

priority

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct process
{
    char p_name[2];
    int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time,prior;
}process;

int main()
{
    int no,i,total_cpu_time=0,j,processes,running;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7],temp[7];
    process small;
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time, CPU time and Priority\n");
        scanf("%s%d%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time,&obj[i].prior);
        total_cpu_time=total_cpu_time+obj[i].cpu_time;
    }
//Sort according to CPU time
    for(i=0;i<no;i++)
    {
        for(j=i+1;j<no;j++)
        {
            if(obj[i].prior>obj[j].prior)
            {   
                small=obj[i];
                obj[i]=obj[j];
                obj[j]=small;
            }
   
        }
    }

    for(i=0;i<no;i++)
    {
        printf("\n%d",obj[i].prior);   
    }

//First process time
    obj[0].start_time=1;
    obj[0].finish_time=obj[0].cpu_time;   
    obj[0].turn_time=obj[0].cpu_time;
    obj[0].waiting_time=0;
   
    avg_turn_time=obj[0].turn_time;
    avg_waiting_time=0;
//calculates time for each process

    for(i=1;i<no;i++)
    {   
        obj[i].start_time=obj[i-1].finish_time+1;
        obj[i].finish_time=obj[i-1].finish_time+obj[i].cpu_time;
        obj[i].turn_time=obj[i].finish_time;
        obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
       
        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }

//Display all times
printf("\n============================================================================================================");
printf("\nP_ID\tArrival_Time   CPU_time     Start_Time   Finish_Time    Turn_Time     Waiting_Time      Priority\n");
printf("==============================================================================================================\n");
    for(i=0;i<no;i++)
    {
printf("\n%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time,obj[i].prior);       
           
               
    }
    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;
   
    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");
   
}


//----------------Output-------------------//
/*
sainath@ubuntu: ./a.out

Enter no of processes:5

Enter Process ID, Arrival Time, CPU time and Priority
p1
0
14
2

Enter Process ID, Arrival Time, CPU time and Priority
p2
3
8
0

Enter Process ID, Arrival Time, CPU time and Priority
p3
5
5
1

Enter Process ID, Arrival Time, CPU time and Priority
p4
9
12
0

Enter Process ID, Arrival Time, CPU time and Priority
p5
16
6
2

0
0
1
2
2
============================================================================================================
P_ID    Arrival_Time   CPU_time     Start_Time   Finish_Time    Turn_Time     Waiting_Time      Priority
==============================================================================================================

p2    3        8        1        8        8        0        0
p4    9        12        9        20        20        8        0
p3    5        5        21        25        25        20        1
p1    0        14        26        39        39        25        2
p5    16        6        40        45        45        39        2

Average Turnaround Time=27.400000
Average Waiting Time=18.400000



*/

FSFS

#include<stdio.h>
#include<malloc.h>
typedef struct process
{
    char p_name[2];
    int cpu_time,arrival_time,finish_time,turn_time,waiting_time,start_time;
}process;

int main()
{
    int no,i;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7];
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time and CPU time\n");
        scanf("%s%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
    }
//first porocess time
    obj[0].start_time=1;
    obj[0].finish_time=obj[0].cpu_time;   
    obj[0].turn_time=obj[0].cpu_time;
    obj[0].waiting_time=0;
   
    avg_turn_time=obj[0].turn_time;
    avg_waiting_time=0;
//calculates time for each process

    for(i=1;i<no;i++)
    {   
        obj[i].start_time=obj[i-1].finish_time+1;
        obj[i].finish_time=obj[i-1].finish_time+obj[i].cpu_time;
        obj[i].turn_time=obj[i].finish_time-obj[i].arrival_time;
        obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
       
        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }

//Display all times

printf("\nP_ID\tArrival_Time   CPU_time     Start_Time   Finish_Time    Turn_Time     Waiting_Time\n");
    for(i=0;i<no;i++)
    {
printf("\n%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);       
           
               
    }
    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;
   
    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");
   
}

//-------------------Output-------------------
/*


Enter no of processes:5

Enter Process ID, Arrival Time and CPU time
p1
0
6

Enter Process ID, Arrival Time and CPU time
p2
3
9

Enter Process ID, Arrival Time and CPU time
p3
5
2

Enter Process ID, Arrival Time and CPU time
p4
8
10

Enter Process ID, Arrival Time and CPU time
p5
11
6

P_ID    Arrival_Time   CPU_time     Start_Time   Finish_Time    Turn_Time     Waiting_Time

p1    0        6        1        6        6        0
p2    3        9        7        15        12        3
p3    5        2        16        17        12        10
p4    8        10        18        27        19        9
p5    11        6        28        33        22        16

Average Turnaround Time=14.200000
Average Waiting Time=7.600000

*/

Count no. of words and vowels

/*
Program where parent process Counts number of vowels in the given sentence
 And child process will count number of words in the same sentence
*/

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>

parent_p(char str[])
{
    int j,a=0,e=0,i=0,o=0,u=0;
    for(j=0;str[j];j++)
    {
        switch(str[j])
        {
            case 'a':
            case 'A':a++;break;
            case 'e':
            case 'E':e++;break;
            case 'i':
            case 'I':i++;break;
            case 'o':
            case 'O':o++;break;
            case 'u':
            case 'U':u++;break;
        }
    }
    printf("\nNumber of a or A = %d",a);
    printf("\nNumber of e or E = %d",e);
    printf("\nNumber of i or I = %d",i);
    printf("\nNumber of o or O = %d",o);
    printf("\nNumber of u or U = %d\n",u);   
}
       
child_p(char str[])
{
    int words=0,i=0,flag=0;
    while(str[i]!='\0')
    {
        while(str[i]==' ' && str[i]!='\0')
        {
            i++;
        }
        while(str[i]!=' ' && str[i]!='\0')
        {
            i++;
            flag=1;
        }
        if(flag)
        {
            words++;
            flag=0;
        }
    }
    printf("\nNumber of words = %d\n",words);
}

int main()
{
    pid_t pid;
    char str[50];
    printf("\nEnter the string\t");
    gets(str);
    pid = fork();
    if(pid<0)
    {
        printf("\nError in process creation");
        exit(-1);
    }
    else if(pid==0)
    {
        printf("\nChild ID = %d",getpid());
        child_p(str);
        exit(0);
    }
    else
    {
        printf("\nParent ID = %d",getppid());
        execlp("a.out","Fork_Vowel.c",parent_p(str),NULL);
        sleep(10);
        wait();
       
        exit(0);
    }
    return 0;
}


       

Unix process control (sort elements)

 /*

    Tile: Program where parent process sorts array elements in descending order
    and child process sorts array elements in ascending order.
*/




//#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>

void ascending(int *a,int size)
{
    int i,j,temp;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            if(a[i] < a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;   
            }
        }
    }
    printf("\nElements in ascending order..\n");
    for(i=0;i<size;i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n");
}

void descending(int *a,int size)
{
    int i,j,temp;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            if(a[i] > a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;   
            }
        }
    }
    printf("\nElements in descending order...\n");
    for(i=0;i<size;i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n");
}

int main()
{
    int *a,size,i,status;
    pid_t pid,pid1;
    printf("\nEnter the size of array\t");
    scanf("%d",&size);
    a = (int*)malloc(size * sizeof(int));
    printf("\nEnter %d elements\n",size);
    for(i=0;i<size;i++)
    {
        scanf("%d",&a[i]);
    }
    pid = fork();
    if(pid<0)
    {
        printf("\nError in process creation");
        exit(-1);
    }
    else if(pid==0)
    {
        printf("\nChild Process");
        printf("\nChild PID = %d",getpid());
        ascending(a,size);
        exit(0);
    }
    else
    {
        printf("\nParent process");
        printf("\nParent PID = %d",getppid());
        pid1 = wait(&status);
        printf("\nPID1 = %d\nStatus = %d",pid1,status);
        descending(a,size);
        exit(0);
    }
    return 0;
}
   

Producer Consumer



#include<stdio.h>

void produce(int data);
void consume();
void wait();
void signal();
void psignal();
void pwait(int data);
void cwait();
void csignal();
void display();
int buffer[10];
int front,rear,size;

struct semaphore
{
 int mutex;
}s;

struct list
{
 int num;
 struct list *next;
}*hp1,*hp2,*cur,*p,*cp,*q;

 void main()
 {
  int ch,k;

  s.mutex=1;
  front=0;
  rear=0;
  hp1=hp2=NULL;
  printf("\nPRODUCER CONSUMER PROBLEM");
  printf("\nEnter the size of buffer (<10) : ");
  scanf("%d",&size);
  size++;

  do
  {
   printf("\n1. Produce an item\n2. Consume an item\n3. exit");
   printf("\n\nEnter your choice : ");
   scanf("%d",&ch);

   switch(ch)
   {
    case 1: wait();
        printf("\nEnter data to be produced : ");
        scanf("%d",&k);
        produce(k);
        csignal();

        printf("\n\n");

        display();
        signal();
        break;
    case 2: wait();
        consume();
        psignal();
        printf("\n\n");
        display();
        signal();
        break;
    case 3: exit(0);
   }
  }while(ch<3);

}

void produce(int data)
{
 if((rear+1)%(size)==front)
 {
  pwait(data);
  printf("\nProducer is Waiting");
 }
 else
 {
  rear=(rear+1)%size;
  buffer[rear]=data;

  printf("\nITEM PRODUCED IS : %d",data);
 }
}

void consume()
{
 int no;
 if (front==rear)
 {
  cwait();
  printf("\nConsumer is waiting");
 }
 else
 {
  front=(front+1)%size;
  no=buffer[front];
  buffer[front]=0;
  printf("\nITEM CONSUMED IS : %d",no);
 }
}

void pwait(int data)
{
 cur=(struct list*)malloc(sizeof(struct list));
 cur->next=NULL;
 cur->num=data;
 if(hp1==NULL)
 {
  hp1=cur;
 }
 else
 {
  q=hp1;
  while(q->next!=NULL)
  q=q->next;
  q->next=cur;
 }
}

void cwait()
{
 cp=(struct list*)malloc(sizeof(struct list));
 cp->next=NULL;
 cp->num=1;
 if(hp2==NULL)
 {
  hp2=cur;
 }
 else
 {
  q=hp2;
  while(q->next!=NULL)
   q=q->next;
  q->next=cp;
 }
}

void csignal()
{
 struct list *c;
 if(hp2==NULL)
  return;
 else
 {
  consume();
  c=hp2;
  hp2=hp2->next;
  free(c);
 }
}

void psignal()
{
 struct list *r;
 if(hp1==NULL)
  return;
 else
 {
  produce(hp1->num);
  r=hp1;
  hp1=hp1->next;
  free(r);
 }
}

void wait()
{
 while(s.mutex==0);
 if(s.mutex==1)
  s.mutex=0;
 }

void signal()
{
 s.mutex=s.mutex+1;
}

void display()
{
 int i,count;
 if(front==-1)
  i=0;
 else
  i=front;
 printf("\n\n");
 for(count=0;count<size;)
 {
  printf("\t%d",buffer[i]);
  count++;
  i=(i+1)%size;
 }
}
/*
        /************OUTPUT***********

PRODUCER CONSUMER PROBLEM
Enter the size of buffer (<10) : 2

1. Produce an item
2. Consume an item
3. exit

Enter your choice : 1

Enter data to be produced : 1

ITEM PRODUCED IS : 1




               1       0
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 1

Enter data to be produced : 2

ITEM PRODUCED IS : 2



               1       2
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 1

Enter data to be produced : 3

Producer is Waiting



               1       2
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 2


ITEM CONSUMED IS : 1
ITEM PRODUCED IS : 3



              2       3
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 2

ITEM CONSUMED IS : 2



               3       0
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 2

ITEM CONSUMED IS : 3



               0       0
1. Produce an item
2. Consume an item
3. exit

Enter your choice : 2

Consumer is waiting



              0       0
1. Produce an item
2. Consume an item
3. exit

Enter your choice :


         */

page repalcement


/* Page Replacement */

#include<stdio.h>
#include<conio.h>
#define frame_size 3
int search(int frame[],int what)
{
    int i;
    for(i=0;i<frame_size;i++)
    {
    if(what==frame[i])
        return i;
    }
    return -1;

}

int compare(int count[])
{
    int i,max,index;
    max=count[0];
    index=0;
    for(i=1;i<frame_size;i++)
    {
        if(count[i]>max)
        {
            max=count[i];
            index=i;
        }
    }
    return index;
}
int fifo(int page[],int page_size)
{
    int i,j,frame[10],flag,pf,num;
    for(i=0;i<frame_size;i++)
    {
        frame[i]=-1;

    }
    num=0;
    j=0;
    pf=0;
    flag=0;

    while(num<page_size)
    {
        if(search(frame,page[num])!=-1)
            flag=0;
        else
        {
            frame[j]=page[num];
            pf++;
            j=(j+1)%frame_size;
            flag=1;
        }

        if(flag)
        {
            printf("\nPage fault occured");
         }

        for(i=0;i<frame_size;i++)
        {
            if(frame[i]==-1)
                printf("\nFrame [%d] : ",i);
            else
                printf("\nFrame [%d] : %d",i,frame[i]);
        }

            num++;
            printf("\n\n");
         }

    printf("Total no. of pages : %d",pf);


return 0;
}


int lru(int page[],int page_size)
{
    int i,j,frame[frame_size],count[frame_size],flag,pf,num,index;
    for(i=0;i<frame_size;i++)
    {
        frame[i]=-1;
        count[i]=0;

    }
    num=0;
    j=0;
    pf=0;
    flag=0;

    while(num<page_size)
    {
        if(index=search(frame,page[num])!=-1)
        {
            count[index]=0;
            flag=0;
        }
        else
        {
            index=compare(count);
            frame[index]=page[num];
            count[index]=0;
            pf++;
            flag=1;
        }


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

{
    if(i!=index)
            count[i]++;
}

        if(flag)
        {
            printf("\nPage fault occured");
         }

        for(i=0;i<frame_size;i++)
        {
            if(frame[i]==-1)
                printf("\nFrame [%d] : ",i);
            else
                printf("\nFrame [%d] : %d",i,frame[i]);
        }

            num++;
            printf("\n\n");
         }

    printf("Total no. of pages : %d",pf);


return 0;
}



int opt(int page[],int page_size)
{
    int i,j,frame[frame_size],count[frame_size],flag,pf,num,index;
    for(i=0;i<frame_size;i++)
    {
        frame[i]=-1;

    }
    num=0;
    j=0;
    pf=0;
    flag=0;

    while(num<page_size)
    {
        if(search(frame,page[num])!=-1)
        {
            flag=0;
        }
        else
        {


            for(i=0;i<frame_size;i++)
            {
                count[i]=0;
            }
            for(i=0;i<frame_size;i++)
            {
            for(j=num;j<page_size;j++)
            {
                if(frame[i]==page[j])
                {
                    break;
                }
                else
                    count[i]++;
            }
            }



            index=compare(count);
            frame[index]=page[num];
            count[index]=0;
            pf++;
            flag=1;
        }



        if(flag)
        {
            printf("\nPage fault occured");
         }

        for(i=0;i<frame_size;i++)
        {
            if(frame[i]==-1)
                printf("\nFrame [%d] : ",i);
            else
                printf("\nFrame [%d] : %d",i,frame[i]);
        }

            num++;
            printf("\n\n");
         }

    printf("Total no. of pages : %d",pf);


return 0;
}





int main()
{
    int ch,i;
    int page[30],size;
    printf("\nEnter no. of pages : ");
    scanf("%d",&size);
    printf("\nEnter %d pages : \n",size);
    for(i=0;i<size;i++)
    {
    scanf("%d",&page[i]);
    }
    do
    {
        printf("\n\n *** Menu ***");
        printf("\n1) FIFO");
        printf("\n2) LRU");
        printf("\n3) OPT");
        printf("\nEnter your choice : ");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            fifo(page,size);
            break;
           case 2:
            lru(page,size);
            break;
        case 3:
            opt(page,size);
            break;
         default:
            printf("Invalid choice");
        }
    }while(1);
    getch();
}



Memory Management (first fit, best fit, worst fit)


/* Memory Management */


#include<stdio.h>
#include<conio.h>
void menu()
{
    printf("\n Memory management !!!");
    printf("\n ** Menu **");
    printf("\n 1) First Fit");
    printf("\n 2) Best Fit");
    printf("\n 3) Worst Fit");
}

void accept(int a[],int n)
{
 int i;
 for(i=0;i<n;i++)
 {
    scanf("%d",&a[i]);
 }

}


void display(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
    printf("\t %d",a[i]);
    }

}


// ascending order

void sort(int a[],int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-1;j++)
     {
        if(a[j] > a[j+1])
        {
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;

        }

     }
    }

}



// decending order
void sort1(int a[],int n)
{
    int i,j,temp;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-1;j++)
     {
        if(a[j] < a[j+1])
        {
            temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;

        }

     }
    }

}



void first_fit(int psize[],int np,int msize[],int nm)
{
    int i,j,itot,etot,flag[30]={0};

    itot=etot=0;
    for(i=0;i<np;i++)
    {

     for(j=0;j<nm;j++)
     {
        if(flag[j]==0 && msize[j] >=psize[i])
        {
         flag[j]=1;
         itot=itot+msize[j]-psize[i];
         break;
        }

     }
     if(j==nm)
     {
        printf("\nThere is no space for process : %d",i);

     }

    }

    for(i=0;i<nm;i++)
    {
        etot=etot+msize[i];
    }


     printf("\nProcess : ");
     display(psize,np);
     printf("\nMemory : ");
     display(msize,nm);

     printf("\nInternal fragmentation : %d",itot);
     printf("\nExternal fragmentation : %d",etot);

}


void best_fit(int psize[],int np,int msize[],int nm)
{
    int i,j,itot,etot,temp[30],flag[30]={0};

    itot=etot=0;
    for(i=0;i<nm;i++)
        temp[i]=msize[j];

    sort(temp,nm);

    for(i=0;i<np;i++)
    {

     for(j=0;j<nm;j++)
     {
        if(flag[j]==0 && temp[j] >=psize[i])
        {
         flag[j]=1;
         itot=itot+temp[j] - psize[i];
         break;
        }

     }
     if(j==nm)
     {
        printf("\nThere is no space for process : %d",i);

     }

    }


    for(i=0;i<nm;i++)
    {
        etot=etot+msize[i];
    }
     printf("\nProcess : ");
     display(psize,np);
     printf("\nMemory : ");
     display(temp,nm);

     printf("\nInternal fragmentation : %d",itot);
     printf("\nExternal fragmentation : %d",etot);


}


void worst_fit(int psize[],int np,int msize[],int nm)
{
    int i,j,itot,etot,temp[30],flag[30]={0};

    itot=etot=0;
    for(i=0;i<nm;i++)
        temp[i]=msize[j];

    sort1(temp,nm);

    for(i=0;i<np;i++)
    {

     for(j=0;j<nm;j++)
     {
        if(flag[j]==0 && temp[j] >=psize[i])
        {
         flag[j]=1;
         itot=itot+temp[j] - psize[i];
         break;
        }

     }
     if(j==nm)
     {
        printf("\nThere is no space for process : %d",i);

     }

    }

    for(i=0;i<nm;i++)
    {
        etot=etot+msize[i];
    }
     printf("\nProcess : ");
     display(psize,np);
     printf("\nMemory : ");
     display(temp,nm);

     printf("\nInternal fragmentation : %d",itot);
     printf("\nExternal fragmentation : %d",etot);


}


void main()
{

    int ch,np,nm,psize[30],msize[30];
//    clrscr();

    printf("\nEnter no. of process : ");
    scanf("%d",&np);
    printf("\nEnter size of process : ");
    accept(psize,np);

    printf("\nEnter no. of memory : ");
    scanf("%d",&nm);
    printf("\nEnter size of memory : ");
    accept(msize,nm);


    while(1)
    {
     menu();
     printf("\n Enter your choice : ");
     scanf("%d",&ch);

     switch(ch)
     {
      case 1:
        printf("\n\n First Fit : \n");
        first_fit(psize,np,msize,nm);
        break;


      case 2:
        printf("\n\n Best Fit : \n");
        best_fit(psize,np,msize,nm);
        break;


     case 3:
        printf("\n\n Next Fit : \n");
        worst_fit(psize,np,msize,nm);
        break;


     case 4:
        exit(0);

     default:
     printf("\n Wrong Choice !!!");

     }

 getch();

 }
}










/*                 OUTPUT

ENTER NO OF PROCESSES::5
ENTER SIZES OF PROCESSES::10 20 15 30 45
ENTER NO MEMORY HOLES::7
ENTER SIZES OF MEMORY HOLES::5 15 10 35 25 20 25

                **MAIN MENU**
        MEMORY MANAGEMENT
        1.FIRST FIT
        2.BEST FIT
        3.WORST FIT
        4.QUIT

ENTER YOUR CHOICE::1

FIRST FIT::
THERE IS NO SPACE FOR PROCESS 3
THERE IS NO SPACE FOR PROCESS 4
PROCESSES::
        10      20      15      30      45
MEMORY HOLES::
        5       15      10      35      25      20      25

TOTAL SUM OF INTERNAL FRAGMENTATION = 30
TOTAL SUM OF EXTERNAL FRAGMENTATION = 60

ENTER YOUR CHOICE::2

        BEST FIT::
THERE IS NO SPACE FOR PROCESS 4
PROCESSES::
        10      20      15      30      45
MEMORY HOLES::
        5       10      15      20      25      25      35
TOTAL SUM OF INTERNAL FRAGMENTATION = 5
TOTAL SUM OF EXTERNAL FRAGMENTATION = 55

ENTER YOUR CHOICE::3
        WORST FIT::
PROCESSES::
        10      20      15      30      45
MEMORY HOLES::
        5       10      15      20      25      25      35

TOTAL SUM OF INTERNAL FRAGMENTATION = 40
TOTAL SUM OF EXTERNAL FRAGMENTATION = 50

ENTER YOUR CHOICE::4                    */

bankers

/* Bankers Algorithm */


#include<stdio.h>
#include<conio.h>
void main()
{
    int np,nr,i,j,k,flag=0,count=0;

    int fin[5],avalres[3],work[3],max[5][3],alloc[5][3],need[5][3];
    printf("Enter no. of processes : ");
    scanf("%d",&np);

    printf("Enter no. of resources : ");
    scanf("%d",&nr);

    printf("Enter available resouces %d",nr);

    for(j=0;j<nr;j++)
    {
        scanf("%d",&avalres[j]);
    }


    printf("\nEnter allocation for each process : ");
    for(i=0;i<np;i++)
    {
    for(j=0;j<nr;j++)
    {
        scanf("%d",&alloc[i][j]);
    }
    }


    printf("\n Enter max of each process : ");
    for(i=0;i<np;i++)
    {
    for(j=0;j<nr;j++)
    {
        scanf("%d",&max[i][j]);
    }
    }




    printf("\n Allocation : ");
    for(i=0;i<np;i++)
    {
        printf("\n");
    for(j=0;j<nr;j++)
    {
    printf("  %d  ",alloc[i][j]);
    }
    }


    printf("\nMax : ");
    for(i=0;i<np;i++)
    {
        printf("\n");
    for(j=0;j<nr;j++)
    {
        printf("  %d  ",max[i][j]);
    }
    }



    printf("\nNeed : ");
    for(i=0;i<np;i++)
    {
        printf("\n");
    for(j=0;j<nr;j++)
    {

        need[i][j]=max[i][j]-alloc[i][j];
        printf("  %d  ",need[i][j]);
    }
    }


    printf("\nAvailable Resorces : ");
    for(j=0;j<nr;i++)
    {
        printf("  %d  ",avalres[j]);
    }


printf("\n\n");

for(i=0;i<np;i++)
{
    fin[i]=0;
}

for(i=0;i<nr;i++)
    work[i]=avalres[i];

printf("\n\n Safety sequence : ");

while(1)
{

    for(i=0;i<np;i++)
    {

        flag=0;
        if(fin[i]==0)
        {
                for(j=0;j<nr;j++)
                {

                    if(work[j]<need[i][j])
                    {
                        flag=1;
                        break;
                    }
                    else
                        flag=0;
                   

                }


if(flag==0)
{

    for(k=0;k<nr;k++)
    {
        fin[i]=1;
        work[k]=work[k]+alloc[i][k];
    }
    printf("\t P %d",i);
    count++;

}
}
}

if(count==np)
    break;



}// end of while


}// end of main