A 9 SysytemCallLab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ASSIGNMENT – 9

1. Write a program that creates 10 threads. Have each thread execute the same function and pass each thread a
unique number. Each thread should print “Hello, World (thread n)” five times where „n‟ is replaced by the
thread‟s number. Use an array of pthread_t objects to hold the various thread IDs. Be sure the program doesn‟t
terminate until all the threads are complete.

#include <stdio.h>
#include <pthread.h>

#define NUM_THREADS 10

void* hello_world(void* arg) {


int thread_num = *(int*)arg;
for (int i = 0; i < 5; ++i) {
printf("Hello, World (thread %d)\n", thread_num);
}
pthread_exit(NULL);
}

int main() {
pthread_t threads[NUM_THREADS];
int thread_nums[NUM_THREADS];

// Create 10 threads
for (int i = 0; i < NUM_THREADS; ++i) {
thread_nums[i] = i + 1;
pthread_create(&threads[i], NULL, hello_world, (void*)&thread_nums[i]);
}

// Wait for all threads to finish


for (int i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}

return 0;
}

2. Write a program to create a thread with default attributes and then change the priority to HIGHPRIORIT

#include <stdio.h>
#include <pthread.h>

#define HIGHPRIORITY 10

void* thread_function(void* arg) {


// Thread logic
return NULL;
}

int main() {
pthread_t thread;
pthread_attr_t attr;
struct sched_param param;
// Initialize thread attributes
pthread_attr_init(&attr);

// Set thread priority


param.sched_priority = HIGHPRIORITY;
pthread_attr_setschedparam(&attr, &param);

// Create thread with modified attributes


pthread_create(&thread, &attr, thread_function, NULL);

// Wait for the thread to finish


pthread_join(thread, NULL);

return 0;
}

3. Write a program for matrix multiplication using multiple threads, where each thread will perform multiplication
of one row from first matrix A and one column from second matrix B. Therefore, each entry in the resultant matrix
will be a result of a specific thread, as [R]m*l = [A]m*n . [B]n*l . Hence the total numbers of threads are m*l.
Dimensions of the matrix should be given as arguments to the main thread.

#include <stdio.h>
#include <pthread.h>

#define MAX_SIZE 100

int matrixA[MAX_SIZE][MAX_SIZE];
int matrixB[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];

int rows, cols, threads_count;

void* multiply_row_column(void* arg) {


int row = *((int*)arg);
int i, j;

for (j = 0; j < cols; ++j) {


result[row][j] = 0;
for (i = 0; i < cols; ++i) {
result[row][j] += matrixA[row][i] * matrixB[i][j];
}
}

pthread_exit(NULL);
}

