0% found this document useful (0 votes)
2 views14 pages

OS CN Lab Programs With Codes

The document contains a series of programming examples and commands related to operating systems and computer networks, including Linux commands, FCFS scheduling, dynamic partitioning, page replacement algorithms, disk scheduling methods, and Hamming code for error detection. Each section provides code snippets in C for various algorithms and commands, along with explanations of their functionalities. The content serves as a practical guide for understanding and implementing these concepts in programming.

Uploaded by

sonipalak049
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

OS CN Lab Programs With Codes

The document contains a series of programming examples and commands related to operating systems and computer networks, including Linux commands, FCFS scheduling, dynamic partitioning, page replacement algorithms, disk scheduling methods, and Hamming code for error detection. Each section provides code snippets in C for various algorithms and commands, along with explanations of their functionalities. The content serves as a practical guide for understanding and implementing these concepts in programming.

Uploaded by

sonipalak049
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OS & CN Lab Programs with Codes

Program 1 - Linux Commands


mkdir mydir
rmdir mydir
pwd
ls -l
cd mydir
touch [Link]
cp [Link] [Link]
mv [Link] [Link]
rm [Link]
cat [Link]
chmod 777 [Link]
chown user:user [Link]

Command Purpose Example


pwd Show current directory pwd
ls List files/directories ls
ls -l Detailed listing ls -l
mkdir Create directory mkdir test
rmdir Remove empty directory rmdir test
cd Change directory cd test
touch Create empty file touch [Link]
cat Display file contents cat [Link]
cp Copy file cp [Link] [Link]
mv Move/Rename file mv [Link] [Link]
rm Delete file rm [Link]
chmod Change permissions chmod 777 [Link]
chown Change ownership sudo chown user:user [Link]
whoami Show current user whoami
date Display date and time date
clear Clear terminal clear
man Command manual man ls
echo Display text echo "Hello"
grep Search text in file grep "hello" [Link]
head First 10 lines of file head [Link]
tail Last 10 lines of file tail [Link]
wc Count lines/words/chars wc [Link]
find Search files find . -name [Link]
ps Show running processes ps
kill Terminate process kill PID
Command Purpose Example
df -h Disk usage df -h

free -m
mkdir mydir
cd mydir
touch [Link]
echo "Hello Linux" > [Link]
cat [Link]
cp [Link] [Link]
mv [Link] [Link]
ls -l
chmod 777 [Link]
rm [Link]
cd ..
rmdir mydir

Program 2 - FCFS Scheduling


#include <stdio.h>

int main()
{
int n, i;
int bt[20], wt[20], tat[20];
float avg_wt = 0, avg_tat = 0;

printf("Enter number of processes: ");


scanf("%d", &n);

printf("Enter Burst Time for each process:\n");


for(i = 0; i < n; i++)
{
printf("P%d: ", i + 1);
scanf("%d", &bt[i]);
}

wt[0] = 0;

for(i = 1; i < n; i++)


{
wt[i] = wt[i - 1] + bt[i - 1];
}

for(i = 0; i < n; i++)


{
tat[i] = wt[i] + bt[i];

avg_wt += wt[i];
avg_tat += tat[i];
}

avg_wt /= n;
avg_tat /= n;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

for(i = 0; i < n; i++)


{
printf("P%d\t%d\t\t%d\t\t%d\n",
i + 1, bt[i], wt[i], tat[i]);
}

printf("\nAverage Waiting Time = %.2f", avg_wt);


printf("\nAverage Turnaround Time = %.2f\n", avg_tat);
return 0;
}Program 3 - Dynamic Partitioning
#include <stdio.h>

