Beruflich Dokumente
Kultur Dokumente
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
2
(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)
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).
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)
AIM:
To write a shell program to simulate the fork (), getpid () system calls
ALGORITHM:
(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.
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();
4
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.out
This line is from pid 3127, value = 1
This line is from pid 3127, value = 2
This line is from pid 3126, value = 1
This line is from pid 3126, value = 2
This line is from pid 3126, value = 3
This line is from pid 3126, value = 4
This line is from pid 3126, value = 5
This line is from pid 3126, value = 6
This line is from pid 3126, value = 7
This line is from pid 3126, value = 8
This line is from pid 3126, value = 9
This line is from pid 3126, value = 10
This line is from pid 3126, value = 11
This line is from pid 3126, value = 12
This line is from pid 3126, value = 13
This line is from pid 3126, value = 14
This line is from pid 3126, value = 15
This line is from pid 3126, value = 16
This line is from pid 3126, value = 17
This line is from pid 3126, value = 18
This line is from pid 3126, value = 19
This line is from pid 3126, value = 20
This line is from pid 3127, value = 3
This line is from pid 3127, value = 4
This line is from pid 3127, value = 5
This line is from pid 3127, value = 6
This line is from pid 3127, value = 7
This line is from pid 3127, value = 8
This line is from pid 3127, value = 9
This line is from pid 3127, value = 10
This line is from pid 3127, value = 11
This line is from pid 3127, value = 12
This line is from pid 3127, value = 13
This line is from pid 3127, value = 14
This line is from pid 3127, value = 15
This line is from pid 3127, value = 16
5
This line is from pid 3127, value = 17
This line is from pid 3127, value = 18
This line is from pid 3127, value = 19
This line is from pid 3127, value = 20
RESULT:
Thus the process id, value were printed by using the fork and getpid
system calls.
AIM:
To write a shell program to implement the execlp( ) function .
ALGORITHM:
1. Start 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);
6
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
ALGORITHM:
// 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
7
{
printf(“\n I am Parent of Child %d”,getpid());
printf(“\n I am Parent’s Parent %d”,getpid());
}
}
OUTPUT:
SLEEP FUNCTION
RESULT:
Thus the sleep function was demonstrated.
AIM:
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. The readdir() function reads the name of the first file from this opened
directory
5. 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 directory
7. Stop the program.
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
main(argc,argv)
int argc;
char *argv[];
{
8
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 cs1
opened cs1
The first entry in this directory is ..
-sh-3.2$ ./a.out gee
opened gee
The 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.
AIM:
To write a shell program to demonstrate a wait system call.
ALGORITHM:
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.out
enter exit stat:3
Exit status from 3928 was 1
-sh-3.2$
RESULT:
FORK FUNCTION
AIM:
ALGORITHM:
PROGRAM:
// FORK FUNCTION //
#include<stdio.h>
main()
{
printf(“Hello”);
fork();
printf(“Bye”);
fork();
}
OUTPUT:
FORK FUNCTION
HelloByeHelloByeHelloByeHelloBye
RESULT:
AIM:
11
To write a program to demonstrate system calls for cat
command.
ALGORITHM:
a .Check buffersize>o.
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);
}
12
OUTPUT:
$cat >abcd
I love my India
./a.out abcd
I love my India
RESULT:
Thus the cat command was simulated.
To write a shell program to simulate file open, read and write operations.
ALGORITHM:
main(argc, argv)
int argc;
char *argv[ ];
13
{
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]);
RESULT:
Thus the file operations were simulated.
AIM:
To demonstrate the concept of copying a file by using system calls.
ALGORITHM:
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 g
The content of y is copied to g
Cat g
Goodmorning
Cat y
Goodmorning
RESULT:
SIMULATION OF LS COMMAND
AIM:
To write a program to simulate the ls command
ALGORITHM:
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 cs1
found entry in cs1:.
found entry in cs1:tfile
found entry in cs1:..
-sh-3.2$
RESULT:
Thus the ls command were simulated
17
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)
AIM:
ALGORITHM:
PROGRAM:
#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");
19
for(i=1;i<=n;i++)
{
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
1 3 0 3
2 4 3 7
3 5 7 12
average turnaroundtime:7.333333
RESULT:
Thus the process burst time, wait time, turned around time was
calculated by using first come first serve algorithm.
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.
20
5. Print the process and compute waiting time.
6. Compute the average waiting time.
Sum of waiting time
Total No of processes
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]);
21
c[0]=0;
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
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)
AIM:
To write a UNIX program to implement the priority scheduling algorithm.
ALGORITHM:
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)
24
OUTPUT:
1 3 0 3
2 4 3 7
3 5 7 12
average turnaroundtime:7.333333
RESULT:
Thus the priority scheduling algorithm has been done and the output
has been verified.
AIM:
To write a program to implement the round robin scheduling.
ALGORITHM:
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++)
26
if((count=proc[i].btm)>0)
{
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:
4
Enter the time slice:
10
Enter the burst time for 4 process
3456
Process synchoronation
Process 1 from 0 to 3
Process 2 from 3 to 7
Process 3 from 7 to 12
Process 1 from 12 to 18
27
RESULT:
Implement the Producer – Consumer problem using semaphores (using UNIX system calls).
28
AIM:
ALGORITHM:
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:
{
29
producer();
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);
}
else
30
printf(“\n The producer queue is full\n”);
}
}
}
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:5
Enter your choice:
1.for producer
2.for consumer
1
Enter the number to be added to the producer:50
The number added is 50 and in the position 0
Do you want to continue
0->No
1->Yes???1
Enter your choice
1.for producer
2.consumer
2
The consumer consumed is 50 from the position 0
Do you want to continue
0->No
1->Yes???0
RESULT:
AIM:
ALGORITHM:
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;
}
else
continue;
}
if(n!=1)
printf(“%d is not allocated to any slots”,jb);
32
n=0;
printf(“\n Enter your choice”);
Ssanf(“%d”,&c);
}
while(c==1)
}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
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
RESULT:
Thus the best fit memory location was executed.
AIM:
ALGORITHM:
5. Compute and allocated the maximum size of memory for the process.
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])
35
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf(“\nThe worst fit process %d\n”,a[i]));
}
OUTPUT:
WORST FIT
RESULT:
Thus the worst fit process was executed and verified.
AIM:
ALGORITHM:
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:
RESULT:
Thus the memory configuration was implemented.