PROGRAM-5
5. Control the number of ports opened by the operating system with
a) Semaphore b) Monitors.
a. SEMAPHORE
AIM: Control the number of ports opened by the operating system with Semaphore
DESCRIPTION:
A semaphore is a synchronization mechanism used to control access to a common resource by multiple
processes in a concurrent system such as an operating system. Semaphores are used to avoid race conditions
and ensure mutual exclusion.
Semaphores are widely used in operating systems for:
Managing access to shared resources like files or database connections.
Implementing process synchronization (e.g., producer-consumer problem).
Controlling the number of instances of a resource that can be used concurrently
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_PORTS 5 // Maximum number of ports that can be opened
sem_t portSemaphore;
void* openPort(void* portNum) {
int port = *((int*)portNum);
// Wait to acquire a semaphore
sem_wait(&portSemaphore);
printf("Port %d opened.\n", port);
// Simulate port activity
sleep(2);
printf("Port %d closed.\n", port);
// Release the semaphore
sem_post(&portSemaphore);
free(portNum);
pthread_exit(NULL);
}
int main() {
pthread_t threads[10];
sem_init(&portSemaphore, 0, MAX_PORTS); // Initialize semaphore with MAX_PORTS
for (int i = 0; i < 10; i++) {
int* portNum = (int*)malloc(sizeof(int));
*portNum = i;
if (pthread_create(&threads[i], NULL, openPort, (void*)portNum) != 0) {
perror("Failed to create thread");
}
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
sem_destroy(&portSemaphore); // Destroy the semaphore
return 0;
}
OUTPUT
Port 0 opened.
Port 1 opened.
Port 2 opened.
Port 3 opened.
Port 4 opened.
Port 1 closed.
Port 4 closed.
Port 2 closed.
Port 0 closed.
Port 8 opened.
Port 3 closed.
Port 5 opened.
Port 6 opened.
Port 7 opened.
Port 9 opened.
Port 9 closed.
Port 6 closed.
Port 7 closed.
Port 8 closed.
Port 5 closed.
b. MONITORS
AIM: Control the number of ports opened by the operating system with Monitors.
DESCRIPTION
Monitors are a high-level synchronization construct used in operating systems to manage concurrent processes
and ensure safe access to shared resources. They provide a convenient and structured way to achieve mutual
exclusion and condition synchronization.
Mutual Exclusion:
Ensures that only one process can execute a critical section of code at a time. This prevents race conditions and
ensures data integrity.
Condition Variables:
Used to allow processes to wait for certain conditions to be met before they proceed. Condition variables are
associated with monitors and are used for signalling.
Monitor Structure:
A monitor is typically implemented as a class or module that encapsulates shared resources, along with methods
to access and manipulate those resources. Mutual exclusion is enforced automatically for the methods within
the monitor.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_PORTS 5 // Maximum number of ports that can be opened
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int available_ports = MAX_PORTS;
void* openPort(void* portNum) {
int port = *((int*)portNum);
pthread_mutex_lock(&mutex);
while (available_ports == 0) {
pthread_cond_wait(&cond, &mutex);
}
available_ports--;
printf("Port %d opened.\n", port);
pthread_mutex_unlock(&mutex);
// Simulate port activity
sleep(2);
pthread_mutex_lock(&mutex);
available_ports++;
printf("Port %d closed.\n", port);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
free(portNum);
pthread_exit(NULL);
}
int main() {
pthread_t threads[10];
for (int i = 0; i < 10; i++) {
int* portNum = (int*)malloc(sizeof(int));
*portNum = i;
pthread_create(&threads[i], NULL, openPort, (void*)portNum);
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
OUTPUT
Port 0 opened.
Port 1 opened.
Port 2 opened.
Port 3 opened.
Port 4 opened.
Port 1 closed.
Port 4 closed.
Port 2 closed.
Port 0 closed.
Port 8 opened.
Port 3 closed.
Port 5 opened.
Port 6 opened.
Port 7 opened.
Port 9 opened.
Port 9 closed.
Port 6 closed.
Port 7 closed.
Port 8 closed.
Port 5 closed.