0% found this document useful (0 votes)
5 views6 pages

Updated_Fixed_CPP_Programs

The document contains updated C++ programs demonstrating multithreading with two threads counting to 100 and five threads printing 'Hello World'. It also includes socket communication examples with a server and client program, as well as implementations of Round Robin and Priority Scheduling algorithms for process management. Each section provides complete code snippets for the respective functionalities.

Uploaded by

musicalabhi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Updated_Fixed_CPP_Programs

The document contains updated C++ programs demonstrating multithreading with two threads counting to 100 and five threads printing 'Hello World'. It also includes socket communication examples with a server and client program, as well as implementations of Round Robin and Priority Scheduling algorithms for process management. Each section provides complete code snippets for the respective functionalities.

Uploaded by

musicalabhi5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Updated Fixed and Corrected C++ Programs

11.a - Two Threads Counting to 100


#include <iostream>
#include <pthread.h>

void* countTo100(void* arg) {


int threadNum = *((int*)arg);
delete (int*)arg;
for (int i = 1; i <= 100; ++i) {
std::cout << "Thread " << threadNum << ": Count " << i << std::endl;
}
pthread_exit(nullptr);
}

int main() {
const int numThreads = 2;
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; ++i) {
int* threadNum = new int(i);
pthread_create(&threads[i], nullptr, countTo100, threadNum);
}
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}

11.b - 5 Threads Printing Hello World


#include <iostream>
#include <pthread.h>

void* printHello(void* arg) {


int threadNum = *((int*)arg);
delete (int*)arg;
std::cout << "Hello World from Thread " << threadNum << std::endl;
pthread_exit(nullptr);
}

int main() {
const int numThreads = 5;
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; ++i) {
int* threadNum = new int(i + 1);
pthread_create(&threads[i], nullptr, printHello, threadNum);
}
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}

12.a - server.cpp (Socket Communication)


#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Error creating socket." << std::endl;
return -1;
}

sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(8080);

if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) ==


-1) {
std::cerr << "Error binding socket." << std::endl;
close(serverSocket);
return -1;
}

if (listen(serverSocket, 5) == -1) {
std::cerr << "Error listening for connections." << std::endl;
close(serverSocket);
return -1;
}

std::cout << " Server listening on port 8080..." << std::endl;

sockaddr_in clientAddress{};
socklen_t clientAddrSize = sizeof(clientAddress);
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress,
&clientAddrSize);
if (clientSocket == -1) {
std::cerr << "Error accepting connection." << std::endl;
close(serverSocket);
return -1;
}

char buffer[1024] = {0};


ssize_t bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead == -1) {
std::cerr << "Error receiving data." << std::endl;
close(clientSocket);
close(serverSocket);
return -1;
}

buffer[bytesRead] = '\0';
std::cout << " Received from client: " << buffer << std::endl;

close(clientSocket);
close(serverSocket);
return 0;
}

12.b - client.cpp (Socket Communication)


#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
std::cerr << "Error creating socket." << std::endl;
return -1;
}

sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
if (inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr) <= 0) {
std::cerr << "Invalid address or not supported." << std::endl;
close(clientSocket);
return -1;
}

if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress))


== -1) {
std::cerr << "Connection failed." << std::endl;
close(clientSocket);
return -1;
}

std::cout << " Connected to the server. Sending data..." << std::endl;
const char* message = "Hello from the client!";
if (send(clientSocket, message, strlen(message), 0) == -1) {
std::cerr << "Error sending data." << std::endl;
close(clientSocket);
return -1;
}

close(clientSocket);
return 0;
}
6. Round Robin Scheduling

#include <iostream>
using namespace std;

int main() {
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;

cout << "Enter Total Number of Processes: ";


cin >> limit;
x = limit;

for (i = 0; i < limit; i++) {


cout << "\nEnter Details of Process[" << i + 1 << "]\n";
cout << "Arrival Time: ";
cin >> arrival_time[i];
cout << "Burst Time: ";
cin >> burst_time[i];
temp[i] = burst_time[i];
}

cout << "\nEnter Time Quantum: ";


cin >> time_quantum;

cout << "\nProcess ID\tBurst Time\tTurnaround Time\tWaiting Time\n";


for (total = 0, i = 0; x != 0;) {
if (temp[i] <= time_quantum && temp[i] > 0) {
total += temp[i];
temp[i] = 0;
counter = 1;
} else if (temp[i] > 0) {
temp[i] -= time_quantum;
total += time_quantum;
}
if (temp[i] == 0 && counter == 1) {
x--;
cout << "\nProcess[" << i + 1 << "]\t\t" << burst_time[i] << "\t\t" << total
- arrival_time[i]
<< "\t\t\t" << total - arrival_time[i] - burst_time[i];
wait_time += total - arrival_time[i] - burst_time[i];
turnaround_time += total - arrival_time[i];
counter = 0;
}
if (i == limit - 1)
i = 0;
else if (arrival_time[i + 1] <= total)
i++;
else
total++;
}

average_wait_time = wait_time * 1.0 / limit;


average_turnaround_time = turnaround_time * 1.0 / limit;

cout << "\n\nAverage Waiting Time: " << average_wait_time;


cout << "\nAvg Turnaround Time: " << average_turnaround_time << endl;
return 0;
}

7. Priority Scheduling

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Process {
int processID;
int burstTime;
int priority;
int waitingTime;
int turnaroundTime;
};

bool comparePriority(const Process &a, const Process &b) {


return a.priority < b.priority;
}

int main() {
int numProcesses;
cout << "Enter the number of processes: ";
cin >> numProcesses;

vector<Process> processes(numProcesses);

for (int i = 0; i < numProcesses; i++) {


processes[i].processID = i + 1;
cout << "Enter burst time for process " << i + 1 << ": ";
cin >> processes[i].burstTime;
cout << "Enter priority for process " << i + 1 << ": ";
cin >> processes[i].priority;
}

sort(processes.begin(), processes.end(), comparePriority);

processes[0].waitingTime = 0;
processes[0].turnaroundTime = processes[0].burstTime;

for (int i = 1; i < numProcesses; i++) {


processes[i].waitingTime = processes[i - 1].waitingTime + processes[i -
1].burstTime;
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
}
double totalWaitingTime = 0;
double totalTurnaroundTime = 0;

for (const Process &p : processes) {


totalWaitingTime += p.waitingTime;
totalTurnaroundTime += p.turnaroundTime;
}

cout << "Process\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n";


for (const Process &p : processes) {
cout << p.processID << "\t\t" << p.burstTime << "\t\t" << p.priority
<< "\t\t" << p.waitingTime << "\t\t" << p.turnaroundTime << endl;
}

cout << "\nAverage Waiting Time: " << totalWaitingTime / numProcesses << endl;
cout << "Average Turnaround Time: " << totalTurnaroundTime / numProcesses << endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy