OS LAB
OS LAB
Vijayawada
LAB NAME: OPERATING SYTSEM
REG:R23 Ac.yr:2024-25
CLASS: II BTech Sem--2
Cd - Finally, typing just cd alone will move you into the home directory.
Output: $ cd example [student@server example]$ View directory (ls)
Head
This command displays the initial part of the file. By default it displays first 10 lines of the file.
Syntax: $head filename
Example: $head file1 (It will show the top 10 lines of file1)
Tail
This command displays the later part of the file. By default it displays first 10 lines of the file.
Syntax: $tail filename
Example: $tail -4 file1 (It will show the last 4 lines of the file1)
Who OpcodeMode
u – User ‘+’ - add Permission r – read
g – Group ‘-‘ - Remove Permission w – Write
o – Other ‘=’ – Assign Absolute x - execute
a – All
Example: $chmod u+r file1 (read mode enabled to the users of the file1)
Absolute Mode:
Syntax: $chmod [octalnumber] filename
Example: $chmod 641 file1
Find file (or) directory (find)
Used to find a particular file or a directory
Syntax: $find filename
Example: $find file1 file1
ln(link)
Establish link between more than one file. The modification of one file reflects in others also.
Syntax: $ln filename1 filename2
Example: $ln raja surya
od(octaldump)
Used to display the ascii octal value of the file contents.
Syntax: od [option] filename
Options, c – to display content of the file in characters b – to display octal value of the content
of file
cd – to display octal value and its character of the file.
Example: od –c file1
Who
It displays user names who are all logging on a system
Syntax: $who
Example: student pts/1 Apr 20 14:40(192.17.6.2)
student2 pts/2 Apr 20 14:10(192.17.6.34)
student5 pts/5 Apr 20 14:16(192.17.6.5)
Who am i
This command tells the login details of a user.
Syntax: $who am i
Example: student pts/1 Apr 20 14:40(192.17.6.2)
Man
The man command displays the online manual pages.
Syntax: $man command name
Example: man ls
tty
Knowing the name of the terminal.
Syntax: $tty Output: /dev/tty 02
Echo
This command used to print
Example: $echo unix lab unix lab
Date
This command used to display system date information
Syntax: $date
Example: $date - To display the system date and time.
Mon Apr 20 15:41:50 EDT 2009
$date +%m - To display only the month number. 4
$date +%h - To display month in character. April
$date +”%h%m” - To display month in character and number April 4
Write
This write command used to send message when the user logging in.
Syntax: $write username message
Example: $write user01 hello
Mail
It allows to send message if the user doesn’t logging in.
Syntax: $mail username Subject:
//message// CC:
Example: $mail student Subject: hai
*****
*****
Pipe symbol
The pipe enables the user to take the output from command1 and feed it directly into the input of
command2.This is done the character “|”, which is placed between the two commands.
Syntax: $command1|command2|…|commandn
Example: $who|wc –l|temp
Combining commands
More than one command can be given in the same command line. Each command must be
separated by a semicolon (;). There are two types
Sequential commands
Give several commands on the same line by separating them with a semicolon (;)
Syntax: $command1; command2; commandn Example:: $chmod + rwx emp;ls – l emp
Command grouping
We can group commands using parentheses. Each command inside the parentheses must be
separated by semicolon. The grouped commands output will be sent to another file. We can see the
output by opening the file.
Syntax: $(commnad1;command2)>filename
Example: $(date;ls –l emp)>temp
Process management:
In Linux process management commands are used to the following purpose:
➢ Identifying process
➢ Monitoring and controlling the process
➢ Modifying and obtaining various information from process
➢ The most familiar process management commands are ps, top, pstree and kill, etc,
Ps:
This command is used to display the process status
Syntax: $ps Example: $ps Output:
PID Terminal Time Command
1096 pts/1 00.01.19 bash
1081 pts/1 00.05.23 man
Pgrep:
➢ It provides an interface, enabling to select processes using simple pattern matching
➢ It lists the process IDs (PIDs) of processes matching the specified pattern.
Syntax: $pgrep [option] [pattern]
Example: $pgrep bash
Pidof
It enables user to locate the Process ID (PID) of a process by name
Syntax: $pidof [option] process
Example: $pidof man
Pstree:
➢ Pstree displays all running process as a tree with init as root.
➢ It will display the parent – child relationship between processes in a clear manner.
Syntax: $pstree [options]
Top:
➢ Top command displays real time CPU and memory usage and current system uptime
information.
➢ It is tool in every system administrator toolbox.
Syntax: $top [options]
Tload:
The tload command display a real-time text mode graph of the system’s load average.
Syntax: $tload[options]
Terminating the process:
These terminating commands are used to stop the individual or all process.
Kill:
This is used to terminating the process. This is the /bin/kill command.
Syntax: $kill [option] IDs
Example: $kill 1029 Kill all:
Kill processes by command name. If more than one process is running the specified command, kill all
of them. Treat command names that contain a / as files; kill all processes that are executing that file.
Syntax: $killall [options] names
OUTPUT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/dir.h>
void main(int age,char *argv[])
{
DIR *dir;
struct dirent *rddir;
printf("\n Listing the directory content\n");
dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t\n",rddir->d_name);
}
closedir(dir);
}
OUTPUT:
RP
roshi.c first.c pk6.c f2 abc FILE1
OUTPUT:
Enter file name: file 5 Enter the pattern: roll no Roll no not found
3B)PROGRAM FOR CP
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char *argv[])
{
FILE *fp; char ch; int sc=0;
fp=fopen(argv[1],"r"); if(fp==NULL)
printf("unable to open a file",argv[1]);
else {
while(!feof(fp))
{
ch=fgetc(fp); if(ch==' ') sc++;
}
printf("no of spaces %d",sc); printf("\n");
fclose(fp);
}
}
OUTPUT:
File opened
No spaces found
3C)PROGRAM FOR LS
#include<stdio.h>
#include<dirent.h>
int main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
scanf("%s",&pathname);
getcwd(pathname);
n=scandir(pathname,&namelist,0,alphasort);
if(n<0)
printf("Error"); else for(i=0;i<n;i++)
printf("%s\n",namelist[i]->d_name);
}
OUTPUT:
OUTPUT:
Enter Burst Time for process 1 -- 24 Enter Burst Time for process 2 -- 3 Enter Burst Time for process 3
-- 3 Enter the size of time slice -- 3
The Average Turnaround time is -- 15.000000 The Average Waiting time is -- 5.000000
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
1 24 6 30
2 3 3 6
3 3 6 9
}
for(i=0;i<n;i++) for(k=i+1;k<n;k++) if(bt[i]>bt[k])
{
temp=bt[i]; bt[i]=bt[k]; bt[k]=temp; temp=p[i]; p[i]=p[k]; p[k]=temp;
}
wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i]; wtavg = wtavg + wt[i]; tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time --
%f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n);
}
OUTPUT:
Enter the number of processes -- 4 Enter Burst Time for Process 0 -- 6 Enter Burst Time for Process 1
-- 7 Enter Burst Time for Process 2 -- 8 Enter Burst Time for Process 3 -- 3
P3 3 0 3
P0 6 3 9
P1 7 9 16
P2 8 16 24
Average Waiting Time -- 7.000000 Average Turnaround Time -- 13.000000
OUTPUT:
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time -- 17.000000 Average Turnaround Time -- 27.000000
OUTPUT:
sem_t mutex;
//signal
printf("\nJust Exiting...\n"); sem_post(&mutex);
}
int main()
{
sem_init(&mutex, 0, 1); pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL); sleep(2); pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL); pthread_join(t2,NULL); sem_destroy(&mutex);
return 0;
}
OUTPUT:
Entered..
Just Exiting...
Entered..
Just Exiting...
8) PROGRAM FOR SHARED MEMORY [IPC]:
OUTPUT:
Key of shared memory is 0
Process attached at 0x7feaa4dea000
Enter some data to write to shared memory kuppam engineering college
You wrote : kuppam engineering college
int available_seats;
int no_served_custs = 0; time_t waiting_time_sum;
else
{
/* Unlock semaphore "modifySeats" */ sem_post(&modifySeats); no_served_custs++;
printf("A Customer left.\n");
}
pthread_exit(NULL);
}
void serve_customer() {
/* Random number between 0 and 400 (miliseconds) */ int s = rand() % 401;
/* Convert miliseconds to microseconds */ s = s * 1000;
usleep(s);
}
int main() {
/* Initialization, should only be called once */ srand(time(NULL));
Average customers' waiting time: 122.500000 ms. Number of customers that were forced to leave: 1
10) PROGRAM FOR DINING PHILOSOPHER’S PROBLEM:
#include<stdio.h> #define n 4
int compltedPhilo = 0,i; struct fork{
int taken;
}ForkAvil[n];
void goForDinner(int philID){ //same like threads concept here cases implemented
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);
//if already completed dinner
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
//if just taken two forks
printf("Philosopher %d completed his dinner\n",philID+1);
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1; printf("Fork %d taken by Philosopher %d\
n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){ //nothing taken yet if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN
reverse DIRECTION
ForkAvil[philID-1].taken = Philostatus[philID].left = 1; printf("Fork %d taken by philosopher %d\
n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{ //except last philosopher case if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1; printf("Fork %d taken by Philosopher %d\
n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
}
}
}else{}
}
while(compltedPhilo<n){
/* Observe here carefully, while loop will run until all philosophers complete dinner Actually problem
of deadlock occur only thy try to take at same time
This for loop will say that they are trying at same time. And remaining status will print by go for
dinner function
*/ for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
}
return 0;}
OUTPUT:
Philosopher 1 completed his dinner Philosopher 2 completed his dinner Philosopher 3 completed his
dinner Philosopher 4 completed his dinner
}
break;;;
printf(“\nEnter the value: “); scanf(“%d”, &produce); buffer[in] = produce;
in = (in+1)%bufsize; case 2: if(in == out)
printf(“\nBuffer is Empty”); else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume); out = (out+1)%bufsize;
}
break;
}}}
OUTPUT:
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
OUTPUT
Memory Management Scheme - Best Fit Enter the number of blocks:3
Enter the number of processes:3
1 25 3 30 5
2 15 2 20 5
3 10 1 10 0
OUTPUT:
OUTPUT
File No File SizeBlock No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults); return 0;
}
OUTPUT:
2 -1 -1 -1
2 5 -1 -1
2 5 4 -1
2 5 4 8
1 5 4 8
Total Page Faults: 5
12B)PROGRAM FOR LRU:
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0; for(i = 1; i < n; ++i){
if(time[i] < minimum){ minimum = time[i]; pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos,
faults = 0;
printf("Enter number of frames: "); scanf("%d", &no_of_frames); printf("Enter number of pages: ");
scanf("%d", &no_of_pages); printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){ scanf("%d", &pages[i]);
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){ if(frames[j] == -1){ counter++;
faults++;
frames[j] = pages[i]; time[j] = counter; flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames); counter++;
faults++;
frames[pos] = pages[i]; time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){ printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
OUTPUT:
4 -1 PF No. 1
4 5 PF No. 2
8 5 PF No. 3
9 5 PF No. 4
int main()
{
int page[MAX],i,n,f,ps,off,pno; int choice=0;
printf("\nEnter the no of peges in memory: "); scanf("%d",&n);
printf("\nEnter page size: "); scanf("%d",&ps); printf("\nEnter no of frames: "); scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n"); printf("\npageno\
tframeno\n-------\t ");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i); scanf("%d",&page[i]);
}
do
{
printf("\n\nEnter the logical address(i.e,page no & offset):"); scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames"); else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off); printf("\nDo you want to
continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1); return 1;
}
OUTPUT:
pageno frameno
0
-1
1
8
2
-1
3
6
#include<stdio.h>
j=0;
for(i=1; i<=n; i++)
{
totalloc[k]=j+resalloc[i][k]; j=totalloc[k];
}
}
for(i=1; i<=n; i++)
{
if(block[i]==1||run[i]==1) active[i]=1;
else active[i]=0;
}
for(k=1; k<=r; k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1; k<=r; k++)
{
if(totext[k]-totalloc[k]<0)
{ u=1;
break;
}
}
if(u==0)
{
for(k=1; k<=r; k++) simalloc[k]=totalloc[k]; for(s=1; s<=n; s++) for(i=1; i<=n; i++)
{
if(active[i]==1)
{ j=0;
for(k=1; k<=r; k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{ j=1;
break;
}
}
}
if(j==0)
{
active[i]=0; for(k=1; k<=r; k++)
simalloc[k]=resalloc[i][k];
}
} m=0;
for(k=1; k<=r; k++) resreq[p][k]=newreq[k]; printf("Deadlock willn't occur");
}
else
{
for(k=1; k<=r; k++)
{
resalloc[p][k]=newreq[k]; totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}
OUTPUT:
15A)PROGRAM FOR SEQUENTIAL FILE ALLOCATION
#include <stdio.h> #include <conio.h> #include <stdlib.h>
14 1
15 1
16 1
The file is allocated to the disk Do you want to enter more files? Press 1 for YES, 0 for NO: 1
Enter the starting block and the length of the files: 14 1 The file is not allocated to the disk
Do you want to enter more files? Press 1 for YES, 0 for NO: 1
Enter the starting block and the length of the files: 14 2 The file is not allocated to the disk
Do you want to enter more files? Press 1 for YES, 0 for NO: 0
15B)PROGRAM FOR INDEXED FILE ALLOCATION
Enter the number of blocks and the number of files needed for the index 5 on the disk: 4 1 2 3 4
Allocated File Indexed 5 > 1 : 1
5 >2:1
5 >3:1
5 >4:1
Do you want to enter more files? Enter 1 for Yes, Enter 0 for No:
15C)PROGRAM FOR LINKED FILE ALLOCATION
#include <stdio.h> #include <conio.h> #include <stdlib.h>
void recursivePart(int pages[]){
Enter the number of blocks already allocated: 3 Enter the blocks already allocated: 1 3 5
Enter the index of the starting block and its length: 2 2 1
2 >1
The block 3 is already allocated 4 >1
Do you want to enter more files?
Enter 1 for Yes, Enter 0 for No: Enter the index of the starting block and its length:
OUTPUT:
#include<string.h>
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f); dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]); goto jmp;
}
}
printf("File %s not found",f); goto jmp;
}
}
printf("Directory %s not found",d); jmp : break;
case 4: printf("\nEnter name of the directory -- "); scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- "); scanf("%s",f); for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f); goto jmp1;
}
}
printf("File %s not found",f); goto jmp1;
}
}
printf("Directory %s not found",d); jmp1: break;
case 5: if(dcnt==0) printf("\nNo Directory's "); else
{
printf("\nDirectory\tFiles"); for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname); for(k=0;k<dir[i].fcnt;k++) printf("\t%s",dir[i].fname[k]);
}
}
break; default:exit(0);
}
}
}
OUTPUT:
Enter name of the directory -- DIR1 Enter name of the file -- aaa
File created
Enter name of the directory -- DIR1 Enter name of the file -- bbb
File created
#define MIN_RANKS 3
#define MAX_RANKS 5
#define PERCENT 30 void main()
{
int i,j,k,nodes=0; srand(time(NULL));
int ranks=MIN_RANKS+(rand()%(MAX_RANKS-MIN_RANKS+1)); printf("DIRECTED ACYCLIC GRAPH\
n");
for(i=1;i<ranks;i++)
{
int new_nodes=MIN_PER_RANK+(rand()%(MAX_PER_RANK-MIN_PER_RANK+1)); for(j=0;j<nodes;j+
+)
for(k=0;k<new_nodes;k++) if((rand()%100)<PERCENT)
printf("%d->%d;\n",j,k+nodes); nodes+=new_nodes;
}
}
OUTPUT
2->7
19) PROGRAM FOR Hierarchical directory File organization technique
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i, gap; if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node)); printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :"); scanf("%d",&(*root)->ftype); (*root)->level=lev;
(*root)->y=50+lev*50; (*root)->x=x;
(*root)->lx=lx; (*root)->rx=rx; for(i=0;i<5;i++)
(*root)->link[i]=NULL; if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)>nc); if((*root)-
>nc==0)
gap=rx-lx; else
gap=(rx-lx)/(*root)->nc; for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap, lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i; settextstyle(2,0,4); settextjustify(1,1); setfillstyle(1,BLUE); setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y); if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0); else
fillellipse(root->x,root->y,20,20); outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++) display(root->link[i]);
}
}
OUTPUT
Enter Name of dir/file (under root): ROOT Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2 Enter Name of dir/file (under ROOT): USER 1 Enter 1 for
Dir /2 for file:1
No of subdirectories /files (for USER 1): 1 Enter Name of dir/file (under USER 1):SUBDIR Enter 1 for
Dir /2 for file:1
No of subdirectories /files (for SUBDIR):
1 Enter Name of dir/file (under USER 1):JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0 Enter Name of
dir/file (under SUBDIR):VB Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0 Enter Name of dir/file (under ROOT):USER2 Enter 1 for Dir /2
for file:1
No of subdirectories /files (for USER2): 2 Enter Name of dir/file (under ROOT):A Enter 1 for Dir /2 for
file:2
Enter Name of dir/file (under USER2):SUBDIR 2 Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR 2):
2 Enter Name of dir/file (under SUBDIR2):PPL Enter 1 for Dir /2 for file:1 No of
subdire
2 Enter Name of dir/file (under PPL):B Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under PPL):C Enter 1 for Dir /2 for file:2
function add()
{
sum=$(($1 + $2)) echo "Sum = $sum"
}
a=10 b=20
#call the add function and pass the values add $a $b
OUTPUT
Sum = 30
OUTPUT
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times
PROGRAM TO FIND LESSER NUMBER
echo "Enter a number" read n
if [ $n -lt 100 ]; then
printf "$n is less than 100\n" fi
OUTPUT
Enter a number 87
87 is lesser than 100