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

Lab7 and Lab8

The document describes two tasks for an operating systems lab assignment. Task 1 involves creating child processes that guess a random number, and Task 2 compares the performance of single-threaded and multi-threaded approaches for calculating factorials and checking primality of array elements.

Uploaded by

Sohail Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab7 and Lab8

The document describes two tasks for an operating systems lab assignment. Task 1 involves creating child processes that guess a random number, and Task 2 compares the performance of single-threaded and multi-threaded approaches for calculating factorials and checking primality of array elements.

Uploaded by

Sohail Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab-Assignment # 3

Course Title: Operating System

Course Instructor
Ms. Zahida Walayat

Submitted by:

Sohail Faiz Rasool (SP22-BCS-095)

Section: BCS-5B
Task 1(lab-7):

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>

const int MAX_GUESS = 10;

void child_process(int pipe_fd[2], int child_num) {


close(pipe_fd[0]); // Close read end of pipe

srand(time(0) + child_num); // Seed the random number generator


int random_number = rand() % (MAX_GUESS + 1);

int guess = -1;


int turns = 0;

while (guess != random_number) {


std::cout << "Child " << child_num << ": Enter your guess (between 0 and 10): ";
std::cin >> guess;
++turns;

if (guess == random_number) {
std::cout << "Child " << child_num << ": Correct! You guessed the number in "
<< turns << " turns.\n";
} else {
std::cout << "Child " << child_num << ": Wrong guess. Try again.\n";
}
}

write(pipe_fd[1], &turns, sizeof(turns));


close(pipe_fd[1]); // Close write end of pipe
exit(0);
}

int main() {
const int NUM_CHILDREN = 3;
int pipe_fd[NUM_CHILDREN][2];
pid_t pid[NUM_CHILDREN];

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


if (pipe(pipe_fd[i]) == -1) {
std::cerr << "Pipe creation failed\n";
return 1;
}

pid[i] = fork();

if (pid[i] < 0) {
std::cerr << "Fork failed\n";
return 1;
}

if (pid[i] == 0) {
// Child process
child_process(pipe_fd[i], i + 1);
}
}

// Parent process
for (int i = 0; i < NUM_CHILDREN; ++i) {
close(pipe_fd[i][1]); // Close write end of pipe in parent process
}

int min_turns = INT_MAX;


int winner = -1;

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


waitpid(pid[i], NULL, 0); // Wait for child process to finish

int turns;
read(pipe_fd[i][0], &turns, sizeof(turns));
close(pipe_fd[i][0]); // Close read end of pipe in parent process

std::cout << "Child " << (i + 1) << " took " << turns << " turns to guess correctly.\
n";
if (turns < min_turns) {
min_turns = turns;
winner = i + 1;
}
}

std::cout << "The winner is Child " << winner << " with " << min_turns << " turns!\
n";
return 0;
}

Task 2(lab-8):
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <random>
#include <cmath>
#include <mutex>

const int ARRAY_SIZE = 1000;


const int MAX_RANDOM = 100;
std::vector<int> numbers(ARRAY_SIZE);
std::vector<unsigned long long> factorials(ARRAY_SIZE);
std::vector<bool> is_prime(ARRAY_SIZE);

void populate_array() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, MAX_RANDOM);
for (int i = 0; i < ARRAY_SIZE; ++i) {
numbers[i] = dis(gen);
}
}

unsigned long long factorial(int n) {


if (n == 0 || n == 1) {
return 1;
}
unsigned long long result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}

bool is_prime_number(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
void calculate_factorials() {
for (int i = 0; i < ARRAY_SIZE; ++i) {
factorials[i] = factorial(numbers[i]);
}
}

void check_primes() {
for (int i = 0; i < ARRAY_SIZE; ++i) {
is_prime[i] = is_prime_number(numbers[i]);
}
}

int count_primes() {
int count = 0;
for (int i = 0; i < ARRAY_SIZE; ++i) {
if (is_prime[i]) {
++count;
}
}
return count;
}

int main() {
// Single-threaded approach
populate_array();

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


calculate_factorials();
check_primes();
int prime_count_single_thread = count_primes();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration_single_thread = end - start;

std::cout << "Single-threaded duration: " << duration_single_thread.count() << "


seconds\n";
std::cout << "Prime numbers found: " << prime_count_single_thread << "\n";

// Multi-threaded approach
populate_array(); // Repopulate the array for a fair comparison
std::thread factorial_thread(calculate_factorials);
std::thread prime_thread(check_primes);

start = std::chrono::high_resolution_clock::now();
factorial_thread.join();
prime_thread.join();
end = std::chrono::high_resolution_clock::now();

int prime_count_multi_thread = count_primes();


std::chrono::duration<double> duration_multi_thread = end - start;

std::cout << "Multi-threaded duration: " << duration_multi_thread.count() << "


seconds\n";
std::cout << "Prime numbers found: " << prime_count_multi_thread << "\n";

// Compare performance
if (duration_single_thread.count() < duration_multi_thread.count()) {
std::cout << "Single-threaded approach is faster.\n";
} else {
std::cout << "Multi-threaded approach is faster.\n";
}

return 0;
}

You might also like