CS1255 OS Lab Manual Good
CS1255 OS Lab Manual Good
CS1255 OS Lab Manual Good
M.A.M
School of Engineering
Siruganur, Trichy-621105.
1
CS1255 Operating system Laboratory
1. COMMAND: CALENDAR
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
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
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
OUTPUT:
[it08b034@linuxlab ~]$ date
Thu Jul 23 23:08:23 IST 2009
OUTPUT:
[it08b034@linuxlab ~]$ date +%h
Jul
OUTPUT:
[it08b034@linuxlab ~]$ date +%d
23
OUTPUT:
OUTPUT:
[it08b034@linuxlab ~]$ date +%H
23
OUTPUT:
[it08b034@linuxlab ~]$ date +%M
15
3
CS1255 Operating system Laboratory
3. COMMAND: 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)
OUTPUT:
[it08b034@linuxlab ~]$ whoami
It08b034
4. COMMAND: ECHO
OUTPUT:
[it08b034@linuxlab ~]$ echo hai
hai
OUTPUT:
[it08b034@linuxlab ~]$ mkdir latha
OUTPUT:
[it08b034@linuxlab latha]$
4
CS1255 Operating system Laboratory
OUTPUT:
[it08b034@linuxlab ~]$ rm friend
[it08b034@linuxlab ~]$ cat friend
cat: friend: No such file or directory
6. COMMAND: LIST
OUTPUT:
[it08b034@linuxlab ~]$ ls
f1 file1 friend latha subjects
7. COMMAND: HELP
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
-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
OUTPUT:
[it08b034@linuxlab ~]$ pwd
/home/It08b034
OUTPUT:
[it08b034@linuxlab ~]$ cat>>friend
vvvv
yyyy
xxxx
zzzz
<ctrl+d>
OUTPUT:
[it08b034@linuxlab ~]$ cat friend
vvvv
yyyy
xxxx
6
CS1255 Operating system Laboratory
zzzz
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
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
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
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
8
CS1255 Operating system Laboratory
OUTPUT:
[it08b034@linuxlab ~]$
OUTPUT:
[it08b034@linuxlab ~]$ bc
15+2
17
<ctrl+d>
OUTPUT:
[it08b034@linuxlab ~]$ spell subjects
Ntework
Tele
OUTPUT:
[it08b034@linuxlab ~]$ tty
/dev/pts/41
9
CS1255 Operating system Laboratory
OUTPUT:
[it08b034@linuxlab ~]$ clear
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
AIM:
To write a C program to implement fork, exec, getpid, exit and wait system calls.
ALGORITHM:
PROGRAM:
#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
OUTPUT:
RESULT:
Thus the C program was written and executed using fork, exec, getpid, exit and
wait system calls.
12
CS1255 Operating system Laboratory
AIM:
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:
#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:
13
CS1255 Operating system Laboratory
Size is 433
Node number is 3670074
Mode is 100664
RESULT:
Thus the C program to implement using stat system call was successfully
executed.
14
CS1255 Operating system Laboratory
AIM:
ALGORITHM:
PROGRAM:
#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:
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
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:
#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:
OUTPUT:
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
AIM:
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:
#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:
RESULT:
21
CS1255 Operating system Laboratory
SIMULATING LS COMMAND
AIM:
ALGORITHM:
PROGRAM:
//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:
24
CS1255 Operating system Laboratory
AIM:
ALGORITHM:
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
FCFS Scheduling
Gantt Chart
----------
|p1|p2|
----------
0 2 5
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
ALGORITHM:
PROGRAM:
[it08b034@linuxlab ~]$ vi rrpriority.c
#include<stdio.h>
#include<string.h>
struct process
{
char name[5];
int arrival,burst,prior,complete,wait,turn;
};
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);
}
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);
}
31
CS1255 Operating system Laboratory
32
CS1255 Operating system Laboratory
OUTPUT:
"rrpriority.c" 148L, 3174C written
[it08b034@linuxlab ~]$ cc rrpriority.c
[it08b034@linuxlab ~]$ ./a.out
Gantt Chart
--------------
| p1 | p3 | p2 |
--------------
2 5 9 11
33
CS1255 Operating system Laboratory
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:
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
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.
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]);
}
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
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 ().
3. Now close the read end of the parent process using closer ().
5. Now close the write end of child process using closer ().
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
RESULT
Thus the C program to implement the interprocess communication using pipes
was successfully completed.
38
CS1255 Operating system Laboratory
AIM:
To write a C program to solve the bounded buffer Producer-Consumer problem
using semaphores.
ALGORITHM:
PROGRAM:
#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:
2.consume process
41
CS1255 Operating system Laboratory
3.view buffer
4.Exit program
processes
process 383 in buffer position 1
Buffer position empty
Buffer position empty
Buffer position empty
2.consume process
3.view buffer
4.Exit program
processes
process 383 in buffer position 1
Buffer position empty
Buffer position empty
Buffer position empty
2.consume process
3.view buffer
4.Exit program
processes
process 383 in buffer position 1
process 886 in buffer position 2
Buffer position empty
42
CS1255 Operating system Laboratory
2.consume process
3.view buffer
4.Exit program
processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
Buffer position empty
2.consume process
3.view buffer
4.Exit program
processes
process 383 in buffer position 1
process 886 in buffer position 2
process 777 in buffer position 3
Buffer position empty
2.consume process
3.view buffer
4.Exit program
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
2.consume process
3.view buffer
4.Exit program
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
2.consume process
3.view buffer
4.Exit program
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
2.consume process
44
CS1255 Operating system Laboratory
3.view buffer
4.Exit program
processes
Buffer position empty
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4
2.consume process
3.view buffer
4.Exit program
processes
Buffer position empty
process 886 in buffer position 2
process 777 in buffer position 3
process 915 in buffer position 4
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
2.consume process
3.view buffer
4.Exit program
processes
Buffer position empty
Buffer position empty
process 777 in buffer position 3
process 915 in buffer position 4
2.consume process
3.view buffer
4.Exit program
processes
Buffer position empty
Buffer position empty
Buffer position empty
process 915 in buffer position 4
2.consume process
3.view buffer
4.Exit program
46
CS1255 Operating system Laboratory
processes
Buffer position empty
Buffer position empty
Buffer position empty
process 915 in buffer position 4
2.consume process
3.view buffer
4.Exit program
processes
Buffer position empty
Buffer position empty
Buffer position empty
Buffer position empty
2.consume process
3.view buffer
4.Exit program
2.consume process
3.view buffer
4.Exit program
47
CS1255 Operating system Laboratory
processes
Buffer position empty
Buffer position empty
Buffer position empty
Buffer position empty
2.consume process
3.view buffer
4.Exit program
RESULT:
Thus the C program to solve bounded buffer producer – consumer problem using
semaphores was implemented and executed successfully.
48
CS1255 Operating system Laboratory
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|
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|
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|
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:
57
CS1255 Operating system Laboratory
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:
#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:
Free List:
|f|100|--->|f|200|--->|f|300|--->|f|400|--->|f|500|
Allocated List:
|p0|50|-->|p1|100|-->|p2|150|-->|p3|200|-->|p4|250|
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:
61
CS1255 Operating system Laboratory
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:
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..
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