Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CS1255 OS Lab Manual Good

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

CS1255 Operating system Laboratory

M.A.M
School of Engineering
Siruganur, Trichy-621105.

Department of Computer Science and Engineering


Sub Name:Operating System Laboratory Year/ Sem : II / IV
Sub Code: CS1255 Degree / Branch :B.E / CSE
LIST OF EXPERIMENTS
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)
Content Beyond The Syllabus
1. Implementation of process communication using Shared memory (Client /Server).

1
CS1255 Operating system Laboratory

BASIC LINUX COMMANDS


Aim:

To operate commands for general utility purposes.

1. COMMAND: CALENDAR

PURPOSE : To display the calendar of current year.


SYNTAX : $ cal

OUTPUT:
[it08b034@linuxlab ~]$ cal
July 2009
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

PURPOSE: To display previous month, current month and next month.


SYNTAX : $ cal -3

OUTPUT:
[it08b034@linuxlab ~]$ cal -3
June 2009 July 2009 August 2009
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30 31

PURPOSE: To display current month starting from monday.


SYNTAX: $ cal -m

OUTPUT:
[it08b034@linuxlab ~]$ cal -m
July 2009
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

2
CS1255 Operating system Laboratory

2. COMMAND: DATE

PURPOSE: To display the system date


SYNTAX: $ date

OUTPUT:
[it08b034@linuxlab ~]$ date
Thu Jul 23 23:08:23 IST 2009

PURPOSE: To display only the name of the month


SYNTAX: $ date +%h

OUTPUT:
[it08b034@linuxlab ~]$ date +%h
Jul

PURPOSE: To display only the date


SYNTAX: $ date +%d

OUTPUT:
[it08b034@linuxlab ~]$ date +%d
23

PURPOSE: To display only the year


SYNTAX: $ date +%y

OUTPUT:

[it08b034@linuxlab ~]$ date +%y


09

PURPOSE: To display time in hours


SYNTAX: $ date +%H

OUTPUT:
[it08b034@linuxlab ~]$ date +%H
23

PURPOSE: To display time in minutes


SYNTAX: $ date +%M

OUTPUT:
[it08b034@linuxlab ~]$ date +%M
15

3
CS1255 Operating system Laboratory

PURPOSE: To display time in seconds


SYNTAX: $ date +%S
OUTPUT:
[it08b034@linuxlab ~]$ date +%S
05

3. COMMAND: WHO

PURPOSE: To display entire list of persons who are logged in.


SYNTAX: $ who

OUTPUT:
[it08b034@linuxlab ~]$ who
premalat pts/1 Jul 23 22:26 (10.201.141.199)
premalat pts/2 Jul 23 22:53 (10.201.141.199)

PURPOSE: To display only our login.


SYNTAX: $ whoami

OUTPUT:
[it08b034@linuxlab ~]$ whoami
It08b034

4. COMMAND: ECHO

PURPOSE: To display message


SYNTAX: $ echo message

OUTPUT:
[it08b034@linuxlab ~]$ echo hai
hai

5. COMMAND: FILES AND DIRECTORIES

PURPOSE: To create a directory


SYNTAX: $ mkdir dirname

OUTPUT:
[it08b034@linuxlab ~]$ mkdir latha

PURPOSE: To change the directory


SYNTAX: $ cd dirname

OUTPUT:
[it08b034@linuxlab latha]$

4
CS1255 Operating system Laboratory

PURPOSE: To delete a file.


SYNTAX: $ rm filename

OUTPUT:
[it08b034@linuxlab ~]$ rm friend
[it08b034@linuxlab ~]$ cat friend
cat: friend: No such file or directory

PURPOSE: To remove a directory.


SYNTAX: $ rm dirname

6. COMMAND: LIST

PURPOSE: To list the contents of a directory.


SYNTAX: $ ls

OUTPUT:
[it08b034@linuxlab ~]$ ls
f1 file1 friend latha subjects

PURPOSE: To list the hidden files.


SYNTAX: $ ls

7. COMMAND: HELP

PURPOSE: To display entire attribute of date


SYNTAX: $ man date

OUTPUT:
[it08b034@linuxlab ~]$ man date
DATE(1) User Commands DATE(1)

NAME
date - print or set the system date and time

SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
Display the current time in the given FORMAT, or set the system date.

-d, --date=STRING
display time described by STRING, not ΓÇÿnowΓÇÖ

-f, --file=DATEFILE

5
CS1255 Operating system Laboratory

like --date once for each line of DATEFILE

-ITIMESPEC, --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format. TIMESPEC=ΓÇÿdateΓÇÖ for date
only, ΓÇÿhoursΓÇÖ, ΓÇÿminutesΓÇÖ, or ΓÇÿsecondsΓÇÖ for date and time to
theindicated precision. --iso-8601 without TIMESPEC defaults to
ΓÇÿdateΓÇÖ.
Ctrl + Z

8. COMMAND: PRESENT WORKING DIRECTORY

PURPOSE: To display current working directory


SYNTAX: $ pwd

OUTPUT:
[it08b034@linuxlab ~]$ pwd
/home/It08b034

9. COMMAND: FILE HANDLING

PURPOSE: To create a file and write to it


SYNTAX: $ cat>>filename

OUTPUT:
[it08b034@linuxlab ~]$ cat>>friend
vvvv
yyyy
xxxx
zzzz
<ctrl+d>

[it08b034@linuxlab ~]$ cat>>fruit


mango
banana
orange
apple
<ctrl+d>

PURPOSE: To display the contents of a file.


SYNTAX: $ cat filename

OUTPUT:
[it08b034@linuxlab ~]$ cat friend
vvvv
yyyy
xxxx

6
CS1255 Operating system Laboratory

zzzz

[it08b034@linuxlab ~]$ cat fruit


mango
banana
orange
apple

PURPOSE: To copy the contents from one file to another. Here the file1 contents
replaces file2 contents. [Ex. Cut + Paste / Overwrite]
SYNTAX: $ cp file1 file2

OUTPUT:
[it08b034@linuxlab ~]$ cp friend fruit
[it08b034@linuxlab ~]$ cat fruit
vvvv
yyyy
xxxx
zzzz

[it08b034@linuxlab ~]$ cat friend


vvvv
yyyy
xxxx
zzzz

PURPOSE: To move the contents from one file to another. Here file1 contents will be
moved to file2 and file1 will get deleted.
SYNTAX: $ mv file1 file2

OUTPUT
[it08b034@linuxlab ~]$ mv fruit friend
[it08b034@linuxlab ~]$ cat friend
vvvv
yyyy
xxxx
zzzz

[it08b034@linuxlab ~]$ cat fruit


cat: fruit: No such file or directory
PURPOSE: To display few lines of a file from the top.
SYNTAX: $ head –n filename

OUTPUT:
[it08b034@linuxlab ~]$ head -2 friend
vvvv

7
CS1255 Operating system Laboratory

yyyy
PURPOSE: To display few lines of a file from the bottom.
SYNTAX: $ head –n filename

OUTPUT:
[it08b034@linuxlab ~]$ tail -3 friend
yyyy
xxxx
zzzz

10. COMMAND: QUERY TERMINFO DATABASE

PURPOSE: Used to position the cursor from 5th to 10th place


SYNTAX: $ tput cup 5 10

OUTPUT:
[it08b034@linuxlab ~]$ cat >subjects
Operating Systems
Database Management Systems
Data Structures
Mathematics
Information Coding Techniques
Discrete Mathematics
Principles of Communication
Digital Signal Processing
Tele Communications
Computer Networks
Mobile Computing
Network Programming and Management
Embedded Systems
Software Engineering
Object Oriented Programming

Ctrl + Z
[5]+ Stopped cat >subjects

[it08b034@linuxlab ~]$ tput cup 5 10

[it08b034@linuxlab ~]$ cat >subjects


Operating Systems
Database Management Systems
Data Structures
Mathematics
Informatio[it08b034@linuxlab ~]$
Discrete Mathematics
Principles of Communication

8
CS1255 Operating system Laboratory

Digital Signal Processing


Tele Communications
Computer Networks
Mobile Computing
Network Programming and Management
Embedded Systems
Software Engineering
Object Oriented Programming

[5]+ Stopped cat >subjects

PURPOSE: Used to clear the terminfo


SYNTAX: $ tput clear

OUTPUT:
[it08b034@linuxlab ~]$

11. COMMAND: CALCULATE

PURPOSE: It is as an arbitrary precision calculator used for doing calculations


SYNTAX: $ bc

OUTPUT:
[it08b034@linuxlab ~]$ bc
15+2
17
<ctrl+d>

12. COMMAND: SPELL CHECK

PURPOSE: It is used to check the spelling of the contents of a particular file.


SYNTAX: $ spell filename

OUTPUT:
[it08b034@linuxlab ~]$ spell subjects
Ntework
Tele

13. COMMAND: TERMINAL

PURPOSE: To know the terminal you are working.


SYNTAX: $ tty

OUTPUT:
[it08b034@linuxlab ~]$ tty
/dev/pts/41

9
CS1255 Operating system Laboratory

14. COMMAND: CLEAR

PURPOSE: To clear the contents of the screen.


SYNTAX: $ clear

OUTPUT:
[it08b034@linuxlab ~]$ clear

15. COMMAND: PASSWORD

PURPOSE: To change the password.


SYNTAX: $ passwd

OUTPUT:
[it08b034@linuxlab ~]$ passwd
Changing password for user it08b034.
Changing password for it08b034
(current) UNIX password:
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

RESULT:

Thus the basic LINIX commands for general utility purpose were studied and
executed.

10
CS1255 Operating system Laboratory

IMPLEMENTATION OF FORK, EXEC, GETPID, EXIT AND WAIT


SYSTEM CALL

AIM:

To write a C program to implement fork, exec, getpid, exit and wait system calls.

ALGORITHM:

1. Using fork system call, create a new process.


2. Assign the process’s id in the variable pid.
3. If pid is equal to –1, then report an error and terminate using the exit system call.
4. If pid is equal to 0, it is a child process. Print the process’s id using the getpid
system call.
5. Otherwise, it is the parent process. Print the ids of both child and parent process.
6. Using execlp system call, the present working directory is displayed.
7. Status of the processes is displayed using wait system call.
8. Terminate the program using the exit system call.

PROGRAM:

[it08b034@linuxlab ~]$ vi gfsystemcall.c

//USING FORK,EXEC,GETPID,EXIT,WAIT SYSTEM CALL

#include<unistd.h>
#include<stdio.h>
main()
{
int pid,status;
pid=fork();
if(pid==-1)
{
perror("Error fork()");
}
else if(pid==0)
{
printf("I am the child process\n");
printf("Process id of the child is=%d\n", getpid());
execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n");

11
CS1255 Operating system Laboratory

printf("Process id of parent=%d\n Process id of


child =%d\n",getpid(),pid);
wait(&status);
printf("Child Returned %d\n",status);
}
exit(0);
}

OUTPUT:

"gfsystemcall.c" 28L, 438C written


[it08b034@linuxlab ~]$ cc gfsystemcall.c
[it08b034@linuxlab ~]$ ./a.out
I am the child process
Process id of the child is=29980
I am the parent process
Process id of parent=29979
Process id of child =29980
/home/it08b034
Child Returned 0

RESULT:

Thus the C program was written and executed using fork, exec, getpid, exit and
wait system calls.

12
CS1255 Operating system Laboratory

IMPLEMENTATION OF STAT SYSTEM CALL

AIM:

To write a C program using stat system call.

ALGORITHM:

1. Pass the filename where status is to be found through the command line.
2. Use stat system call to get the inode information of the file.
3. Print the type, size, inode number and mode of the file.

PROGRAM:

[it08b034@linuxlab ~]$ vi stat.c

//IMPLEMENTATION OF STAT SYSTEM CALL

#include<stdio.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
struct stat s;
stat(argv[1],&s);
if(argc!=2)
{
printf(“Incorrect number of arguments\n”);
exit(1);
}
if(s.st_mode & S_IFREG)
printf(“%s is a regular file”,argv[1]);
if(s.st_mode & S_IFDIR)
printf(“%s is a directory”,argv[1]);
printf(“\n Size is %d”,s.st_size);
printf(“\n Node number is %d”,s.st_ino);
printf(“\n Mode is %o\n”,s.st_mode);
}

OUTPUT:

“stat.c” 20L, 433C written


[it08b034@linuxlab ~]$ cc stat.c
[it08b034@linuxlab ~]$ ./a.out
Incorrect number of arguments
[it08b034@linuxlab ~]$ ./a.out stat.c
stat.c is a regular file

13
CS1255 Operating system Laboratory

Size is 433
Node number is 3670074
Mode is 100664

[it08b034@linuxlab ~]$ mkdir stat


[it08b034@linuxlab ~]$ cc stat.c
[it08b034@linuxlab ~]$ ./a.out stat
stat is a directory
Size is 4096
Node number is 3670069
Mode is 40775

RESULT:

Thus the C program to implement using stat system call was successfully
executed.

14
CS1255 Operating system Laboratory

IMPLEMENTATION OF OPENDIR, READDIR AND CLOSEDIR SYSTEM


CALLS

AIM:

To write a C program using opendir, readdir and closedir system calls.

ALGORITHM:

1. Open the current directory using opendir system call.


2. If the directory is not opened report an error message.
3. Otherwise display the contents of the directory using readdir system call.
4. Close the opened directory using closedir system call.

PROGRAM:

[it08b034@linuxlab ~]$ vi gfsystemcall.c

//IMPLEMENTATION OF OPENDIR, READDIR AND CLOSEDIR

#include<dirent.h>
#include<stdio.h>
main()
{
DIR *dir;
struct dirent *entry;
if((dir=opendir("/"))==NULL)
perror("Error during Opendir() error");
else
{
puts("contents of the current directory");
while((entry=readdir(dir))!=NULL)
printf("%s\n",entry->d_name);
closedir(dir);
}
}

OUTPUT:

"gfsystemcall.c" [New] 17L, 281C written


[it08b034@linuxlab ~]$ cc gfsystemcall.c
[it08b034@linuxlab ~]$ ./a.out
contents of the current directory
root
.bash_history

15
CS1255 Operating system Laboratory

