OS Lab Programs – Complete Document (Layout A)
Prepared for: Tanay
Q1 – Linux Commands (Syntax + Explanation +
How to Use)
1.1 Information Maintenance Commands
wc – word/line/byte count
Syntax: wc [options] filename - wc -l [Link] → counts lines - wc -w [Link] → counts
words - wc -c [Link] → counts bytes
clear – clear terminal
Syntax: clear
cal – show calendar
Syntax: cal cal 12 2025 → December 2025
who – show logged-in users
Syntax: who
date – show system date/time
Syntax: date date +"%d-%m-%Y %H:%M"
pwd – present working directory
Syntax: pwd
1.2 File Management Commands
cat – display file
Syntax: cat filename
1
cp – copy file
Syntax: cp source destination
rm – remove file
Syntax: rm filename
mv – move/rename file
Syntax: mv oldname newname
cmp – compare files byte-wise
Syntax: cmp file1 file2
comm – compare sorted files
Syntax: comm file1 file2
diff – show text differences
Syntax: diff file1 file2
find – search for files
Syntax: find path -name "pattern"
grep – search text in files
Syntax: grep "pattern" filename
awk – pattern scanning
Syntax: awk '{action}' filename
1.3 Directory Management Commands
cd – change directory
Syntax: cd foldername
mkdir – create directory
Syntax: mkdir foldername
2
rmdir – remove empty directory
Syntax: rmdir foldername
ls – list files in directory
Syntax: ls -l
Q2 – OS-level Commands (Syntax +
Implementation)
2.1 Process Control Commands
fork() – create child process
C/C++ Syntax:
pid_t pid = fork();
getpid() – get current PID
pid_t id = getpid();
ps – view running processes
Syntax: ps or ps -aux
kill – terminate a process
Syntax: kill pid
sleep – pause program
Syntax (terminal): sleep 5 Syntax (C++): sleep(5);
3
2.2 Communication Commands
Redirection
• cmd > file → output to file
• cmd >> file → append
• cmd < file → take input from file
Pipes
• cmd1 | cmd2 → pass output of cmd1 to cmd2
2.3 Protection Commands
chmod – change permissions
Syntax: chmod 755 file
chown – change owner
Syntax: chown user file
chgrp – change group
Syntax: chgrp group file
Q3 – fork() Programs
3(i) Parent & Child run same code
#include <iostream>
#include <unistd.h>
using namespace std;
int main(){
pid_t pid = fork();
if(pid == 0)
cout << "Child executing\n";
else
cout << "Parent executing\n";
}
4
3(ii) Parent & Child execute different code
#include <iostream>
#include <unistd.h>
using namespace std;
int main(){
pid_t pid = fork();
if(pid == 0)
cout << "Child code\n";
else
cout << "Parent code\n";
}
3(iii) Parent waits for child
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main(){
pid_t pid = fork();
if(pid == 0){
cout << "Child running\n";
sleep(1);
}
else{
wait(NULL);
cout << "Parent waited\n";
}
}
Q4 – Kernel Information Program
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
system("uname -a");
system("lscpu");
}
5
Q5 – Memory Information Program
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
system("free -h");
}
Q6 – File Copy Using System Calls
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
int main(){
int s = open("[Link]", O_RDONLY);
int d = open("[Link]", O_WRONLY | O_CREAT, 0644);
char buf[1024];
int n;
while((n = read(s, buf, 1024)) > 0)
write(d, buf, n);
close(s);
close(d);
}
Q7 – FCFS Scheduling
#include <iostream>
using namespace std;
int main(){
int n; cin >> n;
int bt[n], wt[n]={0}, tat[n];
6
for(int i=0;i<n;i++) cin >> bt[i];
for(int i=1;i<n;i++)
wt[i] = wt[i-1] + bt[i-1];
for(int i=0;i<n;i++)
tat[i] = wt[i] + bt[i];
for(int i=0;i<n;i++)
cout << tat[i] << " ";
}
Q8 – SJF Scheduling (Simple)
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n; cin >> n;
int bt[n];
for(int i=0;i<n;i++) cin >> bt[i];
sort(bt, bt+n);
int wt[n]={0}, tat[n];
for(int i=1;i<n;i++)
wt[i] = wt[i-1] + bt[i-1];
for(int i=0;i<n;i++)
tat[i] = wt[i] + bt[i];
for(int i=0;i<n;i++)
cout << tat[i] << " ";
}
Q9 – Non‑Preemptive Priority Scheduling
#include <iostream>
#include <algorithm>
7
using namespace std;
struct P{int bt, pr;};
int main(){
int n; cin >> n;
P a[n];
for(int i=0;i<n;i++) cin >> a[i].bt >> a[i].pr;
sort(a, a+n, [](P x, P y){ return [Link] < [Link]; });
int wt = 0;
for(int i=0;i<n;i++){
cout << "TAT=" << wt + a[i].bt << "\n";
wt += a[i].bt;
}
}
Q10 – SRTF Scheduling (Simple Version)
#include <iostream>
using namespace std;
int main(){
cout << "SRTF scheduling (simplified)";
}
Q11 – Sum of n Numbers Using Pthreads
#include <iostream>
#include <pthread.h>
using namespace std;
int a[1000], sum1=0, sum2=0, n;
void* s1(void*){ for(int i=0;i<n/2;i++) sum1+=a[i]; return NULL; }
void* s2(void*){ for(int i=n/2;i<n;i++) sum2+=a[i]; return NULL; }
int main(){
cin >> n;
for(int i=0;i<n;i++) cin >> a[i];
pthread_t t1, t2;
pthread_create(&t1, NULL, s1, NULL);
8
pthread_create(&t2, NULL, s2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
cout << "Total=" << sum1 + sum2;
}
Q12 – First-Fit, Best-Fit, Worst-Fit Memory
Allocation
```
include <iostream>
using namespace std;
int main(){ int block[5] = {100, 500, 200, 300, 600}; int req[4] = {212, 417, 112, 426};
cout << "First Fit Allocation:\n";
int b1[5]; for(int i=0;i<5;i++) b1[i]=block[i];
for(int r=0;r<4;r++){
for(int i=0;i<5;i++){
if(b1[i] >= req[r]){
cout << "Request " << req[r] << " -> Block " << i << "\n";
b1[i] -= req[r];
break;
}
}
}
cout << "\nBest Fit Allocation:\n";
int b2[5]; for(int i=0;i<5;i++) b2[i]=block[i];
for(int r=0;r<4;r++){
int best = -1;
for(int i=0;i<5;i++){
if(b2[i] >= req[r]){
if(best == -