Wednesday 8 May 2013

OPERATING SYSTEM LAB MANUAL

       
                    Index

E.No    NAME OF THE EXPERIMENT
1    STUDY OF LINUX
2.    UNIX COMMANDS
3.A    INVOKE SYSTEM CALLS
3.B    INVOKE SYSTEM CALLS - fork(), getpid()
4    I/O SYSTEM CALLS
5.A    SIMULATION OF  grep COMMAND
5.B    SIMULATION OF ls  COMMAND
6.A    SCHEDULING ALGORTHIM-FCFS
6.B    SCHEDULING ALGORTHIM- SJF
7.A    SCHEDULING ALGORTHIM ROUND ROBIN
7.B    SCHEDULING ALGORTHIM PRIORITY
8    IMPLEMENT PRODUCER CONSUMER PROBLEM-SEMAPHORE
9    MEMORY MANAGEMENT SCHEME I
10    MEMORY MANAGEMENT SCHEME II











































1. STUDY OF LINUX
•    Early computers had a teletype machine with a keyboard for I/O. Later, glass terminals became the norm, but the behavior was much the same — a keyboard, a screen, a text display. A program was responsible for mediating the transaction between the operator and the machine, and as the years passed this program (the command interpreter or shell) became more sophisticated.
•    At this stage the command shell has become rather too sophisticated, typically having a dozen ways to do any particular thing. In this page I will try to limit myself to describing a handful of useful operations, based not on listing everything that can be done, but on solving specific problems. There are some links at the bottom of this page for those wanting more depth.
Preliminaries
•    There are two primary ways to use the shell: interactively and by writing shell scripts.
o    In the interactive mode, the user types a single command (or a short string of commands) and the result is printed out.
o    In shell scripting, the user types anything from a few lines to an entire program into a text editor, then executes the resulting text file as a shell script.
o    It is often the case that an interactive session becomes a shell scripting session, once things get too complicated for simple interactive line entries, or because a specific sequence of commands appears to be generally useful and worth preserving.
•    In a modern Linux environment the user can have more than one shell open at a time, either by moving between a sequence of independent "virtual terminals" in a text-only environment, or by opening any number of shell windows in the X Windows environment.
•    The advantage of having more than one shell available is that one shell can be used for testing one command at a time, while another might provide a text editor for assembling single commands into a shell program.
•    I don't want to get too distribution-specific, but if you are not hosting X Windows and want more than one simultaneous shell session, with many current distributions you can switch between "virtual terminals" by pressing Ctrl+Alt+F(n), n typically between 1 and 6.
•    In an environment that supports X Windows, simply open any desired number of command shell windows and move between them.
Simple Stuff
•    First, a convention. I'll list things for you to type in this format:
$ date                                             
I will list the computer's reply like this:
Tue Dec 23 10:52:51 PST 2003
Notice the "$" symbol in the user entry above. This is a generic shell prompt, and yours will almost certainly look different (but it will include a similar symbol). I'll be using one of two prompts (this is a common convention, worth remembering): I'll use "$" to refer to a normal user session, and "#" to refer to a root session.
•    NOTE: Avoid using root sessions and permissions unless it is required. Misused root authority can cause very serious harm to your system. Since this is a tutorial in which you will want to experiment with different commands, limit the chance for harm by doing so as an ordinary user.
•    To put this another way, enter this example:
#whoami
root
If your session produced the result shown above, please — log out and become an ordinary user.
•    In shell programming, spaces matter. If you see spaces between words and characters in these examples, be sure to include the spaces.
•    In shell programming, case matters also. If you don't get the results shown on this page, look at the case of your entries.
Where are you?
•    As you may be aware, a Linux filesystem is in the form of a large tree with many branches called "subdirectories". When you issue a shell command, it is often necessary to know where you are in the "tree". Type this example:
$pwd
/path/path/path
When you try this example ("pwd" means "print working directory"), your current working directory will be printed.
•    You can decide where you are in the tree. Type this example:
$cd ~
$pwd
/home/username
The symbol "~" is a special shortcut character that can be used to refer to your home directory. You could have typed this —
$ cd /home/username                                                                    
and accomplished the same result, but if you think about it, the "~" character is more portable. Later, when you are writing shell scripts, you might want a command that moves to any user's home directory.
Listing Files
•    Directories contain files, and you can list them in a simple, compact format:
$ls
filename filename filename ...
Or you can list them in more detail:
$-la
(detailed list, one file per line)
And, very important, to find out what a command's options are, use the "man" (manual) command:
$ man    ls
(manual page for "ls")
NOTE: The "man" command allows you to learn a command's options.
To find files by name:
$    find    -name    '*.jpg'
(list of files with .jpg suffix in current and all child directories)
To create a text diagram of the directory tree:
$    tree    -d .
(diagram of the directory tree from the current directory)
The "tree" command is less useful now that directory trees have become so complicated, and now that most distributions support X Windows and sophisticated filesystem browsers.
Examining Files
•    There are a number of things you can do to find out more about the files in the list. Here are just a few:
The "file" command tries to identify files by examining their contents:
$    file    tux_small.png                            
tux_small.png: PNG image data, 128 x 151, 8-bit/color RGB, non-interlaced
The next example uses the obscurely named "cat" command. It prints the contents of a file. Unfortunately if the file's contents are not readable, they get printed anyway.
$    cat    zipcodes.txt
(prints the entire contents of a file named "zipcodes.txt")
If a file is too long to be viewed on one page, you can say:
$    more    zipcodes.txt
(prints file one screenful at a time)
You can also use "grep" to print only those parts of a file you are interested in:
$    grep    10001    zipcodes.txt
(prints only those lines that have the character string "10001" in them)
The "grep" command is very useful, unfortunately it has a difficult-to-remember name. Be sure to:
$ man grep
There are many, many more shell commands to learn and to use.