sys
opt
misc
..
initrd
home
dev
mnt
etc
media
tmp
proc
.
lib
sbin
.autofsck
srv
bin
tftpboot
var
usr
boot
lost+found
selinux

RESULT:

Thus the C program for implementing opendir, readdir and closedir system calls
was executed successfully.

16
CS1255 Operating system Laboratory

IMPLEMENTING I/O SYSTEM CALL

AIM:

To write a C program for copying a file into another using I/O system call.

ALGORITHM:

1. Get the name of the source and destination file from the command line.
2. Open the source file in read-only mode using the open system call. If the file
could not be open, print an error message.
3. Create the destination file using creat system call. If the file could not be open,
print an error message.
4. Read from the source file using the read system call and store the content in the
buffer.
5. From the buffer write to the destination file using the write system call.

PROGRAM:

[it08b034@linuxlab ~]$ vi iosys.c

//IMPLEMENTING I/O SYSTEM CALL

#include<stdio.h>
int main(int argc,char *argv[])
{
int fd1,fd2,n;
char buf[512];
if(argc!=3)
{
printf("Error in the number of argument");
exit(1);
}
if((fd1=open(argv[1],0))<0)
{
printf("Can't open %s",argv[1]);
exit(1);
}
if((fd2=creat(argv[2],0))<0)
{
printf("Can't create %s",argv[2]);
exit(1);
}
n=read(fd1,buf,512);

17
CS1255 Operating system Laboratory

write(fd2,buf,n);
close(fd1);
close(fd2);
}

INPUT FILE:

[it08b034@linuxlab ~]$ cat fork.c


#include<unistd.h>
#include<stdio.h>
main()
{
int pid,status;
pid=fork();
if(pid==-1)
{
perror("Error fork()");
exit(1);
}
else if(pid==0)
{
printf("I am the child process\n");
printf("My process id is: %d\n",getpid());
printf("My parent process id is:%d\n",getppid());
execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n");
printf("My process id is: %d\n",getpid());
printf("My childs process id is: %d\n",pid());
wait(&status);
printf("Child returned %d\n",status);
}
}

OUTPUT:

"iosys.c" 27L, 368C written


[it08b034@linuxlab ~]$ cc iosys.c
[it08b034@linuxlab ~]$ ./a.out fork.c dir.c
[it08b034@linuxlab ~]$ cat dir.c
#include<unistd.h>
#include<stdio.h>

18
CS1255 Operating system Laboratory

main()
{
int pid,status;
pid=fork();
if(pid==-1)
{
perror("Error fork()");
exit(1);
}
else if(pid==0)
{
printf("I am the child process\n");
printf("My process id is: %d\n",getpid());
printf("My parent process id is:%d\n",getppid());
execlp("pwd","pwd",NULL);
}
else
{
printf("I am the parent process\n");
printf("My process id is: %d\n",getpid());
printf("My childs process id is: %d\n",pid());
wait(&status);
printf("Child returned %d\n",status);
}
}

RESULT:

Thus the C program for copying a file into another file using I/O system call was
executed successfully.

19
CS1255 Operating system Laboratory

SIMULATING GREP COMMAND

AIM:

To write a C program to simulate the grep command.

ALGORITHM:

6. Pass the search string and the file name as argument to main function through
command line.
7. Print an error message and exit if there is not enough number of arguments.
8. Open the file in the read-only mode using fopen function. If the file could not be
open, print the error message.
9. Repeat the following steps until the end of the file is reached.
a. Read each line from the file into the buffer.
b. Search for the option string in the line if it is print the line.

PROGRAM:

[it08b034@linuxlab ~]$ vi grep.c

//SIMULATING GREP COMMAND

#include<stdio.h>
#include<string.h>
main(int argc,char *argv[])
{
FILE *fp;
char line[100];
if(argc!=3)
{
printf("error");
exit(1);
}
if((fp=fopen(argv[2], "r"))==NULL)
{
printf("command open file\n");
exit(1);
}
while(fgets(line,100,fp)!=NULL)
{
if(strstr(line,argv[1])!=NULL)
printf("%s",line);
}
}

20
CS1255 Operating system Laboratory

OUTPUT:

"grep.c" 24L, 304C written


[it08b034@linuxlab ~]$ cc grep.c
error[it08b034@linuxlab ~]$ ./a.out printf grep.c
printf("error");
printf("command open file\n");
printf("%s",line);

RESULT:

Thus the C program to simulate grep command was executed successfully.

21
CS1255 Operating system Laboratory

SIMULATING LS COMMAND

AIM:

To write a C program to simulate the ls command.

ALGORITHM:

1. Declare a pointer variable.


2. If no files found, display it as a ‘.’ File.
3. Scan directory and find out the file in an alphabetical order.
4. If no files (n<0), it is an error.
5. Otherwise display the name list, using a pointer.
6. Free the name list from the memory.
7. Display the total number of files.

PROGRAM:

[it08b034@linuxlab ~]$ vi ls.c

//SIMULATING LS COMMAND

#include<dirent.h>
#include<time.h>
int main(int argc,char *argv[])
{
struct dirent **namelist;
int i=0,n;
if(argc<=1)
{
n=scandir(".",&namelist,0,alphasort);
}
else
{
n=scandir(argv[1],&namelist,0,alphasort);
}
if(n<0)
{
printf("No file or directory");
}
else
{
while(i<n)
{

22
CS1255 Operating system Laboratory

printf("%s\n",namelist[i]->d_name);
i++;
}
free(namelist);
printf("Total File:%d\n",n);
}
}

OUTPUT:
"ls.c" [New] 30L, 405C written
[it08b034@linuxlab ~]$ cc ls.c
[it08b034@linuxlab ~]$ ./a.out
.
..
.FCFSANDSJF.C.swp
.aspell.en.prepl
.aspell.en.pws
.bash_history
.bash_logout
.bash_profile
.bashrc
.emacs
.expansion.sh.swp
.fact.sh.swp
.fcfs.c.swp
.gfsystemcall.c.swp
.gtkrc
.iosystemcall.c.swp
.kde
.stat.c.swp
.zshrc
1.sh
FCFSANDSJF.C
a
a.out
dir.c
employee.sh
ewsystemcall.c
expansion.sh
f1
f2.c
fact.sh
fcfs.c
fcfssjf.c
fcfssjfarrv.c

23
CS1255 Operating system Laboratory

file.sh
file1
for.sh
fork.c
fp
fsfssjfarrv.c
gfssyatemcall.c
gfsystemcall.c
grep.c
haai
hai
hello
iosys.c
ls.c
marklist.sh
or.c
orsystemcall.c
p
pattern
pattern.sh
pattern1
pattern1.sh
procons.c
producer.c
producercons.c
pustartandexit.c
rrpriority.c
stat
stat.c
studmarklist.sh
studmarklist1.sh
studmarklistreplace.sh
sub.sh
subjects
sum.sh
sumofdigits.sh
syscal.c
typescript
Total File:71

RESULT:

Thus the C program to simulate ls command was executed successfully.

24
CS1255 Operating system Laboratory

IMPLEMENTATION OF FCFS AND SJF SCHEDULING ALGORITHMS

AIM:

To write a C program to implement FCFS and SJF CPU Scheduling algorithms


with arrival time.

