Loading W Code...
Top questions asked at FAANG & product companies
20
Questions
40
Minutes
FAANG
Companies
empty = N, full = 0, mutex = 1
Producer:
wait(empty)
wait(mutex)
// add item
signal(mutex)
signal(full)
Consumer:
wait(full)
wait(mutex)
// remove item
signal(mutex)
signal(empty)
``
counter = 5
P1: counter++ (read 5, increment, write 6)
P2: counter-- (read 5, decrement, write 4)
If interleaved:
P1 reads 5
P2 reads 5
P1 writes 6
P2 writes 4 ← Wrong! Should be 5
``
Solutions:
1. Mutex/Locks
• Only one process in critical section
2. Semaphores
• More flexible than mutex
3. Monitors
• High-level synchronization
4. Atomic Operations
• Hardware support for atomicity
Key: Always protect shared resources with synchronization primitives!
User Mode
│
│ System Call / Interrupt
▼
Kernel Mode
│
│ Return
▼
User Mode
``
Why Two Modes?
fd = open("file.txt", O_RDONLY); // System call
read(fd, buffer, 100); // System call
close(fd); // System call
``
• File type, permissions
• Owner, group
• Size, timestamps
• Direct pointers (12)
• Single indirect pointer
• Double indirect pointer
• Triple indirect pointer
``c
#include
#include
int main() {
int pid = fork();
if (pid == 0) {
// Child process
printf("I am child, my PID: %d\n", getpid());
} else if (pid > 0) {
// Parent process
printf("I am parent, child PID: %d\n", pid);
} else {
// Error
printf("Fork failed!\n");
}
return 0;
}
``
Copy-on-Write (COW):
Child finishes → sends SIGCHLD to parent
Parent doesn't call wait() → child becomes zombie
``
Fix: