0% found this document useful (0 votes)
5 views

labtask os

Uploaded by

Essa Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

labtask os

Uploaded by

Essa Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name Essa Khan

Reg No Fa22-bce-085
Lab Task Operating System

Task 1:
Write C++ program that 
Declares an array of size 1000 and populates it with random numbers between 1 and
100. Then it finds factorial of these numbers and checks how many of these prime
numbers are. Also find the time taken by the system to perform these tasks.  Now,
create two thread such that the first thread process finds the factorial of these numbers
(given in the above array) while the other finds how many of these are prime. Now,
calculate the time taken by these children processes to perform these tasks.  Now,
compare which approach performs better

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <thread>
#include <mutex>

using namespace std;

// Function to calculate factorial of a number


unsigned long long factorial(int n) {
unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}

// Function to check if a number is prime


bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) return false;
}
return true;
}

// Single-threaded approach
void singleThreaded(vector<int>& arr) {
vector<unsigned long long> factorials;
int primeCount = 0;

auto start = chrono::high_resolution_clock::now();

// Calculate factorial and check prime


for (int num : arr) {
factorials.push_back(factorial(num));
if (isPrime(num)) {
++primeCount;
}
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;

cout << "Single-threaded approach:" << endl;


cout << "Time taken: " << elapsed.count() << " seconds" << endl;
cout << "Prime count: " << primeCount << endl;
}

// Thread function to calculate factorials


void calculateFactorials(const vector<int>& arr, vector<unsigned long long>& factorials)
{
for (int num : arr) {
factorials.push_back(factorial(num));
}
}

// Thread function to count primes


void countPrimes(const vector<int>& arr, int& primeCount) {
for (int num : arr) {
if (isPrime(num)) {
++primeCount;
}
}
}

// Multi-threaded approach
void multiThreaded(vector<int>& arr) {
vector<unsigned long long> factorials;
int primeCount = 0;

auto start = chrono::high_resolution_clock::now();

thread factorialThread(calculateFactorials, ref(arr), ref(factorials));


thread primeThread(countPrimes, ref(arr), ref(primeCount));

factorialThread.join();
primeThread.join();

auto end = chrono::high_resolution_clock::now();


chrono::duration<double> elapsed = end - start;

cout << "Multi-threaded approach:" << endl;


cout << "Time taken: " << elapsed.count() << " seconds" << endl;
cout << "Prime count: " << primeCount << endl;
}

int main() {
vector<int> arr(1000);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 100);

// Populate the array with random numbers


for (int i = 0; i < 1000; ++i) {
arr[i] = dis(gen);
}
cout << "Processing 1000 random numbers:" << endl;

// Single-threaded approach
singleThreaded(arr);

// Multi-threaded approach
multiThreaded(arr);

return 0;
}

Output:

You might also like