ALGORITHM:

1. Get the total number of processes


2. Get the process id and burst time of each process
3. In the fcfs function, the waiting time, completing time and turnaround time of
processes are calculated and the Gantt chart is drawn. The average waiting time
and turnaround time is calculated using a separate function called display.
4. In the sjf function, the processes are arranged in the array in the ascending order
of their burst times. Then, fcfs function is called which draws the Gantt chart and
prints the average waiting time and turnaround time.

PROGRAM:

#include<stdio.h>
struct process
{
char name[5];
int burst,complete,wait,turn,arrival;
};
void fcfs(struct process [],int);
void sjf(struct process [],int);
void chart(struct process [],int);
void display(struct process [],int);
main()
{
int i,np;
struct process pro1[5],pro2[5];
printf("\n enter the number of process");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("\n enter the process id:");
scanf("%s",&pro1[i].name);
printf("\n enter the arrival time:");
scanf("%d",&pro1[i].arrival);
printf("\nenter the burst time:");

25
CS1255 Operating system Laboratory

scanf("%d",&pro1[i].burst);
}
for(i=0;i<np;i++)
pro2[i]=pro1[i];
printf("\n\t\tFCFS Scheduling");
fcfs(pro1,np);
printf("\n\t\tNon-Preemptive SJF Scheduling");
sjf(pro2,np);
}
void fcfs(struct process pro[],int np)
{
struct process temp;
int i,j,tot=0;
pro[0].wait=0;
for(i=0;i<np;i++)
{
for(j=i+1;j<np;j++)
if(pro[i].arrival>pro[j].arrival)
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
tot+=pro[i].burst;
pro[i].complete=tot;
pro[i+1].wait=tot-pro[i+1].arrival;
pro[i].turn=pro[i].complete - pro[i].arrival;
}
chart(pro,np);
}
void chart (struct process pro[],int np)
{
int i,j;
printf("\n\t\tGantt Chart\n");
for(i=1;i<6*np-1;i++)
printf("-");
printf("\n|");
for(i=0;i<np;i++)
{
printf("%s",pro[i].name);
printf("|");
}
printf("\n");
for(i=1;i<6*np-1;i++)
printf("-");
printf("\n%d",pro[0].arrival);

26
CS1255 Operating system Laboratory

for(i=0;i<np;i++)
{
for(j=1;j<=4;j++)
printf(" ");
printf("%d",pro[i].complete);
}
printf("\n\n");
display(pro,np);
}
void sjf(struct process pro[],int np)
{
struct process temp;
int i,j,complete;
complete=pro[0].arrival;
for(i=0;i<np;i++)
{
for(j=i+1;j<np;j++)
if((pro[i].burst>pro[j].burst&&pro[j].arrival<=complete)||(pro[i].burst==pro[j].burst&&p
ro[i].arrival>pro[j].arrival))
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
complete+=pro[i].burst;
pro[i].complete=complete;
pro[i].turn=pro[i].complete-pro[i].arrival;
pro[i].wait=pro[i].turn-pro[i].burst;
}
chart(pro,np);
}
void display(struct process pro[],int np)
{
int i,totwait=0,totturn=0;
float avgwait,avgturn;
for(i=0;i<np;i++)
{
totwait+=pro[i].wait;
totturn+=pro[i].turn;
}
avgwait=(float)totwait/np;
avgturn=(float)totturn/np;
printf("\n average waiting time:%f",avgwait);
printf("\n average turnaround time:%f",avgturn);
}

27
CS1255 Operating system Laboratory

OUTPUT:
[it08b034@linuxlab ~]$ cc algo.c
[it08b034@linuxlab ~]$ ./a.out

enter the number of process2

enter the process id:p1

enter the arrival time:0

enter the burst time:2

enter the process id:p2

enter the arrival time:0

enter the burst time:3

FCFS Scheduling
Gantt Chart
----------
|p1|p2|
----------
0 2 5

average waiting time:1.000000


average turnaround time:3.500000
Non-Preemptive SJF Scheduling
Gantt Chart
----------
|p1|p2|
----------
0 2 5

average waiting time:1.000000


average turnaround time:3.500000

RESULT:

Thus the C program to implement FCFS, SJF scheduling algorithms with arrival
time were implemented and process’s average waiting time; average turnaround times
were computed and Gantt chart was displayed successfully.

28
CS1255 Operating system Laboratory

IMPLEMENTATION OF NON-PREEMPTIVE PRIORITY AND ROUND ROBIN


SCHEDULING ALGORITHMS
AIM:

To write a C program to implement non-preemptive priority scheduling and SJF


CPU Scheduling algorithms with arrival time.

ALGORITHM:

1. Get the total number of processes.


2. Get the process name, burst time and arrival time of each process.
3. When the function priority() is called, go to step (5).
4. When the function rr() is called, go to step (6).
5. Priority Scheduling
a) In the priority function, get the priority of each process.
b) The processes are arranged in the array in the ascending order of their
priority value, also considering their arrival time.
c) Then, the waiting time, time of completion and turnaround time of processes
are calculated.
d) The Gantt chart is drawn by calling the function chart().
e) The function display() is called to display the average waiting time and
average turnaround time.
6. Round Robin Scheduling
a) In the rr function, get the time slice.
b) The processes get the CPU in FCFS order only for the time slice given, in
round-robin fashion.
c) Then, the waiting time, time of completion and turnaround time of processes
are calculated.
d) The Gantt chart is drawn by calling the function chart().
e) The function display() is called to display the average waiting time and
average turnaround time.

PROGRAM:
[it08b034@linuxlab ~]$ vi rrpriority.c
#include<stdio.h>
#include<string.h>
struct process
{
char name[5];
int arrival,burst,prior,complete,wait,turn;
};

void priority(struct process [], int);


void rr(struct process [], int);
void chart(struct process [], int);
void display(struct process[], int);

29
CS1255 Operating system Laboratory

main()
{
int i,n;
struct process pro1[5],pro2[5];
printf("\nEnter the number of processes: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter the process name: ");
scanf("%s", pro1[i].name);
printf("Enter the arrival time: ");
scanf("%d", &pro1[i].arrival);
printf("Enter the burst time in milliseconds: ");
scanf("%d", &pro1[i].burst);
pro2[i]=pro1[i];
}
printf("\n\t\tNon-preemptive Priority Scheduling\n");
priority(pro1,n);
printf("\n\t\tRound Robin Scheduling\n");
rr(pro2,n);
}

void priority(struct process pro[], int np)


