Main ExamS

You might also like

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

Intelligent Systems Engineering Department

The National School of Artificial Intelligence


2nd Year — Semester 2 — 2023/2024

OPERATING SYSTEMS
Midterm Exam (Solution)
March 19th, 2024, from 9:00 am to 10:00 am

This is a closed book exam. All electronic devices There are a total of 20 points. You have 60 minutes
are not permitted. Do not take pictures of the exam. to write the exam.

The course instructor or the proctors cannot answer Do not attempt to cheat, copy, talk to a classmate, or
questions during the exam. If you find an exam prob- give anything to or take anything from another class-
lem unclear, then make reasonable assumptions, write mate (borrowing a correction-pen maybe allowed),
down your assumptions, and solve the problem. otherwise you may be disqualified.

Please write neatly. Write your answer using a pen Write your full name and group on the head of each
or a pencil, but do not write in red or any color that is of your answer sheets, and leave your student ID card
close to red. Use your rough sheet before answering. out to be checked.

Full Name: Dr. Lounis Karim (Winter 2024) Grade: Remarks:

Student #: Group #:ABCDEFG ABCDEFGHIJ ABCDEFGHIJKxQR.

1. The following CISC x-86 assembly code is to be run on an Intel 8086 microprocessor.
[0xE4122] MOV AX, 0XBF0A
[0xE4123] MOV CX, 0xFFFA
[0xE4124] MOV DX, 0X0005
XXXXXXXLABEL:
[0xE4125]XXINC CX
[0xE4126]XXDEC DX
[0xE4127]XXJNZ LABEL
[0xE4128] DIV CX
[0xE4129] INC CX
[0xE412A] DIV CX
[0xE412B] JMP LABEL
[0xE412C] HLT

Copyright © Dr. Karim Lounis, 2023/2024. Page 1/6


(a) What is the value stored in register CX when the PC — Program Counter — is
containing the value 0xE4129 [1.0 point]?

The value is: 0xFFFF

(b) The JMP LABEL instruction allows a program to jump to an address of an instruc-
tion (to where the LABEL is defined) in the code. Does the program ever terminate?
Explain [2.0 points].

The program terminates [0.5 point] following an exception, which was due to a division
by zero. The CX register held the value of 0 at the instruction DIV CX. [1.5 point]

(c) Assuming one instruction takes one CPU cycle (i.e., fetch, decode, and execute
— all in one cycle), how many CPU cycles does this program consume [1.0 point]?

There will be 21 cpu cycles (I gave half mark for those who wrote 20, 22, and 23).
XXX

2. Consider the following C program code (fork() is a system call to create a process):

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


{
XXfor (char k=‘z’; k>‘t’; k--)
XX{
XXXXfork();
XXXXprintf("Running...\n");
XXXXk--;
XX}
XXreturn ‘O’;
} See tree below

(a) Draw the process-tree that is created by the execution of this program (The re-
quired tree does not contain duplicated nodes. Each node would represent a process)
— use the empty space on the right of the code to draw the process tree [3.0 points].
(b) How many lines will be printed by each process at the end of the execution? (write
the number inside the node of each process in the process tree you drew) [2.0 points].
In the below tree, inside each process box, we indicate a print operating (a printed
line) with a “*”. Hence, a total of 14 lines will be printed.
Copyright © Dr. Karim Lounis, 2023/2024. Page 2/6
3. Use Begin/End and CoBegin/CoEnd programming constructs to write code matching
this precedence graph (use the empty space on the right of the PPG) [2.0 points].

P1 P2 P3

P4 P5 P6

Copyright © Dr. Karim Lounis, 2023/2024. Page 3/6


4. Use three semaphores X, Y, and Z, to write code matching the precedence graph in
Question 3. Note that for each semaphore that you use, it needs to be declared and
initialized. Also, you are not allowed to merge codes or use busy-waiting [3.0 points].

Semaphore X=0 ; Semaphore Y= 0 ; Semaphore Z=0 ;


P1 P2 P3 P4 P5 P6
Code 1; Code 2; Code 3; P(X); P(Y); P(Z);
V(X); V(Y); V(Z); Code 4; P(Y); P(Z);
V(Y); Code 5; Code 6;
V(Z);

OR
Semaphore X=0 ; Semaphore Y= -1 ; Semaphore Z=-1 ;
P1 P2 P3 P4 P5 P6
Code 1; Code 2; Code 3; P(X); P(Y); P(Z);
V(X); V(Y); V(Z); Code 4; Code 5; Code 6;
V(Y); V(Z);

[0.5 point] for the initialization AND [1.0 point] for P1, P2, and P3 (all of them)
[0.5 point] for each of P4, P5, and P6.
5. Consider the Peterson’s algorithm for the critical section problem.
Process 0:XXXXXXXXXXXXXXXXXXXProcess 1:
XXBeginXXXXXXXXXXXXXXXXXXXXXBegin
XXXwhile(true)XXXXXXXXXXXxxxxxXXwhile(true)
XXX{XXXXXXXXXXXXXXXXXXXXXXXX{
XXXXX(1) flag[0]=true;XXXXXXXXXXXX(1) flag[1]=true;
XXXXX(2) turn=1;XXXXXXX.XXXXXXXXX(2) turn=0;
XXXXX(3) while(flag[1] && turn==1);XXX(3) while(flag[0] && turn==0);
XXXXX(4) ... Critical Section ...;XXX(4) ... Critical Section ...;
XXXXX(5) flag[0]=false;XXXXXXXXXXxX(5) flag[1]=false;
XXX}XXXXXXXXXXXXXXXXXXXXXXXX}
XXEndXXXXXXXXXXXXXXXXXXXXXXXEnd
Copyright © Dr. Karim Lounis, 2023/2024. Page 4/6
If the two statements (1) and (2) are swapped in both processes (i.e., you swap within
a given code), state whether the altered code still satisfies the three requirements of
the critical section problem. Explain which requirement has been violated [2.0 points].

The mutual exclusion is violated [0.5 point]


This can happen as follows [1.5 point]:
a) P0 sets turn to 1 and get interrupted,
b) P1 sets turn to 0,
c) P1 sets flag[1] to true, and enters the critical section.
d) P0 sets flag[0] to true, and enters the critical section.

6. In the following, check the box of the correct answer [0.5 × 8 = 4 points]:

(a) Can an operating system deallocate the CPU from a process or thread that is
currently executing its critical section?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No
(b) Peterson’s algorithm can be formulated to as a solution to the critical section
problem involving n ≥ 2 processes?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No

7. Consider the Peterson’s algorithm for the critical section problem.

(c) If there is a context-switching, then an interrupt has occurred?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No
(d) If there is an interrupt, a context-switching occurs?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No
(e) When an interrupt occurs, the interrupt handler uses the kernel stack to save the
state (registers content) of the interrupted process?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No
(f) A thread shares the code segment, data segment, and the heap segment with the
main process that created it?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No

Copyright © Dr. Karim Lounis, 2023/2024. Page 5/6


(g) Assuming a byte-addressable memory (RAM) and a 16 bit address bus, an oper-
ating system can store up to 32 MB of information on that RAM?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No
(h) The PC (Program Counter) register is always incremented by 1?

XXXXXX□ YesXXXXXXXXXXXXXXXX□ No

Copyright © Dr. Karim Lounis, 2023/2024. Page 6/6

You might also like