labtask os
labtask os
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>
// Single-threaded approach
void singleThreaded(vector<int>& arr) {
vector<unsigned long long> factorials;
int primeCount = 0;
// Multi-threaded approach
void multiThreaded(vector<int>& arr) {
vector<unsigned long long> factorials;
int primeCount = 0;
factorialThread.join();
primeThread.join();
int main() {
vector<int> arr(1000);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 100);
// Single-threaded approach
singleThreaded(arr);
// Multi-threaded approach
multiThreaded(arr);
return 0;
}
Output: