0% found this document useful (0 votes)
11 views16 pages

C++ Operating Sytem Threads Task

Uploaded by

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

C++ Operating Sytem Threads Task

Uploaded by

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

ALL TASK OF LAB 10 ARE AVAILABLE IN THIS DOCUMENT

Here's a basic example of using pthread_create() to create a new thread:

#include <stdio.h>

#include <pthread.h>

void *thread_function(void *arg) {

printf("Hello from the thread! Argument: %s\n", (const char *)arg);

pthread_exit(NULL);

int main() {

pthread_t thread;

const char *message = "Thread Argument";

if (pthread_create(&thread, NULL, thread_function, (void *)message)) {

fprintf(stderr, "Error creating thread\n");

return 1;

if (pthread_join(thread, NULL)) {

fprintf(stderr, "Error joining thread\n");

return 2;

printf("Thread finished execution\n");

return 0;
}

10.1.1. Implementation of Multithreading

#include <iostream>

#include <thread>

using namespace std;

// Function executed by the thread

void printMessage(const string& message, int count) {

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

cout << message << endl;

int main() {

cout << "Main thread: Starting two threads." << endl;

// Creating two threads

thread t1(printMessage, "Hello from Thread 1!", 3);

thread t2(printMessage, "Hello from Thread 2!", 2);

thread t3(printMessage, "Hello from Thread 3!", 4);

// Wait for the threads to finish

t1.join();

t2.join();

t3.join();
cout << "Main thread: All threads finished." << endl;

return 0;

Example 2

#include <iostream>

#include <thread>

using namespace std;

void computeSum(int start, int end, int& result) {

result = 0;

for (int i = start; i <= end; i++) {

result += i;

int main() {

int result1 = 0;

int result2 = 0;

cout << "Main thread: Starting two threads to calculate partial sums." << endl;

// Create two threads to compute partial sums

thread t1(computeSum, 1, 50, ref(result1)); // Sum from 1 to 50

thread t2(computeSum, 51, 100, ref(result2)); // Sum from 51 to 100

// Wait for both threads to finish


t1.join();

t2.join();

// Compute the total sum

int totalSum = result1 + result2;

cout << "Main thread: The total sum from 1 to 100 is " << totalSum << "." << endl;

return 0;

Lab Task

Write a program that perform addition of 2*2 matrix without using multithreading

#include <iostream>

using namespace std;

int main() {

// Declare two 2x2 matrices and a result matrix

int matrix1[2][2], matrix2[2][2], result[2][2];

// Input elements of the first matrix

cout << "Enter elements of the first 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";

cin >> matrix1[i][j];

}
}

// Input elements of the second matrix

cout << "Enter elements of the second 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";

cin >> matrix2[i][j];

// Perform matrix addition

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

for (int j = 0; j < 2; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Display the result matrix

cout << "\nThe resulting 2x2 matrix after addition is:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << result[i][j] << " ";

cout << endl;

}
return 0;

 Write a program that perform addition of 2*2 matrix using multithreading (Total 4
threads used).

#include <iostream>

#include <thread>

using namespace std;

// Matrices and result matrix

int matrix1[2][2], matrix2[2][2], result[2][2];

// Function to perform addition for a specific element

void addElements(int row, int col) {

result[row][col] = matrix1[row][col] + matrix2[row][col];

int main() {

// Input for the first 2x2 matrix

cout << "Enter elements of the first 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

cin >> matrix1[i][j];

}
// Input for the second 2x2 matrix

cout << "Enter elements of the second 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

cin >> matrix2[i][j];

// Create 4 threads, one for each element in the result matrix

thread t1(addElements, 0, 0); // First element (0,0)

thread t2(addElements, 0, 1); // Second element (0,1)

thread t3(addElements, 1, 0); // Third element (1,0)

thread t4(addElements, 1, 1); // Fourth element (1,1)

// Wait for all threads to finish execution

t1.join();

t2.join();

t3.join();

t4.join();

// Display the resulting matrix

cout << "\nThe resulting 2x2 matrix after addition is:" << endl;

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

for (int j = 0; j < 2; j++) {


cout << result[i][j] << " ";

cout << endl;

return 0;

10.2. Tasks

1. Implement the following.

a) Create two functions one for addition and other for multiplication. Call them in main
function (with out Multi thread).

#include <iostream>

using namespace std;

// Function for matrix addition

void addMatrices(int matrix1[2][2], int matrix2[2][2], int result[2][2]) {

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

for (int j = 0; j < 2; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Function for matrix multiplication

void multiplyMatrices(int matrix1[2][2], int matrix2[2][2], int result[2][2]) {

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


for (int j = 0; j < 2; j++) {

result[i][j] = 0; // Initialize result matrix element

for (int k = 0; k < 2; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

int main() {

int matrix1[2][2], matrix2[2][2], resultAdd[2][2], resultMul[2][2];

// Input for the first matrix

cout << "Enter elements of the first 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";

cin >> matrix1[i][j];

// Input for the second matrix

cout << "Enter elements of the second 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";
cin >> matrix2[i][j];

// Perform matrix addition

addMatrices(matrix1, matrix2, resultAdd);

// Perform matrix multiplication

multiplyMatrices(matrix1, matrix2, resultMul);

// Display the result of addition

cout << "\nResult of matrix addition:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << resultAdd[i][j] << " ";

cout << endl;

// Display the result of multiplication

cout << "\nResult of matrix multiplication:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << resultMul[i][j] << " ";

cout << endl;


}

return 0;

a) Implement above task using multithreading

#include <iostream>

#include <thread>

using namespace std;

// Function for matrix addition

void addMatrices(int matrix1[2][2], int matrix2[2][2], int result[2][2]) {

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

for (int j = 0; j < 2; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Function for matrix multiplication

void multiplyMatrices(int matrix1[2][2], int matrix2[2][2], int result[2][2]) {

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

for (int j = 0; j < 2; j++) {

result[i][j] = 0; // Initialize result matrix element

for (int k = 0; k < 2; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

}
}

int main() {

int matrix1[2][2], matrix2[2][2], resultAdd[2][2], resultMul[2][2];

// Input for the first matrix

cout << "Enter elements of the first 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";

cin >> matrix1[i][j];

// Input for the second matrix

cout << "Enter elements of the second 2x2 matrix:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << "Element [" << i+1 << "][" << j+1 << "]: ";

cin >> matrix2[i][j];

// Create threads for addition and multiplication


thread addThread(addMatrices, matrix1, matrix2, resultAdd);

thread mulThread(multiplyMatrices, matrix1, matrix2, resultMul);

// Wait for both threads to finish

addThread.join();

mulThread.join();

// Display the result of addition

cout << "\nResult of matrix addition:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << resultAdd[i][j] << " ";

cout << endl;

// Display the result of multiplication

cout << "\nResult of matrix multiplication:" << endl;

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

for (int j = 0; j < 2; j++) {

cout << resultMul[i][j] << " ";

cout << endl;

return 0;
}

1. Make a program using multi-threading to sum and subtract two 3x3 matrices.

#include <iostream>

#include <thread>

using namespace std;

// Function to add two matrices

void addMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3]) {

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

for (int j = 0; j < 3; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Function to subtract two matrices

void subtractMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3]) {

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

for (int j = 0; j < 3; j++) {

result[i][j] = matrix1[i][j] - matrix2[i][j];

int main() {

int matrix1[3][3], matrix2[3][3], resultAdd[3][3], resultSub[3][3];


// Input for the first 3x3 matrix

cout << "Enter elements of the first 3x3 matrix:" << endl;

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

for (int j = 0; j < 3; j++) {

cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

cin >> matrix1[i][j];

// Input for the second 3x3 matrix

cout << "Enter elements of the second 3x3 matrix:" << endl;

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

for (int j = 0; j < 3; j++) {

cout << "Element [" << i + 1 << "][" << j + 1 << "]: ";

cin >> matrix2[i][j];

// Create threads for addition and subtraction

thread addThread(addMatrices, matrix1, matrix2, resultAdd);

thread subtractThread(subtractMatrices, matrix1, matrix2, resultSub);

// Wait for both threads to finish

addThread.join();

subtractThread.join();
// Display the result of addition

cout << "\nResult of matrix addition:" << endl;

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

for (int j = 0; j < 3; j++) {

cout << resultAdd[i][j] << " ";

cout << endl;

// Display the result of subtraction

cout << "\nResult of matrix subtraction:" << endl;

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

for (int j = 0; j < 3; j++) {

cout << resultSub[i][j] << " ";

cout << endl;

return 0;

You might also like