Top Banner
VEL TECH MULTI TECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEEING LAB MANUAL CS 2257- OPERATING SYSTEMS LAB Prepared By Mr.N.KUMAR M.Tech(CSE) Assistant Professor Department of Computer Science and Engineering 1
45
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: NEWOS_LAB_MANUAL_CS2257

VEL TECH MULTI TECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEEING

LAB MANUAL

CS 2257- OPERATING SYSTEMS LAB

Prepared By

Mr.N.KUMAR M.Tech(CSE) Assistant Professor Department of Computer Science and Engineering

1

Page 2: NEWOS_LAB_MANUAL_CS2257

CS 2257 OPERATING SYSTEMS LAB (Common to CSE & IT)

(Implement the following on LINUX or other UNIX like platform. Use C for high level language implementation)

1. Write programs using the following system calls of UNIX operating system:Fork, exec, getpid, exit, wait, close, stat, opendir, readdir

2. Write programs using the I/O system calls of UNIX operating system (open, read, write, etc)

3. Write C programs to simulate UNIX commands like ls, grep, etc.

4. Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for FCFS and SJF. For each of the scheduling policies, compute and print the average waiting time and average turnaround time. (2 sessions)

5. Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for Priority and Round robin. For each of the scheduling policies, compute and print the average waiting time and average turnaround time. (2 sessions)

6. Developing Application using Inter Process communication (using shared memory, pipes or message queues)

7. Implement the Producer – Consumer problem using semaphores (using UNIX system calls).

8. Implement some memory management schemes – I

9. Implement some memory management schemes – II

10. Implement any file allocation technique (Linked, Indexed or Contiguous)

2

Page 3: NEWOS_LAB_MANUAL_CS2257

Write programs using the following system calls of UNIX operating system:Fork, exec, getpid, exit, wait, close, stat, opendir, readdir

Write programs using the I/O system calls of UNIX operating system (open, read, write, etc)

TO ILLUSTRATE THE USE OF fork () AND Getpid () SYSTEM CALLS

AIM:

To write a shell program to simulate the fork (), getpid () system calls

ALGORITHM:

1. Initialize the max_count value and buffer size 2. Get the fork () system call to execute the process. It makes two identical

copies of address spaces, one for the parent and the other for the child. 3. Both processes start their execution right after the system call fork().

(i) If both processes have identical but separate address spaces,

(ii) The fork () call have the same values in both address spaces. Since every process has its own address space, any modifications will be independent of the others.

4. Get the process id by using getpid() system call.5. The printf() is “buffered" meaning printf() will group the output of a process

together. It Print the process id and value.6. Stop the program

PROGRAM:#include <stdio.h>#include <string.h>#include <sys/types.h>

#define MAX_COUNT 20#define BUF_SIZE 100

main(void){ pid_t pid; int i; char buf[BUF_SIZE];

fork(); pid = getpid();

3

Page 4: NEWOS_LAB_MANUAL_CS2257

for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf, "This line is from pid %d, value = %d\n", pid, i); write(1, buf, strlen(buf)); }}

OUTPUT:-sh-3.2$ cc fork1.c-sh-3.2$ ./a.outThis line is from pid 3127, value = 1This line is from pid 3127, value = 2This line is from pid 3126, value = 1This line is from pid 3126, value = 2This line is from pid 3126, value = 3This line is from pid 3126, value = 4This line is from pid 3126, value = 5This line is from pid 3126, value = 6This line is from pid 3126, value = 7This line is from pid 3126, value = 8This line is from pid 3126, value = 9This line is from pid 3126, value = 10This line is from pid 3126, value = 11This line is from pid 3126, value = 12This line is from pid 3126, value = 13This line is from pid 3126, value = 14This line is from pid 3126, value = 15This line is from pid 3126, value = 16This line is from pid 3126, value = 17This line is from pid 3126, value = 18This line is from pid 3126, value = 19This line is from pid 3126, value = 20This line is from pid 3127, value = 3This line is from pid 3127, value = 4This line is from pid 3127, value = 5This line is from pid 3127, value = 6This line is from pid 3127, value = 7This line is from pid 3127, value = 8This line is from pid 3127, value = 9This line is from pid 3127, value = 10This line is from pid 3127, value = 11This line is from pid 3127, value = 12This line is from pid 3127, value = 13This line is from pid 3127, value = 14This line is from pid 3127, value = 15This line is from pid 3127, value = 16

4

Page 5: NEWOS_LAB_MANUAL_CS2257

This line is from pid 3127, value = 17This line is from pid 3127, value = 18This line is from pid 3127, value = 19This line is from pid 3127, value = 20

RESULT:Thus the process id, value were printed by using the fork and getpid

system calls.

TO DEMONSTRATE EXECLP ( ) FUNCTION

AIM: To write a shell program to implement the execlp( ) function .

ALGORITHM: 1. Start the program.

2. Obtain the pid value using fork ( ) function.

3. If pid < 0, then print fork failed.

4. Else if pid = 0 , execlp( ) function will invoked .

5. Else print child process complete.

6. Terminate the program.

PROGRAM:

#include<stdio.h>main(int argc,char *argv[]){int pid;pid =fork();if(pid<0){fprintf(stderr,”Fork failed \n”);exit(-1);}else if(pid==0){execlp(“/bin/ls”,”ls”,NULL);}else{wait(NULL);

5

Page 6: NEWOS_LAB_MANUAL_CS2257

printf(“Child Complete”);exit(0);}

OUTPUT:

a.out fib.sh mov.c len,sh str.sh fact.sh copy.c cat.c even.c odd.sh child complete

RESULT: Thus the execlp function were implemented

TO DEMONSTRATE SLEEP FUNCTION

AIM:

To write a shell program to demonstrate the sleep () function.

ALGORITHM:

1. Start the program.

2. Get the pid using fork () function.

3. Check pid value

a. if pid = 0 then display child process and put the process to sleep.

b. else display parent process function .

c. display the pid .

4. Terminate the process.

PROGRAM:

// SLEEP FUNCTION //

#include<stdio.h>main(){int p;p=fork();if(p==0){printf(“\n I am Child process %d”,getpid());sleep(15);printf(“\n I am Parent of Child process %d”,getpid());}else

6

Page 7: NEWOS_LAB_MANUAL_CS2257

{printf(“\n I am Parent of Child %d”,getpid());printf(“\n I am Parent’s Parent %d”,getpid());}}

OUTPUT:

SLEEP FUNCTION

I am a child process 2600I am parent of child process 2600I am parent process 2675I am parent’s parent process 2675

RESULT: Thus the sleep function was demonstrated.

TO DEMONSTRATE OPENDIR, READDIR FUNCTIONS

AIM:

To write a shell program to demonstrate the I/O system calls

ALGORITHM:1. Start the program2. To define a pointer to a structure ,dirname predefined structure DIR and

another pointer to a structure called preaddir3. The directory is opened using the opendir() function.4. The readdir() function reads the name of the first file from this opened

directory5. The third call to this function gets the first entry,whose name is stored in

the member p_name of the structure preaddir.6. The closedir() function closes this open directory7. Stop the program.

PROGRAM:

#include<stdio.h>#include<sys/types.h>#include<dirent.h>main(argc,argv)int argc;char *argv[];{

7

Page 8: NEWOS_LAB_MANUAL_CS2257

DIR *dirname;struct dirent *preaddir;dirname=opendir(argv[1]);preaddir=readdir(dirname);preaddir=readdir(dirname);preaddir=readdir(dirname);printf("opened %s\n",argv[1]);printf("The first entry in this directory is %s\n",preaddir->d_name);closedir(dirname);}

OUTPUT:-sh-3.2$ cc ex4.c-sh-3.2$ ./a.out cs1opened cs1The first entry in this directory is ..-sh-3.2$ ./a.out geeopened geeThe first entry in this directory is ..-sh-3.2$

RESULT:

Thus we opened, read and write the directory data’s by using I/O system calls.

TO DEMONSTRATE A wait SYSTEM CALL

AIM: To write a shell program to demonstrate a wait system call.

ALGORITHM:

1. Start the program

2. Create the process and get the process id.

8

Page 9: NEWOS_LAB_MANUAL_CS2257

3. Check the status of the process.

4. If the process id is equal to 0 get and print the status.

5. Else process is waits till the previous process execution completed.

6. Stop the program

PROGRAM:

#include<stdio.h> #include<sys/types.h>#include<sys/wait.h>main(){int i,pid,exitstat,status;pid=fork();if(pid==0){printf("enter exit stat:");scanf("%d",&i);exit(1);}else{wait(&status);if((status & 0xff)!=0){printf("Signal interrupted\n");}else{exitstat=(int)status/256;printf("Exit status from %d was %d\n",pid,exitstat);

OUTPUT:-sh-3.2$ cc ex1.c-sh-3.2$ ./a.outenter exit stat:3Exit status from 3928 was 1-sh-3.2$

RESULT:

Thus the wait system call was implemented.

9

Page 10: NEWOS_LAB_MANUAL_CS2257

FORK FUNCTION

AIM:

To write a program to implement the fork system call.

ALGORITHM: 1. Start the program. 2. In prinf() function give “hello” .

3. Invoke fork () function.

4. In printf() function give “bye” .

5. Invoke fork () function.

6. Terminate the program.

PROGRAM:

// FORK FUNCTION //

#include<stdio.h>main(){printf(“Hello”);fork();printf(“Bye”);fork();}

OUTPUT:FORK FUNCTION

HelloByeHelloByeHelloByeHelloBye

RESULT: Thus the Fork function was executed .

SYSTEM CALL FOR CAT COMMAND AIM:

10

Page 11: NEWOS_LAB_MANUAL_CS2257

To write a program to demonstrate system calls for cat command.

ALGORITHM:

1. Start the program.

2. Pass arguments at argc and argr.

3. Create a buffer array.

4. Open a file with parameters argv0] and 1.

5. Initiate while loop.

a .Check buffersize>o.

b .Write contents into the file.

6. Terminate while loop.

7. Terminate program.

PROGRAM:

#include<stdio.h>main(argc,argv)int argc;cahr *argv[];{int f,d,n;char buff[2048];d=open(argv[],0);while((n=read(d,buff,sizeof(buff)))>0)write(0,buff n);}

11

Page 12: NEWOS_LAB_MANUAL_CS2257

OUTPUT:

$cat >abcdI love my India./a.out abcdI love my India

RESULT: Thus the cat command was simulated.

TO SIMULATE FILE OPEN, READ, WRITE OPERATIONSAIM: To write a shell program to simulate file open, read and write operations.

ALGORITHM:

1. Start the program

2. Define the buffer size,Pmode value

3. The open function used to open the file and the creat function used

to create the file

4. To read or write a file assumed to exist already.

5. The returned value fildes is a file-descriptor used to denote the file in

subsequent calls that read, write or otherwise manipulate the file.

6. The function open returns -1 if any error occurs; otherwise, it returns a

valid open file-descriptor.

7. If the file is brand new, creat creates it with the protection mode specified

by the pmode argument.

8. Stop the execution

PROGRAM:#include<stdio.h>#define BUFSIZE 512#define PMODE 0644char *progname;

main(argc, argv) int argc; char *argv[ ];{

12

Page 13: NEWOS_LAB_MANUAL_CS2257

int fd1, fd2, n; char buf[BUFSIZE];progname=argv[0];

if (argc != 3) error("Usage: cp from to", progname); if ((fd1 = open(argv[1], 0)) == -1) error("cp: can't open %s", argv[1]); if ((fd2 = creat(argv[2], PMODE)) == -1) error("cp: can't create %s", argv[2]);

while ((n = read(fd1, buf, BUFSIZE)) > 0) if (write(fd2, buf, n) != n) error("cp: write error", (char *)0);exit(0);}OUTPUT:-sh-3.2$ cat y1sampleprogram to file operations-sh-3.2$ read y1

RESULT: Thus the file operations were simulated.

TO DEMONSTRATE THE COPYING A FILE

AIM:

To demonstrate the concept of copying a file by using system calls.

ALGORITHM:

1. Start the program.

2. In the main function pass parameter as argc and argv.

3. Open a file with argument argv[1] and 0.

4. Initiate while loop.

a. Check if buffer size is greater than zero. b. write content to file.

13

Page 14: NEWOS_LAB_MANUAL_CS2257

5. Terminate while loop.

6. Terminate program.

PROGRAM://CONCEPT OF COPYING A FILE//#include<stdio.h>main(argc,argv)int argc;char *argv[];{int f,d,n;char buff[2048];f=creat(argv[2],0777);d=(argv[1],0);while((n=read(d,buff,size of (buff)))>0)write(f,buff,n);printf(“The content of %s is copied to %s \n”,argv[1],argv[2]);}

OUTPUT:

COPYING A FILE

./a.out y gThe content of y is copied to gCat gGoodmorningCat yGoodmorning

RESULT: Thus the file was copied by using the system calls.

14

Page 15: NEWOS_LAB_MANUAL_CS2257

Write C programs to simulate UNIX commands like ls, grep, etc.

SIMULATION OF LS COMMAND

AIM: To write a program to simulate the ls command

ALGORITHM:

1. Start the program

2. To define a pointer to a structure ,dirname predefined structure DIR and

another pointer to a structure called preaddir

3. The directory is opened using the opendir() function.

4. In the while loop read each entry using the readdir() function which returns

a pointer.

5. If no pointer is returned the program is exited, else the name of the entry is

displayed.

6. The closedir() function closes this open directory

7. Stop the program.

15

Page 16: NEWOS_LAB_MANUAL_CS2257

PROGRAM:

#include<stdio.h>#include<sys/types.h>#include<dirent.h>main(argc,argv)int argc;char *argv[];{

DIR *dirname;struct dirent *rdir;dirname=opendir(argv[1]);while(1){rdir=readdir(dirname);if(rdir==NULL){closedir(dirname);exit(0);}printf("found entry in %s:%s\n",argv[1],rdir->d_name);}closedir(dirname);exit(0);}

OUTPUT:

-sh-3.2$ cc ex3.c-sh-3.2$ ./a.out cs1found entry in cs1:.found entry in cs1:tfilefound entry in cs1:..-sh-3.2$

RESULT: Thus the ls command were simulated

16

Page 17: NEWOS_LAB_MANUAL_CS2257

Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for FCFS and SJF. For each of the scheduling policies, compute and print the average waiting time and average turnaround time. (2 sessions)

TO IMPLEMENT FIRST COME FIRST SERVE (FCFS) SCHEDULING ALGORITHM

AIM:

To write a C program to implement the first come first serve scheduling algorithm.

ALGORITHM:

1. Start the program.

2. Obtain ptime, pid, and burst time.

3. Obtain the burst time for each process.

4. Calculate the average waiting time (AWT),

Sum of waiting time Total No of processes

5. Calculate the average turn around time (TAT).

17

Page 18: NEWOS_LAB_MANUAL_CS2257

Waiting time+Brust timeTotal No of processes

6. Print the output.

7. Stop the program.

PROGRAM:

//FIRST COME FIRST SERVE SCHEDULING

#include<stdio.h>struct process{ int btime; int wtime; int ttime;} p[50];

main(){ int n,i; float tot_turn=0.0,tot_wait=0.0, avg_turn=0.0,avg_wait=0.0; printf("\n Enter number of process:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Enter the burst time of each process:"); scanf("%d",&p[i].btime); } i=1; p[i].wtime=0; p[i].ttime= p[i].btime; tot_wait = p[i].wtime; tot_turn = p[i].ttime; for(i=2;i<=n;i++) { p[i].wtime=p[i-1].wtime+p[i-1].btime; p[i].ttime=p[i].wtime+p[i].btime; tot_wait=tot_wait+p[i].wtime; tot_turn=tot_turn+p[i].ttime; } avg_wait=tot_wait/n; avg_turn=tot_turn/n; printf("\n Process burst time waiting time turnaround time \n"); for(i=1;i<=n;i++)

18

Page 19: NEWOS_LAB_MANUAL_CS2257

{ printf("%5d%10d%15d%15d",i,p[i].btime, p[i].wtime, p[i].ttime); printf("\n"); } printf("\n average waiting time:%f\n",avg_wait); printf("\n average turnaroundtime:%f\n", avg_turn);}

OUTPUT: Enter number of process:3

Enter the burst time of each process:3 Enter the burst time of each process:4 Enter the burst time of each process:5

Process burst time waiting time turnaround time

1 3 0 3 2 4 3 7 3 5 7 12

average waiting time:3.333333

average turnaroundtime:7.333333

RESULT:

Thus the process burst time, wait time, turned around time was

calculated by using first come first serve algorithm.

TO IMPLEMENT SHORTEST JOB FIRST (SJF) SCHEDULING ALGORITHM

 AIM:

    To write a program to implement shortest job first scheduling.

ALGORITHM:

        1. Start the program.

        2. Enter the no of process.

        3. Enter the process and waiting process.

        4. Sort the process according to ascending order.

        5. Print the process and compute waiting time.

19

Page 20: NEWOS_LAB_MANUAL_CS2257

6. Compute the average waiting time.

Sum of waiting time Total No of processes

7. Calculate the average turn around time (TAT). Waiting time+Brust time

Total No of processes

8. Stop the program.

       

PROGRAM:

#include<stdio.h>

int main()

{

 int i,j,n,t,tot=0,p[20],c[20];

printf("Enter the no of process");

scanf("%d",&n);

printf("Enter the %d process",n);

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

scanf("%d",&p[i]);

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

if(p[i]>p[j])

{

t=p[i];

p[i]=p[j];

p[j]=t;

}

printf("\n Sorted process \n");

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

printf("%d\n",p[i]);

c[0]=0;

20

Page 21: NEWOS_LAB_MANUAL_CS2257

for(i=0;i<n-1;i++)

c[i+1]=c[i]+p[i];

printf("\n s.no \t process \t waiting time");

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

{

printf("\n%d\t%d\t\t%d\n",i+1,p[i],c[i]);

tot=tot+c[i];

}

printf("Average waiting time %f",((float)tot/n));

printf("\n");

}

OUTPUT:

[guest@WKS47 guest]$./a.out

Enter the no of process

5

Enter the 5 process

22

44

11

55

33

Sorted process

11

22

33

44

55

s.no process waiting time

1 11 0

2 22 11

21

Page 22: NEWOS_LAB_MANUAL_CS2257

3 33 33

4 44 66

5 55 110

Average waiting time 44.000000

RESULT:

      Thus the shortest job first scheduling algorithm was implemented.          

Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for Priority and Round robin. For each of the scheduling policies, compute and print the average waiting time and average turnaround time. (2 sessions)

TO IMPLEMENT PRIORITY SCHEDULING ALGORITHM

AIM:To write a UNIX program to implement the priority scheduling algorithm.

ALGORITHM:

1. Start the program.

2. Obtain the burst time and priority for each process.

3. Rearrange the process according to the priority.

4. Calculate the waiting time and turnaround time.

5. Calculate the average waiting time as

Sum of waiting time

Total No of processes

6. Calculate the average turn around time (TAT).

Waiting time+Brust timeTotal No of processes

7. Display the result.

8. Stop the program.

22

Page 23: NEWOS_LAB_MANUAL_CS2257

PROGRAM:

//Priority Scheduling

#include<stdio.h>struct process{ int btime;int wtime;int ttime;int pr;int s;}p[50]; main(){ float totwait,tottu,avgwtime,avgttime; int i,j,n,d,d1,d2; printf("\n Enter the number of process"); scanf("%d",&n);totwait=tottu=0;for(i=1;i<=n;i++) {printf("\n Enter the burst time for %d process:",i);scanf("%d",&p[i].btime);printf("\n Enter the priority:");scanf("%d",&p[i].pr);p[i].s=i;}for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(p[i].pr>p[j].pr)

23

Page 24: NEWOS_LAB_MANUAL_CS2257

OUTPUT:

Enter number of process:3

Enter the burst time of each process:3

Enter the burst time of each process:4

Enter the burst time of each process:5

Process burst time waiting time turnaround time

1 3 0 3 2 4 3 7 3 5 7 12

average waiting time:3.333333

average turnaroundtime:7.333333

RESULT:

Thus the priority scheduling algorithm has been done and the output has been verified.

TO IMPLEMENT THE ROUND ROBIN (RR) SCHEDULING ALGORITHM AIM: To write a program to implement the round robin scheduling.

ALGORITHM:

1. Start the program.

2. Enter the no of process.

3. Enter the no of slice.

24

Page 25: NEWOS_LAB_MANUAL_CS2257

4. Enter the burst time of each process.

5. Calculate the turn around time for each process.

6. Calculate the total waiting time.

7. Calculate the average waiting time.

8. Calculate the total turn around time.

9. Calculate the average turn around time.

10. Print the AWT, Average TAT.

11. Stop the program..

PROGRAM:

# include<stdio.h>typedef struct{int pno,btm,sbtm,wtm,lst;}spcb;int main(){int pp=-1,ts,flag,count,pno,ptm=0,i,n,twt=0,tttm=0;spcb proc[100];printf("\n\t\t\t ROUND ROBIN SCHEDULING\n");printf("Enter the no.of process ready in queue:\n");scanf("%d",&n);printf("\n Enter the time slice:\n");scanf("%d",&ts);printf("\n Enter the burst time for %d process:\n",n);for(i=0;i<n;i++){scanf("%d",&proc[i].btm);proc[i].wtm=proc[i].lst=0;proc[i].pno=i+1;proc[i].sbtm=proc[i].btm;}printf("\n process synchoronation");do{flag=0;for(i=0;i<n;i++)if((count=proc[i].btm)>0)

25

Page 26: NEWOS_LAB_MANUAL_CS2257

{flag=-1;count=(count>=ts)?ts:count;printf("to%d",(ptm+=count));proc[i].btm-=count;if(pp!=i){proc[i].wtm+=ptm-proc[i].lst-count;proc[i].lst=ptm;}}} while(flag);printf("\n\n\t process no\twaiting time\tturn around time");for(i=0;i<n;i++){printf("\n process %d from %d",proc[i],pno,ptm);twt+=proc[i].wtm;tttm+=proc[i].wtm+proc[i].sbtm;printf("\t\t%d\t\t%d\t\t",proc[i].pno,proc[i].wtm);printf("%d",proc[i].wtm+proc[i].sbtm);printf("\n");}printf("\n Total waiting time=%d",twt);printf("\n Avg waiting time=%f",(float)twt/n);printf("\n Total turn around time=%d",tttm);printf("\n Avg turn around time=%f\n",(float)tttm/n);}

OUTPUT: ROUND ROBIN SCHEDULING

Enter the no of process ready in queen:4Enter the time slice:10Enter the burst time for 4 process3 4 5 6Process synchoronationProcess 1 from 0 to 3Process 2 from 3 to 7Process 3 from 7 to 12Process 1 from 12 to 18

26

Page 27: NEWOS_LAB_MANUAL_CS2257

Process no waiting time turn around time1 0 3 2 3 73 7 124 12 18Total waiting time =22Avg waiting time =5.500000Total turn around time=40

Avg turn around time=10.000000

RESULT:

Thus the Round Robin scheduling algorithm was implemented.

Implement the Producer – Consumer problem using semaphores (using UNIX system calls).

27

Page 28: NEWOS_LAB_MANUAL_CS2257

TO IMPLEMENT THE PRODUCER-CONSUMER PROBLEM

AIM:

To write a c program to implement Producer- consumer problem

ALGORITHM:

1. Start the program.

2. Declare the variables required.

3. Get the value and add it in the queue

4. Check for empty space in the queue

5. Display the number added to the queue.

6. In customer check whether the queue is empty.

7. Decrement the queue and display the values in the customer.

8. Stop the program

