os lab manuval
os lab manuval
Experiment No-1:
cp, ps, ls, mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown,
1. man: Short for "manual," the man command is used to display the manual or
documentation
for other commands. It provides detailed information on how to use a specific command, its
2. who: The who command displays a list of currently logged-in users on the system, along
3. cat: The cat command is used to concatenate and display the content of one or multiple
files. It is also frequently used to create and edit files directly in the terminal.
4. cd: The cd command is used to change the current working directory in the terminal. It
allows you to navigate through the directory structure of the file system.
5. cp: The cp command is used to copy files and directories from one location to another.
6. ps: The ps command stands for "processstatus" and is used to display information about
7. ls: The ls command lists the contents of a directory, showing files and subdirectories in the
specified location.
9. rm: The rm command is used to remove (delete) files and directories. Be cautious when
10. mkdir: The mkdir command is used to create new directories (folders) in the file system.
11. rmdir: The rmdir command is used to remove empty directories (folders) from the file
system.
12. echo: The echo command is used to display a message or text on the terminal. It is also
13. more: The more command is used to display the content of a file one screen at a time,
14. date: The date command displays the current date and time.
15. time: The time command is used to measure the execution time of other commands.
16. kill: The kill command is used to terminate or send signals to running processes. It is
17. history: The history command displays a list of previously executed commands in the
current session.
18. chmod: The chmod command is used to change the permissions (read, write, execute) of
19. chown: The chown command is used to change the ownership of files and directories,
20. finger: The finger command is used to display information about user accounts on thesystem.
21. pwd: The pwd command stands for "print working directory," and it displays the full
path ofthe current working directory.
22. cal: The cal command displays the calendar for the specified month or year.
23. logout: The logout command is used to log out from the current user session in the
terminal.
24. shutdown: The shutdown command is used to shut down or restart the system.
Experiment No-2:
Aim: Write programs using the following UNIX operating system calls
Source code :
#include <stdio.h>
int main() {
FILE *file;
char buffer[100];
file = fopen("testfile.txt", "w+"); // "w+" creates a file for reading and writing
if (file == NULL) {
return 1;
fseek(file, 0, SEEK_SET);
getch(); // Wait for key press before exiting (specific to Turbo C++)
return 0;
Output:
B. opendir, closedir
Source code :
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include<conio.h>
int main(void) {
DIR *d;
struct dirent *dir;
clrscr();
scanf("%s",directory_path);
d = opendir(directory_path);
if (d) {
printf("%s\n", dir->d_name);
closedir(d);
} else {
return EXIT_FAILURE;
getch();
return EXIT_SUCCESS;
Output:
C:/TURBOC3/Sourcedir1
file1.txt
file2.c
subdir1
program.exe
C:/TURBOC3/Sourcedir2
Experiment No-3:
AIM:
Source code :
#include <stdio.h>
#include <stdlib.h>
if (argc != 3) {
printf("Usage: ./cp_simulation<source_file><destination_file>\n");
return 1;
}
source = fopen(argv[1], "r");
if (source == NULL) {
printf("Unable to open source file: %s\n", argv[1]);
return 1;
}
return 0;
}
Source code :
#include<stdio.h>
#include<dirent.h>
main(intargc,char**argv)
DIR*dp;
printf(“\ncontentsofthedirectory%sare\n”,argv[1]); while((link=readdir(dp))!=0)
printf(“%s”,link->d_name);
closedir(dp);
Output: ./list_directoryexample_dir
..
file1.txt
file2.txt
subdir1
subdir2
Experiment 4:
Aim: Simulate the following CPU scheduling algorithms:
(a) Round Robin (b) SJF (c) FCFS (d) Priority
A) Round Robin CPU SCHEDULING ALGORITHM
PROGRAM:
Source code :
#include<stdio.h>
int main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n%d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]);
printf("\nAvg wait time is %f Avgturn around time is %f",awt,atat);
return 0;
}
INPUT:
Enter number of processes:3
Enter burst time for sequences:12
8
20
Enter time quantum:5
#include<stdio.h>
int main()
{
int i,j,bt[10],n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
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];
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("\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]);
printf("aw=%f\n,at=%f\n",aw,at);
return 0;
}
INPUT:
enter no. of processes:
3
enter the burst time of processes:12
8
20
EXPECTED OUTPUT:
btwttt
12 0 12
8 12 20
20 20 40
aw=10.000000
,at=24.000000
Experiment 5:
Aim: Control the number of ports opened by the operating system with
a. Semaphore
b. Monitor
A. Semaphore
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define MAX_PORTS 3
sem_tsemaphore;
void* open_port(void* arg) {
int port_number = *((int*)arg);
printf("Thread %d waiting to open a port...\n", port_number);
sem_wait(&semaphore);
sem_post(&semaphore);
free(arg);
return NULL;
}
int main() {
sem_init(&semaphore, 0, MAX_PORTS);
pthread_tthreads[5];
return 0;
}
Output:
Thread 1 waiting to open a port...
Thread 1 opened a port.
Thread 2 waiting to open a port...
Thread 2 opened a port.
Thread 3 waiting to open a port...
Thread 3 opened a port.
Thread 4 waiting to open a port...
Thread 5 waiting to open a port...
Thread 1 closed the port.
Thread 4 opened a port.
Thread 2 closed the port.
Thread 5 opened a port.
Thread 3 closed the port.
Thread 4 closed the port.
Thread 5 closed the port.
B. Monitors
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_PORTS 3
int open_ports = 0;
pthread_mutex_tmutex;
pthread_cond_tcond;
void* open_port(void* arg) {
int port_number = *((int*)arg);
printf("Thread %d waiting to open a port...\n", port_number);
pthread_mutex_lock(&mutex);
open_ports++;
printf("Thread %d opened a port. Currently opened ports: %d\n", port_number, open_ports);
pthread_mutex_unlock(&mutex);
sleep(2);
pthread_mutex_lock(&mutex);
open_ports--;
printf("Thread %d closed the port. Currently opened ports: %d\n", port_number, open_ports);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
free(arg);
return NULL;
}
int main() {
pthread_tthreads[5];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
Output:
Thread 1 waiting to open a port...
Thread 1 opened a port. Currently opened ports: 1
Thread 2 waiting to open a port...
Thread 2 opened a port. Currently opened ports: 2
Thread 3 waiting to open a port...
Thread 3 opened a port. Currently opened ports: 3
Thread 4 waiting to open a port...
Thread 4 is waiting since all ports are busy.
Thread 5 waiting to open a port...
Thread 5 is waiting since all ports are busy.
Thread 1 closed the port. Currently opened ports: 2
Thread 4 opened a port. Currently opened ports: 3
Thread 2 closed the port. Currently opened ports: 2
Thread 5 opened a port. Currently opened ports: 3
Thread 3 closed the port. Currently opened ports: 2
Thread 4 closed the port. Currently opened ports: 1
Thread 5 closed the port. Currently opened ports: 0
Experiment No-6:
OUT PUT:
Main: creating thread 0
Main: creating thread 1
Main: creating thread 2
Main: creating thread 3
Main: creating thread 4
Thread 0 is starting...
Thread 1 is starting...
Thread 2 is starting...
Thread 3 is starting...
Thread 4 is starting...
Thread 0 is finishing...
Thread 1 is finishing...
Thread 2 is finishing...
Thread 3 is finishing...
Thread 4 is finishing...
All threads have completed.
Experiment No-7:
Experiment No-8:
AIM : Implement the following memory allocation methods for fixed partition a) First
fit b) Worst fit c) Best fit
First Fit Memory Allocation Source Code:
Source code :
#include <stdio.h>
void firstFit(int blocks[]
, int m,
int processes[],
int n) {
int allocation[n];
for (int i = 0; i< n; i++) {
allocation[i] = -1;
}
for (int i = 0; i< n; i++) {
for (int j = 0; j < m; j++) {
if (blocks[j] >= processes[i]) {
allocation[i] = j;
blocks[j] -= processes[i];
break;
}
}
}
printf("First Fit Allocation:\n");
for (int i = 0; i< n; i++) {
if (allocation[i] != -1)
printf("Process %d allocated to Block %d\n", i + 1, allocation[i] + 1);
else
printf("Process %d not allocated\n", i + 1);
}
}
int main() {
int blocks[] = {100, 500, 200, 300, 600};
int processes[] = {212, 417, 112, 426};
int m = sizeof(blocks) / sizeof(blocks[0]);
int n = sizeof(processes) / sizeof(processes[0]);
firstFit(blocks, m, processes, n);
return 0;
}
OUTPUT:
First Fit Allocation:
Process 1 allocated to Block 2
Process 2 allocated to Block 5
Process 3 allocated to Block 2
Process 4 not allocated
if (bestIdx != -1) {
allocation[i] = bestIdx;
blocks[bestIdx] -= processes[i];
}
}
int main() {
int blocks[] = {100, 500, 200, 300, 600}; // Memory blocks
int processes[] = {212, 417, 112, 426}; // Processes requiring memory
int m = sizeof(blocks) / sizeof(blocks[0]);
int n = sizeof(processes) / sizeof(processes[0]);
Source code:
#include <stdio.h>
void worstFit(int blocks[], int m, int processes[], int n) {
int allocation[n];
for (int i = 0; i< n; i++) {
allocation[i] = -1; // Initialize allocation array
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blocks[worstIdx] -= processes[i];
}
}
return 0;
}
Output:
Worst Fit Allocation:
Process 1 allocated to Block 5
Process 2 allocated to Block 2
Process 3 allocated to Block 5
Process 4 not allocated
Experiment No-9:
AIM Simulate the following page replacement algorithms a) FIFO b) LRU c) LFU
a) FIFO Source Code:
Source code:
#include <stdio.h>
void fifoPageReplacement(int pages[], int n, int capacity) {
int frames[capacity];
int front = 0, page_faults = 0;
for (int i = 0; i< capacity; i++)
frames[i] = -1;
printf("FIFO Page Replacement:\n");
for (int i = 0; i< n; i++) {
int page = pages[i];
int flag = 0;
for (int j = 0; j < capacity; j++) {
if (frames[j] == page) {
flag = 1; // Page found, no fault
break;
}
}
if (flag == 0) {
frames[front] = page;
front = (front + 1) % capacity;
page_faults++;
printf("Page %d caused a page fault.\n", page);
}
printf("Frames: [");
for (int j = 0; j < capacity; j++) {
if (frames[j] != -1) {
printf("%d ", frames[j]);
} else {
printf("- ");
}
}
printf("]\n");
}
printf("Total Page Faults: %d\n\n", page_faults);
}
int main() {
int pages[] = {1, 3, 0, 3, 5, 6, 3, 1, 6, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3;
fifoPageReplacement(pages, n, capacity);
return 0;
}
Output :
Page 1 caused a page fault.
Frames: [1 - - ]
Page 3 caused a page fault.
Frames: [1 3 - ]
Page 0 caused a page fault.
Frames: [1 3 0 ]
Frames: [1 3 0 ]
Page 5 caused a page fault.
Frames: [5 3 0 ]
Page 6 caused a page fault.
Frames: [5 6 0 ]
Frames: [5 6 0 ]
Frames: [5 6 0 ]
Frames: [5 6 0 ]
Total Page Faults: 5
Experiment No: 10
Aim: Simulate Paging Technique of memory management.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 4
#define MEMORY_SIZE 16
#define NUM_PAGES MEMORY_SIZE / PAGE_SIZE
int main() {
int logical_address, page_number, offset, physical_address;
int page_table[NUM_PAGES];
for (int i = 0; i < NUM_PAGES; i++) {
page_table[i] = i;
}
printf("PAGE TABLE:\n");
printf("Page Number -> Frame Number\n");
for (int i = 0; i < NUM_PAGES; i++) {
printf(" %d -> %d\n", i, page_table[i]);
}
printf("\nEnter a logical address (0 to %d): ", MEMORY_SIZE - 1);
scanf("%d", &logical_address);
if (logical_address < 0 || logical_address >= MEMORY_SIZE) {
printf("Invalid logical address! Please enter an address between 0 and %d.\n", MEMORY_SIZE
- 1);
return 1;
}
page_number = logical_address / PAGE_SIZE;
offset = logical_address % PAGE_SIZE;
physical_address = (page_table[page_number] * PAGE_SIZE) + offset;
printf("\nLogical Address: %d\n", logical_address);
printf("Page Number: %d, Offset: %d\n", page_number, offset);
printf("Physical Address: %d (Frame Number: %d, Offset: %d)\n", physical_address,
page_table[page_number], offset);
return 0;
}
Output:
PAGE TABLE:
Page Number -> Frame Number
0 -> 0
1 -> 1
2 -> 2
3 -> 3
Logical Address: 6
Page Number: 1, Offset: 2
Physical Address: 6 (Frame Number: 1, Offset: 2)
Experiment No: 11
Aim: Implement Bankers Algorithm for Dead Lock avoidance and prevention
Source Code:
#include <stdio.h>
#include <stdbool.h>
#define P 5
#define R 3
bool isSafe(int processes[], int avail[], int max[][R], int alloc[][R], int need[][R]) {
int work[R], finish[P] = {0};
for (int i = 0; i < R; i++) {
work[i] = avail[i];
}
int safeSeq[P];
int count = 0;
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) { // If process is not finished
bool possible = true;
for (int j = 0; j < R; j++) {
if (need[p][j] > work[j]) {
possible = false;
break;
}
}
if (possible) {
for (int k = 0; k < R; k++) {
work[k] += alloc[p][k];
}
safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}
if (!found) {
return false;
}
}
printf("System is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < P; i++) {
printf("%d ", safeSeq[i]);
}
printf("\n");
return true;
}
int main() {
int processes[P] = {0, 1, 2, 3, 4};
int avail[R] = {3, 3, 2};
int max[P][R] = {{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};
int alloc[P][R] = {{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
int need[P][R];
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
return 0;
}
Output:
System is in a safe state.
Safe sequence is: 1 3 4 0 2
Experiment No: 12
Aim: Simulate the following file allocation strategies a) Sequential b) Linked c) Indexed
int main() {
int startBlock, fileSize;
for (int i = 0; i< MAX_BLOCKS; i++) {
blocks[i] = 0;
}
printf("Enter the starting block and file size: ");
scanf("%d%d", &startBlock, &fileSize);
sequentialAllocation(startBlock, fileSize);
return 0;
}
Output:
Enter the starting block and file size: 5 3
File allocated using Sequential Allocation from block 5 to block 7.
#include <stdio.h>
#define MAX_BLOCKS 100
struct Block {
int nextBlock;
int isAllocated;
};
struct Block blocks[MAX_BLOCKS];
void linkedAllocation(int startBlock, int fileSize) {
int count = 0, currentBlock = startBlock;
Output:
Enter the starting block and file size: 3 3
Enter the next block number after 3: 5
Enter the next block number after 5: 7
File allocated using Linked Allocation starting from block 3.
./nachos -x ../test/halt