2.UNIX COMMANDS



BASIC COMMANDS:

$ls    -     To display all the files in the current directory.
$man    -    To get the Syntax and details of commands.
$date    -    To display the current date and time.
$who    -    To display the list of terminals connected to the system.


COMMANDS RELATED TO FILES OR DIRECTORIES:

$mkdir    -    To create a directory.
$rmdir    -    To remove directory.
$cd    -    Changes the directory to home directory.


FILE RELATED COMMANDS:

$cp    -    To copy files or a group of files.
$mv    -    To move a file.
$rm    -    removes particular file from a directory.
&cat>    -    used to concatenate and view the file.



UNIX UTILITIES:

$read    -    reads inputs from user and assign it to the variable specified.
$echo    -    Display the values or statements.



GENERAL PURPOSE UTILITIES:

more    -    command grouping
tty    -    prints the terminal type
wc    -    used to count the number of lines, words and char in a file a standard             
    input.
$cmp    -    used to count the number of lines, words and char in a file or from a
    standard input.
$diff    -    used to display the file different and the lines in one file that has to be
        changed to make the two files in\identical.
FILTERS:

$sort    -    Sorts the contents of the given file based on the first character of each line.
$cut    -    Cuts or picks up the given number of characters or fields from the
    specified file.
$grep    -    Searches the input fully for a match with the supplied pattern and displays
    it
$head    -    Displays the top end line of a file.
$tail    -    Displays last ten lines of a file as a default.
$paste    -    To fix to cut portions of a file vertically.




















                           
3.A.INVOKE SYSTEM CALLS
AIM:
    To write a C program to invoke system calls(readdir(), opendir(),   closedir())

ALGORITHM:

STEP1: Start the process.

STEP2: read the directory name from the user.

STEP3: Open the directory.

STEP4: Display the content of the directory.

STEP5: Close the directory.

STEP6: Stop the process.









PROGRAM:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])


    {
    char buff[256];
    DIR *dirp;
    printf("\n\n Enter directory Name");
    scanf("%s",buff);
    if((dirp=opendir(buff))==NULL)


        {
        printf("Error");
        exit(1);
    }


    while(dptr=readdir(dirp))


        {
        printf("%s\n",dptr->d_name);
    }


    closedir(dirp);
}
OUTPUT:



 Enter directory Name
inf


in
eng



3.B. INVOKE SYSTEM CALLS - fork(), getpid()
AIM:
    To write a C program to invoke system calls (fork(), getpid() )

ALGORTHIM:

STEP1: Start the process

STEP2: Get the Process ID using getpid()

STEP3: Get the parents ID using getpid()

STEP4:Get the child ID

STEP5: Display the parents, process, child ID’S

STEP6: Stop the process.

PROGRAM:
#include<stdio.h>
int main()
    {
    int pid;
    printf("\n\n Fork Function Testing");
    printf("\n\n My Process ID=%d",getpid());
    printf("\n\n My Parent's ID=%d",getpid());
    if(pid==-1)
       {
        printf("\n\n Error occurred... \n Process cannot be created ");
    }


    if(pid!=0)
     {
        printf("\n\n Parent : My ID=%d",getpid());
        printf("\n\n My Parent ID=%d",getpid());
    }




    else
        {
        printf("\n\nChild : My ID=%d",getpid());
        printf("\n\n My Parents's ID=%d",getpid());
    }
return 0;
}
OUTPUT:

Fork Function Testing

 My Process ID=23709

 My Parent's ID=23709

Child : My ID=23709

 My Parents's ID=23709

4.I/O SYSTEM CALLS


AIM:
         To write a c program to implement the file operation using i/o system calls

ALGORTHIM:

Step1: Include the header file.

Step2: Create a file.

Step3: Get the keyboard i/p from the user.

Step4: Write contents of file.

Step5: While reading contents, if a new line character occurs, increase the number of lines.

Step6: Similar steps are followed for character increment also.

Step7: End program.



PROGRAM:

#include<fcntl.h>
#include<stdio.h>
#include<string.h>

int main()
{
        int fd,sz;
char buffer[50];
fd=open("info9",O_WRONLY|O_TRUNC);
if(fd<0)
        printf("ERROR");
scanf("%s",buffer);
sz=write(fd,buffer,strlen(buffer));
close(fd);
}

OUTPUT:
[tit04@linuxserver ~]$ ./a.out
information
[tit04@linuxserver ~]$ cat info9
information





















5.A.SIMULATION OF  grep COMMAND


AIM:
    To write a C program to simulate grep command

ALGORITHM:

Step 1:        Get the string to be searched from the user.

Step 2:        Pass the string to function signal () as a parameter.

Step 3:        Search for the string using for loop.

Step 4:    Display the information, If the string is found, display ‘The string is found’ along with the line in which string is found.

Step 5:    If string is not found, try searching for next time.


PROGRAM :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1024
void usage()
{
printf("usage:\t grep filename pattern\n");
printf("example:grep grep.c int\n");
}
int main(int argc,char *argv[])
{
FILE *fp;
char fline[max];
char *newline=0;
int count=0;
int occurences=0;

if(argc!=3)
{
usage();
exit(1);
}
if(!(fp=fopen(argv[1],"r")))
{
printf("grep:could not openfile:%s\n",argv[1]);
exit(1);
}
while(fgets(fline,max,fp)!=NULL)
{
count++;
if(newline=strchr(fline,'\n'))
*newline='\0';
if(strstr(fline,argv[2])!=NULL)
{
printf("%s%d%s\n",argv[1],count,fline);
occurences++;
}
}
return 0;
}

OUTPUT:

[tit04@linuxserver ~]$ ./a.out sat in
sat1info
sat2institute
sat4engineering

5.B.SIMULATION OF ls  COMMAND


AIM:
    To write a C program to simulate ls command


ALGORITHM :

Step 1:        Open the current directory

Step 2:        Read all the files in directory.

Step 3:        Display the node number of each file.

Step 4:        Display the name of all files
                           