int main(int argc, char* argv[]) {


if (argc != 4) {
printf("Usage: %s <rows> <cols> <threads_count>\n", argv[0]);
return 1;
}

rows = atoi(argv[1]);
cols = atoi(argv[2]);
threads_count = atoi(argv[3]);
pthread_t threads[threads_count];
int thread_args[threads_count];

// Initialize matrices with some values (you can modify as needed)


// ...

// Create threads for matrix multiplication


for (int i = 0; i < threads_count; ++i) {
thread_args[i] = i;
pthread_create(&threads[i], NULL, multiply_row_column, (void*)&thread_args[i]);
}

// Wait for all threads to finish


for (int i = 0; i < threads_count; ++i) {
pthread_join(threads[i], NULL);
}

// Print the resulting matrix


printf("Resultant Matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

4. Write a program that computes the square roots of the integers from 0 to 99 in a separate thread and returns an
array of doubles containing the results. In the meantime the main thread should display a short message to the
user and then display the results of the computation when they are ready.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>

#define ARRAY_SIZE 100

double result[ARRAY_SIZE];

void* compute_square_roots(void* arg) {


for (int i = 0; i < ARRAY_SIZE; ++i) {
result[i] = sqrt((double)i);
}
pthread_exit(NULL);
}

int main() {
pthread_t thread;

// Create thread to compute square roots


pthread_create(&thread, NULL, compute_square_roots, NULL);

// Display a short message


printf("Main thread waiting for square roots computation...\n");
// Wait for the thread to finish
pthread_join(thread, NULL);

// Display the results


printf("Square Roots:\n");
for (int i = 0; i < ARRAY_SIZE; ++i) {
printf("%d: %.4f\n", i, result[i]);
}

return 0;
}

5. (a) Write a program to simulate deposit/withdraw activities on a banking account: Initialize the beginning
balance (global variable) to 1 million, withdraw 600 thousands, and then deposit 500 thousands. Create two Posix
threads in main(), which call the withdraw and the deposit functions respectively. Both withdraw and deposit
functions have one parameter, which represent the amount to withdraw or deposit. You can create these two
threads in any order to perform withdraw and deposit action. However, before you create the second thread, use
pthread_join() to wait for the first thread to terminate. Finally print out the ending balance. (b) Move the calls to
pthread_join() function after the creation of both pthreads. Run the program several times. Do you see different
result? (c) Use pthread-mutex_lock() and pthread-mutex_unlock() functions to ensure mutual exclusion between
the two pthreads. Check the ending balance now

#include <stdio.h>
#include <pthread.h>

#define INITIAL_BALANCE 1000000

int balance = INITIAL_BALANCE;


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* withdraw(void* arg) {


int amount = *(int*)arg;
pthread_mutex_lock(&mutex);
printf("Withdraw: %d\n", amount);
balance -= amount;
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}

void* deposit(void* arg) {


int amount = *(int*)arg;
pthread_mutex_lock(&mutex);
printf("Deposit: %d\n", amount);
balance += amount;
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}

int main() {
pthread_t withdraw_thread, deposit_thread;
int withdraw_amount = 600000;
int deposit_amount = 500000;

// Create threads
pthread_create(&withdraw_thread, NULL, withdraw, (void*)&withdraw_amount);
pthread_create(&deposit_thread, NULL, deposit, (void*)&deposit_amount);
// Wait for the first thread to finish
pthread_join(withdraw_thread, NULL);

// Wait for the second thread to finish


pthread_join(deposit_thread, NULL);

// Display ending balance


printf("Ending Balance: %d\n", balance);

return 0;
}

6. The Program should demonstrates the use of several Pthread condition variable (pthread_mutex_t,
pthread_cond_t) routines. The main routine creates three threads. Two of the threads perform work and update a
varible "race".The first thread add 3 in the “race” and second thread substract 1 from the “race”. The third thread
waits until the count variable reaches 100

#include <stdio.h>
#include <pthread.h>

#define COUNT_TARGET 100

int race = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* add_thread(void* arg) {


for (int i = 0; i < COUNT_TARGET; ++i) {
pthread_mutex_lock(&mutex);
race += 3;
pthread_mutex_unlock(&mutex);
}
pthread_cond_signal(&cond);
pthread_exit(NULL);
}

void* subtract_thread(void* arg) {


for (int i = 0; i < COUNT_TARGET; ++i) {
pthread_mutex_lock(&mutex);
race -= 1;
pthread_mutex_unlock(&mutex);
}
pthread_cond_signal(&cond);
pthread_exit(NULL);
}

void* wait_thread(void* arg) {


pthread_mutex_lock(&mutex);

while (race < COUNT_TARGET) {


pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);

printf("Final value of race: %d\n", race);


pthread_exit(NULL);
}

int main() {
pthread_t thread_add, thread_subtract, thread_wait;

// Create threads
pthread_create(&thread_add, NULL, add_thread, NULL);
pthread_create(&thread_subtract, NULL, subtract_thread, NULL);
pthread_create(&thread_wait, NULL, wait_thread, NULL);

// Wait for all threads to finish


pthread_join(thread_add, NULL);
pthread_join(thread_subtract, NULL);
pthread_join(thread_wait, NULL);

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