PROGRAM:#include<stdio.h>void producer();void consumer();int q[100],num,n,count=0;int front,rear;main(){int ch,count=0;int choice=1;front=rear=1;printf(“Enter the size of queue”);scanf(“%d”,&n);while(choice!=0){printf(“\nEnter your choice\n 1.for producer\n 2.for consumer\n”);scanf(“%d”,&ch);switch(ch){Case 1:{producer();

28

Page 29: NEWOS_LAB_MANUAL_CS2257

break;}Case 2:{consumer();break;}default:{printf(“Enter the correct option(1\2):”);break;}}printf(“\n Do you want to continue\n 0->No \n 1->Yes ???”);scanf(“%d”,&choice);}}void producer(){if(rear+1)%n==front)printf(“\n The producer queue is full \n”);else{if(front==-1){front=0;printf(“ Enter the number to be added to the producer:”);scanf(“%d”,&num);rear=(rear+1)%n;q[rear]=num;count++;printf(“\n The number added is %d and in the position %d”,q[rear],rear);}else{if(rear+1)%n!=front){printf(“-----------------“);printf(“\n Enter the number to be added to the producer”);scanf(“%d”,&num);rear=(rear+1)%n;q[rear]=num;count++;printf(“The number added is %d and in the position %d”,q[rear],rear);}elseprintf(“\n The producer queue is full\n”);

29

Page 30: NEWOS_LAB_MANUAL_CS2257

}}}void consumer(){if(count==0)printf(“\n The consumer consumed is %d from the position %d”,q]front],front);front++;count--;}

OUTPUT:Enter the size of queue:5Enter your choice:1.for producer2.for consumer 1Enter the number to be added to the producer:50The number added is 50 and in the position 0Do you want to continue0->No1->Yes???1Enter your choice1.for producer2.consumer 2The consumer consumed is 50 from the position 0Do you want to continue0->No1->Yes???0

RESULT:

Thus the producer consumer problem was implemented by using semaphore.

Implement some memory management schemes – I

TO IMPLEMENT THE MEMORY MANAGEMENT SCHEMES USING FIRST FIT

AIM:

To write a UNIX program for the implementation of dynamic memory

allocation using first fit.

30

Page 31: NEWOS_LAB_MANUAL_CS2257

ALGORITHM:

1. Start the program.

2. Enter the number of states.

3. Enter the job size.

4. Enter the remaining slot size.

5. Enter the allocated job size.

6. Allocate slotes until all the jobs are completed.

7. Stop the program.

PROGRAM:

#include<stdio.h>main(){int i,j,n,ns,jb,c,ss[20];printf(“\n Enter the no of slots “);scanf(“%d”,&ns);printf(“\n Enter the %d slot “,ns);for(i=0;i<ns;i++)scanf(“%d”,&ss[i]);do{Printf(“\n Enter the job size”);scanf(“%d”,&jb);for(i=0;i<ns;i++){if(jb<=ss[i]){printf(“%d is allocated to slot %d of size %d”,jb,i+1,ss[i]);ss[i]=ss[i]-jb;printf(“\n Remaining space of slots %d is %d”,i+1,ss[i]);n=1;break;}elsecontinue;}if(n!=1)printf(“%d is not allocated to any slots”,jb);n=0;

31

Page 32: NEWOS_LAB_MANUAL_CS2257

printf(“\n Enter your choice”);Ssanf(“%d”,&c);}while(c==1)}

OUTPUT:

Enter the no of slots 3Enter the 3 slots 356Enter the job size 22 is allocated to slot 1 of size 3

Remaining space of slot 1 is 1

Enter your choice 1

Enter the job size 33 is allocated to slot 2 of size 5

Remaining space of slot 2 is 2Enter your choice 5

RESULT:

Thus the first fit dynamic memory allocation was performed.

Implement some memory management schemes – II

TO IMPLEMENT THE MEMORY MANAGEMENT SCHEMES USING BEST FIT

AIM:

To write a program to implement dynamic memory allocation of best fit.

ALGORITHM:

1. Start the program.