PROGRAM :
#include<dirent.h>
#include<stdio.h>
int main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
getcwd(pathname);
n=scandir(pathname,&namelist,0,alphasort);
if(n<0)
        printf("Error");
else
{
        for(i=0;i<n;i++)
                printf("%s\n",namelist[i]->d_name);
}
}









OUTPUT:

[tit04@linuxserver ~]$ ./a.out
in
in1
in10
in10a.c
in10b
in10b.c
in11
in12
in13
in14

6.A.SCHEDULING ALGORTHIM-FCFS
AIM:
          To write a c program to implement fcfs algorthim.

ALGORTHIM:

Step1:read the process,process time.

Step2:find the waiting time.

Step3:calculate turn around time=waiting time+burst time

Step2:Calculate the average waiting time and average turn around time

Step5:display the details.

PROGRAM :

#include<stdio.h>
int main()
{
 int nop,wt=0,tat=0,i;
 int bt[10];
 float totwt=0.0,tottat=0.0;
 float avgwt=0.0,avgtat=0.0;


 printf("\n\tEnter the number of process:");
 scanf("%d",&nop);
 for(i=1;i<=nop;i++)
 {
  printf("\n\tEnter the burst time for the process%d in msecs:",i);
  scanf("%d",&bt[i]);
 }
 for(i=0;i<nop;i++)
 {
  bt[0]=0;
  wt = wt + bt[i];
  totwt=totwt + wt;
 }
 printf("\n\t\tTotal waiting time: %0.2f ms",totwt);
 avgwt=(float)totwt/(float)nop;
 printf("\n\t\tAverage waiting time:%0.2f ms",avgwt);
 for(i=0;i<=nop;i++)
 {
   tat=tat + bt[i];
   tottat=tottat + tat;
 }
 printf("\n\t\tTotal Turn around time: %0.2f ms",tottat);
 avgtat=(float)tottat/(float)nop;
 printf("\n\t\tAverage Turn around time: %0.2f ms",avgtat);
 return 0;
}

OUTPUT:

[tit04@linuxserver ~]$ ./a.out

        Enter the number of process:3
        Enter the burst time for the process1 in msecs:24
        Enter the burst time for the process2 in msecs:15
        Enter the burst time for the process3 in msecs:33
                Total waiting time: 63.00 ms
                Average waiting time:21.00 ms
                Total Turn around time: 135.00 ms
                Average Turn around time: 45.00 ms


6.B.SCHEDULING ALGORTHIM- SJF
AIM:
          To write a c program to implement sjf algorthim.

ALGORTHIM:

Step1:read the process, process time and sort the process according  burst time.

Step2:find the waiting time calculate turn around time by adding
waiting and process Time.
                    
                                                                                                                                                  Step3:calculate the average waiting time and average turn around time.

Step4: Display the result.

PROGRAM :

#include<stdio.h>
int main()
{
        int nop,i,j,t;
        int bt[10],wt=0,tat=0;
        float totwt=0.0,tottat=0.0;
        float avgwt=0.0,avgtat=0.0;
        printf("\n\tEnter the number of processes:");
        scanf("%d",&nop);
        for(i=1;i<=nop;i++){
                printf("\n\tEnter the burst time for process %d in ms:",i);
                scanf("%d",&bt[i]);
        }
        for(i=1;i<=nop;i++)
        for(j=1;j<=nop;j++)
        {
                if(bt[i] < bt[j])
                {
                        t=bt[i];
                        bt[i]=bt[j];
                        bt[j]=t;
                }
        }
      
 for(i=0;i<nop;i++)
        {
                bt[0]=0;
                wt=wt + bt[i];
                totwt = totwt + wt;
        }
        printf("\n\t\tTotal waiting time: %0.2f ms",totwt);
        avgwt=(float)totwt / (float)nop;
        printf("\n\t\tAverage waiting time: %0.2f ms",avgwt);
        for(i=1;i<=nop;i++)\
        {
                tat=tat + bt[i];
                tottat=tottat + tat;
        }
        printf("\n\t\tTotal turn around time: %0.2f ms",tottat);
        avgtat=(float)tottat / (float)nop;
        printf("\n\t\tAverage Turn around time: %0.2f ms",avgtat);
        return 0;

}               
OUTPUT:

