5).
Control the number of ports opened by the operating system with (a)Semaphore(b)Monitors
b)Aim: Control the number of ports opened by the operating system with Monitors
Description:
Controlling the number of ports opened by the operating system typically involves using system-specific
APIs or configuration settings. In this example, uses monitors to control the number of concurrent
connections. we'll use POSIX threads(limited to MAX_PORTS) and monitors to manage a pool of
available ports. When a client requests a port, it will wait if all ports are currently in use.
i) When a client thread wants to use a port, it checks if there are any available.
ii) If no ports are available, it waits for a signal from other threads when a port becomes
available.
iii) When a port is acquired and released, the client thread simulates some work and then
releases the port.
Additionally, modern network servers often use asynchronous I/O or multithreading techniques to
efficiently manage many incoming connections.
Program:
#include <stdio.h>
#include <pthread.h>
#define MAX_PORTS 5
pthread_mutex_t mutex;
pthread_cond_t port_available;
int ports_in_use = 0;
void* client_thread(void* arg) {
int client_id = *(int*)arg;
while (1) {
pthread_mutex_lock(&mutex);
if (ports_in_use < MAX_PORTS) {
printf("Client %d is using a port.\n", client_id);
ports_in_use++;
pthread_mutex_unlock(&mutex);
// Simulate client work
sleep(2);
pthread_mutex_lock(&mutex);
ports_in_use--;
printf("Client %d released the port.\n", client_id);
pthread_cond_signal(&port_available);
pthread_mutex_unlock(&mutex);
// Simulate client disconnecting
sleep(1);
} else {
printf("Client %d is waiting for an available port.\n", client_id);
pthread_cond_wait(&port_available, &mutex);
}
}
return NULL;
}
int main() {
pthread_t clients[MAX_PORTS];
int client_ids[MAX_PORTS];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&port_available, NULL);
for (int i = 0; i < MAX_PORTS; i++) {
client_ids[i] = i + 1;
pthread_create(&clients[i], NULL, client_thread, &client_ids[i]);
}
for (int i = 0; i < MAX_PORTS; i++) {
pthread_join(clients[i], NULL);
}
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&port_available);
return 0;
}
output
Client 1 is using a port.
Client 2 is using a port.
Client 4 is using a port.
Client 5 is using a port.
Client 3 is using a port.
Client 1 released the port.
Client 2 released the port.
Client 4 released the port.
Client 3 released the port.
Client 5 released the port.
Client 1 is using a port.
Client 2 is using a port.
Client 5 is using a port.
Client 3 is using a port.
Client 4 is using a port.
Client 1 released the port.
Client 5 released the port.
Client 2 released the port.
Client 4 released the port.
Client 3 released the port.
Client 1 is using a port.
Client 5 is using a port.
Client 2 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 1 released the port.
Client 5 released the port.
Client 4 released the port.
Client 3 released the port.
Client 2 released the port.
Client 1 is using a port.
Client 5 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 2 is using a port.
Client 5 released the port.
Client 1 released the port.
Client 4 released the port.
Client 2 released the port.
Client 3 released the port.
Client 5 is using a port.
Client 1 is using a port.
Client 2 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 5 released the port.
Client 1 released the port.
Client 2 released the port.
Client 3 released the port.
Client 4 released the port.
Client 5 is using a port.
Client 4 is using a port.
Client 1 is using a port.
Client 3 is using a port.
Client 2 is using a port.
Client 5 released the port.
Client 4 released the port.
Client 1 released the port.
Client 3 released the port.
Client 2 released the port.
Client 5 is using a port.
Client 4 is using a port.
Client 1 is using a port.
Client 3 is using a port.
Client 2 is using a port.
Client 5 released the port.
Client 4 released the port.
Client 1 released the port.
Client 2 released the port.
Client 3 released the port.
Client 5 is using a port.
Client 2 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 1 is using a port.
Client 5 released the port.
Client 2 released the port.
Client 4 released the port.
Client 3 released the port.
Client 1 released the port.
Client 5 is using a port.
Client 2 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 1 is using a port.
Client 5 released the port.
Client 4 released the port.
Client 3 released the port.
Client 2 released the port.
Client 1 released the port.
Client 5 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 2 is using a port.
Client 1 is using a port.
Client 5 released the port.
Client 4 released the port.
Client 3 released the port.
Client 2 released the port.
Client 1 released the port.
Client 5 is using a port.
Client 4 is using a port.
Client 3 is using a port.
Client 2 is using a port.
Client 1 is using a port.
Client 5 released the port.
Client 4 released the port.
Client 3 released the port.
Client 2 released the port.
Client 1 released the port.
Client 5 is using a port.
Result