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

Exam Solutions

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)
14 views9 pages

Exam Solutions

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 to the Exam

**Exercice 1: Synchronization of Sequential Tasks Using


Semaphores**

We have six tasks {A, B, C, D, E, F} with the following precedence


constraints:
- Task A must precede tasks B, C, and D.
- Tasks B and C must precede task E.
- Tasks D and E must precede task F.

To synchronize these tasks using semaphores, we define the


following approach:

1. **Semaphores Initialization:**
- `sem_B = sem_C = sem_D = 0`: Ensure that tasks B, C, and D wait
for task A.
- `sem_E = 0`: Ensure that task E waits for tasks B and C.
- `sem_F = 0`: Ensure that task F waits for tasks D and E.

2. **Pseudo-code Implementation:**
```c
Semaphore sem_B = 0, sem_C = 0, sem_D = 0;
Semaphore sem_E = 0, sem_F = 0;

Task A() {
// Task A's code
Signal(sem_B);
Signal(sem_C);
Signal(sem_D);
}

Task B() {
Wait(sem_B);
// Task B's code
Signal(sem_E);
}

Task C() {
Wait(sem_C);
// Task C's code
Signal(sem_E);
}

Task D() {
Wait(sem_D);
// Task D's code
Signal(sem_F);
}

Task E() {
Wait(sem_E);
// Task E's code
Signal(sem_F);
}

Task F() {
Wait(sem_F);
// Task F's code
}
```

**Exercice 2: Sum and Average Using Threads**

We want to calculate the sum and average of an array of integers


using threads:
- Two threads (`TH1` and `TH2`) calculate the sum in parallel.
- A third thread (`TH3`) calculates the average after the sum is
computed.
- The main thread handles user input and displays the results.

1. **Thread Functions:**

- `TH1` calculates the sum of the first half of the array.


- `TH2` calculates the sum of the second half of the array.
- `TH3` computes the average after `TH1` and `TH2` complete.

2. **Pseudo-code Implementation:**
```c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int *array, total_sum = 0, size;


float average;
pthread_mutex_t mutex;

void *calculate_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);
total_sum += local_sum;
pthread_mutex_unlock(&mutex);

return NULL;
}

void *calculate_average(void *arg) {


average = (float)total_sum / size;
return NULL;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s num1 num2 ...\n", argv[0]);
return 1;
}

size = argc - 1;
array = malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
array[i] = atoi(argv[i + 1]);
}

pthread_t th1, th2, th3;


pthread_mutex_init(&mutex, NULL);

int args1[] = {0, size / 2};


int args2[] = {size / 2, size};

pthread_create(&th1, NULL, calculate_sum, args1);


pthread_create(&th2, NULL, calculate_sum, args2);

pthread_join(th1, NULL);
pthread_join(th2, NULL);

pthread_create(&th3, NULL, calculate_average, NULL);


pthread_join(th3, NULL);
printf("Sum = %d\n", total_sum);
printf("Average = %.2f\n", average);

pthread_mutex_destroy(&mutex);
free(array);
return 0;
}
```

**Exercice 3: Synchronization of Processes Using Semaphores**

We have three processes {P1, P2, P3} with actions {A1, A2, A3}. The
synchronization constraints are:

1. **Case 1:** Actions A1, A2, and A3 must never execute


simultaneously.

```c
Semaphore mutex = 1;

P1() {
Wait(mutex);
// Action A1
Signal(mutex);
}
P2() {
Wait(mutex);
// Action A2
Signal(mutex);
}

P3() {
Wait(mutex);
// Action A3
Signal(mutex);
}
```

2. **Case 2:** Actions A1, A2, and A3 must execute in the order A1,
A2, A3, and repeat.

```c
Semaphore sem_A1 = 1, sem_A2 = 0, sem_A3 = 0;

P1() {
Wait(sem_A1);
// Action A1
Signal(sem_A2);
}

P2() {
Wait(sem_A2);
// Action A2
Signal(sem_A3);
}

P3() {
Wait(sem_A3);
// Action A3
Signal(sem_A1);
}
```

3. **Case 3:** Actions A1, A2, and A3 must execute in the order A1
(A2 or A3), A1 (A2 or A3), and so on.

```c
Semaphore sem_A1 = 1, sem_A23 = 0;

P1() {
Wait(sem_A1);
// Action A1
Signal(sem_A23);
}

P2() {
Wait(sem_A23);
// Action A2
Signal(sem_A1);
}

P3() {
Wait(sem_A23);
// Action A3
Signal(sem_A1);
}
```

You might also like