OS LAB FILE surendra
OS LAB FILE surendra
SESSION: 2024-2025
SEMESTER:- 4th
WINDOWS XP
1
• Keyboard and a Microsoft Mouse or some other compatible
pointing device Video adapter and monitor with Super
VGA(800x600) or higher resolution Sound card
• Speakers or headphones
UNIX OS:-
• RAM:2GB
• Processor: IBM 604e processor with a clock speed of 375
MHz or faster
• Free disk space: / tmp must have 1GB free disk space. If
Tivoli Identity Manager installs Web Sphere Application
Server, {WAS_HOME} must have 800 MB free disk space
and / var must have 300MB free disk space. Allocate 500
MB for /itim45
LINUX
• 32 bit Intel compatible processor running at 2GHz orgreater
512 MB RAM
• Disk space: 2.5 GB for Pipeline Pilot server plus
components
• A DVD-ROM drive
2
EXPERIMENT:- (02-04)
Content:
The interface between a process and an operating system is
provided by system calls. In general, system calls are
available as assembly language instructions. They are also
included in the manuals used by the assembly level
programmers.
Unix System Calls
System calls in Unix are used for file system control, process
control, interprocess communication etc. Access to the Unix
kernel is only available through these system calls. Generally,
system calls are similar to function calls, the only difference is
that they remove the control from the user process. There are
around 80 system calls in the Unix interface currently. Details
about some of the important ones are given as follows:
access(): This checks if a calling process has access to the
required file
chdir(): The chdir command changes the current directory of
the system
chmod(): The mode of a file can be changed using this
command
3
chown(): This changes the ownership of a particular file
kill(): This system call sends kill signal to one or more
processes
link(): A new file name is linked to an existing file using link
system call
open(): This opens a file for the reading or writing process
pause(): The pause call suspends a file until a particular
signal occurs
stime(): This system call sets the correct time
times(): Gets the parent and child process times
alarm(): The alarm system call sets the alarm clock of a
process
folk(): Anew process is created using this command
chroot(): This changes the root directory of a file
exit(): This exit system call is used to exit a process
File Structure Related calls:
Creating a Channel: creat()
open()
close()
4
Aliasing and Removing: stat()
Files: fstat()
File status: access()
chmod()
chown()
umask()
Device Control:-
• Process Related Calls:
Process Creation and Termination exec()
fork()
wait()
5
msgrcv()
msgtl()
Semaphores semget()
semop()
Shared Memory shmget()
shmat()
shmdt()
/*errmsgl.c
Print all system error messages using “perror()”
*/
#include <stdio.h>
int main()
{
Int I;
extern int errno, sys_nerr;
for(i=0;i<sys_nerr;++i);
{
fprintf(stderr,”%3d”,i);
errno=I;
perror(“”);
}
exit(0);
}
/*errmsg2.c
Print all system error messages using the global massage
table */
#include <stdio.h>
int main()
{
int i;
extern int sys_nerr;
extern char *sys_errlist[];
fprintf(stderr, “Here are the current %d error messages:\n\n”,sys_nerr);
for(i=0;i<sys_nerr; ++i)
6
fprintf(stderr,”%3d:%s\n”,I, sys_errlist[i]);
}
/* creat.c*/
#include<stdio.h>
#include<sys/types.h> /*definestypes used by sys/stat.h*/
#include<sys/stat.h> /*defines S_IREAD & S_IWRITE*/
int main()
{
Int fd;
fd=creat(“datafile.dat”,S_Iread|S_IWRITE);
if(fd==_1)
printf(“Error in opening datafile.dat\n”);
else
{
printf(“ datafile.dat opened for read/write access\n”);
printf(“datafile.dat is currently empty\n”);
}
close(fd);
exit(0);
}
The following is a sample of the manifest constants for the mode argument as
defined in /usr/include/sys/stat.h:
#define S_IRWXU 0000700 /*-rwx ---------- */
#define S_IREAD 0000400 /*read permission, owner*/
#define S_IRUSR S_IREAD
#define S_IWRITE 0000200 /*write permission, owner*/
#define S_IWUSR S_IWRITE
#define S_IEXEC 0000100 /*execute/search permission,
owner*/ #define S_IXUSR S_IEXEC
#define S_IRWXG 0000070 /*----rwx ---- */
#define S_IRGRP 0000040 /*read permission,group*/
#define S_IWGRP0000020 /*write “ “ */
#define S_IXGRP 0000010 /*execute/search” “ */
#define S_IRWXO 0000007 /* --------- rwx*/
#define S_IROTH 0000004 /*read permission,other*/
#define S_IWOTH 0000002 /*write “ “ */
#define S_IXOTH 0000001 /*execute/search “ “ */
open()
Next is the open() system call. Open() lets youy open a file fpor
reading,writing,or reading and writing.
The prototype for the open() system call is:
7
#include<fcntl.h>
int open(file_name, option_flags[,mode])
char*file_name;
int option_flags, mode;
10
where file_name is a pointer to the character string that names the file,
option_flags represent the type of channel,
and mode defines the filesaccess permissionsif the file is being created.
The allowable option_flags as defined in “/usr/include/fcntl.h”are:
#define O_RDONLY 0 /*Open the file for reading only*/
#define O_WRONGLY 1 /*Open the file for writing only*/
#define O_RDWR 2 /*Open the file for both reading and writing*/
#define O_NDELAY 04 /*Non-blockingI/O*/
#define O_APPEND 010 /*append(writes guaranteed at the end)*/
#define O_CREAT 00400 /*open with file creat( uses third open arg)*/
#define O_TRUNC 01000 /*open with truncation*/ #define O_EXCL
02000 /*exclusiveopen */
8
EXPERIMENT:-05
Objective: Implement CPU Scheduling Policy:
( i ) FCFS
Content:
FCFS CPU SCHEDULING ALGORITHM
For FCFS scheduling algorithm, read the number of
processes/jobs in the system, their CPU burst times . The
scheduling is performed on the basis of arrival time of the
processes irrespective of their other parameters. Each process
will be executed according to its arrival time. Calculate the
waiting time and turnaround time of each of the processes
accordingly .
Program
1. FCFS SCHEDULING
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt[10],n,wt[10],tt[10],w1=0,t1=0
12
float aw,at;
clrscr();
printf(“enter no. of processes:\n”);
scanf(“%d”,&n);
printf(“enter the burst time of processes:”);
for(i=0;i<n;i++)
scanf(“%d”,&bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
9
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
printf(“\nbt\t wt\t tt\n”);
printf(“%d\t %d\n”,bt[i],wt[i],tt[i]);
printf(“aw=%f\n,at=%f\n”,aw,at);
getch();
}
INPUT
Enter no of processes
3
Enter burst time
12
8
20
EXPECTED OUTPUT
bt wt tt
12 0 12
8 12 20
20 20 40
aw=10.666670
at=24.00000
10
EXPERIMENT:-06
Program
2. SJF SCHEDULING
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
clrscr();
printf(“enter no. of processes:\n”);
scanf(“%d”,&n);
printf(“enter the burst time of processes:”);
for(i=0;i<n;i++)
scanf(“%d”,&bt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
11
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
for(i=0;i<n;i++)
printf(“%d”,bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf(“\nbt\t wt\t tt\n”);
for(i=0;i<n;i++)
printf(“%d\t %d\t %d\n”,bt[i],wt[i],tt[i]);
16
printf(“aw=%f\n,at=%f\n”,aw,at);
getch();
}
INPUT
Enter no of processes
3
Enter burst time
12
8
20
12
OUTPUT
bt wt tt
128 20
808
20 20 40
aw=9.33
at=22.64
13
EXPERIMENT:-07
Objective: Implement CPU Scheduling Policy:
( i ) Priority
Content:
PRIORITY CPU SCHEDULING ALGORITHM
For priority scheduling algorithm, read the number of
processes/jobs in the system, their CPU bursttimes, and the
priorities. Arrange all the jobs in order with respect to their
priorities. There may be two jobs in queue with the same
priority, and the FCFS approach is to be performed. Each
process will be executed according to its priority. Calculate
the waiting time and turnaround time of each of the processes
accordingly.
Program
3. PRIORITY SCHEDULING
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s;
float aw,at;
clrscr();
printf(“enter the number of processes:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“The process %d:\n”,i+1);
printf(“Enter the burst time of processes:”);
scanf(“%d”,&bt[i]);
printf(“Enter the priority of processes %d:”,i+1);
scanf(“%d”,&prior[i]);
pno[i]=i+1;
14
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]<prior[j])
{
s=prior[i];
prior[i]=prior[j];
prior[j]=s;
s=bt[i];
bt[i]=bt[j];
bt[j]=s;
s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
aw=w1/n;
at=t1/n;
}
printf(“\n job \t bt \t wt \t tat \t prior\n”);
for(i=0;i<n;i++);
printf(“%d \t %d \t %d\t %d\t %d\n”,pno[i],bt[i],wt[i],tt[i],prior[i]);
printf(“aw=%f\t at=%f\n”,aw,at);
getch();}
15
EXPERIMENT:-08
Objective: Implement file storage allocation technique:
( i ) Contiguous (using array)
Content:
The allocation methods define how the files are stored in the
disk blocks. There are three main disk space or file allocation
methods.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
The main idea behind these methods is ton provide:
• Efficient disk space utilization.
• Fast access to the blocks.
Program:
SEQUENTIAL FILE ALLOCATION
#include<stdio.h>
main()
{
int f[50],I,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf(“\n Enter the starting block & length of file”);
scanf(“%d%d”,&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
16
f[j]=1:
printf(“\n%d->%d”,j,f[j]);
}
else
{
printf(“Block already allocated”);
break;
}
if(j==(st+len))
printf(“\n the file is allocated to disk”);
printf(\n if u want to enter more files?(y-1/n-0)”);
scanf(“%d”,&c);
if(c==1)
goto X;
else
exit();
getch();}
OUTPUT:
Enter the starting block & length of file of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk
If you want toenter more files?(Y-1/N-0)
17
EXPERIMENT:- 09
Program:-
LINKED FILE ALLOCATION
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf(“Enter how many blocks that are already allocated”);
scanf(“%d”,&p);
printf(“\nEnter the blocks no.s that are already allocated”);
for(i=0;i<p;i++)
{
scanf(“%d”,&a);
f[a]=1;
}
X:
printf(“Enter the starting index block & length”);
scanf(“%d%d”,&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf(“\n%d->%d”,j,f[j]);
}
Else
{
printf(“\n%d->file is already allocated”,j);
18
k++;
}
}
printf(“\n If u want to enter one more file?(yes-1/no-0)”); scanf(“%d”,&c);
if(c==1) goto X; else exit();
getch();}
19
EXPERIMENT:- 10
Program:
#include<stdio.h>
int f[50],I,k,j,inde[50],n,c,count=0,p; main()
{
clrscr(); for(i=0;i<50;i++) f[i]=0;
x:
printf(“enter index block\t”);
scanf(“%d”,&p);
if(f[p]==0)
{ f[p]=1;
printf(“enter no of fileson index\t”);
scanf(“%d”,&n);
}
else
{
Printf(“Block already allocated\n”);
goto x;
}
for(i=0;i<n;i++)
scanf(“%d”,&inde[i]);
for(i=0;i<n;i++) if(f[inde[i]]==1)
{
printf(“Block already allocated”);
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf(“\n file allocated”);
printf(“\n file indexed”);
for(k=0;k<n;k++)
20
printf(“\n %d:%d”,p,inde[k],f[inde[k]]);
printf(“Enter 1 to enter more files and 0 to
exit\t”);
scanf(“%d”,&c); if(c==1)
go to x;
else
exit();
getch();
}
21