0% found this document useful (0 votes)
19 views9 pages

Solutions Operating Systems Exam

Uploaded by

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

Solutions Operating Systems Exam

Uploaded by

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

Solutions for Operating Systems Exam

Exercise 1: Task Synchronization using Semaphores

The precedence graph indicates the dependencies of tasks:

- A -> {B, C, D}

- {B, C} -> E

- {D, E} -> F

Solution in C (using POSIX semaphores):

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

sem_t semB, semC, semD, semE, semF;

void* taskA(void* arg) {

printf("Task A completed\n");

sem_post(&semB);

sem_post(&semC);

sem_post(&semD);

return NULL;

void* taskB(void* arg) {

sem_wait(&semB);

printf("Task B completed\n");
sem_post(&semE);

return NULL;

void* taskC(void* arg) {

sem_wait(&semC);

printf("Task C completed\n");

sem_post(&semE);

return NULL;

void* taskD(void* arg) {

sem_wait(&semD);

printf("Task D completed\n");

sem_post(&semF);

return NULL;

void* taskE(void* arg) {

sem_wait(&semE);

sem_wait(&semE);

printf("Task E completed\n");

sem_post(&semF);

return NULL;

void* taskF(void* arg) {


sem_wait(&semF);

sem_wait(&semF);

printf("Task F completed\n");

return NULL;

int main() {

pthread_t threads[6];

sem_init(&semB, 0, 0);

sem_init(&semC, 0, 0);

sem_init(&semD, 0, 0);

sem_init(&semE, 0, 0);

sem_init(&semF, 0, 0);

pthread_create(&threads[0], NULL, taskA, NULL);

pthread_create(&threads[1], NULL, taskB, NULL);

pthread_create(&threads[2], NULL, taskC, NULL);

pthread_create(&threads[3], NULL, taskD, NULL);

pthread_create(&threads[4], NULL, taskE, NULL);

pthread_create(&threads[5], NULL, taskF, NULL);

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

pthread_join(threads[i], NULL);

sem_destroy(&semB);

sem_destroy(&semC);
sem_destroy(&semD);

sem_destroy(&semE);

sem_destroy(&semF);

return 0;

}
Exercise 2: Sum and Average with Threads

We need to calculate the sum and average of an array using threads:

- TH1 and TH2 compute parts of the sum in parallel.

- TH3 calculates the average after the sum is completed.

- The main thread displays the results.

Solution in C:

#include <stdio.h>

#include <pthread.h>

#define MAX 100

int array[MAX];

int n, sum = 0;

float average;

pthread_mutex_t mutex;

void* compute_sum(void* arg) {

int start = ((int*)arg)[0];

int end = ((int*)arg)[1];

int local_sum = 0;

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

local_sum += array[i];

}
pthread_mutex_lock(&mutex);

sum += local_sum;

pthread_mutex_unlock(&mutex);

return NULL;

void* compute_average(void* arg) {

average = (float)sum / n;

return NULL;

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

pthread_t threads[3];

pthread_mutex_init(&mutex, NULL);

n = argc - 1;

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

array[i] = atoi(argv[i + 1]);

int range1[] = {0, n / 2};

int range2[] = {n / 2, n};

pthread_create(&threads[0], NULL, compute_sum, range1);

pthread_create(&threads[1], NULL, compute_sum, range2);


pthread_join(threads[0], NULL);

pthread_join(threads[1], NULL);

pthread_create(&threads[2], NULL, compute_average, NULL);

pthread_join(threads[2], NULL);

printf("Sum = %d, Average = %.2f\n", sum, average);

pthread_mutex_destroy(&mutex);

return 0;

}
Exercise 3: Process Synchronization with Semaphores

Three synchronization scenarios using semaphores:

1. Ai actions must not be simultaneous.

2. Ai actions follow the order A1, A2, A3 in a cyclic manner.

3. Ai actions alternate as A1 -> (A2 or A3).

Solutions in C:

// Case 1: Ai actions must not be simultaneous.

sem_t semA1, semA2, semA3;

void* action1(void* arg) {

sem_wait(&semA1);

printf("A1 executed\n");

sem_post(&semA2);

return NULL;

void* action2(void* arg) {

sem_wait(&semA2);

printf("A2 executed\n");

sem_post(&semA3);

return NULL;

void* action3(void* arg) {

sem_wait(&semA3);
printf("A3 executed\n");

sem_post(&semA1);

return NULL;

// Initialization and threads creation

sem_init(&semA1, 0, 1);

sem_init(&semA2, 0, 0);

sem_init(&semA3, 0, 0);

You might also like