CS604 – Operating Systems: Midterm Important Subjective (59 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS604 Course Page · CS604 MCQ Pack (281 MCQs)
-
Which command is used in the Linux environment to get information about processes? Describe its commonly used options.
Topic: Processes, Threads & UNIX/Linux Commands
The ps command gives a snapshot of the current processes. Without options, ps prints information about processes owned by the user. Some of the commonly used options are: -e : selects all processes -l : formats the output in the long format -u : displays the information in user-oriented format The top command can also be used to display information about the top processes continuously.
-
If a process is in the background and we want to move it into the foreground, which UNIX/Linux command is used? Also write the command used to put a job into the background.
Topic: Processes, Threads & UNIX/Linux Commands
The fg command is used to resume the execution of a suspended job in the foreground or to move a background job into the foreground. Its syntax is: fg [%job_id] where job_id is the job ID (not the process ID) of the suspended or background process. If %job_id is omitted, the current job is assumed. Similarly, the bg command is used to put the current or a suspended process into the background: bg [%job_id]
-
How can a foreground process be suspended? How can a process be terminated in UNIX/Linux?
Topic: Processes, Threads & UNIX/Linux Commands
A foreground process can be suspended by pressing <Ctrl-Z>, which sends a STOP/SUSPEND signal to the process. A foreground process can be terminated by pressing <Ctrl-C>. A process can also be terminated with the kill command, which sends a signal to the process whose process ID is specified in the command line. Its syntax is: kill [-signal] PID When executed without a signal number, the kill command sends the SIGTERM signal to the process.
-
How would you display the status of suspended and background jobs in UNIX/Linux?
Topic: Processes, Threads & UNIX/Linux Commands
The jobs command is used to display the status of suspended and background processes (jobs) of the current shell session.
-
What is the purpose of the fork() system call? Discuss what happens when it is executed, from the user's and the kernel's point of view.
Topic: Processes, Threads & UNIX/Linux Commands
When the fork system call is executed, a new process is created. The original process is called the parent process and the new process is called the child process. The new process consists of a copy of the address space of the parent, so the child gets copies of the parent's code, data and stack. This mechanism allows the parent process to communicate easily with its child process. On success, both processes continue execution at the instruction after the fork call, with one difference: the return code for the fork system call is zero (0) for the child process, while the process identifier (PID) of the child is returned to the parent process. On failure, -1 is returned in the parent's context, no child process is created, and an error number (errno) is set appropriately.
-
Write down the similarities and dissimilarities between a process and a thread.
Topic: Processes, Threads & UNIX/Linux Commands
Similarities: 1) Both share the CPU and execute sequentially. 2) Both can create children (a process can create child processes; a thread can create child threads). 3) If one process/thread is blocked, the next can start to run. Dissimilarities: 1) Threads are not independent like processes; processes are independent of one another. 2) All threads can access every address in the task (they share the address space), unlike processes, which have separate address spaces. 3) Threads are designed to assist one another, whereas processes may or may not assist one another because they may originate from different users.
-
What is a thread? What does a thread comprise of, and what does it share with other threads of the same process?
Topic: Processes, Threads & UNIX/Linux Commands
A thread, sometimes called a lightweight process (LWP), is a basic unit of CPU utilization and executes within the address space of the process that creates it. It comprises a thread ID, a program counter, a register set, errno, and a stack. It shares with other threads belonging to the same process its code section, data section, current working directory, user and group IDs, signal setup and handlers, and other operating system resources such as open files.
-
What are the advantages and disadvantages of multithreading? Is resource sharing an advantage or a disadvantage of threads? Explain briefly.
Topic: Processes, Threads & UNIX/Linux Commands
Advantages of threads: 1) Responsiveness: Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. 2) Resource sharing: By default, threads share the memory and the resources of the process to which they belong. Code sharing allows an application to have several different threads of activity all within the same address space. 3) Economy: Allocating memory and resources for process creation is costly. Because threads share the resources of the process to which they belong, it is more economical to create and context-switch threads. 4) Utilization of multiprocessor architectures: The benefits of multithreading can be greatly increased in a multiprocessor environment, where each thread may be running in parallel on a different processor. A single-threaded process can only run on one CPU no matter how many are available. Disadvantages of threads: 1) Resource sharing: Whereas resource sharing is one of the major advantages of threads, it is also a disadvantage because proper synchronization is needed between threads for accessing the shared resources (e.g., data and files). 2) Difficult programming model: It is difficult to write, debug, and maintain multi-threaded programs for an average user. This is particularly true when it comes to writing code for synchronized access to shared resources. So, resource sharing is both an advantage and a disadvantage: it makes communication between threads easy and fast, but it requires proper synchronization to keep shared data consistent.
-
Differentiate between user-level threads and kernel-level threads.
Topic: Processes, Threads & UNIX/Linux Commands
User threads are supported above the kernel and are implemented by a thread library at the user level. The library provides support for thread creation, scheduling, and management with no support from the kernel. Since the kernel is unaware of user-level threads, all thread creation and scheduling are done in the user space without the need for kernel intervention, and therefore they are fast to create and manage. However, if the kernel is single-threaded, any user-level thread performing a blocking system call will cause the entire process to block, even if other threads are available to run within the application. Examples of user thread libraries are POSIX Pthreads, Solaris 2 UI-threads, and Mach C-threads. Kernel threads are supported directly by the operating system. The kernel performs thread creation, scheduling, and management in kernel space, so kernel-level threads are slower to create and manage compared to user-level threads. However, since the kernel is managing the threads, if a thread performs a blocking system call, the kernel can schedule another thread in the application for execution. Windows NT, Windows 2000, Solaris, BeOS and Tru64 UNIX support kernel threads.
-
What is the main disadvantage of the one-to-one multithreading model?
Topic: Processes, Threads & UNIX/Linux Commands
In the one-to-one model, each user thread is mapped to a separate kernel thread, which provides full concurrency. Its main disadvantage is the overhead of creating a kernel thread for every user thread, which can burden the performance of the system if a large number of threads are created.
-
What will happen if exit() is called instead of pthread_exit() in a multithreaded program?
Topic: Processes, Threads & UNIX/Linux Commands
If the exit() system call is used, it will terminate the whole process instead of just the calling thread, because the purpose of the exit() system call is to terminate a process, not a thread. To terminate only the calling thread while keeping the process (and its other threads) alive, pthread_exit() must be used.
-
If a process exits and there are still threads of that process running, will they continue to run? Justify your answer.
Topic: Processes, Threads & UNIX/Linux Commands
No. Threads exist only within the address space of their process, so when a process terminates, all of its threads terminate with it. Similarly, if a thread receives a signal (for example, Ctrl-C) whose default action is to terminate the process, the whole process — including all of its running threads — is terminated.
-
Write a C code fragment to create a thread. Take the thread ID in a variable named newThread and run the MyThreadFunc function in the new thread.
Topic: Processes, Threads & UNIX/Linux Commands
#include <stdio.h> #include <stdlib.h> #include <pthread.h> /* Prototype for the function to be passed to our thread */ void* MyThreadFunc(void *arg); int main() { pthread_t newThread; /* Create a thread and have it run MyThreadFunc */ pthread_create(&newThread, NULL, MyThreadFunc, NULL); return 0; } void* MyThreadFunc(void* arg) { printf("The thread.\n"); return NULL; }
-
How are C programs compiled and executed in Linux using the gcc compiler? Explain the use of the -o and -l options.
Topic: Processes, Threads & UNIX/Linux Commands
To compile a source file titled program.c, type: $ gcc program.c This produces an executable file named a.out, which can be run by typing: $ ./a.out The executable program can be stored in a specific file by using the -o option. For example, the following command stores the executable in a file named assignment: $ gcc program.c -o assignment A library can be linked explicitly by using the -l option. For example, the following command links the math library with the program while creating the executable file assignment: $ gcc program.c -o assignment -lm
-
What does the term 'redirection' mean in UNIX/Linux? Write down the basic syntax for input, output and error redirection.
Topic: Processes, Threads & UNIX/Linux Commands
The standard files stdin, stdout and stderr are attached to the terminal by default. Linux redirection features can be used to detach these default files from stdin, stdout and stderr and attach other files with them for a single execution of a command. This act of detaching the default files and attaching other files is known as input, output and error redirection. Input redirection (stdin is attached to a file instead of the keyboard): command < input-file or command 0< input-file Output redirection (stdout is attached to a file instead of the screen): command > output-file or command 1> output-file Error redirection (stderr is attached to a file instead of the screen): command 2> error-file
-
UNIX/Linux provides many commands. Write the method through which these commands can communicate with each other.
Topic: Processes, Threads & UNIX/Linux Commands
Commands communicate with each other through pipes. A pipe connects the standard output of one command to the standard input of another command, so the output of the first command becomes the input of the second command without creating any temporary file. On the command line, a pipe is represented by the | symbol, and several commands can be chained together, for example: Cmd1 | Cmd2 | ... | CmdN For example, the output of the ls command can be passed to the wc command to count the number of files in a directory.
-
What are named pipes or FIFOs?
Topic: Inter-Process Communication: Pipes & FIFOs
FIFOs (also known as named pipes) are a method of inter-process communication used for communication between related or unrelated processes on a UNIX/Linux system. Unlike an unnamed pipe, a FIFO has a name in the file system, so any process that knows the name can open and use it for communication.
-
Differentiate between a pipe and a FIFO.
Topic: Inter-Process Communication: Pipes & FIFOs
A FIFO is similar to a pipe: both provide a one-way (half-duplex) flow of data. The difference between pipes and FIFOs is the manner in which they are created and opened; once these tasks have been accomplished, I/O on pipes and FIFOs has exactly the same semantics. A FIFO is identified in the file system with a name (it is a named pipe), whereas an unnamed pipe is not. FIFOs are identified by an access point which is a file within the file system, whereas pipes are identified by an access point which is simply an allotted inode. Another major difference is that FIFOs last throughout the life-cycle of the system (until explicitly removed using unlink), while pipes last only during the life-cycle of the process in which they were created. Also, pipes can be inherited only by related processes (descendants of a single process), whereas FIFOs can be shared by unrelated processes as well.
-
How can a pipe be created? What does the pipe() system call return, and when does it fail?
Topic: Inter-Process Communication: Pipes & FIFOs
A pipe is created with the pipe() system call: int pipe(int pfds[2]); The pipe() system call creates a pipe and returns two file descriptors in the array pfds: pfds[0] is the descriptor for reading from the pipe and pfds[1] is the descriptor for writing to the pipe. On success it returns 0; on failure it returns -1 and no pipe is created. The pipe() system call can fail for reasons including the following: 1) At least two slots are not empty in the per-process file descriptor table. 2) Too many files or pipes are already open in the process. 3) Buffer space is not available in the kernel. 4) The system-wide file table is full.
-
Why are pipes and FIFOs useful? Give at least two uses.
Topic: Inter-Process Communication: Pipes & FIFOs
Two common uses of pipes/FIFOs are: 1) In client-server applications, FIFOs are used to pass data between a server process and its client processes. 2) They are used by shell commands to pass data from one shell pipeline to another, without creating temporary files.
-
The mknod() system call may fail due to different reasons. Identify at least three failure scenarios of mknod(). Do you think the reasons for the failure of the mknod() system call and the mkfifo() library call are the same? Give a reason to support your answer.
Topic: Inter-Process Communication: Pipes & FIFOs
As is the case with all system calls in UNIX/Linux, mknod() returns -1 on failure and errno is set accordingly. Some of the reasons for this call to fail are: 1) A file with the given name already exists. 2) The pathname is too long. 3) A component in the pathname is not searchable, non-existent, or not a directory. 4) The destination directory is read-only. 5) Not enough memory space is available. 6) A signal is caught during the execution of mknod(). Yes, the reasons are pretty much the same, because the normal file I/O system calls (close(), read(), write(), unlink(), etc.) all work with FIFOs, and mkfifo() internally invokes the mknod() system call to create the FIFO. Hence the reasons for the failure of mkfifo() are pretty much the same as those for the mknod() call.
-
Consider the following statement: read(pfds[1], buf, -5); Do you think the given statement will work properly? Give reasons to support your answer.
Topic: Inter-Process Communication: Pipes & FIFOs
No, the statement will not work properly, for two reasons: 1) pfds[1] is the write end of the pipe; data is read from a pipe through pfds[0], the read end. Using pfds[1] for reading is incorrect. 2) The third argument of read() is the number of bytes to read, which cannot be negative; -5 is an invalid count. Because of these errors, the read() call will fail and return -1.
-
Consider the following session: $ gcc server.c -o server $ gcc client.c -o client $ server & [1] 432056 $ client Hello, world! Hello, class! Out of the server and client processes in the given session, which process runs in the foreground and which runs in the background? Also tell what [1] and 432056 represent.
Topic: Inter-Process Communication: Pipes & FIFOs
The server process runs in the background, because its command line is terminated with the & operator, which runs a command in the background. The client process runs in the foreground, because it is executed without the & operator. In the output line [1] 432056: [1] is the job number (job ID) assigned by the shell to the background job. 432056 is the process ID (PID) of the server process.
-
How can you differentiate between a library call and a system call? Explain briefly, giving at least one example.
Topic: Inter-Process Communication: Pipes & FIFOs
System calls are the functions provided by the kernel; they form the interface through which user programs request services from the operating system, and they are handled directly by the kernel. Examples are read(), write(), fork() and mknod(). Library calls are functions provided within program libraries. The program making a library call uses the library code linked with it, and a library call may or may not invoke system calls internally. For example, mkfifo() is a library call that internally invokes the mknod() system call to create a FIFO.
-
List and define the different metrics (criteria) by which we can evaluate a CPU scheduler. List at least four.
Topic: CPU Scheduling
CPU scheduling algorithms can be evaluated using the following criteria: 1) CPU utilization: The percentage of time the CPU is kept busy. We want to keep the CPU as busy as possible. 2) Throughput: The number of processes completed per unit time. 3) Turnaround time: The interval from the time of submission of a process to the time of its completion. 4) Waiting time: The total time a process spends waiting in the ready queue. CPU scheduling affects only the amount of time a process spends waiting in the ready queue. 5) Response time: The time from the submission of a request until the first response (output) is produced; it is important for user-friendly, interactive systems. When evaluating a scheduler's performance, utilization and throughput are generally traded off for better response time.
-
What is the difference between preemptive and non-preemptive scheduling? Give one example of each.
Topic: CPU Scheduling
Preemptive scheduling allows a process to be interrupted in the middle of its execution; the CPU is taken away from it and allocated to another process. Non-preemptive scheduling ensures that a process relinquishes control of the CPU only when it finishes its current CPU burst (or blocks for I/O or terminates). FCFS is a non-preemptive scheduling algorithm. Round Robin is a preemptive scheduling algorithm. The SJF algorithm may be either preemptive or non-preemptive; preemptive SJF scheduling is sometimes called shortest-remaining-time-first scheduling.
-
Write the difference between the SJF and the Shortest Remaining Time First (SRTF) scheduling algorithms.
Topic: CPU Scheduling
Shortest Job First (SJF) is the non-preemptive version: once the CPU is given to a process, it cannot be preempted until it completes its CPU burst. It is generally used for long-term scheduling in batch systems, where the length of a job can be taken as the process time limit that a user specifies when submitting the job. Shortest Remaining Time First (SRTF) is the preemptive version of SJF: whenever a new job arrives, its remaining service time is compared with the remaining time of the currently running job, and the job with the shorter remaining time gets the CPU. For all but the longest jobs, SRTF gives better (lower) average waiting time and a better response ratio than SJF.
-
Calculate the average waiting time for the following set of processes using preemptive Shortest Job First (Shortest Remaining Time First) scheduling: Process Arrival Time Burst Time P1 0 7 P2 2 4 P3 4 1 P4 5 4
Topic: CPU Scheduling
Preemptive SJF scheduling is sometimes called shortest-remaining-time-first scheduling. The Gantt chart for the given processes is: | P1 | P2 | P3 | P2 | P4 | P1 | 0 2 4 5 7 11 16 Waiting time of each process = (completion time - arrival time - burst time): P1: 16 - 0 - 7 = 9 P2: 7 - 2 - 4 = 1 P3: 5 - 4 - 1 = 0 P4: 11 - 5 - 4 = 2 Average waiting time = (9 + 1 + 0 + 2) / 4 = 12 / 4 = 3.
-
Consider the performance of the FCFS algorithm for three compute-bound processes P1 (24 seconds), P2 (3 seconds) and P3 (3 seconds), arriving in the order P1, P2, P3. Calculate the waiting time per process, the average waiting time, and the average turnaround time.
Topic: CPU Scheduling
Under FCFS, the processes execute in the order P1, P2, P3. Waiting times: P1 = 0; P2 = 24; P3 = 27. Average waiting time = (0 + 24 + 27) / 3 = 17. Turnaround times: P1 = 24; P2 = 27; P3 = 30. Average turnaround time = (24 + 27 + 30) / 3 = 27. Note that if the processes arrived in the order P2, P3, P1, the average waiting time would drop to (0 + 3 + 6) / 3 = 3, which shows that the average waiting time under FCFS is highly dependent on the arrival order (the convoy effect).
-
Calculate the performance of the FCFS algorithm for three processes ready in the queue at time 0 with burst times P1 = 20 seconds, P2 = 5 seconds and P3 = 4 seconds. Calculate the waiting time per process and the average waiting time when (i) the processes arrive in the order P1, P2, P3, and (ii) the processes arrive in the order P3, P2, P1.
Topic: CPU Scheduling
(i) Arrival order P1, P2, P3: Waiting times: P1 = 0; P2 = 20; P3 = 25. Average waiting time = (0 + 20 + 25) / 3 = 45 / 3 = 15. (ii) Arrival order P3, P2, P1: Waiting times: P3 = 0; P2 = 4; P1 = 9. Average waiting time = (0 + 4 + 9) / 3 = 13 / 3 = 4.33 (approximately). The comparison shows that the average waiting time under FCFS varies greatly with the arrival order of the processes.
-
What is the convoy effect in FCFS scheduling?
Topic: CPU Scheduling
When the FCFS scheduling algorithm is used, the convoy effect occurs when short processes wait behind one long (CPU-bound) process to use the CPU, and then enter the ready queue in a convoy after completing their I/O. As a result, CPU and device utilization can be lower than possible, and the average waiting time becomes quite long.
-
Write briefly about multilevel feedback queue scheduling.
Topic: CPU Scheduling
Multilevel feedback queue scheduling allows a process to move between queues. The idea is to separate processes with different CPU burst characteristics. If a process uses too much CPU time, it will be moved to a lower-priority queue. This scheme leaves I/O-bound and interactive processes in the higher-priority queues. Similarly, a process that waits too long in a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents starvation. In general, a multilevel feedback queue scheduler is defined by the following parameters: 1) The number of queues. 2) The scheduling algorithm for each queue. 3) The method used to determine when to upgrade a process to a higher-priority queue. 4) The method used to determine when to demote a process to a lower-priority queue. 5) The method used to determine which queue a process will enter when it needs service.
-
Define any three queues that are used in multilevel feedback queue scheduling.
Topic: CPU Scheduling
A typical multilevel feedback queue scheduler uses three queues, for example: 1) Queue 0: Round Robin scheduling with a time quantum of 8 milliseconds. New processes enter this queue; if a process finishes within the quantum, it leaves the system. 2) Queue 1: Round Robin scheduling with a time quantum of 16 milliseconds. A process that does not complete in queue 0 is demoted to this queue. 3) Queue 2: First-Come, First-Served scheduling. A process that does not complete in queue 1 is demoted to this queue and runs only when the higher queues are empty. In this way, short and interactive processes finish quickly in the higher queues, while long CPU-bound processes sink to the lower queues.
-
Describe the UNIX System V scheduling algorithm.
Topic: CPU Scheduling
The UNIX System V scheduling algorithm is essentially a multilevel feedback priority queues algorithm with Round Robin within each queue, the quantum being equal to 1 second. The priorities are divided into two groups/bands: 1) Kernel Group 2) User Group Priorities in the Kernel Group are assigned in a manner to minimize bottlenecks, i.e., processes waiting in a lower-level routine get higher priorities than those waiting at relatively higher-level routines. In decreasing order of priority, the kernel bands are: 1) Swapper 2) Block I/O device control processes 3) File manipulation processes 4) Character I/O device control processes 5) User processes User processes are the lowest-prioritized processes in the Kernel Group bands, and priorities of user processes are adjusted dynamically using their recent CPU usage and nice value.
-
How is Round Robin scheduling implemented?
Topic: CPU Scheduling
To implement Round Robin scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. One of the two things will then happen: 1) The process may have a CPU burst of less than 1 time quantum, in which case the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. 2) If the CPU burst of the currently running process is longer than one time quantum, the timer will go off and cause an interrupt to the operating system. A context switch will happen, the current process will be put at the tail of the ready queue, and the newly scheduled process will be given the CPU.
-
What are the advantages and disadvantages of the Round Robin scheduling algorithm?
Topic: CPU Scheduling
Advantages: 1) It is simple and easy to implement. 2) It is fair: every process gets an equal share of the CPU, so no process suffers starvation. 3) It gives good response time, which makes it well suited for time-sharing systems. Disadvantages: 1) The average waiting time under the RR policy is often quite long. 2) Its performance depends heavily on the size of the time quantum: if the quantum is too large, RR degenerates into FCFS; if it is too small, frequent context switching causes high overhead. 3) It does not give priority to short or interactive processes over long ones.
-
Out of Shortest Remaining Time First (SRTF) and Round Robin scheduling algorithms, which one is best suited for a time-sharing system where response time is an important performance criterion? Give reasons.
Topic: CPU Scheduling
Typically, Round Robin has a higher average turnaround time than SJF/SRTF, but a better (shorter) response time. In time-sharing systems, a shorter response time for a process is more important than a shorter turnaround time, because many users interact with the system simultaneously. Thus, the Round Robin scheduler matches the requirements of time-sharing systems better than the SJF algorithm. The SJF scheduler is better suited for batch systems, in which minimizing the turnaround time is the main criterion.
-
Why is indefinite blocking (starvation) a major problem with priority scheduling algorithms? How can it be solved?
Topic: CPU Scheduling
A major problem with priority scheduling algorithms is indefinite blocking (or starvation). A process that is ready to run but lacking the CPU can be considered blocked — waiting for the CPU. A priority scheduling algorithm can leave some low-priority processes waiting indefinitely for the CPU, because higher-priority processes keep arriving and are always scheduled first. Legend has it that when they were phasing out the IBM 7094 at MIT in 1973, they found a process stuck in the ready queue since 1967! This problem can be solved by aging: gradually increasing the priority of processes that wait in the system for a long time, so that every process eventually gets the CPU.
-
Differentiate between turnaround time and response time.
Topic: CPU Scheduling
Turnaround time is the interval from the time of submission of a process to the time of its completion, i.e., it includes the time spent waiting in the ready queue, executing on the CPU, and doing I/O. Response time is the time from the submission of a request until the first response (first output) is produced. It measures how soon the system starts responding, which is important in interactive systems.
-
Define waiting time with respect to CPU scheduling. How does waiting time relate to the evaluation of CPU scheduling algorithms?
Topic: CPU Scheduling
Waiting time is the total time a process spends waiting in the ready queue. It is one of the important evaluation criteria (metrics) for CPU scheduling algorithms, because CPU scheduling does not affect the time a process spends executing or doing I/O — it affects only the amount of time the process spends waiting in the ready queue. CPU schedulers aim at minimizing the average waiting time of processes; however, the average waiting time is different for different scheduling algorithms, so algorithms are compared on the basis of the average waiting time they produce for a given workload.
-
Which scheduling algorithm is best according to the waiting time criterion? Also classify the following algorithms as preemptive or non-preemptive: FCFS, SJF, SRTF, Priority, Round Robin.
Topic: CPU Scheduling
The Shortest Job First (SJF) algorithm is the best (optimal) scheduling algorithm according to the waiting time criterion, because it gives the minimum average waiting time for a given set of processes. Its preemptive version, Shortest Remaining Time First (SRTF), is optimal when processes arrive over time. Classification: 1) FCFS: Non-preemptive. 2) SJF: Can be preemptive or non-preemptive. 3) SRTF: Preemptive (it is the preemptive version of SJF). 4) Priority scheduling: Can be preemptive or non-preemptive. 5) Round Robin: Preemptive (preemption is based on the time quantum).
-
You may imagine that the multilevel feedback queue (MLFQ) scheduling algorithm is the same as the Shortest Job First scheduling algorithm. Justify your answer, either yes or no.
Topic: CPU Scheduling
No, the two algorithms are not the same, although MLFQ tries to approximate the behaviour of SJF. SJF requires knowledge of the length of the next CPU burst of each process in advance, which is generally not available. MLFQ, on the other hand, does not need this information: it learns from the past behaviour of processes. Processes that use little CPU (short or I/O-bound jobs) stay in the higher-priority queues and therefore run first — just as they would under SJF — while processes that use too much CPU time are demoted to lower-priority queues. MLFQ is also preemptive, uses different scheduling algorithms (usually Round Robin) in its queues, and applies aging to prevent starvation, none of which is part of plain SJF.
-
How does open source software help us to test scheduling algorithms?
Topic: CPU Scheduling
The open source software licensing has made it possible for us to test various scheduling algorithms by implementing them in the Linux kernel and measuring their true performance. The major difficulty is the cost of this approach: the expense is incurred in coding the algorithm and modifying the operating system to support it, as well as its required data structures. The other difficulty with any algorithm evaluation is that the environment in which the algorithm will be used keeps changing.
-
Write down the structure of the software-based solution for the critical section problem.
Topic: Process Synchronization & Semaphores
In the software-based solution, each process has the following structure: do { entry section critical section exit section remainder section } while (1); The entry section is the section of code in which the process requests permission to enter its critical section; the exit section follows the critical section; and the rest of the code is the remainder section.
-
Differentiate between the entry section and the remainder section.
Topic: Process Synchronization & Semaphores
Each process must first request permission to enter its critical section. The section of code implementing this request is called the entry section. The remaining code of the process — the part that does not involve the shared data and in which the process does its other work after leaving the critical section — is called the remainder section.
-
What are the three requirements that a solution to the critical section problem must satisfy?
Topic: Process Synchronization & Semaphores
A solution to the critical section problem must satisfy the following three requirements: 1) Mutual Exclusion: If a process is executing in its critical section, then no other process can be executing in its critical section. 2) Progress: If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefinitely. 3) Bounded Waiting: There exists a bound on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.
-
Differentiate between 'progress' and 'bounded waiting' in the critical section problem.
Topic: Process Synchronization & Semaphores
Progress: If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder section can participate in the decision on which will enter its critical section next, and this selection cannot be postponed indefinitely. Bounded Waiting: There exists a bound on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.
-
What assumptions are made while formulating a solution to the critical section problem?
Topic: Process Synchronization & Semaphores
While formulating a solution to the critical section problem, the following assumptions are made: 1) Each process executes at a nonzero speed. 2) No assumption can be made regarding the relative speeds of the N processes.
-
Define race condition. How can this condition be prevented?
Topic: Process Synchronization & Semaphores
A situation in which several processes access and manipulate the same data concurrently, and the outcome of the manipulation depends on the particular order in which the access takes place, is called a race condition. To prevent race conditions, concurrent processes must be synchronized: the processes must be prevented from being simultaneously inside the code that accesses the shared data. This is done by identifying the critical sections and ensuring, through software, operating system or hardware based solutions (such as semaphores), that only one process executes in its critical section at a time (mutual exclusion).
-
Why is process synchronization needed when concurrent processes share data and resources?
Topic: Process Synchronization & Semaphores
Concurrent processes or threads often need access to shared data and shared resources. If there is no controlled access to shared data, it is often possible to obtain an inconsistent state of this data. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes, and hence various process synchronization methods are used.
-
Analyze the following algorithm (proposed for process Pi, where the other process is Pj) to solve the critical section problem, and explain whether it satisfies the mutual exclusion requirement. Justify your answer. boolean flag[2]; int turn; do { flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } while (1);
Topic: Process Synchronization & Semaphores
Yes, the algorithm satisfies mutual exclusion. To enter its critical section, Pi sets flag[i] to true and sets turn to j, asserting that if the other process wishes to enter its critical section, it may do so. If both processes try to enter at the same time, they will attempt to set turn to i and j respectively; however, only one of these assignments will last — the other will be overwritten — so the eventual value of turn decides which process gets to enter its critical section. To prove mutual exclusion, note that Pi enters its critical section only if either flag[j] == false or turn == i. Also, if both processes were executing in their critical sections at the same time, then flag[0] == flag[1] == true. These two observations suggest that P0 and P1 could not have found both conditions in the while statement true at the same time, since the value of turn can be either 0 or 1. Hence, only one process, say P0, must have successfully exited the while statement, and mutual exclusion is preserved. The algorithm also satisfies progress and bounded waiting: a process can be delayed only while flag[j] == true and turn == j, and after Pj leaves its critical section (or sets turn to i), Pi enters its critical section after at most one entry by Pj.
-
Analyze the following algorithm to solve the critical section problem and identify its shortcomings. do { while (turn != i); critical section turn = j; remainder section } while (1);
Topic: Process Synchronization & Semaphores
This solution ensures mutual exclusion, that is, only one process at a time can be in its critical section. However, it does not satisfy the progress requirement, since it requires strict alternation of the processes in the execution of the critical section. For example, if turn == 0 and P1 is ready to enter its critical section, P1 cannot do so even though P0 may be in its remainder section and does not wish to enter. The bounded waiting condition is satisfied, though, because there is an alternation between the turns of the two processes.
-
Highlight the shortcoming in the following hardware-based solution to the critical section problem: do { while (TestAndSet(lock)); critical section lock = false; remainder section } while (1);
Topic: Process Synchronization & Semaphores
The above TestAndSet-based solution is not fully satisfactory because, even though mutual exclusion and progress are satisfied, bounded waiting is not. A process waiting to enter its critical section keeps testing the lock, and there is no guarantee about the order in which waiting processes acquire the lock, so a process may have to wait indefinitely. The same is true for the Swap-based solution: mutual exclusion can be implemented with a global Boolean variable lock initialized to false and a local Boolean variable key for each process, but this solution also satisfies mutual exclusion and progress only, not bounded waiting.
-
What is the important characteristic of the TestAndSet instruction? What are its advantages?
Topic: Process Synchronization & Semaphores
The important characteristic of the TestAndSet instruction is that it is executed atomically. Thus, if two TestAndSet instructions are executed simultaneously (on different processors), they will be executed sequentially in some arbitrary order. Advantages: 1) It is simple to implement and understand. 2) It needs only one memory location (the lock) for arbitrarily many CPUs/processes.
-
Explain the working of a semaphore. Write the classical definitions of the wait and signal operations, and show how semaphores can be used to solve the n-process critical section problem.
Topic: Process Synchronization & Semaphores
Hardware solutions to synchronization problems are not easy to generalize to more complex problems. To overcome this difficulty, we use a synchronization tool called a semaphore. A semaphore S is an integer variable that, apart from initialization, is accessible only through two standard atomic operations: wait and signal. These operations were originally termed P (for wait) and V (for signal). Their classical definitions are: wait(S) { while (S <= 0) ; // no operation S--; } signal(S) { S++; } Modifications to the integer value of the semaphore in the wait and signal operations must be executed indivisibly: when one process is modifying the value of a semaphore, no other process can simultaneously modify that same semaphore value. In addition, in the case of wait(S), the testing of the integer value of S (S <= 0) and its possible modification (S--) must be executed without interruption. We can use semaphores to deal with the n-process critical section problem. The n processes share a semaphore, mutex (standing for mutual exclusion), initialized to 1. Each process Pi is organized as follows: do { wait(mutex); critical section signal(mutex); remainder section } while (1);
-
What are the drawbacks/disadvantages of using semaphores?
Topic: Process Synchronization & Semaphores
The main disadvantage of the semaphore described with busy waiting is that it requires busy waiting. While a process is in its critical section, any other process that tries to enter its critical section must loop continuously in the entry code. This continual looping is clearly a problem in a real multiprogramming system, where a single CPU is shared among many processes: busy waiting wastes CPU cycles that some other process may be able to use productively. This type of semaphore is also called a spinlock. In addition: 1) Semaphores are too low-level; simple algorithms may require more than one semaphore, which increases the complexity of the solution. 2) It is easy to make programming mistakes (e.g., wait(s) followed by wait(s) instead of signal(s)). 3) The programmer must keep track of all calls to wait and signal on the semaphore; if these are not done in the correct order, programmer error can cause deadlock. 4) Semaphores are used both for condition synchronization and for mutual exclusion, and it is difficult to know which meaning any given semaphore may have.
-
What is a spinlock? Why are spinlocks useful in multiprocessor systems?
Topic: Process Synchronization & Semaphores
A semaphore that uses busy waiting is called a spinlock, because the process 'spins' (loops continuously) while waiting for the lock. Spinlocks are useful in multiprocessor systems. The advantage of a spinlock is that no context switch is required when a process must wait on a lock, and a context switch may take considerable time. Thus, spinlocks are useful when locks are expected to be held for short times: while one process spins on one processor, other processes can execute on the other processors.
-
What are the common data structures used in Lamport's bakery algorithm?
Topic: Process Synchronization & Semaphores
Lamport's bakery algorithm, based on the 'first come, first served' numbering scheme used in bakeries and ice-cream stores, uses two shared data structures for n processes: 1) boolean choosing[n]: initialized to false. A process sets choosing[i] = true while it is picking its number, and resets it to false afterwards. 2) int number[n]: initialized to 0. On entering, a process chooses a number one greater than the maximum of the current numbers (number[i] = 1 + max(number[0], ..., number[n-1])). The process holding the smallest (number, process ID) pair enters its critical section first; ties between equal numbers are broken by the process ID. When a process leaves its critical section, it sets number[i] = 0.
-
Differentiate between independent processes and cooperating processes.
Topic: Process Synchronization & Semaphores
An independent process is a process that cannot affect or be affected by any other process executing in the system; it does not need any other external factor to trigger it, and its execution is not influenced by other processes. A cooperating process is a process that can affect or be affected by other processes executing in the system; it works on the occurrence of events and shares data with other processes, so its outcome can affect other parts of the system. Cooperating processes communicate through inter-process communication (IPC).