[tit04@linuxserver ~]$ ./a.out
Enter the number of processes:3

        Enter the burst time for process 1 in ms:6

        Enter the burst time for process 2 in ms:9

        Enter the burst time for process 3 in ms:4

                Total waiting time: 14.00 ms
                Average waiting time: 4.67 ms
                Total turn around time: 33.00 ms
                Average Turn around time: 11.00 ms

7.A.SCHEDULING ALGORTHIM ROUND ROBIN


  AIM:
        To write a c program to implement round-robin scheduling.

ALGORITHM:

Step1:read the time quantum,process,process time

Step2:execute the process for time=time quantum

Step3:after all the process have been executed display the details.

PROGRAM:

#include<stdio.h>
int main()
{
int bursttime[10][10],bt[10][10];
int i,j,nop,k=0,tq,sum=0;
float avg;
printf("\nEnter the Number of processes:");
scanf("%d",&nop);
printf("\n\tEnter the time quantum;");
scanf("%d",&tq);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
        bursttime[i][j]=0;
bt[i][j]=0;
}
for(i=0;i<nop;i++)
{
        j=0;
printf("\n\tEnter the process time for process %d;",i+1);
scanf("%d",&bursttime[i][j]);}
for(j=0;j<10;j++)
for(i=0;i<nop;i++)
{
                bt[2*j][i]=k;

if((bursttime[i][j]<=tq)&&(bursttime[i][j]!=0))
{
                k=k+bursttime[i][j];
bt[2*j+1][i]=k;
}
else if(bursttime[i][j]!=0)
{
                bursttime[i][j+1]=bursttime[i][j]-tq;
                k=k+tq;
                bt[2*j+1][i]=k;
}
else
{
                bt[2*j][i]=0;
                bt[2*j+1][i]=0;
}
}

for(i=0;i<nop;i++)
        sum=sum+bt[0][i];
for(i=0;i<nop;i++)
for(j=1;j<10;j++)
{
        if((bt[j][i]!=0)&&(bt[j+1][i]!=0)&&((j+1)%2==0))
        {
                sum=sum+((bt[j+1][i]-bt[j][i]));
        }
}
avg=(float)sum/nop;
printf("\n\n\t\tAverage waiting time:%0.2f millisecs",avg);
sum=avg=0;
for(j=0;j<nop;j++)
{
        i=1;
        while(bt[i][j]!=0)
                i++;
        sum=sum+bt[i-1][j];
}
avg=(float)sum/nop;
printf("\n\n\t\tAverage Turnaround time: %0.2f ms\n",avg);
return 0;
}


OUTPUT:

[tit04@linuxserver ~]$ ./a.out

Enter the Number of processes:3

        Enter the time quantum;6

        Enter the process time for process 1;9

        Enter the process time for process 2;4

        Enter the process time for process 3;7


                Average waiting time:9.67 millisecs

                Average Turnaround time: 16.33 ms



7.B.SCHEDULING ALGORTHIM PRIORITY


  AIM:
        To write a c program to implement round priority scheduling.

ALGORITHM:

Step1: read the process,process time and priority.

Step2: sort the process according to their priorities.

Step3: find the waiting time

Step4: calculate the turn around time=waiting time+bus time

Step5: display the details.

   

PROGRAM :

