Achieving Multiprocessing in Operating
System
1. What is Multiprocessing?
Multiprocessing is the ability of an operating system to execute multiple processes
simultaneously using more than one CPU core.
If a system has:
● 1 CPU → multitasking (time-sharing)
● 2 or more CPUs/Cores → multiprocessing (true parallelism)
2. Why Multiprocessing is Needed
● Faster execution
● Better CPU utilization
● Parallel task execution
● Increased system reliability
● Improved system throughput
3. How Operating System Achieves Multiprocessing
An OS achieves multiprocessing using:
(a) Process Creation
● Using system calls like fork()
● Each process has its own PID
(b) CPU Scheduling
● Scheduler assigns processes to different CPUs
● Uses algorithms like Round Robin, Priority, etc.
(c) Inter-Process Communication (IPC)
● Pipes
● Shared Memory
● Message Queues
(d) Synchronization
● Semaphores
● Mutex
● Locks
4. Types of Multiprocessing
Type Description
Symmetric Multiprocessing (SMP) All CPUs are equal
Asymmetric Multiprocessing (AMP) One master CPU controls others
Linux uses SMP.
LAB WORK 1
Multiprocessing using fork() (Process Creation)
Objective
To create multiple processes and observe parallel execution.
Concept
● fork() creates a child process
● Parent and child run simultaneously
● OS schedules them on different CPU cores
Program (C Language)
#include <stdio.h>
#include <unistd.h>
int main() {
fork(); // create child process
fork(); // create another process
printf("Process ID: %d\n", getpid());
return 0;
}
Steps to Run (Linux / Ubuntu)
gcc fork.c -o fork
./fork
Explanation
● First fork() → 2 processes
● Second fork() → 4 processes
● Each process prints its PID
● OS schedules them on different cores
Output (Sample)
Process ID: 3451
Process ID: 3452
Process ID: 3453
Process ID: 3454
Conclusion
This shows multiprocessing through process creation.
LAB WORK 2
Multiprocessing using Multiple Programs (Parallel
Execution)
Objective
To demonstrate true parallel execution using independent processes.
Concept
Each program runs independently → OS executes them simultaneously.
Program 1: task1.c
#include <stdio.h>
#include <unistd.h>
int main() {
for(int i=1; i<=5; i++) {
printf("Task 1 running: %d\n", i);
sleep(1);
}
return 0;
}
Program 2: task2.c
#include <stdio.h>
#include <unistd.h>
int main() {
for(int i=1; i<=5; i++) {
printf("Task 2 running: %d\n", i);
sleep(1);
}
return 0;
}
🛠 Steps
gcc task1.c -o task1
gcc task2.c -o task2
./task1 & ./task2
(& runs programs in background)
Output (Interleaved)
Task 1 running: 1
Task 2 running: 1
Task 1 running: 2
Task 2 running: 2
...
Conclusion
● Programs run in parallel
● OS assigns them to different CPUs
● This demonstrates multiprocessing
LAB WORK 3
Multiprocessing using IPC (Shared Memory)
Objective
To achieve multiprocessing with inter-process communication.
Concept
● Multiple processes access shared memory
● Used in producer-consumer problems
Program
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main() {
int shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
int *data = (int*) shmat(shmid, NULL, 0);
if (fork() == 0) {
*data = 100;
printf("Child wrote: %d\n", *data);
} else {
sleep(1);
printf("Parent read: %d\n", *data);
}
return 0;
}
Steps
gcc shm.c -o shm
./shm
Output
Child wrote: 100
Parent read: 100
Conclusion
● Parent & child run on different CPUs
● Data is shared
● Shows multiprocessing + IPC
5. Final Comparison of Lab Works
Lab Technique Purpose
Lab 1 fork() Process creation
Lab 2 Parallel programs True multiprocessing
Lab 3 Shared Memory IPC in multiprocessing
6. Final Summary
● Multiprocessing allows parallel execution
● OS achieves it using process creation, scheduling, IPC, synchronization
● Linux supports SMP
● Practical labs help understand real OS behavior
Problem Statement
Implement Producer–Consumer problem using:
● Shared Memory
● Semaphores
to achieve safe multiprocessing.
Working
● Producer process writes data into shared memory
● Consumer process reads data from shared memory
● Semaphore ensures:
○ Producer writes first
○ Consumer reads after writing
Program (C – Linux)
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <fcntl.h>
int main() {
int shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
int *buffer = (int*) shmat(shmid, NULL, 0);
sem_t *sem = sem_open("/sem1", O_CREAT, 0644, 0);
if (fork() == 0) {
// Producer
for (int i = 1; i <= 5; i++) {
*buffer = i;
printf("Producer produced: %d\n", i);
sem_post(sem); // signal consumer
sleep(1);
} else {
// Consumer
for (int i = 1; i <= 5; i++) {
sem_wait(sem); // wait for producer
printf("Consumer consumed: %d\n", *buffer);
sem_close(sem);
sem_unlink("/sem1");
return 0;
Steps to Execute
gcc advanced.c -o advanced -pthread
./advanced
Sample Output
Producer produced: 1
Consumer consumed: 1
Producer produced: 2
Consumer consumed: 2
Producer produced: 3
Consumer consumed: 3
...
Why This Is an Advanced Program?
Feature Reason
fork() True multiprocessing
Shared Memory Fast IPC
Semaphores Synchronization
Producer–Consumer Real OS problem
Applications in Real OS
● CPU scheduling
● Database transactions
● Web servers
● Parallel computing systems