1 PDF
1 PDF
#include <queue>
#include <vector>
#include <climits>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
int number_of_processes;
struct Request {
int timestamp;
int process_id;
};
// Global variables
std::vector<int> timestamps;
std::vector<bool> waiting;
std::queue<Request> request_queue;
if (a.timestamp == b.timestamp)
cout << "Process " << process_id << " is requesting the resource with timestamp " << timestamp <<
endl;
request_queue.push({timestamp, process_id});
waiting[process_id] = true;
cv.wait(lock);
resource_available = false;
waiting[process_id] = false;
cout << "Process " << process_id << " is accessing the resource." << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "Process " << process_id << " has released the resource." << endl;
resource_available = true;
request_queue.pop();
cv.notify_all();
std::this_thread::sleep_for(std::chrono::seconds(2 + process_id));
requestResource(process_id);
int main() {
number_of_processes = 3; // You can modify this value
timestamps.resize(number_of_processes, 0);
waiting.resize(number_of_processes, false);
std::vector<std::thread> threads;
threads.push_back(std::thread(process, i));
th.join();
return 0;
}
Sample Output
Process 0 is requesting the resource with timestamp 1
#include <vector>
#include <algorithm>
return (a > b) ? a : b;
class Process {
public:
int process_id;
int clock;
// Constructor
Process(int id) {
process_id = id;
void event() {
clock++;
cout << "Event in Process " << process_id << " | Logical Clock: " << clock << endl;
cout << "Process " << process_id << " sent a message to Process " << receiver.process_id << " |
Clock: " << clock << endl;
receiver.receive_message(clock);
}
// Method to receive a message and update the clock
cout << "Process " << process_id << " received a message | Updated Logical Clock: " << clock <<
endl;
};
int main() {
// Create processes
Process p1(1);
Process p2(2);
Process p3(3);
return 0;
}
Sample Output
Event in Process 1 | Logical Clock: 1
#include <vector>
#include <queue>
class Process {
public:
};
queue<int> probeQueue;
probeQueue.push(initiator);
visited[initiator] = true;
cout << "Process " << initiator << " initiates deadlock detection." << endl;
while (!probeQueue.empty()) {
probeQueue.pop();
if (waitingFor == initiator) {
cout << "Deadlock detected: Process " << initiator << " is involved in a cycle!" << endl;
return true;
if (!visited[waitingFor]) {
visited[waitingFor] = true;
cout << "Probe sent from Process " << currentProcess << " to Process " << waitingFor << endl;
probeQueue.push(waitingFor);
return false;
int main() {
int numProcesses = 4;
vector<Process> processes;
processes.push_back(Process(i));
// Example: Process 0 is waiting for Process 1, and Process 1 is waiting for Process 2, etc.
int initiator = 0;
detectDeadlock(processes, initiator);
return 0;
}
Sample Output
Process 0 initiates deadlock detection.
#include <thread>
#include <mutex>
mutex mtx;
int shared_counter = 0;
cout << "Thread " << thread_id << " is trying to acquire the lock..." << endl;
// Lock the critical section using lock_guard (RAII: automatically locks and unlocks)
lock_guard<mutex> lock(mtx);
cout << "Thread " << thread_id << " has acquired the lock." << endl;
shared_counter++;
cout << "Thread " << thread_id << " incremented the counter to " << shared_counter << endl;
this_thread::sleep_for(chrono::milliseconds(500));
cout << "Thread " << thread_id << " has released the lock." << endl;
int main() {
t1.join();
t2.join();
t3.join();
cout << "Final value of shared_counter: " << shared_counter << endl;
return 0;
}
Sample Output
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
int main() {
if (server_fd == 0) {
return -1;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
return -1;
listen(server_fd, 3);
cout << "Server is listening on port " << PORT << endl;
if (client_socket < 0) {
return -1;
// Perform addition
performAddition(client_socket);
close(client_socket);
close(server_fd);
return 0;
}
Client Code (client.cpp)
#include <iostream>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
int main() {
int sock = 0;
// Create socket
if (sock < 0) {
return -1;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return -1;
return -1;
int sum;
cout << "Result from server: " << sum << endl;
close(sock);
return 0;
Sample Output
On the server terminal:
arduino
Client connected!
#include <string>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
int main() {
if (server_fd == 0) {
return -1;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
return -1;
listen(server_fd, 3);
cout << "Server is listening on port " << PORT << endl;
if (client_socket < 0) {
return -1;
// Perform addition
performAddition(client_socket);
close(client_socket);
close(server_fd);
return 0;
}
Client Code (client.cpp)
#include <iostream>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
int main() {
int sock = 0;
// Create socket
if (sock < 0) {
return -1;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return -1;
return -1;
int sum;
cout << "Result from server: " << sum << endl;
close(sock);
return 0;
}
Sample Output
On the server terminal:
arduino
Client connected!
#include <iostream>
#include <thread>
#include <string>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
using namespace std;
const int PORT = 8080;
void receiveMessages(int socket) {
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
int bytesRead = recv(socket, buffer, sizeof(buffer), 0);
if (bytesRead <= 0) {
break; // Server disconnected
}
cout << "Message: " << buffer << endl;
}
}
int main() {
int socket_fd;
struct sockaddr_in serverAddr;
// Create socket
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
cerr << "Socket creation error!" << endl;
return -1;
}
// Define the server address
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr) <= 0) {
cerr << "Invalid address/Address not supported!" << endl;
return -1;
}
// Connect to the server
if (connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
cerr << "Connection to server failed!" << endl;
return -1;
}
// Start a thread to receive messages
thread(receiveMessages, socket_fd).detach();
// Main loop to send messages
string message;
while (true) {
cout << "Enter your message: ";
getline(cin, message);
send(socket_fd, message.c_str(), message.size(), 0);
}
close(socket_fd);
return 0;
}
Sample Output
On the server terminal:
vbnet
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <queue>
using namespace std;
class Process {
public:
Process(int id) : id(id), state(0), terminationDetected(false) {}
void run();
void sendMessage(Process &receiver, const string &message);
void receiveMessage(const string &message);
void setPassive();
void checkTermination();
int id;
int state; // 0 for active, 1 for passive
bool terminationDetected;
private:
mutex mtx;
condition_variable cv;
queue<string> messageQueue;
};
void Process::sendMessage(Process &receiver, const string &message) {
unique_lock<mutex> lock(receiver.mtx);
receiver.messageQueue.push(message);
cv.notify_all(); // Notify the receiving process
}
void Process::receiveMessage(const string &message) {
unique_lock<mutex> lock(mtx);
messageQueue.push(message);
cout << "Process " << id << " received message: " << message << endl;
}
void Process::setPassive() {
unique_lock<mutex> lock(mtx);
state = 1; // Set to passive
cout << "Process " << id << " is now passive." << endl;
cv.notify_all(); // Notify termination checking
}
void Process::checkTermination() {
unique_lock<mutex> lock(mtx);
if (state == 1 && messageQueue.empty()) {
terminationDetected = true;
cout << "Termination detected in process " << id << "!" << endl;
}
}
Main Function to Simulate the System
int main() {
Process p1(1);
Process p2(2);
// Simulating sending messages
thread t1([&]() {
this_thread::sleep_for(chrono::seconds(1));
p1.sendMessage(p2, "Hello from Process 1");
this_thread::sleep_for(chrono::seconds(1));
p1.setPassive();
});
thread t2([&]() {
this_thread::sleep_for(chrono::seconds(2));
p2.sendMessage(p1, "Hello from Process 2");
this_thread::sleep_for(chrono::seconds(1));
p2.setPassive();
});
// Simulating message receiving and termination checking
while (!p1.terminationDetected || !p2.terminationDetected) {
this_thread::sleep_for(chrono::milliseconds(500));
p1.checkTermination();
p2.checkTermination();
// Check for incoming messages
if (!p1.messageQueue.empty()) {
string msg = p1.messageQueue.front();
p1.messageQueue.pop();
p2.receiveMessage(msg);
}
if (!p2.messageQueue.empty()) {
string msg = p2.messageQueue.front();
p2.messageQueue.pop();
p1.receiveMessage(msg);
}
}
t1.join();
t2.join();
return 0;
}
Sample Output