int main()
{
int blockSize[20], processSize[20];
int allocation[20];
int m, n, i, j, choice;

printf("Enter number of memory blocks: ");


scanf("%d", &m);

printf("Enter sizes of memory blocks:\n");


for(i = 0; i < m; i++)
{
scanf("%d", &blockSize[i]);
}

printf("Enter number of processes: ");


scanf("%d", &n);

printf("Enter sizes of processes:\n");


for(i = 0; i < n; i++)
{
scanf("%d", &processSize[i]);
}

printf("\n1. First Fit");


printf("\n2. Best Fit");
printf("\n3. Worst Fit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

for(i = 0; i < n; i++)


allocation[i] = -1;

if(choice == 1)
{
// First Fit
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
if(blockSize[j] >= processSize[i])
{
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
}

else if(choice == 2)
{
// Best Fit
for(i = 0; i < n; i++)
{
int bestIdx = -1;

for(j = 0; j < m; j++)


{
if(blockSize[j] >= processSize[i])
{
if(bestIdx == -1 ||
blockSize[j] < blockSize[bestIdx])
{
bestIdx = j;
}
}
}

if(bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
}

else if(choice == 3)
{
// Worst Fit
for(i = 0; i < n; i++)
{
int worstIdx = -1;

for(j = 0; j < m; j++)


{
if(blockSize[j] >= processSize[i])
{
if(worstIdx == -1 ||
blockSize[j] > blockSize[worstIdx])
{
worstIdx = j;
}
}
}

if(worstIdx != -1)
{
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");

for(i = 0; i < n; i++)


{
printf("%d\t\t%d\t\t",
i + 1, processSize[i]);

if(allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}

return 0;

}
Program 4 - Page Replacement
[Link]
#include <stdio.h>

int main()
{
int pages[50], frames[10];
int n, f, i, j, k = 0, flag, faults = 0;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter page reference string:\n");


for(i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

for(i = 0; i < f; i++)


frames[i] = -1;

for(i = 0; i < n; i++)


{
flag = 0;

for(j = 0; j < f; j++)


{
if(frames[j] == pages[i])
{
flag = 1;
break;
}
}

if(flag == 0)
{
frames[k] = pages[i];
k = (k + 1) % f;
faults++;
}
}

printf("Total Page Faults = %d\n", faults);

return 0;
}

2. LRU
#include <stdio.h>

int main()
{
int pages[50], frames[10], time[10];
int n, f, i, j;
int faults = 0, counter = 0;
int found, pos, min;

printf("Enter number of pages: ");


scanf("%d", &n);
printf("Enter page reference string:\n");
for(i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

for(i = 0; i < f; i++)


{
frames[i] = -1;
time[i] = 0;
}

for(i = 0; i < n; i++)


{
found = 0;

for(j = 0; j < f; j++)


{
if(frames[j] == pages[i])
{
counter++;
time[j] = counter;
found = 1;
break;
}
}

if(found == 0)
{
min = time[0];
pos = 0;

for(j = 1; j < f; j++)


{
if(time[j] < min)
{
min = time[j];
pos = j;
}
}

frames[pos] = pages[i];
counter++;
time[pos] = counter;
faults++;
}
}

printf("Total Page Faults = %d\n", faults);

return 0;
}

[Link]
#include <stdio.h>

int main()
{
int pages[50], frames[10];
int n, f, i, j, k;
int faults = 0, flag;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter page reference string:\n");


for(i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

for(i = 0; i < f; i++)


frames[i] = -1;

for(i = 0; i < n; i++)


{
flag = 0;

for(j = 0; j < f; j++)


{
if(frames[j] == pages[i])
{
flag = 1;
break;
}
}

if(flag == 0)
{
int pos = -1, farthest = i + 1;

for(j = 0; j < f; j++)


{
int found = 0;

for(k = i + 1; k < n; k++)


{
if(frames[j] == pages[k])
{
if(k > farthest)
{
farthest = k;
pos = j;
}
found = 1;
break;
}
}

if(!found)
{
pos = j;
break;
}
}

if(pos == -1)
pos = 0;

frames[pos] = pages[i];
faults++;
}
}

printf("Total Page Faults = %d\n", faults);

return 0;
Program 5 - Disk Scheduling
FCFS,
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, head, i;
int req[50];
int seek = 0;

printf("Enter number of requests: ");


scanf("%d", &n);

printf("Enter request sequence:\n");


for(i = 0; i < n; i++)
scanf("%d", &req[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

for(i = 0; i < n; i++)


{
seek += abs(req[i] - head);
head = req[i];
}

printf("Total Seek Time = %d\n", seek);

return 0;
}

SSTF
#include <stdio.h>
#include <stdlib.h>

int main()
{
int req[50], visited[50];
int n, head, i, j;
int seek = 0;

printf("Enter number of requests: ");


scanf("%d", &n);

printf("Enter request sequence:\n");


for(i = 0; i < n; i++)
{
scanf("%d", &req[i]);
visited[i] = 0;
}

printf("Enter initial head position: ");


scanf("%d", &head);

for(i = 0; i < n; i++)


{
int min = 9999;
int index = -1;

for(j = 0; j < n; j++)


{
if(!visited[j])
{
int dist = abs(req[j] - head);
if(dist < min)
{
min = dist;
index = j;
}
}
}

visited[index] = 1;
seek += min;
head = req[index];
}

printf("Total Seek Time = %d\n", seek);

return 0;
}

SCAN
#include <stdio.h>
#include <stdlib.h>

int main()
{
int req[50], n, head;
int disk_size = 200;
int seek = 0;
int i, j, temp;

printf("Enter number of requests: ");


scanf("%d", &n);

printf("Enter request sequence:\n");


for(i = 0; i < n; i++)
scanf("%d", &req[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

for(i = 0; i < n - 1; i++)


{
for(j = 0; j < n - i - 1; j++)
{
if(req[j] > req[j + 1])
{
temp = req[j];
req[j] = req[j + 1];
req[j + 1] = temp;
}
}
}

int pos = 0;

for(i = 0; i < n; i++)


{
if(req[i] >= head)
{
pos = i;
break;
}
}

for(i = pos; i < n; i++)


{
seek += abs(req[i] - head);
head = req[i];
}

seek += abs((disk_size - 1) - head);


head = disk_size - 1;

for(i = pos - 1; i >= 0; i--)


{
seek += abs(req[i] - head);
head = req[i];
}

printf("Total Seek Time = %d\n", seek);

return 0;
}

Program 6 - Hamming Code


Hamming Code generation
#include <stdio.h>
#include <math.h>

int main()
{
int data[20], hamming[30];
int m, r = 0;
int i, j, k;

printf("Enter number of data bits: ");


scanf("%d", &m);

printf("Enter data bits:\n");


for(i = m; i >= 1; i--)
scanf("%d", &data[i]);

while(pow(2, r) < (m + r + 1))


r++;

int totalBits = m + r;

j = 1;
k = 1;

for(i = 1; i <= totalBits; i++)


{
if(i == pow(2, j - 1))
{
hamming[i] = 0;
j++;
}
else
{
hamming[i] = data[k];
k++;
}
}

for(i = 0; i < r; i++)


{
int pos = pow(2, i);
int parity = 0;

for(j = pos; j <= totalBits; j += 2 * pos)


{
for(k = j; k < j + pos && k <= totalBits; k++)
{
parity ^= hamming[k];
}
}

hamming[pos] = parity;
}

printf("\nHamming Code: ");


for(i = totalBits; i >= 1; i--)
printf("%d", hamming[i]);

printf("\n");

return 0;
}

error detection
#include <stdio.h>
#include <math.h>

int main()
{
int code[30];
int n, r = 0;
int i, j, k;
int errorPos = 0;

printf("Enter number of bits in received code: ");


scanf("%d", &n);

printf("Enter received code bits:\n");

for(i = n; i >= 1; i--)


scanf("%d", &code[i]);

while(pow(2, r) < n + 1)
r++;

for(i = 0; i < r; i++)


{
int pos = pow(2, i);
int parity = 0;

for(j = pos; j <= n; j += 2 * pos)


{
for(k = j; k < j + pos && k <= n; k++)
{
parity ^= code[k];
}
}

if(parity)
errorPos += pos;
}

if(errorPos == 0)
printf("\nNo Error Detected\n");
else
printf("\nError at Position = %d\n", errorPos);

return 0;
}
Program 7 - TCP Client Server
import socket

choice = input("Enter s for Server or c for Client: ")

if choice == 's':
server = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link](("localhost", 8080))
[Link](1)

print("Waiting for client...")


conn, addr = [Link]()

data = [Link](1024).decode()
print("Client:", data)

[Link]("Hello Client".encode())

[Link]()
[Link]()

elif choice == 'c':


client = [Link](socket.AF_INET, socket.SOCK_STREAM)

[Link](("localhost", 8080))

[Link]("Hello Server".encode())

data = [Link](1024).decode()

print("Server:", data)

[Link]()

Program 8 - UDP Client Server


import socket

choice = input("Enter s for Server or c for Client: ")

if choice == 's':
server = [Link](socket.AF_INET, socket.SOCK_DGRAM)

[Link](("localhost", 8080))

print("Waiting for client message...")

data, addr = [Link](1024)

print("Client:", [Link]())

[Link]("Hello Client".encode(), addr)

[Link]()

elif choice == 'c':


client = [Link](socket.AF_INET, socket.SOCK_DGRAM)

[Link]("Hello Server".encode(), ("localhost", 8080))

data, addr = [Link](1024)

print("Server:", [Link]())

[Link]().

Program 9 - Sliding Window


1.
#include <stdio.h>

int main()
{
int frames, windowSize, i;

printf("Enter number of frames to send: ");


scanf("%d", &frames);

printf("Enter window size: ");


scanf("%d", &windowSize);

printf("\nTransmission Process\n");

for(i = 0; i < frames; i++)


{
printf("Frame %d sent\n", i);
}

printf("\nAcknowledgement Process\n");

for(i = 0; i < frames; i++)


{
printf("Acknowledgement for Frame %d received\n", i);
}

printf("\nAll Frames Successfully Transmitted\n");

return 0;
}

[Link] and back with retransmission


#include <stdio.h>

int main()
{
int totalFrames, windowSize;
int i;

printf("Enter Total Frames: ");


scanf("%d", &totalFrames);

printf("Enter Window Size: ");


scanf("%d", &windowSize);

for(i = 0; i < totalFrames; i++)


{
printf("Sending Frame %d\n", i);

if(i == 3)
{
printf("Frame %d Lost\n", i);

printf("Retransmitting Frames:\n");

for(int j = i; j < totalFrames && j < i + windowSize; j++)


{
printf("Frame %d Retransmitted\n", j);
}
break;
}
}

return 0;
}

You might also like