Operating Systems
Code:
BCA-2003T (For theory)
BCA-2003P (For practical)
Paper: CC-V
L+T: 3L+T: 2P
4 Credits (45 hour theory and 30 hour practical)
Max Marks:
Theory: 100 (Int: 25; Ext: 75)
Practical: 100
Course Outcomes:
Upon completion of the course, students will be able to:
CO1: Understand the components, services, and structures of operating systems and different types of OS.
CO2: Analyze process scheduling, multithreading, and CPU scheduling algorithms.
CO3: Apply concepts of process synchronization and solve deadlock problems using appropriate techniques.
CO4: Implement memory management techniques, file systems, and disk scheduling algorithms.
LAB Outcomes:
CO1: Implement CPU scheduling algorithms.
CO2: Understand and solve critical section problems.
CO3: Apply file allocation and frame management techniques.
CO4: Implement page replacement algorithms.
Unit-wise Syllabus
Unit I – Operating Systems (OS) Overview (11 Lectures)
Operating Systems (OS) Overview:
Definition, Evaluation of OS, Components & Services of OS, Structure, Architecture, types of Operating Systems, Batch Systems, Concepts of Multiprogramming and Time Sharing, Parallel, Distributed, and real-time Systems.
Operating Systems Structures:
Operating system services and system calls, system programs, operating system structure, operating systems generations.
Unit II – Process Management (11 Lectures)
Process Management:
Process Definition, Process states, Process State transitions, Process Scheduling, Process Control Block, Threads, Concept of multithreads, Benefits of threads, Types of threads.
Process Scheduling:
Definition, Scheduling objectives, Scheduling algorithms, CPU scheduling Preemptive and Non-preemptive Scheduling algorithms (FCFS, SJF and RR), Performance evaluation of the scheduling algorithms.
Unit III – Process Synchronization (11 Lectures)
Introduction, Inter-process Communication, Race Conditions, Critical Section Problem, Mutual Exclusion, Semaphores, problems of synchronization, readers and writers problem, dining philosophers problem, Monitors.
Deadlocks:
System model, deadlock characterization, deadlock prevention, avoidance, Banker’s algorithm, Deadlock detection, and recovery from deadlocks.
Unit IV – Memory Management (12 Lectures)
Logical and Physical address map, Swapping, Memory allocation, MFT, MVT, Internal and External fragmentation and Compaction, Paging, and Segmentation.
Virtual Memory:
Demand paging, Page Replacement algorithms, Allocation of frames, thrashing.
I/O Management:
Principles of I/O Hardware: Disk structure, Disk scheduling algorithms.
File System Interface:
File Concept, Access Methods, Directory Structure, File System Structure, Allocation Methods, and Free-Space Management.
System Protection:
Goals, Principles, Domain of Protection, Access Matrix, Access Control.
Lab Programs
- Write a C program to simulate the FCFS CPU Scheduling algorithm.
- Write a C program to simulate the SJF CPU Scheduling algorithm.
- Write a C program to simulate the Round Robin CPU Scheduling algorithm.
- Write a C program to simulate Bankers Algorithm for Deadlock Avoidance.
- Write a C program to implement the Producer–Consumer problem using semaphores.
- Write a C program to illustrate the IPC mechanism using Pipes.
- Write a C program to illustrate the IPC mechanism using FIFOs.
- Write a C program to simulate Paging memory management technique.
- Write a C program to simulate Segmentation memory management technique.
- Write a C program to simulate the Best Fit contiguous memory allocation technique.
- Write a C program to simulate the First Fit contiguous memory allocation technique.
- Write a C program to simulate the concept of Dining-Philosophers problem.
- Write a C program to simulate the MVT algorithm.
- Write a C program to implement FIFO page replacement technique.
- Write a C program for implementing sequential file allocation method.
📘 Unit I
Operating Systems (OS)
Overview
(BCA 2nd Semester)
Operating Systems | CC-V
🖥️ 1. Operating System (OS) Overview
🔹 1.1 Definition of Operating System
Operating System (OS) ek system software hai jo user aur computer hardware ke beech interface ka kaam karta hai.
👉 Simple words mein:
OS computer ka manager hota hai jo CPU, Memory, Disk, aur Devices ko control karta hai.
📌 Definition (Exam Ready):
An Operating System is a system software that acts as an interface between the user and computer hardware and manages all system resources efficiently.
💡 Examples:
- Windows
- Linux
- macOS
- Android
🔹 1.2 Evaluation of Operating System
OS ko evaluate karne ke kuch important parameters:
- Performance – Fast execution
- Efficiency – Resources ka best use
- Reliability – System crash na ho
- Security – Data protection
- Scalability – Large systems handle kare
- User-friendly Interface
🔹 1.3 Components of Operating System
Main Components:
- Process Management
- Memory Management
- File System Management
- I/O System Management
- Secondary Storage Management
- Security & Protection
- Networking
🔹 1.4 Services of Operating System
OS user aur programs ko following services deta hai:
- Program execution
- I/O operations
- File system manipulation
- Communication (IPC)
- Error detection
- Resource allocation
- Accounting
- Protection and security
🏗️ 2. Structure and Architecture of OS
🔹 2.1 Structure of OS
OS alag-alag tarah se design kiya ja sakta hai:
1️⃣ Simple Structure
Basic OS design (example: MS-DOS)
2️⃣ Layered Structure
OS ko layers mein divide kiya jata hai.
Har layer niche wali layer ka use karti hai.
3️⃣ Microkernel Structure
Sirf essential services kernel mein hoti hain.
4️⃣ Monolithic Structure
Saare services ek hi large kernel mein.
5️⃣ Hybrid Structure
Monolithic + Microkernel ka mix
(Example: Windows)
🔹 2.2 OS Architecture
Architecture batata hai system ka internal design.
Types:
- Single Processor System
- Multiprocessor System
- Clustered System
🖥️ 3. Types of Operating Systems
🔹 3.1 Batch Operating System
- Jobs ko batch mein execute karta hai.
- User interaction nahi hota.
📌 Example: Early IBM systems
🔹 3.2 Multiprogramming OS
- Multiple programs memory mein load hote hain.
- CPU idle time reduce hota hai.
🔹 3.3 Time Sharing System
- Multiple users ek system share karte hain.
- Har user ko small time slice milta hai.
📌 Example: UNIX
🔹 3.4 Parallel System
- Multiple CPUs ek saath kaam karte hain.
- Speed aur reliability improve hoti hai.
🔹 3.5 Distributed System
- Multiple computers network se connected hote hain.
- Ek system ki tarah kaam karte hain.
🔹 3.6 Real-Time Operating System (RTOS)
Fixed time limit ke andar response dena zaroori hota hai.
Types:
- Hard Real-Time
- Soft Real-Time
📌 Example: ATM machines, Air traffic control systems
🔁 4. Concepts of Multiprogramming and Time Sharing
Multiprogramming
- Multiple jobs memory mein loaded
- CPU ek job se dusre job par switch karta hai
Time Sharing
- CPU ko small time slices mein divide kiya jata hai
- Har user ko fair time milta hai
🏛️ 5. Operating System Structures
🔹 5.1 OS Services and System Calls
System Call kya hota hai?
System Call ek interface hota hai jiske through user program OS se service request karta hai.
📌 Example:
- fork()
- read()
- write()
- open()
Categories:
- Process Control
- File Management
- Device Management
- Information Maintenance
- Communication
🔹 5.2 System Programs
System programs wo utilities hote hain jo system ko manage karte hain.
Examples:
- File management tools
- Compilers
- Text editors
- Command interpreters
🔹 5.3 Operating System Generations
🟢 First Generation (1940–1955)
- No OS
- Manual operation
🟢 Second Generation (1955–1965)
- Batch systems
🟢 Third Generation (1965–1980)
- Multiprogramming
- Time-sharing
🟢 Fourth Generation (1980–Present)
- GUI-based OS
- Distributed systems
- Mobile OS
📌 Important Exam Questions
- Define Operating System and explain its functions.
- Explain types of Operating Systems.
- Difference between Multiprogramming and Time Sharing.
- What are System Calls? Explain types.
- Explain Batch OS and Real-Time OS.
- Describe OS structure models.
📌 Important Exam Questions
- Define Operating System and explain its functions.
- Explain types of Operating Systems.
- Difference between Multiprogramming and Time Sharing.
- What are System Calls? Explain types.
- Explain Batch OS and Real-Time OS.
- Describe OS structure models.
📘 Unit II
Process Management
(BCA 2nd Semester)
Operating Systems | CC-V
🧠 1. Process Management
🔹 1.1 Process Definition
📌 What is a Process?
👉 Process = Program in Execution
Jab koi program memory mein load hoke run kar raha hota hai, tab use process kehte hain.
📌 Definition (Exam Ready):
A process is a program in execution that includes program code, current activity, stack, data section, and heap.
🔎 Process Includes:
- Program Code (Text Section)
- Program Counter
- Stack (Function calls, local variables)
- Data Section (Global variables)
- Heap (Dynamic memory)
🔄 1.2 Process States
Process apne life cycle mein different states se guzarta hai:
🔹 Main 5 States:
- New – Process create ho raha hai
- Ready – CPU ke liye wait kar raha hai
- Running – CPU execute kar raha hai
- Waiting (Blocked) – I/O ya event ka wait
- Terminated – Execution complete
🔁 1.3 Process State Transitions
↓
Waiting
Explanation:
- Ready → Running (Scheduler CPU deta hai)
- Running → Waiting (I/O request)
- Waiting → Ready (I/O complete)
- Running → Terminated (Process complete)
⚙️ 1.4 Process Scheduling
CPU ek time par sirf ek process execute karta hai.
Isliye OS decide karta hai kaunsa process kab run karega — isko Process Scheduling kehte hain.
📋 1.5 Process Control Block (PCB)
PCB ek data structure hai jo har process ki information store karta hai.
PCB mein hoti hai:
- Process ID (PID)
- Process State
- Program Counter
- CPU Registers
- Scheduling Information
- Memory Management Info
- I/O Status Info
📌 PCB = Identity card of proces
🧵 1.6 Threads
🔹 Thread kya hai?
Thread ek lightweight process hai.
Ek process ke andar multiple threads ho sakte hain jo same memory share karte hain.
📌 Definition:
A thread is a basic unit of CPU utilization consisting of thread ID, program counter, register set, and stack.
🔹 1.7 Concept of Multithreading
Multithreading ka matlab hai ek hi process ke andar multiple threads run karna.
Example:
Web browser:
- Ek thread page load karta hai
- Ek thread video play karta hai
- Ek thread user input handle karta hai
🔹 1.8 Benefits of Threads
- Responsiveness
- Resource sharing
- Economy (Low cost creation)
- Better CPU utilization
- Faster execution
🔹 1.9 Types of Threads
1️⃣ User-Level Threads
- User space mein manage hote hain
- Fast but limited
2️⃣ Kernel-Level Threads
- OS kernel manage karta hai
- More powerful but slower
🕒 2. Process Scheduling
🔹 2.1 Definition
Process Scheduling ek technique hai jisme OS decide karta hai kaunsa process CPU use karega.
🔹 2.2 Scheduling Objectives
- Maximum CPU Utilization
- Maximum Throughput
- Minimum Turnaround Time
- Minimum Waiting Time
- Minimum Response Time
- Fairness
🔹 2.3 Scheduling Algorithms
CPU scheduling do type ka hota hai:
⚡ 2.4 Preemptive Scheduling
CPU kisi process se forcefully le sakte hain.
Example:
- Round Robin
- Shortest Remaining Time First
🕰️ 2.5 Non-Preemptive Scheduling
Process complete hone tak CPU nahi chodega.
Example:
- FCFS
- SJF (Non-preemptive)
🔹 2.6 Important CPU Scheduling Algorithms
🟢 1. FCFS (First Come First Serve)
- Jo pehle aata hai woh pehle execute hota hai
- Simple but waiting time zyada ho sakta hai
🟢 2. SJF (Shortest Job First)
- Sabse chota burst time wala process pehle
- Minimum average waiting time
Types:
- Preemptive (SRTF)
- Non-preemptive
🟢 3. Round Robin (RR)
- Har process ko fixed time slice (Quantum) milta hai
- Time sharing systems mein use hota hai
📊 2.7 Performance Evaluation
Scheduling algorithms ko evaluate karte hain:
- CPU Utilization
- Throughput
- Turnaround Time
Completion Time – Arrival Time
- Waiting Time
Turnaround Time – Burst Time
- Response Time
First Response – Arrival Time
📌 Important Differences
| FCFS | SJF | Round Robin |
|---|---|---|
| Simple | Fastest average | Fair for all |
| Long wait possible | Need burst time | Time quantum needed |
| Non-preemptive | Both types | Preemptive |
🎯 Exam Important Questions
- Define Process and explain its states.
- Explain PCB with diagram.
- What is thread? Explain types.
- Difference between Process and Thread.
- Explain FCFS, SJF and Round Robin.
- Difference between Preemptive and Non-preemptive scheduling.
- What are scheduling criteria?
📘 Unit III
Process Synchronization
(BCA 2nd Semester)
Operating Systems | CC-V
🔐 1. Introduction to Process Synchronization
Jab multiple processes ya threads ek hi shared resource (memory, file, variable) use karte hain, to problem create ho sakti hai.
👉 Isi problem ko solve karne ke liye Process Synchronization use hota hai.
📌 Definition (Exam Ready):
Process Synchronization is a technique used to coordinate the execution of multiple processes to ensure data consistency and avoid conflicts.
🔄 2. Inter-Process Communication (IPC)
IPC ka matlab hai processes ke beech communication.
IPC ke Types:
1️⃣ Shared Memory
Multiple processes ek hi memory area share karte hain
2️⃣ Message Passing
Processes messages send/receive karte hain
📌 Example:
- Pipes
- Message Queues
- Sockets
⚠️ 3. Race Condition
📌 Race Condition kya hoti hai?
Jab do ya zyada processes ek hi data ko same time par access karte hain aur final result unpredictable ho jata hai.
Example:
Agar do processes same variable count ko increment karein bina control ke, to wrong value aa sakti hai.
🚪 4. Critical Section Problem
🔹 Critical Section kya hota hai?
Program ka woh part jahan shared resource access hota hai.
Structure:
Critical Section
Exit Section
Remainder Section
🎯 Conditions for Solution:
- Mutual Exclusion
- Progress
- Bounded Waiting
🔒 5. Mutual Exclusion
Ek time par sirf ek process critical section mein enter kare — isi ko mutual exclusion kehte hain.
🔑 6. Semaphores
Semaphore ek synchronization tool hai.
📌 Definition:
A semaphore is a synchronization variable used to control access to shared resources.
Types of Semaphores:
1️⃣ Binary Semaphore (0 ya 1)
2️⃣ Counting Semaphore
Operations:
wait()(P operation)signal()(V operation)
⚙️ 7. Problems of Synchronization
- Producer–Consumer Problem
- Readers–Writers Problem
- Dining Philosophers Problem
📖 8. Readers and Writers Problem
Problem:
- Multiple readers ek file read kar sakte hain
- Lekin jab writer likh raha ho to koi aur access na kare
Solution:
- Use semaphores
- Ensure writer ko exclusive access mile
🍽️ 9. Dining Philosophers Problem
5 philosophers table par baithe hain. Har philosopher ko khane ke liye 2 forks chahiye.
Problem:
Deadlock ho sakta hai agar sab ek fork utha lein.
Solution:
- Semaphores use karna
- Resource hierarchy follow karna
🏛️ 10. Monitors
Monitor ek high-level synchronization construct hai.
Features:
- Mutual exclusion automatically provide karta hai
- Condition variables use karta hai
💀 11. Deadlocks
🔹 Deadlock kya hota hai?
Jab 2 ya zyada processes ek dusre ke resources ka wait karte rahte hain aur kabhi complete nahi hote.
📌 Definition:
A deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource held by another process.
🔎 12. System Model
Deadlock tab hota hai jab:
- Resources limited ho
- Processes resource request karein
- Resource allocation cycle ban jaye
🧩 13. Deadlock Characterization
Deadlock ke 4 necessary conditions (Coffman Conditions):
1️⃣ Mutual Exclusion
2️⃣ Hold and Wait
3️⃣ No Preemption
4️⃣ Circular Wait
Agar ye 4 conditions simultaneously true ho, to deadlock possible hai.
🚫 14. Deadlock Prevention
Deadlock ko prevent karne ke liye in conditions ko break karte hain.
Example:
- Hold and wait remove karna
- Circular wait avoid karna
🛑 15. Deadlock Avoidance
System safe state maintain karta hai.
Safe State:
Aisa state jahan se system safe sequence mein execute ho sake.
🏦 16. Banker’s Algorithm
Deadlock avoidance ke liye use hota hai.
Idea:
System pehle check karega ki resource allocate karne ke baad system safe state mein rahega ya nahi.
Steps:
- Available resources check karo
- Need matrix calculate karo
- Safe sequence find karo
🔍 17. Deadlock Detection
System periodically check karta hai ki deadlock hua hai ya nahi.
Technique:
- Resource Allocation Graph
- Wait-for Graph
🔄 18. Recovery from Deadlock
1️⃣ Process Termination
2️⃣ Resource Preemption
3️⃣ Rollback
📌 Important Differences
| Prevention | Avoidance | Detection |
|---|---|---|
| Condition break | Safe state check | After deadlock detect |
| Simple logic | Banker’s algorithm | Recovery required |
| Less flexible | Flexible | Complex |
🎯 Important Exam Questions
- Define Race Condition and Critical Section Problem.
- Explain Semaphores with types.
- Explain Readers-Writers problem.
- Explain Dining Philosophers problem.
- Define Deadlock and explain its conditions.
- Explain Banker’s Algorithm.
- Difference between Deadlock Prevention and Avoidance.
📘 Unit IV
Memory Management
(BCA 2nd Semester)
Operating Systems | CC-V
🧠 1. Memory Management
Memory Management OS ka important function hai jo main memory (RAM) ko efficiently manage karta hai.
🔹 1.1 Logical and Physical Address Map
📌 Logical Address
- CPU generate karta hai
- Virtual address bhi kehte hain
📌 Physical Address
Actual RAM location
👉 Logical → Physical mapping Memory Management Unit (MMU) karta hai.
Diagram:
🔹 1.2 Swapping
Swapping technique mein process ko temporarily memory se disk par shift kar diya jata hai.
👉 Jab memory free hoti hai tab process wapas RAM mein aata hai.
📌 Advantage: Memory utilization improve hota hai
📌 Disadvantage: Slow (Disk access time high hota hai)
🏗️ 2. Memory Allocation
🔹 2.1 MFT (Multiprogramming with Fixed Tasks)
- Memory fixed size partitions mein divide hoti hai
- Har partition mein ek process
📌 Problem: Internal Fragmentation
🔹 2.2 MVT (Multiprogramming with Variable Tasks)
- Memory dynamic partitions mein divide hoti hai
- Process ke size ke according allocate hoti hai
📌 Problem: External Fragmentation
🔹 2.3 Fragmentation
🟢 Internal Fragmentation
Allocated memory ke andar unused space
🔵 External Fragmentation
Free memory scattered ho jati hai
🔹 2.4 Compaction
External fragmentation ko remove karne ke liye processes ko shift karke memory ko contiguous banaya jata hai.
📄 3. Paging
Paging mein memory ko equal-size blocks mein divide kiya jata hai.
- Logical memory → Pages
- Physical memory → Frames
👉 Page Table mapping karta hai page number ko frame number se.
Advantage:
No external fragmentation
Disadvantage:
Internal fragmentation possible
📚 4. Segmentation
Segmentation logical divisions par based hota hai:
Example:
- Code Segment
- Data Segment
- Stack Segment
👉 Har segment ka alag base address aur limit hota hai.
💻 5. Virtual Memory
Virtual Memory allow karta hai large programs ko execute karna even if RAM small ho.
🔹 5.1 Demand Paging
Page tabhi memory mein load hota hai jab uski zarurat ho.
Agar required page memory mein na ho to Page Fault hota hai.
🔹 5.2 Page Replacement Algorithms
Jab memory full ho jaye to kisi page ko remove karna padta hai.
Important Algorithms:
1️⃣ FIFO (First In First Out)
2️⃣ LRU (Least Recently Used)
3️⃣ Optimal Algorithm
4️⃣ Second Chance
🔹 5.3 Allocation of Frames
Frames allocate karne ke methods:
- Equal Allocation
- Proportional Allocation
- Priority Allocation
🔥 5.4 Thrashing
Jab system zyada time page swapping mein waste kare aur performance drop ho jaye.
👉 Cause: Too many processes
Solution:
- Reduce multiprogramming level
- Use proper page replacement
💾 6. I/O Management
🔹 6.1 Principles of I/O Hardware
I/O devices 3 types ke hote hain:
- Block Devices (Disk)
- Character Devices (Keyboard)
- Network Devices
🔹 6.2 Disk Structure
Disk mein hota hai:
- Platter
- Tracks
- Sectors
- Cylinder
🔹 6.3 Disk Scheduling Algorithms
1️⃣ FCFS
2️⃣ SSTF (Shortest Seek Time First)
3️⃣ SCAN
4️⃣ C-SCAN
5️⃣ LOOK
📌 Objective: Seek time reduce karna
📂 7. File System Interface
🔹 7.1 File Concept
File = Collection of related information stored on disk.
File attributes:
- Name
- Size
- Type
- Location
- Protection
🔹 7.2 Access Methods
1️⃣ Sequential Access
2️⃣ Direct Access
3️⃣ Indexed Access
🔹 7.3 Directory Structure
- Single-Level Directory
- Two-Level Directory
- Tree Structure
- Acyclic Graph
🔹 7.4 File System Structure
- Boot Control Block
- Volume Control Block
- Directory Structure
- File Control Block
🔹 7.5 Allocation Methods
1️⃣ Contiguous Allocation
2️⃣ Linked Allocation
3️⃣ Indexed Allocation
🔹 7.6 Free-Space Management
- Bit Vector
- Linked List
- Grouping
- Counting
🔐 8. System Protection
🔹 8.1 Goals
- Unauthorized access rokna
- Data integrity maintain karna
- Resource protection
🔹 8.2 Principles
- Least Privilege
- Separation of Privilege
- Fail-Safe Defaults
🔹 8.3 Domain of Protection
Domain define karta hai:
- Kaun kaunsa resource access kar sakta hai
🔹 8.4 Access Matrix
Rows = Domains
Columns = Objects
Matrix batata hai kis domain ko kis object par kya permission hai.
🔹 8.5 Access Control
Types:
1️⃣ Discretionary Access Control (DAC)
2️⃣ Mandatory Access Control (MAC)
3️⃣ Role-Based Access Control (RBAC)
🎯 Important Exam Questions
- Difference between Paging and Segmentation.
- Explain Internal and External Fragmentation.
- What is Virtual Memory?
- Explain Page Replacement Algorithms.
- What is Thrashing?
- Explain Disk Scheduling Algorithms.
- Explain File Allocation Methods.
- What is Access Matrix?
📘 Lab Programs
(BCA 2nd Semester)
Operating Systems | CC-V
Write a C program to simulate the FCFS CPU Scheduling algorithm.
#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);
for(i = 0; i < n; i++) {
printf(“Enter Burst Time for 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];
}
printf(“\nProcess\tBT\tWT\tTAT\n”);
for(i = 0; i < n; i++)
printf(“P%d\t%d\t%d\t%d\n”, i+1, bt[i], wt[i], tat[i]);
printf(“\nAverage Waiting Time = %.2f”, avg_wt/n);
printf(“\nAverage Turnaround Time = %.2f”, avg_tat/n);
return 0;
}
Write a C program to simulate the SJF CPU Scheduling algorithm.
#include <stdio.h>
int main() {
int bt[20], wt[20], tat[20], n, i, j, temp;
float avg_wt = 0, avg_tat = 0;
printf(“Enter number of processes: “);
scanf(“%d”, &n);
for(i = 0; i < n; i++) {
printf(“Enter Burst Time for P%d: “, i+1);
scanf(“%d”, &bt[i]);
}
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
if(bt[i] > bt[j]) {
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
}
}
}
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];
}
printf(“\nProcess\tBT\tWT\tTAT\n”);
for(i = 0; i < n; i++)
printf(“P%d\t%d\t%d\t%d\n”, i+1, bt[i], wt[i], tat[i]);
printf(“\nAverage Waiting Time = %.2f”, avg_wt/n);
printf(“\nAverage Turnaround Time = %.2f”, avg_tat/n);
return 0;
}
Write a C program to simulate the Round Robin CPU Scheduling algorithm.
#include <stdio.h>
int main() {
int n, tq, i, time = 0, remain;
int bt[20], rt[20], wt[20], tat[20];
printf(“Enter number of processes: “);
scanf(“%d”, &n);
for(i = 0; i < n; i++) {
printf(“Enter Burst Time for P%d: “, i+1);
scanf(“%d”, &bt[i]);
rt[i] = bt[i];
}
printf(“Enter Time Quantum: “);
scanf(“%d”, &tq);
remain = n;
while(remain > 0) {
for(i = 0; i < n; i++) {
if(rt[i] > 0) {
if(rt[i] <= tq) {
time += rt[i];
wt[i] = time – bt[i];
rt[i] = 0;
remain–;
} else {
time += tq;
rt[i] -= tq;
}
}
}
}
for(i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
printf(“\nProcess\tBT\tWT\tTAT\n”);
for(i = 0; i < n; i++)
printf(“P%d\t%d\t%d\t%d\n”, i+1, bt[i], wt[i], tat[i]);
return 0;
}
Write a C program to simulate Bankers Algorithm for Deadlock Avoidance.
#include <stdio.h>
int main() {
int n, m, i, j, k;
printf(“Enter number of processes and resources: “);
scanf(“%d%d”, &n, &m);
int alloc[n][m], max[n][m], avail[m], need[n][m], finish[n];
printf(“Enter Allocation Matrix:\n”);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf(“%d”,&alloc[i][j]);
printf(“Enter Max Matrix:\n”);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf(“%d”,&max[i][j]);
printf(“Enter Available Resources:\n”);
for(i=0;i<m;i++)
scanf(“%d”,&avail[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j] = max[i][j] – alloc[i][j];
for(i=0;i<n;i++) finish[i]=0;
int count=0;
while(count<n) {
for(i=0;i<n;i++) {
if(!finish[i]) {
int flag=0;
for(j=0;j<m;j++)
if(need[i][j] > avail[j]) {
flag=1;
break;
}
if(flag==0) {
for(k=0;k<m;k++)
avail[k]+=alloc[i][k];
finish[i]=1;
count++;
printf(“P%d “,i);
}
}
}
}
return 0;
}
Write a C program to implement the Producer–Consumer problem using semaphores.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define SIZE 5
int buffer[SIZE];
int in = 0, out = 0;
sem_t empty, full;
pthread_mutex_t mutex;
void *producer(void *arg) {
int item;
for(int i=0;i<10;i++) {
item = rand()%100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
printf(“Produced: %d\n”, item);
in = (in+1)%SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
}
void *consumer(void *arg) {
int item;
for(int i=0;i<10;i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
printf(“Consumed: %d\n”, item);
out = (out+1)%SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(1);
}
}
int main() {
pthread_t p, c;
sem_init(&empty,0,SIZE);
sem_init(&full,0,0);
pthread_mutex_init(&mutex,NULL);
pthread_create(&p,NULL,producer,NULL);
pthread_create(&c,NULL,consumer,NULL);
pthread_join(p,NULL);
pthread_join(c,NULL);
return 0;
}
Write a C program to illustrate the IPC mechanism using Pipes.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd[2];
char msg[] = “Hello from Parent”;
char buffer[50];
pipe(fd);
if(fork()==0) {
read(fd[0], buffer, sizeof(buffer));
printf(“Child received: %s\n”, buffer);
} else {
write(fd[1], msg, strlen(msg)+1);
}
return 0;
}
Write a C program to illustrate the IPC mechanism using FIFOs.
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
char *fifo = “myfifo”;
mkfifo(fifo, 0666);
int fd = open(fifo, O_WRONLY);
write(fd, “Hello FIFO”, 11);
close(fd);
return 0;
}
Write a C program to simulate Paging memory management technique.
#include <stdio.h>
int main() {
int pages, frames;
printf(“Enter number of pages: “);
scanf(“%d”,&pages);
printf(“Enter number of frames: “);
scanf(“%d”,&frames);
printf(“Paging possible without external fragmentation.\n”);
return 0;
}
Write a C program to simulate Segmentation memory management technique.
#include <stdio.h>
int main() {
int base, limit, logical;
printf(“Enter Base Address: “);
scanf(“%d”,&base);
printf(“Enter Limit: “);
scanf(“%d”,&limit);
printf(“Enter Logical Address: “);
scanf(“%d”,&logical);
if(logical < limit)
printf(“Physical Address = %d\n”, base + logical);
else
printf(“Segmentation Fault!\n”);
return 0;
}
Write a C program to simulate the Best Fit contiguous memory allocation technique.
#include <stdio.h>
int main() {
int block[10], process[10], i, j, nb, np, min;
printf(“Enter number of blocks: “);
scanf(“%d”,&nb);
printf(“Enter number of processes: “);
scanf(“%d”,&np);
for(i=0;i<nb;i++)
scanf(“%d”,&block[i]);
for(i=0;i<np;i++)
scanf(“%d”,&process[i]);
for(i=0;i<np;i++) {
min = -1;
for(j=0;j<nb;j++) {
if(block[j] >= process[i]) {
if(min==-1 || block[j] < block[min])
min = j;
}
}
if(min!=-1) {
printf(“Process %d allocated to block %d\n”, i+1, min+1);
block[min] -= process[i];
}
}
return 0;
}
Write a C program to simulate the First Fit contiguous memory allocation technique.
#include <stdio.h>
int main() {
int block[10], process[10], i, j, nb, np;
printf(“Enter number of blocks: “);
scanf(“%d”,&nb);
printf(“Enter number of processes: “);
scanf(“%d”,&np);
for(i=0;i<nb;i++)
scanf(“%d”,&block[i]);
for(i=0;i<np;i++)
scanf(“%d”,&process[i]);
for(i=0;i<np;i++) {
for(j=0;j<nb;j++) {
if(block[j] >= process[i]) {
printf(“Process %d allocated to block %d\n”, i+1, j+1);
block[j] -= process[i];
break;
}
}
}
return 0;
}
Write a C program to simulate the concept of Dining-Philosophers problem.
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define N 5
sem_t chopstick[N];
void *philosopher(void *num) {
int id = *(int*)num;
while(1) {
printf(“Philosopher %d thinking\n”, id);
sleep(1);
sem_wait(&chopstick[id]);
sem_wait(&chopstick[(id+1)%N]);
printf(“Philosopher %d eating\n”, id);
sleep(1);
sem_post(&chopstick[id]);
sem_post(&chopstick[(id+1)%N]);
}
}
int main() {
pthread_t p[N];
int i, id[N];
for(i=0;i<N;i++)
sem_init(&chopstick[i],0,1);
for(i=0;i<N;i++) {
id[i]=i;
pthread_create(&p[i],NULL,philosopher,&id[i]);
}
for(i=0;i<N;i++)
pthread_join(p[i],NULL);
return 0;
}
Write a C program to simulate the MVT algorithm.
#include <stdio.h>
int main() {
int total, process;
printf(“Enter total memory: “);
scanf(“%d”,&total);
printf(“Enter process size: “);
scanf(“%d”,&process);
if(process <= total)
printf(“Memory allocated dynamically.\n”);
else
printf(“Not enough memory.\n”);
return 0;
}
Write a C program to implement FIFO page replacement technique.
#include <stdio.h>
int main() {
int pages[50], frame[10], n, f, i, j, k=0, fault=0;
printf(“Enter number of pages: “);
scanf(“%d”,&n);
printf(“Enter page reference string: “);
for(i=0;i<n;i++)
scanf(“%d”,&pages[i]);
printf(“Enter number of frames: “);
scanf(“%d”,&f);
for(i=0;i<f;i++)
frame[i] = -1;
for(i=0;i<n;i++) {
int found=0;
for(j=0;j<f;j++)
if(frame[j]==pages[i])
found=1;
if(!found) {
frame[k] = pages[i];
k=(k+1)%f;
fault++;
}
}
printf(“Total Page Faults = %d\n”, fault);
return 0;
}
Write a C program for implementing sequential file allocation method
#include <stdio.h>
int main() {
int start, length;
printf(“Enter starting block: “);
scanf(“%d”,&start);
printf(“Enter file length: “);
scanf(“%d”,&length);
printf(“Allocated blocks: “);
for(int i=0;i<length;i++)
printf(“%d “, start+i);
return 0;
}
📘 Operating System
Lab Viva
Questions & Answers
(BCA 2nd Semester)
Operating Systems | CC-V
🔹 CPU Scheduling Viva
1️⃣ What is CPU Scheduling?
CPU scheduling ek process selection technique hai jisme OS decide karta hai kaunsa process next CPU use karega.
2️⃣ What is FCFS?
FCFS (First Come First Serve) mein jo process pehle aata hai woh pehle execute hota hai.
3️⃣ What is SJF?
SJF (Shortest Job First) mein sabse chhota burst time wala process pehle execute hota hai.
4️⃣ What is Round Robin?
Round Robin mein har process ko fixed time quantum diya jata hai.
5️⃣ What is Time Quantum?
Time Quantum ek fixed CPU time slice hai jo Round Robin mein har process ko milta hai.
6️⃣ Difference between Preemptive and Non-Preemptive Scheduling?
| Preemptive | Non-Preemptive |
|---|---|
| CPU forcibly le sakte hain | Process khud CPU chodega |
| Example: RR | Example: FCFS |
🔹 Deadlock & Banker’s Algorithm Viva
7️⃣ What is Deadlock?
Deadlock ek situation hai jahan processes ek dusre ke resources ka wait karte rehte hain aur execution stop ho jata hai.
8️⃣ What are the 4 conditions of Deadlock?
Mutual Exclusion
Hold and Wait
No Preemption
Circular Wait
9️⃣ What is Banker’s Algorithm?
Banker’s Algorithm deadlock avoidance technique hai jo safe state check karta hai resource allocation se pehle.
🔟 What is Safe State?
Safe state wo state hai jahan system safe sequence mein execute ho sakta hai.
🔹 Process Synchronization Viva
1️⃣1️⃣ What is Critical Section?
Program ka woh part jahan shared resource access hota hai.
1️⃣2️⃣ What is Race Condition?
Jab multiple processes same data ko access karein aur unpredictable result aaye.
1️⃣3️⃣ What is Semaphore?
Semaphore ek synchronization variable hai jo shared resource ko control karta hai.
1️⃣4️⃣ Types of Semaphore?
Binary Semaphore
Counting Semaphore
1️⃣5️⃣ What is Producer-Consumer Problem?
Ek problem jahan producer data produce karta hai aur consumer consume karta hai — synchronization required hota hai.
1️⃣6️⃣ What is Dining Philosophers Problem?
Classic synchronization problem jahan philosophers forks share karte hain aur deadlock ho sakta hai.
🔹 Memory Management Viva
1️⃣7️⃣ What is Paging?
Memory ko fixed size pages aur frames mein divide karna paging kehlata hai.
1️⃣8️⃣ What is Segmentation?
Memory ko logical segments (code, data, stack) mein divide karna segmentation kehlata hai.
1️⃣9️⃣ Difference between Paging and Segmentation?
| Paging | Segmentation |
|---|---|
| Fixed size | Variable size |
| No external fragmentation | External fragmentation possible |
2️⃣0️⃣ What is Fragmentation?
Unused memory space ko fragmentation kehte hain.
2️⃣1️⃣ Types of Fragmentation?
Internal
External
2️⃣2️⃣ What is Thrashing?
Jab system zyada time page swapping mein waste kare aur performance slow ho jaye.
🔹 Disk Scheduling Viva
2️⃣3️⃣ What is Disk Scheduling?
Disk requests ko optimize karne ki technique disk scheduling kehlati hai.
2️⃣4️⃣ Name some Disk Scheduling Algorithms.
- FCFS
- SSTF
- SCAN
- C-SCAN
🔹 File System Viva
2️⃣5️⃣ What is File Allocation Method?
Technique jisse disk blocks file ko assign kiye jate hain.
2️⃣6️⃣ Types of File Allocation?
Contiguous
Linked
Indexed
2️⃣7️⃣ What is Sequential File Allocation?
Blocks sequential order mein allocate hote hain.
🔹 IPC Viva
2️⃣8️⃣ What is IPC?
Inter Process Communication — processes ke beech communication technique.
2️⃣9️⃣ Types of IPC?
Shared Memory
Message Passing
3️⃣0️⃣ What is Pipe?
Pipe ek IPC mechanism hai jo parent-child process communication ke liye use hota hai.
🎯 Most Important Viva Questions (Teacher Favorite)
- What is difference between Process and Thread?
- What is PCB?
- What is Page Fault?
- What is Safe Sequence?
- What is Mutual Exclusion?