2. Enter the number of unlocated space.

32

Page 33: NEWOS_LAB_MANUAL_CS2257

3. Enter the size of allocated space.

4. Enter the space required for each process.

5. Select least space required for process.

6. Sop the program.

PROGRAM:#include<stdio.h>main(){int a,b[50],i,j,temp,s;printf(“\n\n\tBEST FIT”);printf(“\n Enter the number of unallocated space:”);scanf(“%d”,&a);printf(“\n Enter the unallocated space in KB\n”);for(i=1;i<=a;i++)scanf(“%d”,&b[i]);for(j=i+1;j<=a;j++)if(b[i]>b[j]){temp=b[j];b[j]=b[i];b[i]=temp;}Ppintf(“\nEnter the space for required process”);scanf(“%d”,&s);for(i=1;i<=a;i++){if(s<b[i]){printf(“\n The best fit for required space is %d\n”,b[i]);break;}}}

OUTPUT: BEST FIT

Enter the number of unallocated space:3

Enter the unallocated space in KB246Enter the space for required process 4

33

Page 34: NEWOS_LAB_MANUAL_CS2257

The best fit for required space is 6

RESULT:Thus the best fit memory location was executed.

TO IMPLEMENT THE MEMORY MANAGEMENT SCHEMES USING WORST FIT

AIM:

To write a program to implement dynamic memory allocation worst fit.

ALGORITHM:

1. Start the program.

2. Enter the number of unlocated memories

3. Enter the memory space for each process.

4. Enter the size of process

5. Compute and allocated the maximum size of memory for the process.

6. Stop the program.

PROGRAM: #include<stdio.h>

main( ){int a[50],n,i,j,p,t;printf(“\n\t\tWORST FIT”);printf(“\n Enter the unallocated space”);scanf(“%d”,&n);printf(“\nEnter the memory space\n”);for(i=1;i<=n;i++)scanf(“%d”,&a[i]);printF(“\n Enter the memory required for the process:\n”);scanf(“%d”,&p);for(i=1;i<n;i++)for(j=i+1;j<=n;j++)if(a[i]>a[j]){t=a[i];

34

Page 35: NEWOS_LAB_MANUAL_CS2257

a[i]=a[j];a[j]=t;}printf(“\nThe worst fit process %d\n”,a[i]));}

OUTPUT:

WORST FIT

Enter the unallocated space 3

Enter the memory space12345

Enter the memory required for the process6The worst fit process 34

RESULT:Thus the worst fit process was executed and verified.

TO IMPLEMENT THE MEMORY CONFIGURATION

AIM:

To write a c program to display memory configuration.

ALGORITHM:

1. Start the program.

2. Data variables are declared as integer values.

3. File memory information is read using read function.

4. Memory configuration is displayed

5. Stop the program.

35

Page 36: NEWOS_LAB_MANUAL_CS2257

PROGRAM:

#include<stdio.h>main(){int fp,n,arr[1000];printf(“\n Sample for memory information\n”);fp=open(“/proc/meminfo”,0);n=read(fp,arr,1000);if(n<0){printf(file is not found\n”);}else{arr[n]=’\0’;printf(”%s”,arr);}}

OUTPUT:

Sample for memory information Total: Used: free: shared:buffers:cached:Mem :514420736 142082048 372338688 0 23068672 48861184Swap : 534601728 0 534601728MemTotal: 502364 KBMemFree: 363612 KBMemShared : 0 KBBuffers: 22528 KBCached: 47716 KBSwapCached : 0 KBActive : 104012 KBActiveAnon: 45472 KBActiveCache: 58540 KBInact_dirty: 11312 KBInact_laundry : 0KBInact_clean: 0 KBInact_target: 230964 KBHighTotal: 0KBHighFree: 0 KBLowTotal: 502364KBLowFree: 363612 KB

36

Page 37: NEWOS_LAB_MANUAL_CS2257

SwapTotal: 522072KBSwpFree: 522072 KBHugePages_Total: 0HugePages_Free: 0HugePagesize: 2048KB

RESULT: Thus the memory configuration was implemented.

37