{
struct process temp;
int i,j,complete;
complete=pro[0].arrival;
for(i=0;i<np;i++)
{
printf("Enter the priority of %s: ", pro[i].name);
scanf("%d", &pro[i].prior);
}
for(i=0;i<np;i++)
{
for(j=i+1;j<np;j++)
if((pro[i].prior>pro[j].prior &&
pro[j].arrival<=complete) ||
(pro[i].prior==pro[j].prior &&
pro[i].arrival > pro[j].arrival))
{
temp=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
complete+=pro[i].burst;
pro[i].complete=complete;

30
CS1255 Operating system Laboratory

pro[i].turn=pro[i].complete-pro[i].arrival;
pro[i].wait=pro[i].turn-pro[i].burst;
}
chart(pro,np);
display(pro,np);
}

void chart(struct process pro[], int np)


{
int i,j;
printf("\n\t\tGantt Chart\n ");
for(i=1;i<=5*np-1;i++)
printf("-");
printf("\n|");
for(i=0;i<np;i++)
{
printf(" %s ", pro[i].name);
printf("|");
}
printf("\n ");
for(i=1;i<=5*np-1;i++)
printf("-");
printf("\n%d", pro[0].arrival);
for(i=0;i<np;i++)
{
for(j=1;j<=4;j++)
printf(" ");
printf("%d", pro[i].complete);
}
printf("\n\n");
}

void display(struct process pro[], int np)


{
int i,totwait=0,totturn=0;
float avgwait,avgturn;
for(i=0;i<np;i++)
{
totwait+=pro[i].wait;
totturn+=pro[i].turn;
}
avgwait=(float)totwait/np;
avgturn=(float)totturn/np;
printf("\nAverage waiting time: %f milliseconds", avgwait);
printf("\nAverage Turnaround time: %f milliseconds\n", avgturn);
}

31
CS1255 Operating system Laboratory

void rr(struct process pro[], int np)


{
int ts,i,j=0,totitr=0,complete;
struct process temp[15],old[5];
printf("\nEnter the time slice in milliseconds: ");
scanf("%d", &ts);
temp[0].arrival=complete=pro[0].arrival;
for(i=0;i<np;i++)
{
old[i]=pro[i];
if(pro[i].burst%ts==0)
totitr+=pro[i].burst/ts;
else
totitr+=pro[i].burst/ts+1;
}
for(j=0;j<totitr;)
for(i=0;i<np;i++)
if(pro[i].arrival<=complete && pro[i].burst!=0)
{
if(pro[i].burst<=ts)
{
complete+=pro[i].burst;
temp[j].burst=pro[i].burst;
pro[i].burst=0;
}
else
{
complete+=ts;
temp[j].burst=ts;
pro[i].burst-=ts;
}
strcpy(temp[j].name,pro[i].name);
temp[j++].complete=complete;
}
for(i=0;i<np;i++)
{
for(j=0;j<totitr;j++)
if(strcmp(old[i].name,temp[j].name)==0)
old[i].complete=temp[j].complete;
old[i].turn=old[i].complete-
old[i].arrival;
old[i].wait=old[i].turn-
old[i].burst;
}
chart(temp,totitr); display(old,np);

32
CS1255 Operating system Laboratory

OUTPUT:
"rrpriority.c" 148L, 3174C written
[it08b034@linuxlab ~]$ cc rrpriority.c
[it08b034@linuxlab ~]$ ./a.out

Enter the number of processes: 3

Enter the process name: p1


Enter the arrival time: 2
Enter the burst time in milliseconds: 3

Enter the process name: p2


Enter the arrival time: 0
Enter the burst time in milliseconds: 2

Enter the process name: p3


Enter the arrival time: 4
Enter the burst time in milliseconds: 4

Non-preemptive Priority Scheduling


Enter the priority of p1: 2
Enter the priority of p2: 3
Enter the priority of p3: 1

Gantt Chart

--------------
| p1 | p3 | p2 |
--------------
2 5 9 11

Average waiting time: 3.333333 milliseconds


Average Turnaround time: 6.333333 milliseconds

33
CS1255 Operating system Laboratory

Round Robin Scheduling

Enter the time slice in milliseconds: 2

Gantt Chart
------------------------
| p1 | p2 | p3 | p1 | p3 |
------------------------
2 4 6 8 9 11
Average waiting time: 3.666667 milliseconds
Average Turnaround time: 6.666667 milliseconds

RESULT:

Thus the C program to implement non-preemptive priority, preemptive round


robin scheduling algorithms was implemented and process’s average waiting time;
average turnaround times were computed and Gantt chart was displayed successfully.

34
CS1255 Operating system Laboratory

IMPLEMENTATION OF
INTER PROCESS COMMUNICATION USING SHARED MEMORY

AIM
To write a C program to implement inter process communication using shared
memory.

ALGORITHM
1. Create the child process using fork ( ).

2. Create the shared memory for parent process using shmget() system call.

3. Now allow the parent process to write in shared memory using shmpet pointer

which is return type of shmat().

4. Now across and attach the same shared memory to the child process.

5. The data in the shared memory is read by the child process using the shnot

pointer.

6. Now, detach and rebase the shared memory.

PROGRAM
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child, shmid, i;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,0666|IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf ("\n PARENT WAITING\n");
for(i=0;i<10;i++)
{
shmptr[i]='a' + i;
putchar(shmptr[i]);
}

printf("\n\n %s", shmptr);


wait(NULL);

35
CS1255 Operating system Laboratory

}
else
{
shmid = shmget(2041,32,0666);
shmptr = shmat(shmid, 0,0);
printf("\n CHILD IS READING :");
for(i=0;i<10; i++)
putchar(shmptr[i]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
return 0;

OUTPUT

[it08b034@linuxlab] cc shared.c
[it08b034@linuxlab] $ ./a.out

PARENT WAITING
abcdefghij

CHILD IS READING :abcdefghij

RESULT
Thus the C program to implement the interprocess communication using shared
memory was completed successfully.

36
CS1255 Operating system Laboratory

IMPLEMENTATION OF
INTER PROCESS COMMUNICATION USING PIPES
AIM
To write a C program to implement inter process communication using pipes.

ALGORITHM
1. Create the child process using fork ().

2. Create the pipe structure using pipe ()

3. Now close the read end of the parent process using closer ().

4. Write the data in the pipe using write.

5. Now close the write end of child process using closer ().

6. Read the data in the pipe using read ().

7. Display the string.

PROGRAM

#include<stdio.h>
int main()
{
int fd[2], child;
char a[10];
printf("\n Enter the string to enter in to the pipe:");
scanf("%s",&a);
pipe(fd);
child=fork();
if(!child )
{
close(fd[0]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("\n The string retrieved from the pipe is %s\n",a);
}
return 0;
}

37
CS1255 Operating system Laboratory

OUTPUT

[it08b034@linuxlab]$ cc pipe.c
[it08b034@linuxlab]$ ./a.out

Enter the string to enter in to the pipe: abcdefghijklmnopqrstuvwxyz

The string retrieved from the pipe is : abcdefghijklmnopqrstuvwxyz

RESULT
Thus the C program to implement the interprocess communication using pipes
was successfully completed.

38
CS1255 Operating system Laboratory

IMPLEMENTATION OF PRODUCER-CONSUMER PROBLEM USING


SEMAPHORES

AIM:
To write a C program to solve the bounded buffer Producer-Consumer problem
using semaphores.

ALGORITHM:

1. Get the size of the buffer through the command line.


2. Declare the semaphore objects mutex for mutual exclusion, nempty for the
number of empty positions in the buffer, nstored for the number of full positions
in the buffer.
3. Initialize the semaphore objects.
4. Create two threads, one for running the function produce( ) and the other for
running the function consume( ). Write the functions in such a way that the
producer and the consumer are in synchronization with each other.
5. Wait for the termination of the threads.
6. Destroy the semaphore objects.

PROGRAM:

[it08b034@linuxlab ~]$ vi producercons.c

#include<stdio.h>
#define BUF_SIZ 4
int in=0,out=-1;
int count=0;
typedef struct
{
int stat,pid;
}Item;
Item item[BUF_SIZ];
void producer()
{
if(in==out&&in==0)
{
printf("Buffer overflow\n");
printf("In:%d \tout:%d\n",in,out);
}
else
{
if(item[in%BUF_SIZ].stat==0)
{

39
CS1255 Operating system Laboratory

Item temp;
temp.stat=1;
temp.pid=rand()%1000;
item[(in%BUF_SIZ)]=temp;
in=(in%BUF_SIZ)+1;
printf("Process added with
id:%d\n",temp.pid);
printf("In:%d\t out:%d\n",in,out);
if(count>=BUF_SIZ)
count=(count%BUF_SIZ)+1;
else
count++;
}
else
{
printf("Error: Buffer overflow\n");
printf("In:%d\t Out:%d\n",in,out);
}
}
}
void view()
{
int i;
printf("\n processes\n");
for(i=0;i<BUF_SIZ;i++)
{
if(item[i].stat==0)
printf("Buffer position empty\n");
else
printf("process %d in buffer position %d\n",item[i].pid,i+1);
}
}

void consume()
{
if(in==0&&out==-1)
{
printf("No process to consume\n");
}
else
{
if(item[(out+1)%BUF_SIZ].stat==0)
printf("Error cannot consume process\n");
else
{
out=(out+1)%BUF_SIZ;

40
CS1255 Operating system Laboratory

item[out].stat=0;
printf("In:%d\t out:%d\n",in,out);
printf("Item consumed\n");
}
}
}

int main(void)
{
int ch=0;
system("clear");
do{
printf("\n1.produce new process to buffer\n");
printf("\n2.consume process\n");
printf("\n3.view buffer\n");
printf("\n4.Exit program\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
producer();
view();
break;
case 2:
consume();
view();
break;
case 3:
view();
break;
}
}
while(ch!=4);
}

OUTPUT:

"producercons.c" 99L, 1532C written


[it08b034@linuxlab ~]$ cc producercons.c
[it08b034@linuxlab ~]$ ./a.out

1.produce new process to buffer

2.consume process

41
CS1255 Operating system Laboratory

3.view buffer

4.Exit program

Enter your choice:1


Process added with id:383
In:1 out:-1

processes
process 383 in buffer position 1
Buffer position empty
Buffer position empty
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

processes
process 383 in buffer position 1
Buffer position empty
Buffer position empty
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:1


Process added with id:886
In:2 out:-1

processes
process 383 in buffer position 1
process 886 in buffer position 2
Buffer position empty

42
CS1255 Operating system Laboratory

Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:1


Process added with id:777
In:3 out:-1

processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:1


Process added with id:915

43
CS1255 Operating system Laboratory

In:4 out:-1

processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:1


Error: Buffer overflow
In:4 Out:-1

processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

44
CS1255 Operating system Laboratory

3.view buffer

4.Exit program

Enter your choice:2


In:4 out:0
Item consumed

processes
Buffer position empty
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

processes
Buffer position empty
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program
Enter your choice:2
In:4 out:1
Item consumed

processes
Buffer position empty
Buffer position empty
process 777 in buffer position 3

45
CS1255 Operating system Laboratory

process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

processes
Buffer position empty
Buffer position empty
process 777 in buffer position 3
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:2


In:4 out:2
Item consumed

processes
Buffer position empty
Buffer position empty
Buffer position empty
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3

46
CS1255 Operating system Laboratory

processes
Buffer position empty
Buffer position empty
Buffer position empty
process 915 in buffer position 4

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:2


In:4 out:3
Item consumed

processes
Buffer position empty
Buffer position empty
Buffer position empty
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:3


processes
Buffer position empty
Buffer position empty
Buffer position empty
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

47
CS1255 Operating system Laboratory

Enter your choice:2


Error cannot consume process

processes
Buffer position empty
Buffer position empty
Buffer position empty
Buffer position empty

1.produce new process to buffer

2.consume process

3.view buffer

4.Exit program

Enter your choice:4


[it08b034@linuxlab ~]$

RESULT:

Thus the C program to solve bounded buffer producer – consumer problem using
semaphores was implemented and executed successfully.

48
CS1255 Operating system Laboratory

IMPLEMENTATION OF MEMORY MANAGEMENT SHEME I – MEMORY


ALLOCATION USING LINKED LISTS

AIM:
To write a C program to implement memory management for first-fit, best-fit and
worst-fit memory allocation using linked lists.

ALGORITHM:

1. Create two lists, one for free list and another for allocated list and get new process
to be allocated.
2. Give choice to the users. 1.Fist-Fit, 2.Best-Fit, 3.Worst-Fit and 4.Exit.
3. If the choice is 1, do first-fit as follows.
Traverse the free list from the start. If the memory element of the list
satisfies the given memory requirement, then delete it from free list and allocate
to allocated list.
4. If the choice is 2, do best-fit as follows.
Arrange the free list elements in ascending order and now traverse from
the start to find the best fit. Delete it from free list and add to allocated list.
5. If the choice is 3, do worst-fit as follows.
Arrange the free list elements in descending order and now traverse list to
get worst fit. Delete the selected element from free list and add to allocated list.
6. If the choice is 4, exit from the program.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char pid[3];
int mem;
struct node *next;
}*flist,*alist;
void flcreate()
{
int k=100,i;
struct node *nn,*p;
nn=(struct node*)malloc(sizeof(struct node));
flist=nn;
nn->pid[0]='f';
nn->pid[1]='\0';
nn->mem=100;
p=nn;
for(i=0;i<5;i++)

49
CS1255 Operating system Laboratory

{
nn=(struct node*)malloc(sizeof(struct node));
nn->pid[0]='f';
nn->pid[1]='\0';
nn->mem=k;
p->next=nn;
nn->next=NULL;
p=p->next;
k=k+100;
}
}
void fldisplay()
{
struct node *p;
p=flist->next;
while(p->next!=NULL)
{
printf("|%s|%d|--->n",p->pid,p->mem);
p=p->next;
}
printf("|%s|%d|",p->pid,p->mem);
}
void alcreate()
{
int k=50,i,j=48;
struct node *nn,*p;
nn=(struct node*)malloc(sizeof(struct node));
alist=nn;
nn->pid[0]='p';
nn->pid[1]=(char)j;
nn->pid[2]='\0';
nn->mem=100;
p=nn;
for(i=0;i<5;i++,j++)
{
nn=(struct node*)malloc(sizeof(struct node));
nn->pid[0]='p';
nn->pid[1]=(char)j;
nn->pid[2]='\0';
nn->mem=k;
p->next=nn;
nn->next=NULL;
p=p->next;
k=k+50;
}
}

50
CS1255 Operating system Laboratory

void aldisplay()
{
struct node *p;
p=alist->next;
while(p->next!=NULL)
{
printf("|%s|%d|-->",p->pid,p->mem);
p=p->next;
}
printf("|%s|%d|",p->pid,p->mem);
}
void allocate1()
{
char id[3];
int m,i;
struct node *p,*q,*nn,*r;
printf("\n\nEnter the process details:");
printf("\n Enter the process id:");
scanf("%s",id);
printf("\nEnter the memory req:");
scanf("%d",&m);
q=p=flist;
q=(struct node*)malloc(sizeof(struct node));
p=(struct node*)malloc(sizeof(struct node));
if(flist->mem>=m)
flist->next=flist->next->next;
else
{
q=p;
p=flist->next;
while(p!=NULL)
{
if(p->mem>=m)
{
q->next=p->next;
break;
}
q=p;
p=p->next;
}
}
nn=(struct node*)malloc(sizeof(struct node));
for(i=0;id[i];i++)
nn->pid[i]=id[i];
nn->mem=m;
p=alist;

51
CS1255 Operating system Laboratory

while(p->next!=NULL)
p=p->next;
p->next=nn;
nn->next=NULL;
}
void allocate2()
{
char id[3];
struct node *p,*q,*nn,*r;
int m,i=0,temp,j,k,a[10];
printf("\n\nEnter the process details:");
printf("\n enter the process id:");
scanf("%s",id);
printf("\n\nenter the memory request:");
scanf("%d",&m);
p=flist;
while(p!=NULL)
{
a[i++]=p->mem;
p=p->next;
}
for(j=0;j<i;j++)
for(k=0;k<i;k++)
{
if(a[j]<a[k])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
p=flist;i=0;
while(p!=NULL)
{
p->mem=a[i++];
p=p->next;
}
printf("\nFree List:\n");
fldisplay();
q=p=flist;
q=(struct node*)malloc(sizeof(struct node));
p=(struct node*)malloc(sizeof(struct node));
if(flist->mem>=m)
flist->next=flist->next->next;
else
{

52
CS1255 Operating system Laboratory

q=p;
p=flist->next;
while(p!=NULL)
{
if(p->mem>=m)
{
q->next=p->next;
break;
}
q=p;
p=p->next;
}
}
nn=(struct node*)malloc(sizeof(struct node));
for(i=0;id[i];i++)
nn->pid[i]=id[i];
nn->mem=m;
p=alist;
while(p->next!=NULL)
p=p->next;
p->next=nn;
nn->next=NULL;
}

void allocate3()
{
char id[3];
int m,i=0,temp,j,k,a[10];
struct node *p,*q,*nn,*r;
printf("\n\nEnter the process details: \n");
printf("Enter the process id: ");
scanf("%s",id);
printf("\nEnter the memory req: ");
scanf("%d",&m);
p=flist;
while(p!=NULL)
{
a[i++]=p->mem;
p=p->next;
}
for(j=0;j<i;j++)
for(k=0;k<i;k++)
{
if(a[j]>a[k])
{
temp=a[j];

53
CS1255 Operating system Laboratory

a[j]=a[k];
a[k]=temp;
}
}
p=flist;i=0;
while(p!=NULL)
{
p->mem=a[i++];
p=p->next;
}
printf("\nFree List:\n");
fldisplay();
q=p=flist;
q=(struct node*)malloc(sizeof(struct node));
p=(struct node*)malloc(sizeof(struct node));
if(flist->mem>=m)
flist->next=flist->next->next;
else
{
q=p;
p=flist->next;
while(p!=NULL)
{
if(p->mem>=m)
{
break;
}
q=p;
p=p->next;
}
}
nn=(struct node*)malloc(sizeof(struct node));
for(i=0;id[i];i++)
nn->pid[i]=id[i];
nn->mem=m;
p=alist;
while(p->next!=NULL)
p=p->next;
p->next=nn;
nn->next=NULL;

}
main()
{
int c;
while(1)

54
CS1255 Operating system Laboratory

{
printf("\n1.First fit\n 2. Best Fit \n 3. Worst Fit\n 4. Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nFree List:\n");
flcreate();
fldisplay();
printf("\nAllocated List:\n");
alcreate();
aldisplay();
allocate1();
printf("\nNew Lists are:\n");
printf("\nFree List:\n");
fldisplay();
printf("\nAllocated List:\n");
aldisplay();
break;
case 2:
printf("\nFree List:\n");
flcreate();
fldisplay();
printf("\nAllocated List:\n");
alcreate();
aldisplay();
allocate2();
printf("\nNew Lists are:\n");
printf("\nFree List:\n");
fldisplay();
printf("\nAllocated List:\n");
aldisplay();
break;
case 3:
printf("\nFree List:\n");
flcreate();
fldisplay();
printf("\nAllocated List:\n");
alcreate();
aldisplay();
allocate3();
printf("\nNew Lists are:\n");
printf("\nFree List: \n");
fldisplay();
printf("\nAllocated List: \n");
aldisplay();

55
CS1255 Operating system Laboratory

break;

case 4:
exit(0);
}
}
}
OUTPUT:
"mm1.c" 304L, 6914C written
[it08b034@linuxlab ~]$ ./c

1.First fit
2. Best Fit
3. Worst Fit
4. Exit
1

Free List:
|f|100|--->n|f|200|--->n|f|300|--->n|f|400|--->n|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|

Enter the process details:


Enter the process id:p5
Enter the memory req:300

New Lists are:


Free List:
|f|100|--->n|f|200|--->n|f|400|--->n|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|-->|p5|300|
1.First fit
2. Best Fit
3. Worst Fit
4. Exit
2

Free List:
|f|100|--->n|f|200|--->n|f|300|--->n|f|400|--->n|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|

Enter the process details:


Enter the process id:p5

Enter the memory request:300

56
CS1255 Operating system Laboratory

Free List:
|f|100|--->n|f|200|--->n|f|300|--->n|f|400|--->n|f|500|
New Lists are:
Free List:
|f|100|--->n|f|200|--->n|f|400|--->n|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|-->|p5|300|
1.First fit
2. Best Fit
3. Worst Fit
4. Exit
3

Free List:
|f|100|--->n|f|200|--->n|f|300|--->n|f|400|--->n|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|

Enter the process details:


Enter the process id: p5

Enter the memory req: 30

Free List:
|f|400|--->n|f|300|--->n|f|200|--->n|f|100|--->n|f|100|
New Lists are:

Free List:
|f|300|--->n|f|200|--->n|f|100|--->n|f|100|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|-->|p5|30|
1.First fit
2. Best Fit
3. Worst Fit
4. Exit
4
[it08b034@linuxlab ~]$

RESULT:

Thus the C program to implement memory management scheme I – allocation of


memory using linked list was written and executed successfully.

57
CS1255 Operating system Laboratory

IMPLEMENTATION OF MEMORY MANAGEMENT SHEME II – MEMORY


DE-ALLOCATION USING LINKED LISTS

AIM:
To write a C program to implement memory management for memory de-
allocation using linked lists.

ALGORITHM:

1. Create two lists, one for free list and another for allocated list.
2. Get the process id of the completed process.
3. De-allocate that process from the allocated list.
4. Allocate this memory to free list by joining it to existing free list.

PROGRAM:

[it08b034@linuxlab ~]$ vi mdealloc.c

#include<stdio.h>
struct node
{ char pid[3];
}*flist,*alist;
void flcreate()
{
int k=100,i;
struct node *nn,*p;
nn=(struct node*)malloc(sizeof(struct node));
flist=nn;
nn->pid[0]='f';
nn->pid[1]='\0';
nn->mem=100;
p=nn;
for(i=0;i<5;i++)
{
nn=(struct node *)malloc(sizeof(struct node));
nn->pid[0]='f';
nn->pid[1]='\0';
nn->mem=k;
p->next=nn;
nn->next=NULL;
p=p->next;
k=k+100;
}

58
CS1255 Operating system Laboratory

}
void fldisplay()
{
struct node *p;
p=flist->next;
while(p->next!=NULL)
{
printf("|%s|%d|--->",p->pid,p->mem);
p=p->next;
}
printf("|%s|%d|",p->pid,p->mem);
}
void alcreate()
{
int k=50,i,j=48;
struct node *nn,*p;
nn=(struct node*)malloc(sizeof(struct node));
alist=nn;
nn->pid[0]='p';
nn->pid[1]=(char)j;
nn->pid[2]='\0';
nn->mem=100;
p=nn;
for(i=0;i<5;i++,j++)
{
nn->pid[0]='p';
nn->pid[1]=(char)j;
nn->pid[2]='\0';
nn->mem=k;
p->next=nn;
nn->next=NULL;
p=p->next;
k=k+50;
}
}
void aldisplay()
{
struct node *p;
p=alist->next;
while(p->next!=NULL)
{
printf("|%s|%d|-->",p->pid,p->mem);
p=p->next;
}
printf("|%s|%d|",p->pid,p->mem);

59
CS1255 Operating system Laboratory

void deallocate()
{
char id[3];
int m,i;
struct node *p,*q,*nn,*r;
printf("\n\nEnter the process details: \n");
printf("Enter the process id and memory to be deallocated: ");
scanf("%s %d",id,&m);
q=p=alist;
q=(struct node*)malloc(sizeof(struct node));
p=(struct node*)malloc(sizeof(struct node));
if(alist->mem>=m)
alist->next=alist->next->next;
else
{
q=p;
p=alist->next;
while(p!=NULL)
{
if(p->mem>=m)
{
q->next=p->next;
break;
}
q=p;
p=p->next;
}
}
nn=(struct node*)malloc(sizeof(struct node));
for(i=0;id[i];i++)
nn->pid[i]=id[i];
nn->mem=m;
p=flist;
while(p->next!=NULL)
p=p->next;
p->next=nn;
nn->next=NULL;
}

main()
{
printf("\nFree List:\n");
flcreate();
fldisplay();

60
CS1255 Operating system Laboratory

printf("\nAllocated List:\n");
alcreate();
aldisplay();
deallocate();
printf("\nNew Lists are:\n");
printf("\nFree List:\n");
fldisplay();
printf("\nAllocated List:\n");
aldisplay();
printf("\n");

OUTPUT:

"mdealloc.c" 151L, 3203C written


[it08b034@linuxlab ~]$ cc -o d mdealloc.c
[it08b034@linuxlab ~]$ ./d

Free List:
|f|100|--->|f|200|--->|f|300|--->|f|400|--->|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|

Enter the process details:


Enter the process id and memory to be deallocated: p3 200

New Lists are:

Free List:
|f|100|--->|f|200|--->|f|300|--->|f|400|--->|f|500|--->|p3|200|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p4|250|

RESULT:

Thus the C program to implement memory management scheme II – de-allocation


of memory using linked list was implemented and executed successfully.

61
CS1255 Operating system Laboratory

IMPLEMENTATION OF FILE ALLOCATION TECHNIQUE

AIM:
To write a C program to implement file allocation technique.

ALGORITHM:

1.
Create three files named as “DATA”, “ODD” and “EVEN” respectively
2.
Declare the “DATA” file in write mode.
3.
Store the input numbers in “DATA” file using putw() function.
4.
Identify the odd and even numbers and
5.
Open two files “DATA” in read mode.
6.
Get the identified odd and even numbers and store those numbers in “ODD”
and “EVEN” files respectively, using getw() function.
7. Display the content of the three files.
PROGRAM:

[it08b034@linuxlab ~]$ vi fm2.c


/*Example program for using getw and putw functions*/
#include<stdio.h>
main()
{
FILE *f1,*f2,*f3;
int number,I;
printf("Contents of the data file\n\n");
f1=fopen("DATA","w");
for(I=1;I<=10;I++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
while((number=getw(f1))!=EOF)/* Read from data file*/
{
if(number%2==0)
putw(number,f3);/*Write to even file*/
else
putw(number,f2);/*write to odd file*/
}

62
CS1255 Operating system Laboratory

fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\nContents of the odd file\n\n");
while((number=getw(f2))!=EOF)
printf("%d\n",number);
printf("\n\nContents of the even file");
while((number=getw(f3))!=EOF)
printf("%d\n",number);
fclose(f2);
fclose(f3);
}

OUTPUT:
"fm2.c" 43L, 831C written
[it08b034@linuxlab ~]$ cc fm2.c
[it08b034@linuxlab ~]$ ./a.out
Contents of the data file
1
2
3
4
5
Contents of the odd file
1
3
5
Contents of the even file
2
4
[it08b034@linuxlab ~]$

RESULT:

Thus the C program to implement file allocation technique was written and
executed successfully

63
CS1255 Operating system Laboratory

IMPLEMENTATION OF
INTER PROCESS COMMUNICATION USING SHARED MEMORY
(CLIENT/SERVER)
AIM
To write a C program to implement inter process communication using shared
memory.(client/Server)

ALGORITHM
1. Create the shared memory size.

2. Create the shared memory for parent process using shmget() system call in

server .

3. Now allow the parent process to write in shared memory , which is return type

of shmat().

4. Now across and attach the same shared memory to the child process.

5. The data in the shared memory is shared by the client and is marked by * if its

shared..

6. Now, detach and rebase the shared memory.

PROGRAM
//Server.c
#include<sys/types.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm,*s;
key =5678;
if((shmid=shmget(key,SHMSZ,IPC_CREAT |0666))<0)
{
perror("shmget");
exit(1);
}

64
CS1255 Operating system Laboratory

if((shm=shmat(shmid,NULL,0))==(char *)-1)
{
perror("shmat");
exit(1);
}
s=shm;
for(c='a';c<='z';c++)
*s++=c;
*s=NULL;
while(*shm!='*')
sleep(1);
exit(0);
}

//Client.c

#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm,*s;
key =5678;
if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
{
perror("shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char *)-1)
{
perror("shmat");
exit(1);
}
for(s=shm;*s!=NULL;s++)
putchar(*s);
putchar('\n');
*shm='*';
exit(0);
}

65
CS1255 Operating system Laboratory

OUTPUT
[it08b034@linuxlab ~]$ cc client.c
[it08b034@linuxlab ~]$ ./a.out
abcdefghijklmnopqrstuvwxyz
[it08b034@linuxlab ~]$ cc server.c
[it08b034@linuxlab ~]$ ./a.out
[it08b034@linuxlab ~]$ ./a.out
*bcdefghijklmnopqrstuvwxyz
[it08b034@linuxlab ~]$ ./a.out
*bcdefghijklmnopqrstuvwxyz

RESULT
Thus the C program to implement the interprocess communication using shared
memory (client/server) was shared successfully.

66

You might also like