System Programming Cat 2-1
System Programming Cat 2-1
CAT TWO
CIT-227-093/2023
PHILIP KARANJA WAMBUGU
a) Explain the difference between a mutex and a semaphore in thread synchronization. [4 Marks]
Mutex (Mutual Exclusion): A locking mechanism that allows only one thread to access a
critical section at a time.
Semaphore: A signalling mechanism that allows multiple threads to access a resource,
depending on the counter value (binary or counting semaphores).
Difference: A mutex is strictly for mutual exclusion, while a semaphore allows a controlled
number of threads to access a resource.
b) Explain the concept of paging and how it is used in modern operating systems. [8 Marks]
Paging: A memory management technique that divides processes into fixed-size pages and
maps them to physical frames in RAM.
Usage in Modern OS:
Prevents fragmentation by allocating fixed-size memory blocks.
Uses a page table to track logical-to-physical address mapping.
Supports virtual memory, allowing processes to use more memory than physically available
by swapping pages.
c) Describe three advantages and three disadvantages of using shared memory for inter-process
communication. [6 Marks]
Advantages:
1. Fast data exchange – No need for kernel intervention.
2. Efficient communication – Reduces overhead compared to other IPC methods.
3. Large data transfer – Suitable for handling large amounts of data.
Disadvantages:
1. Synchronization required – Processes must use locks to prevent race conditions.
2. Security risks – Unauthorized processes might access shared data.
3. Complex setup – Requires manual allocation and management.
d) Explain how strace can be used for debugging in Linux systems. [6 Marks]
Strace: A Linux utility that traces system calls made by a process.
Usage in Debugging:
Helps identify issues in program execution by showing system calls.
Detects permission errors, missing files, or unexpected behavior.
Command: strace -o log.txt ./program (Logs system calls for debugging).
e) Explain the purpose of cron jobs and provide an example of a cron job that runs a script every day
at 4 AM. [6 Marks]
Purpose: Automates task execution at scheduled times.
Example cron job:
Runs /path/to/script.sh daily at 4 AM.
f) Explain two advantages and two disadvantages of using multi-threading in systems programming.
[4 Marks]
Advantages:
1. Improved performance – Enables concurrent execution of tasks.
2. Efficient resource use – Shares memory space between threads.
Disadvantages:
1. Synchronization complexity – Requires careful handling of shared resources.
2. Difficult debugging – Race conditions and deadlocks make debugging harder.
g) How does the malloc() and free() function work in C? [6 Marks]
malloc(): Allocates a block of memory dynamically.
o Example: int *ptr = (int*) malloc(sizeof(int));
free(): Releases allocated memory to prevent memory leaks.
Example: free(ptr);
h) How does the fork() system call work in UNIX/Linux? Provide an example code. [10 Marks]
Fork(): Creates a child process that runs concurrently with the parent
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
printf("Child process\n");
} else {
printf("Parent process\n");
}
return 0;}