Operating Systems LAB programs
Operating Systems LAB programs
Experiment-1
4. cd - change directory
Syntax: cd [option] directory
$ cd dir1
5. cp - copy files
Syntax: cp [option] source destination
$ cp file1 file2
$ ps
$ ls
$ mv file1 file2
$ rm file1
2
$ mkdir dir1
$ rmdir dir1
$ echo $x
13. more - basic pagination when viewing text files or parsing UNIX commands output
Syntax: more [-options] [-num] [+/pattern] [+linenum] [file_name]
$ date +%d/%m/%y
$ time
$ kill 4901
$ history
The first optional parameter indicates who – this can be (u)ser, (g)roup, (o)thers or (a)ll
The second optional parameter indicates opcode – this can be for adding (+), removing (-) or
3
Numeric mode
The mode is a combination of three digits – the first digit indicates the
permission for the user, the second digit for the group, and the third digit for
others.
Each digit is computed by adding the associated permissions. Read
permission is ‘4’, write permission is ‘2’ and execute permission is ‘1’.
$ chmod 777 file1
$ pwd
$ cal 6 2019
$ logout
$ shutdown
4
Study of vi editor.
Visual editor for editing programs.
The vi editor is the most popular and classic text editor in the Linux family.
This editor enables you to edit lines in context with other lines in the file.
Below, are some reasons which make it a widely used editor?
It is available in almost all Linux Distributions
It works the same across different platforms and Distributions
It requires very few resources.
It is more user-friendly than other editors such as the ed or the ex.
We can use the vi editor to edit an existing file or to create a new file from scratch.
We can also use this editor to just read a text file.
An improved version of the vi editor which is called the VIM has also been
made available now. Here, VIM stands for Vi IMproved.
Starting the vi Editor
To launch the vi Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
& if you specify an existing file, then the editor would open it for you to edit. Else, you
can create a new file.
The following table lists out the basic commands to use the vi editor –
S. No Command Description
1 vi filename Creates a new file if it already does not exist, otherwise opens
an existing file
2 vi –R filename Opens an existing file in the read-only mode.
Operation Modes
Command mode − This mode enables you to perform administrative tasks such
as saving the files, executing the commands, moving the cursor, cutting (yanking)
and pasting the lines or words, as well as finding and replacing. In this mode,
whatever you type is interpreted as a command.
Insert mode − This mode enables you to insert text into the file. Everything that's
typed in this mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode for
which simply type i. To come out of the insert mode, press the Esc key, which will take
5
vi Editing commands
vi editor is case-sensitive so make sure to type the commands in the right letter-case,
otherwise you will end up making undesirable changes to the file.
Command Description
S. No
1 i Insert at cursor (goes into insert mode)
2 a Write after cursor (goes into insert mode)
3 A Write at the end of line (goes into insert mode)
4 ESC Terminate insert mode
5 u Undo last change
6 U Undo all changes to the entire line
7 o Open a new line (goes into insert mode)
8 dd Delete line
9 3dd Delete 3 lines.
10 D Delete contents of line after the cursor
11 C Delete contents of a line after the cursor and insert new text. Press ESC
key to end insertion.
12 dw Delete word
13 4dw Delete 4 words
14 cw Change word
15 x Delete character at the cursor
16 r Replace character
17 R Overwrite characters from cursor onward
18 s Substitute one character under cursor continue to insert
19 S Substitute entire line and begin to insert at the beginning of the line
20 ~ Change case of individual character
The default keys for navigation are mentioned below else; you can also use the arrow keys
on the keyboard.
S. Command Description
No
6
1 k Move cursor up
2 j Move cursor down
3 h Move cursor left
4 l Move cursor right
Note: You should be in the “command mode” to exit the editor and save changes to the file.
S. Command Description
No
1 Shift+zz Save the file and quit
2 :w Save the file but keep it open
3 :q Quit without saving
4 :wq Save the file and quit
Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system. What Is a
Shell?
A shell is a program that provides an interface between a user and an operating system
(OS) kernel.
An OS starts a shell for each user when the user logs in or opens a terminal or console
window. A kernel is a program that:
Controls all computer operations.
Coordinates all executing utilities
Ensures that executing utilities do not interfere with each other or consume all
system resources.
Schedules and manages all system processes.
By interfacing with a kernel, a shell provides a way for a user to execute utilities and
programs.
Types of Shell
1. / – Root
Every single file and directory starts from the root directory.
Please note that /root is root user’s home directory, which is not same as /.
8
Common linux commands you need to use in single-user modes are located
under this directory.
Commands used by all the users of the system are located here.
But, the linux commands located under this directory are used typically by
system aministrator, for system maintenance purpose.
This also contains startup and shutdown shell scripts used to start/stop
individual programs.
These include terminal devices, usb, or any device attached to the system.
This is a virtual filesystem with text information about system resources. For
example:
/proc/uptime
Content of the files that are expected to grow can be found under this directory.
This includes — system log files (/var/log); packages and database files (/var/lib);
emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files
needed across reboots (/var/tmp);
/usr/bin contains binary files for user programs. If you can’t find a user binary
under /bin, look under /usr/bin. For example: at, awk, cc, less, scp
/usr/sbin contains binary files for system administrators. If you can’t find a system
binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd,
userdel
/usr/local contains users programs that you install from source. For example,
when you install apache from source, it goes under /usr/local/apache2
Contains library files that supports the binaries located under /bin and /sbin
Experiment-2
11
Write programs using the following UNIX operating system calls fork, exec, getpid,
exit, wait, close, stat, opendir and readdir
AIM: To write a ‘C’ Program using system calls opendir, readdir, and closedir.
#include<stdio.h>
#include<dirent.h>
hi
13
#include<stdio.h>
#include<unistd.h>;
#include<fcntl.h>;
#include<sys/stat.h>;
int fd,n,i=1;
while(i<argc)
fd=open(argv[i],O_RDONLY);
fd=fstat(fd,&buf);
if(S_ISREG(buf.st_mode))
printf("File is regular\n");
else if(S_ISDIR(buf.st_mode))
printf("It is a directory\n");
else
printf("Number of links:%d\n",buf.st_nlink);
i++;
}
14
OUTPUT :
./a.out cse1
It is a directory
Number of links:2
AIM: To write programs using the following UNIX operating system calls fork, exec,
getpid, exit, wait
#include<stdio.h>
#include<unistd.h>
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
exit(1);
else if(pid==0)
execlp("/bin/ls","ls",NULL);
else
wait(NULL);
pid1=getpid();
printf("Child completed");
exit(0);
}
16
OUTPUT:
Child completed
17
Experiment-3
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int fd1,fd2;
int n;
char buf;
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR|S_IRUSR);
while((n=read(fd1,&buf,1))>0)
write(fd2,&buf,1);
return (0);
cat>hai
hai everyone
how are u?
Helo cse
^Z
hai everyone
how are u?
Helo cse
19
#include<stdio.h>
#include<dirent.h>
DIR *dp;
dp=opendir(argv[1]);
while((link=readdir(dp))!=0)
printf("%s",link->d_name);
closedir(dp);
OUTPUT :
./a.out cse1
.hai.c. .a.out hi
20
void usage()
FILE *fp;
char fline[max];
char *newline;
int count=0;
int occurrences=0;
if(argc!=3)
usage();
exit(1);
if(!(fp=fopen(argv[1],"r")))
exit(1);
while(fgets(fline,max,fp)!=NULL)
count++;
if(newline=strchr(fline, '\n'))
21
*newline='\0';
if(strstr(fline,argv[2])!=NULL)
occurrences++;
OUTPUT:
./a.out hai h
Experiment-4
a) AIM: To write a ‘C’ Program to simulate First Come First Served CPU Scheduling
(FCFS) Algorithm.
main()
{
int n,i,twt,ttt;
char p[20][20];
int b[20],w[20],tt[20];
float awt,att;
printf("Enter no.of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process:");
scanf("%s",p[i]);
printf("Enter burst time:");
scanf("%d",&b[i]);
}
w[1]=0;
tt[1]=w[1]+b[1];
twt=0;
ttt=tt[1];
for(i=2;i<=n;i++)
{
w[i]=w[i-1]+b[i-1];
tt[i]=w[i]+b[i];
twt=w[i]+twt;
23
ttt=tt[i]+ttt;
}
printf("sno\tprocess name \tburst time \twaiting time \tturn around time\n");
for(i=1;i<=n;i++)
{
printf("%d\t%s\t\t%d\t\t%d\t\t%d\t\n",i,p[i],b[i],w[i],tt[i]);
}
printf("Total waiting time: %d\n",twt);
printf("Total turn around time: %d\n",ttt);
printf("Avg waiting time: %f\n",(float)twt/n);
printf("Avg turn around time: %f\n",(float)ttt/n);
}
Output :
Enter no.of process:4
Enter process:1
Enter burst time:4
Enter process:2
Enter burst time:3
Enter process:3
Enter burst time:2
Enter process:4
Enter burst time:8
sno process name burst time waiting time turn around time
1 1 4 0 4
2 2 3 4 7
3 3 2 7 9
4 4 8 9 17
Total waiting time: 20
Total turn around time: 37
Avg waiting time: 5.000000
Avg turn around time: 9.250000
24
b) AIM: To write a ‘C’ Program to simulate Shortest Job First CPU scheduling (SJF)
Algorithm.
main()
{
int n,i,twt,ttt,k,j,temp;
char p[20][20],c[20];
int b[20],w[20],tat[20];
float awt,att;
printf("Enter no.of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter process:");
scanf("%s",p[i]);
printf("Enter burst time:");
scanf("%d",&b[i]);
}
for(k=1;k<=n-1;k++)
{
for(j=1;j<=n-k;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
strcpy(c,p[j]);
strcpy(p[j],p[j+1]);
strcpy(p[j+1],c);
25
}
}
}
w[1]=0;
tat[1]=w[1]+b[1];
twt=0;
ttt=tat[1];
for(i=2;i<=n;i++)
{
w[i]=w[i-1]+b[i-1];
tat[i]=w[i]+b[i];
twt=w[i]+twt;
ttt=tat[i]+ttt;
}
printf("s.no\tprocessname\tburst\ttwait\ttturn around\n");
for(i=1;i<=n;i++)
{
printf("%d\t%s\t\t%d\t%d\t%d\t\n",i,p[i],b[i],w[i],tat[i]);
}
printf("Total waiting time: %d\n",twt);
printf("Total turn around time: %d\n",ttt);
printf("Avg waiting time: %f\n",(float)twt/n);
printf("Avg turn around time: %f\n",(float)ttt/n);
}
OUTPUT:
Enter no.of process:4
Enter process:1
Enter burst time:5
Enter process:2
26
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
strcpy(c,p[j]);
strcpy(p[j],p[j+1]);
strcpy(p[j+1],c);
}
}}
w[1]=0;
tat[1]=w[1]+b[1];
twt=0;
ttt=tat[1];
for(i=2;i<=n;i++)
{
w[i]=w[i-1]+b[i-1];
tat[i]=w[i]+b[i];
twt=w[i]+twt;
ttt=tat[i]+ttt;
}
printf("s.no\tpname\tburst\tpriority\ttwait\ttturn around\n");
for(i=1;i<=n;i++)
{
printf("%d\t%s\t%d\t\t%d\t%d\t%d\n",i,p[i],b[i],pr[i],w[i],tat[i]);
}
printf("Total waiting time: %d\n",twt);
printf("Total turn around time: %d\n",ttt);
printf("Avg waiting time: %f\n",(float)twt/n);
printf("Avg turn around time: %f\n",(float)ttt/n);
}
29
OUTPUT:
Enter no.of process:4
Enter process:1
Enter burst time:4
Enter priority:2
Enter process:2
Enter burst time:2
Enter priority:3
Enter process:3
Enter burst time:6
Enter priority:4
Enter process:4
Enter burst time:1
Enter priority:1
s.no pname burst priority twait tturn around
1 4 1 1 0 1
2 1 4 2 1 5
3 2 2 3 5 7
4 3 6 4 7 13
Total waiting time: 13
Total turn around time: 26
Avg waiting time: 3.250000
Avg turn around time: 6.500000
30
#include<stdio.h>
#include<string.h>
int main()
char p[10][5];
int et[10],wt[10],timer,count,pt[25],rt,i,j,totwt=0,n,found=0,m,tat[10],ttt=0;
float avgwt;
scanf("%d",&n);
scanf("%d",&timer);
for(i=0;i<n;i++)
scanf("%s",&p[i]);
scanf("%d",&pt[i]);
m=n;
wt[0]=0;
i=0;
do
if(pt[i]>timer)
{
31
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
else
et[i]=pt[i];
i++;
wt[i]=wt[i-1]+et[i-1];
while(i<n);
count=0;
for(i=0;i<n;i++)
for(j=i+1;j<=n;j++)
if(strcmp(p[i],p[j])==0)
count++;
found=j;
if(found!=0)
{
32
wt[i]=wt[found]-(count*timer);
count=0;
found=0;
for(i=0;i<m;i++)
totwt+=wt[i];
tat[i]=wt[i]+pt[i];
ttt=ttt+tat[i];
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
OUTPUT:
4
33
1 4 5 9
2 3 7 10
3 1 4 5
4 5 10 15
Experiment-5
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t port_semaphore;
if (sem_wait(&port_semaphore) == 0)
sem_post(&port_semaphore);
}
35
else
printf("Thread %d: Could not open a port, semaphore wait failed.\n", *((int*)arg));
return NULL;
int main()
pthread_t threads[10];
int thread_ids[10],i;
if (sem_init(&port_semaphore, 0, MAX_PORTS) != 0)
return -1;
return -1;
36
pthread_join(threads[i], NULL);
sem_destroy(&port_semaphore);
return 0;
Output:
b) AIM: Write a C program to control the number of ports opened by the operating system
with Monitor.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_PORTS 5 // Maximum number of ports allowed
// Monitor structure for managing ports
typedef struct {
int available_ports; // Number of available ports
pthread_mutex_t mutex; // Mutex for critical section
pthread_cond_t cond_var; // Condition variable for waiting
} PortMonitor;
// Initialize the port monitor
void init_monitor(PortMonitor* monitor) {
monitor->available_ports = MAX_PORTS;
pthread_mutex_init(&monitor->mutex, NULL);
pthread_cond_init(&monitor->cond_var, NULL);
}
// Acquire a port from the monitor
void acquire_port(PortMonitor* monitor) {
pthread_mutex_lock(&monitor->mutex);
// Wait if no ports are available
while (monitor->available_ports == 0) {
printf("No available ports. Waiting...\n");
pthread_cond_wait(&monitor->cond_var, &monitor->mutex);
}
// Allocate a port (decrease available ports)
monitor->available_ports--;
printf("Port acquired. Available ports: %d\n", monitor->available_ports);
pthread_mutex_unlock(&monitor->mutex);
}
39
Experiment-6
exit(0);
}
Output:
before thread
thread1
thread2
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
j=10
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
exit from thread1
Exit from thread2
44
Experiment-7
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
void producer()
--mutex;
++full;
--size;
data++;
++mutex;
void consumer()
--mutex;
--full;
++size;
data--;
++mutex;
45
int main()
int n, i;
while(1)
scanf("%d", &n);
switch (n)
case 1:
producer();
else
break;
case 2:
consumer();
46
else
break;
case 3:
exit(0);
break;
}}}
Output:
3. Enter 3 to Exit
--------------------------------
47
Experiment-8
Implement the following memory allocation methods for fixed partition
a) First fit b) Worst fit c) Best fit
AIM: To write a C program to implement first fit memory allocation method for fixed
partition
PROGRAM
#include<stdio.h>
void main()
flags[i] = 0;
scanf("%d", &b);
scanf("%d", &bsize[i]);
scanf("%d", &p);
{
48
scanf("%d", &psize[i]);
allocation[i] = j;
bsize[j] -= psize[i];
break;
if(allocation[i] != -1)
else
printf("Not Allocated\n");
Output:
100
200
300
400
150
100
250
380
450
1 150 2
2 100 1
3 250 3
4 380 4
b) Worst fit
#include <stdio.h>
#define MAX 25
void main() {
scanf("%d", &numBlocks);
scanf("%d", &numFiles);
scanf("%d", &blockSize[i]);
scanf("%d", &fileSize[i]);
highest = -1;
fileAllocated[i] = -1;
if (!blockAllocated[j]) {
51
fileAllocated[i] = j;
highest = temp;
if (fileAllocated[i] != -1) {
frag[i] = highest;
blockAllocated[fileAllocated[i]] = 1;
} else {
frag[i] = -1;
printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment");
if (fileAllocated[i] != -1) {
} else {
Output:
Block 1: 100
Block 2: 500
Block 3: 200
Block 4: 300
Block 5: 600
File 1: 212
File 2: 417
File 3: 112
File 4: 426
2 417 2 500 83
c) Best fit
#include <stdio.h>
#define MAX 25
void main() {
scanf("%d", &numBlocks);
scanf("%d", &numFiles);
scanf("%d", &blockSize[i]);
scanf("%d", &fileSize[i]);
lowest = -1;
fileAllocated[i] = -1;
54
if (!blockAllocated[j]) {
fileAllocated[i] = j;
lowest = temp;
if (fileAllocated[i] != -1) {
frag[i] = lowest;
blockAllocated[fileAllocated[i]] = 1;
} else {
frag[i] = -1;
if (fileAllocated[i] != -1) {
} else {
}
55
Output:
Block 1: 100
Block 2: 200
Block 3: 300
Block 4: 400
File 1: 55
File 2: 130
File 3: 250
File 4: 190
File 5: 380
1 55 1 100 45
2 130 2 200 70
3 250 3 300 50
--------------------------------
56
Experiment-9
a) FIFO
PROGRAM:
#include <stdio.h>
int main() {
scanf("%d", &nop);
scanf("%d", &pages[i]);
scanf("%d", &ps);
frames[i] = -1;
found = 0;
if (frames[j] == pages[i]) {
57
found = 1;
break;
if (found == 0) {
frames[q++] = pages[i];
if (q == ps) q = 0;
npf++;
if (frames[j] != -1)
else
printf("- ");
if (!found)
printf("\n");
return 0;
Output:
Page 0 -> 7 0 1 2
Page 0 -> 3 0 1 2
Page 2 -> 3 4 1 2
Page 3 -> 3 4 1 2
Page 3 -> 3 4 0 2
Page 2 -> 3 4 0 2
Page 3 -> 3 4 0 2
b) LRU
#include<stdio.h>
void main ( )
int a[50],c[10],i,j,k,ps,nop,npf=0,nps=0;
scanf("%d",&nop);
for(i=0;i<nop;i++)
scanf("%d",&a[i]);
scanf ("%d",&ps);
for(i=0;i<nop;i++)
for(k=0;c[k]!=a[i]&&k<nps;k++);
if(k==nps)
npf++;
if(nps==ps)
for(k=0;k<nps-1;k++)
c[k]=c[k+1];
c[k]=a[i];
if(nps<ps)
nps++;
60
else
for (j=k;j<nps-1;j++)
c[j]=c[j+1];
c[j]=a[i];
printf("\n") ;
for(k=0;k<nps;k++)
printf("%d" ,c[k]) ;
Output:
Enter pages :2 3 2 1 5 2 4 5 3 2 5 2
23
32
321
3215
61
3152
1524
1245
2453
4532
4325
4352
LFU
#include<stdio.h>
int main()
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
scanf("%d",&f);
scanf("%d",&p);
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<50;i++)
count[i]=0;
for(i=0;i<p;i++)
scanf("%d",&pages[i]);
printf("\n");
for(i=0;i<p;i++)
{
63
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
if(frame[j]==-1 || frame[j]==pages[i])
if(frame[j]!=-1)
hit++; }
flag=0;
frame[j]=pages[i];
break; }
if(count[least]>count[frame[j]])
least=frame[j];
}}
if(flag)
minTime=50;
for(j=0;j<f;j++) {
temp=j;
minTime=time[frame[j]]; }}
count[frame[temp]]=0;
64
frame[temp]=pages[i]; }
for(j=0;j<f;j++) {
printf("%d ",frame[j]); }
printf("\n");
return 0;
Output:
Enter no of frames : 3
Enter no of pages : 7
Enter page no :
1234215
1 -1 -1
1 2 -1
123
423
423
421
521
Page hit = 1
Experiment-10
65
Program:
#include <stdio.h>
int i;
return;
printf("\nLogical Address: (Page %d, Offset %d) ? Physical Address: %d\n", pageNum, offset,
physicalAddress);
66
int main() {
scanf("%d", &numPages);
scanf("%d", &numFrames);
scanf("%d", &pageSize);
return 1;
// Simulate paging
simulatePaging(numPages, numFrames);
int choice;
do {
} else {
scanf("%d", &choice);
return 0;
Output:
0 -> 0
1 -> 1
2 -> 2
3 -> 0
4 -> 1
4 700
Experiment-11
69
AIM: To write a C program to implement Bankers Algorithm for Dead Lock Avoidance
and prevention
#include <stdio.h>
void main() {
int n, m, i, j, k, y, ind = 0;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &alloc[i][j]);
scanf("%d", &max[i][j]);
scanf("%d", &avail[i]);
work[i] = avail[i];
printf("\n");
71
finish[i] = 0;
// Safety algorithm
int flag = 0;
flag = 1;
break;
finish[i] = 1;
}
72
int safe = 1;
if (finish[i] == 0) {
safe = 0;
break;
if (safe) {
} else {
Output:
010
200
302
211
002
753
322
902
422
533
332
743
122
600
211
531
P1 P3 P4 P0 P2
Experiment-12
74
i) Sequential
#include<stdio.h>
void main()
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
scanf("%d",&x);
printf("\nlength is:%d",b[x-1]);
75
printf("\nblocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d\t",c[x-1][i]);
Output:
1 5 4
2 10 3
3 15 2
length is:3
blocks occupied: 10 11 12
76
b) Linked
#include<stdio.h>
struct file
char fname[10];
int start,size,block[10];
}f[10];
main()
int i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&f[i].fname);
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
scanf("%d",&f[i].size);
for(j=1;j<=f[i].size;j++)
scanf("%d",&f[i].block[j]);
}
77
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
Output:
file1 5 3 6--->7--->8
file2 10 4 11--->12--->13--->14
EXPERIMENT 13:
Download and install nachos operating system and experiment with it
The Nachos software is built using the GNU make program. You will need to modify
your PATH environment variable so that when you run make, GNU make, and not some
other make, will be executed. Your PATH should also include /u/cs350/bin, since there are a
number of useful Nachos-related programs there.
Your PATH is normally set by a command in your .cshrc file, in your home directory, each time
you log in. (If you are using some shell other than csh, it should have its own startup file, which
you can modify.) In the CSCF standard .cshrc file, you can do this by changing the line
This change will take effect the next time you log in. If you would like the change to take effect
immediately, you need to get your shell to re-load the initialization file you just changed.
Assuming you are using csh, you can do that by typing source ~/.cshrc.
Installing Nachos
Create a directory that will contain the nachos installation (ie ~/cs350).
To install Nachos, use the script /u/cs350/bin/install_nachos. This script takes one parameter,
which is the name of the directory into which Nachos should be installed. Assuming that
your PATH has been set up as described above, you should be able to type
install_nachos cs350
where cs350 is the name of the directory.
Building Nachos
Once Nachos is installed, you should try compiling it. There are actually three separate chunks of
code that get installed in your account when you run install_nachos. One is the code for
the nachos program itself. This program is written in C++ and implements the machine
simulation and the operating system for the simulated machine. The second is a set of test
programs. These programs are written in C. They run on the simulated machine, and are used to
test its operating system. The third is the coff2noff program for converning a COFF (Common
Object File Format) file to a NOFF file (Nachos Object File Format). The first chunk is by far
the largest of the three.
These three chunks of code must be built separately. First build the coff2noff program in the
directory coff2noff . (All relative pathnames are assumed to be relative to the main Nachos
79
make
The test programs live in, and are built in, the directory code/test. (They require the use of
coff2noff, so you must compile coff2noff first.) In the directory code/test , you will find a
Makefile, which can be used to build the test programs. The Makefile contains instructions on
how to do the build.
The source code for the nachos program itself lives in several directories: code/lib,
code/machine, code/threads, code/filesys, code/userprog, and code/network. The program is built
using the Makefile in one of the "build" directories. You need to choose a build directory
depending on the type of machine you are currently logged in to. As of January 2003, all of the
CPU servers in the CSCF undergraduate environment are running Solaris, so you should work in
the build.solaris directory.
Once you are in the appropriate build directory, read the Makefile carefully, and follow the
instructions in it. It explains how to build the nachos program.
Running Nachos
Once you have built nachos, you should be able to run it by simply typing
./nachos
in the build directory. The command line argument -u will give you a list of the available
command line options. Using these options, you can control what nachos does when it runs. For
starters, you may wish to try the -K and -C flags, which run some simple self-tests. You can also
try asking Nachos to run a very simple test program called halt, which is found in
the code/test directory. To try this, type
./nachos -d ca -x ../test/halt
The -d ca turns on the address space (a) and system call (c) related debugging output.
The halt program simply makes the "Halt" system call, which causes the machine simulator to
halt and print out some statistics. Have a look at the halt program. It is in code/test/halt.c.
For more details, you can look in the file code/threads/main.cc. In fact, this is the best place to
start reading the Nachos code, since this is where it all begins.
c++-example/
The Nachos operating system is written in a subset of C++. This directory contains an
excellent primer on C++. This primer is good reading for those who need a refresher, and
even for those of you who are experienced C++ programmers. In particular, it explains
why some of the "features" of C++ are not used at all in Nachos.
code/
All of the source code for the Nachos operating system, the machine simulation, and the
test programs can be found here. This code is organized into several sub-directories:
lib
utilities used by the rest of the Nachos code
machine/
The machine simulation. Except as noted in machine.h, you may not modify the code in
this directory.
threads/
Nachos is a multi-threaded program. Thread support is found here. This directory also
contains the main() routine of the nachos program, in main.cc.
userprog/
Nachos operating system code to support the creation of address spaces, loading of user
(test) programs, and execution of test programs on the simulated machine. The exception
handling code is here, in exception.cc.
filesys/
Two different file system implementations are here. The "real" file system uses the
simulated workstation's simulated disk to hold files. A "stub" file system translates
Nachos file system calls into UNIX file system calls. This is useful initially, so that files
can be used (e.g., to hold user programs) before you have had a chance to fix up the
"real" file system. By default, the "stub" file system is build into the nachos program and
the "real" file system is not. This can be changed by setting a flag in the Nachos makefile.
network/
Nachos operating system support for networking, which implements a simple "post
office" facility. Several independent simulated Nachos machines can talk to each other
through a simulated network. Unix sockets are used to simulate network connections
among the machines.
test/
User test programs to run on the simulated machine. As indicated earlier, these are
separate from the source for the Nachos operating system and workstation simulation.
This directory contains its own Makefile. The test programs are very simple and are
written in C rather than C++.