OS fall2023
OS fall2023
(Fall 2023)
Solution by (CH MUZAMMIL) & (MISS HARAM)
// Producer Process
while (true) {
wait(empty);
wait(mutex);
// Add item to buffer
signal(mutex);
signal(full);
}
// Consumer Process
while (true) {
wait(full);
wait(mutex);
// Remove item from buffer
signal(mutex);
signal(empty);
}
(D) What are Zombie Processes? When are these created and
terminated?
Zombie Processes: These are processes that have completed execution but still occupy an entry
in the process table. They occur when a child process ends, but its parent has not yet collected
its exit status using wait().
• Creation: A zombie process is created when a process ends, but its parent hasn’t collected
the exit status using the wait() system call.
• Termination: Zombie processes are removed when the parent process calls wait() to
get the exit status. If the parent doesn’t do this, the zombie process will stay in the process
table until the parent itself finishes, at which point the system will clean up the zombie.
LONG QUESTIONS
(A) Consider a system with a 6-bit virtual address space and a size
of frames/pages of 16 bytes. The page table provides a mapping
from logical to physical memory:
Page # Frame #
0 8
1 3
2 11
3 1
Translate the following virtual addresses to physical addresses (all addresses are in decimal):
Virtual address Physical address
20
40
25
15
35
32
Answer:
Given a 6-bit virtual address space and a 16-byte page size, we use a 2-bit offset for each
address (since log2(16) = 4).
Steps:
1. Split the virtual address into page number and offset.
2. Map the page number to the frame number using the table.
3. Calculate the physical address by shifting frame number and adding the offset.
Virtual Address: 20
• Page number: 20÷16 = 1 (Page 1)
• Offset: 20 mod 16 = 4
• Mapped frame: Frame 3 (from the page table for Page 1)
• Physical address: Frame 3×16+4 = 48+4 = 52
Virtual Address: 40
• Page number: 40÷16 = 2 (Page 2)
• Offset: 40 mod 16 = 8
• Mapped frame: Frame 11 (from the page table for Page 2)
• Physical address: Frame 11×16+8 = 176+8 = 184
Virtual Address: 25
• Page number: 25÷16 = 1 (Page 1)
• Offset: 25 mod 16 =9
• Mapped frame: Frame 3 (from the page table for Page 1)
• Physical address: Frame 3×16+9 = 48+9 =57
Virtual Address: 15
• Page number: 15÷16 = 0 (Page 0)
• Offset: 15 mod 16=15
• Mapped frame: Frame 8 (from the page table for Page 0)
• Physical address: Frame 8×16+15 = 128+15 = 143
Virtual Address: 35
• Page number: 35÷16 = 2 (Page 2)
• Offset: 35 mod 16 =335
• Mapped frame: Frame 11 (from the page table for Page 2)
• Physical address: Frame 11×16+3 = 176+3 = 179
Virtual Address: 32
• Page number: 32÷16 = 2 (Page 2)
• Offset: 32 mod 16 =0
• Mapped frame: Frame 11 (from the page table for Page 2)
• Physical address: Frame 11×16+0 = 176+0 =176
(B) Assume there are 5 processes (P0 through P4) and 4 types of
resources (A, B, C, and D). At T0, the allocation and maximum demand
are provided in tables.
Allocation Max
A B C D A B C D
P0 0 1 1 0 0 2 1 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
If the system has one instance of A, five instances of B, and two instances of C in hand,
determine if the system is in a safe state. If yes, provide the safe sequence.
Answer:
Step 3: Conclusion
All processes can finish, so the system is in a safe state. The safe sequence is:
P0 → P3 → P1 → P2 → P4.
// Create 4 threads
for (int i = 0; i < 4; i++) {
pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]);
}
Explanation:
1. pthread_create() is used to create each thread, passing the function thread_function to be
executed by each thread.
2. pthread_join() ensures that the main program waits for all threads to finish before it ends.
--------------------------------------------------------------------------------------------------------------------