#include<stdio.h>
#include<string.h>
int main()
{
  float avgwt,avgtt;
char pname[10][10],c[10][10];
int wt[10],pt[10],tt[10],bt[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=10;
printf("\n\n Enter the no of process");
scanf("%d",&n);
printf("\n\nEnter the name and bursttime");
for(i=0;i<n;i++)
{
  printf("\n\nNAME:");
  scanf("%s",pname[i]);
printf("\n\n BURSTTIME");
scanf("%d",&bt[i]);
}
printf("\n\n enter the priority of the process:");
for(i=0;i<n;i++)
{
 printf("\n\n priority of the process");
 scanf("%d",&pt[i]);
}
    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
{
  if(pt[i]>pt[j])
  {
      t=pt[i];
      pt[i]=pt[j];
      pt[i]=t;
        q=bt[i];
        bt[i]=bt[j];
        bt[j]=q;
                strcpy(c[i],pname[i]);
                strcpy(pname[i],pname[j]);
                strcpy(pname[j],c[i]);
        }
}


wt[0]=0;
for(i=0;i<n;i++)
{
        wt[i+1]=wt[i]+bt[i];
        sum=sum+wt[i];
        sbt=sbt+wt[i+1];
        tt[i]=wt[i]+bt[i];
        ss=ss+bt[i];
}

printf("\n\nGENTT CHART");
printf("\n^^^^^^^^^^^^^^\n");
printf("\n TOTAL WAITING TIME OF THE PROCESS=%d",sum);
printf("\n TOTAL TURN AROUNT TIME OF THE PROCESS=%d",sbt);
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n AVERAGE WAITING TIME=%f",avgwt);
printf("\n AVERAGE TURN AROUND TIME=%f",avgtt);
return 0;
}

OUTPUT:

[tit04@linuxserver ~]$ ./a.out
 Enter the no of process2
Enter the name and bursttime
NAME:art
 BURSTTIME
6
NAME:th
 BURSTTIME
4
 enter the priority of the process:
 priority of the process
2
 priority of the process
3
TOTAL WAITING TIME OF THE PROCESS=6
 TOTAL TURN AROUNT TIME OF THE PROCESS=16
 AVERAGE WAITING TIME=3.000000
 AVERAGE TURN AROUND TIME=8.000000


8.IMPLEMENT PRODUCER CONSUMER PROBLEM-SEMAPHORE
AIM:
    To implement producer – consumer problem using semaphore.

ALGORITHM:

1.    Start the program.
2.    Declare three semaphore variables.
•    Mutex initialised to 0 which allows only one process to execute at any time.
•    Two variables to indicate the limit of buffer.
3.    Wait and signal are two functions to implement the semaphore.
Wait-waits until semaphore variable reach 1 and then decrements it.
Signal – increments the semaphore variable by 1.
4.    The reader process, checks if any process is writing. If so it waits else it reads the content of shared variable and then signals.
5.    The Writer process checks if any other process is accessing the shared variable. If not it changes the value of shared variable and then signals.
6.    End he program.

PROGRAM
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
static int full,empty,mutex;
int buffer[5],in=0,out=0;
void wait(int *a);
void signal(int *b);
void producer()
{
int nextp;

printf("producer\n");
wait(&empty);
wait(&mutex);
nextp=rand()%10+1;
buffer[in]=nextp;
printf("produced item is %d\n",nextp);
in=(in+1)%5;
signal(&mutex);
signal(&full);
printf("full=%d\t empty=%d\n",full,empty);
}
void consumer()
{
int nextc;
printf("consumer\n");
wait(&full);
wait(&mutex);
nextc=buffer[out];
printf("consumerd item is %d\n",nextc);
out=(out+1)%5;
signal(&mutex);
signal(&empty);
printf("full=%d\t empty=%d\n",full,empty);
}
void wait(int *a)
{
while(*a<=0);
*a=*a-1;
}
void signal(int *b)
{
*b=*b+1;
}
main()
{
int c;
mutex=1;
empty=5;
full=0;
clrscr();
while(1)
{
printf("1.producer\t 2.consumer\t 3.both\t 4.Exit\n");

printf("choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
    if(empty==0)
    printf("producer has to wait\n");
    else
    {
    producer();
    }
    break;
case 2:
    if(full==0)
    printf("consumer has to wait");
    else   
    {
    consumer();
    }
    break;
case 3:
    if(!empty)
    {
    printf("producer has to wait\n");
    consumer();
    }
    else if(!full)
    {
    printf("consumer has to wait\n");
    producer();
    }
    else
    {
    consumer();
    producer();
    }
    break;
    case 4:
    exit(0);
    break;
    }}getch();
    return 0;
       }

OUTPUT:

1.producer       2.consumer      3.both  4.Exit
choice
1
producer
produced item is 7
full=1   empty=4
1.producer       2.consumer      3.both  4.Exit
choice
2
consumer
consumerd item is 7
full=0   empty=5
1.producer       2.consumer      3.both  4.Exit
choice
3
consumer has to wait
producer
produced item is 1
full=1   empty=4

1.producer       2.consumer      3.both  4.Exit
choice



9.MEMORY MANAGEMENT SCHEME I
AIM:
    To implement Best fit, First Fit Algorithm for Memory Management.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a menu for First fit and Best fit.
Step 3: Get the number of holes and size of holes.
Step 4: Enter the number of processes and their sizes for process creation.

Step 5: Compare the process and size then the process is successfully to allocate given hole.
Step 6: In first fit memory management scheme the first biggest hole is allocated first.
Step 7: In best-fit memory management scheme allocates the smallest hole that is big enough.

PROGRAM:

#include<conio.h>
#include<stdio.h>
#include<process.h>
typedef struct
{
int size,pro,alloc;
}node;
void main()
{
node s[10];
int i,j,n,size,p,cc,t=0,q;
clrscr();
menu:
printf("\t\t\t\tmenu\n");
printf("\t\t\t1.First Fit.\n");
printf("\t\t\t2.Best Fit.\n");
printf("\t\t\t3.Exit\n");
printf("\n\n Enter your choice\n");
scanf("%d",&q);
switch(q)
{
case 1:
{
printf("\t\tFirst Fit\n");
printf("\n\tEnter the number of holes:\n");
scanf("%d",&n);
printf("\n\t\tenter the size of the holes\n");
for(i=1;i<=n;i++)
{
s[i].pro=-1;
scanf("%d",&s[i].size);
}
a:

printf("\n\t\t1.Process insertion\n");
printf("\n\t\t2.Exit\n");
printf("\n\t\tEnter your choice:\n");
scanf("%d",&cc);
if(cc==1)
{
printf("\n\t\tenter the process and size\n");
scanf("%d%d",&p,&size);
for(i=1;i<=n;i++)
{
if(s[i].pro==-1&&(s[i].size>=size));
{
 s[i].alloc=size;
 s[i].pro=p;
 printf("\n\t\t\theprocess%dsucessfully allocated to hole size:%d\n",s[i].pro,s[i].size);
 goto a;
 }
 if(i>=n)
 {
 printf("\nMemeory not available\n");
 goto a;
 }
 }
 }
 if(cc==2)
 {
 goto menu;
 }
 getch();
 }
 break;
 case 2:
 {
 printf("\t\tBest Fit\n");
 printf("\n\tEnter the number of holes:\n");
 scanf("%d",&n);
 printf("\n\n\tenter the size of the holes\n");
 for(i=1;i<=n;i++)
 {
 s[i].pro=-1;
 scanf("%d",&s[i].size);
 }
 b:

 printf("\n\t\t1.process insertion\n");
 printf("\n\t\t2.Exit\n");
 printf("\n\t\tenter your choice:\n");
 scanf("%d",&&cc);
 if(cc==1)
 {
 j=-1;
 printf("\n\t\tenter the process and size\n");
 scanf("%d%d,"&p,&size);
 for(i=1;i<=n;i++)
 {
 if(s[i].pro==-1&&(s[i].size>=size))
 {
 if(j==-1)||(s[j].size>s[i].size))
 {
 j=i;
 }
 }
 }
 if(j!=-1)
 {
 s[j].pro=p;
 s[j].alloc=size;
 printf("\n\t\t the process%d is allocated to %d \n",s[j].pro,s[j].size);
 }
 else
 {
 printf("\n\t\t\t\tNo Space\n");
 }
 goto b;
 }
 if(cc==2)
 {
 goto menu;
 }

 getch();
 }
 break;
 case 3:
 exit(0);
 default:
 printf("\n wrong choice\n");

 goto menu;
 }
 getch();
 }

