---
# System prepended metadata

title: Untitled

---

## Operating System Exam

### Question 1:

```cpp
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>

using namespace std;

struct params {
  int *arr_1;
  int *arr_2;
  int *result;
  int size;
};

void *merge_two(void *args) {
  int *arr_1 = ((struct params *)args)->arr_1;
  int *arr_2 = ((struct params *)args)->arr_2;
  int *result = ((struct params *)args)->result;
  const int N = ((struct params *)args)->size;
  int i = 0, j = 0, k = 0;

  for (; i < N && j < N;) {
    if (arr_1[i] > arr_2[j]) {
      result[k] = arr_2[j];
      k++;
      j++;
    } else {
      result[k] = arr_1[i];
      k++;
      i++;
    }
  }
  while (i < N)
    result[k++] = arr_1[i++];
  while (j < N)
    result[k++] = arr_2[j++];
  return NULL;
}


void merge_four(int *arr_1, int *arr_2, int *arr_3, int *arr_4, int size) {
  pthread_t t1, t2, t3;

  int *res_1 = new int[2 * size];
  int *res_2 = new int[2 * size];
  int *result = new int[4 * size];

  params a, b, c;
  a.size = b.size = size;
  a.arr_1 = arr_1;
  a.arr_2 = arr_2;
  a.result = res_1;
  b.arr_1 = arr_3;
  b.arr_2 = arr_4;
  b.result = res_2;

  // Two threads for concurrent computation of merging two-two arrays pair.
  pthread_create(&t1, NULL, merge_two, &a);
  pthread_create(&t2, NULL, merge_two, &b);
  pthread_join(t1, NULL);
  pthread_join(t2, NULL);

  // No need for another thread as it would only slow down copmutation 
  // because computation of other four arrays are required before
  // merge them. Hence, the result needs to be computed beforehand.
  c.size = 2 * size;
  c.arr_1 = a.result;
  c.arr_2 = b.result;
  c.result = result;

  merge_two(&c);

  // Memory Cleanup
  delete[] res_1;
  delete[] res_2;
  delete[] result;
}
```


### Question 2:

`int sem_init(sem_t *sem, int pshared, unsigned int value);`
pshared is 0, value is 1

Code Syntax:

```
Global Variable: sem_t mutex;
Initialized: sem_init(&mutex, 0, 1);
```

```cpp
void *function1(void *arg) {
  sem_wait(&mutex);
  printf("a");
  sem_post(&mutex);
}

void *function2(void *arg) {
  // wait
  sem_wait(&mutex);
  // critical section
  printf("b");
  // signal
  sem_post(&mutex);
}
```
### Question 3:

```cpp


sem_t mutex, mutex2;

int poll_event = 0;

void *function1(void *arg) {
  if (poll_event == 0) {
    sem_wait(&mutex);
    printf("a");
    poll_event++;
    sem_post(&mutex2);
  } else {
    poll_event = 0;
    sem_wait(&mutex);
    sem_post(&mutex2);
  }
}

void *function2(void *arg) {
  printf("b");
  sem_post(&mutex);
  return nullptr;
}

```

### Question 4:

### Question 5:

```
(0) Answer
Page size: 8 Words
page = 45 // 8 = 5
frame = 2
offset = 45 % 8
Physical Address = 2 * 8 + 5
Logical Address: 45 -> 21


(1) 16-bit
For Page size: 16 ; 1 word = 2 bytes
page = 45 // 16 = 2
frame = 9
offset = 45 mod 16
Physical Address = 9 * 16 + 13
Logical Address: 45 -> 157
```

```
(2) 32-bit
Page size: 32; 1 word = 4 bytes
page = 45 // 32 = 1
frame = 0
offset = 45 % 32
Physical Address = 0 * 32 + 13
Logical Address: 45 -> 13
```

### Question 6:

memory access = 18ns
overhead = 4111ns
page fault probability = 0.85

```
EAT = (1 – p) x memory access + p x (page fault overhead)
EAT = (1 - 0.85) * 18 + 0.85 * (4111)
EAT = 3497ns
```