OUTPUT:

Menu

1.First Fit
2.Best Fit
3.Exit
enter your choice 1
First Fit
Enter the number of holes : 5
Enter the size of the holes
800
400
300
500
200
1.process insertion
2.exit

enter your choice:1
enter the process and size 1   450
the process 1 successfully allocated to hole size :800
1.process insertion
2.exit
enter your choice : 1
enter the process and size 1   400
the process 1 successfully allocated to hole size :400

1.process insertion
2.exit

Menu
1.First Fit
2.Best Fit
3.Exit

enter your choice :2
enter the number of holes: 5
enter the size of holes
800
400
300
500
200


1.process insertion
2.exit
enter your choice : 1
enter the process and size 1   150
the process 1 successfully allocated to hole size :200

1.process insertion
2.exit

enter your choice : 1
enter the process and size 1   150
the process 1 successfully allocated to hole size :200
1.process insertion
2.exit
enter your choice : 1
enter the process and size 1   250
the process 1 successfully allocated to hole size :300



10.MEMORY MANAGEMENT SCHEME II


AIM:
    To write a C program to implement worse fit, best fit and first fit allocation methods.

ALGORITHM:

Step 1:        Create the linked list containing free memory.

Step 2:        Read the number of processes.

Step 3:        Read the process id and process size.

Step 4:        Get user choice on the Strategies.

Step 5:    If choice is ‘1’, first fit, then delete the corresponding node in free memory.

Step 6:    If the choice is ‘2’, fit the process in the location that best fit the process.

Step 7:    If the choice is ‘3’, fit the process in available location.

Step 8:    Display the processes that are fitted in the locations.

Step 9:    Delete the process if necessary

Step 10:    Display the current free memory details and process details.

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
struct linked
{
        int size;
        struct linked*next;
};

typedef struct linked node;
node*head;
int create(node *p);
int display(node *p);
int choice(node *p);

int  main()
{
        node *head;
        int create(node *p);
        int display(node *p);
        int choice(node *p);
        head=(node*)malloc(sizeof(node));
        choice(head);
        return 0;
}

int create(node *list)
{
        int st,end;
        int ch;
        printf("Enter the starting and ending address");
        scanf("%d%d",&st,&end);
        list->size=(end-st);
        printf("Do u want to continue??(Press 1 to continue)\n");
        scanf("%d",&ch);
        if(ch==1)
        {
                list->next=(node*)malloc(sizeof(node));
                create(list->next);
        }
        else
        {
                list->next=NULL;
        }
        return 0;
}

int display(node *list)
{
        if(list->next!=NULL)
        {
                printf("%d=>",list->size);
                if(list->next->next==NULL)
                        printf("%d",list->next->size);
                display(list->next);
        }
        return 0;
}


int choice(node *p)
{
        int c;
        do
        {
                printf("\nEnter ur choice\n1.Create     2.Display       3.Exit\n");
                scanf("%d",&c);
                switch(c)
                {
                        case 1:
                                create(p);
                                break;
                        case 2:
                                display(p);
                                break;
                        case 3:
                                break;
                }
        }
        while(c<3);
        return 0;
}

OUTPUT:
[tit04@linuxserver ~]$ ./a.out
Enter ur choice
1.Create        2.Display       3.Exit
1
Enter the starting and ending address
330
600
Do u want to continue??(Press 1 to continue)
1
Enter the starting and ending address
200
240
Do u want to continue??(Press 1 to continue)

2

Enter ur choice
1.Create        2.Display       3.Exit
2
270=>40
Enter ur choice
1.Create        2.Display       3.Exit
3








No comments:

Post a Comment