Operating System 1 : Chapter 2 : EXAM PREPARATION : TYBCS : SPPU

Chapter 2: Processes and Threads

1 Mark Questions:

Q. What do you understand by a thread? Q. Explain posix pthread.

Answer: POSIX stands for portable operating system interface, it is a set of standard operating system interfaces based on UNIX operating system.

POSIX threads are often referred to as pthreads. They are used for developing concurrent and parallel applications. pthreads library provides a set of functions and conventions for creating, manipulating and syncronizing threads. Q. Give examples of operating systems using pthread. Answer: Here are examples:

1) LINUX

2) UNIX

3) mac OS

4) Solaris

Q. List any two operating system examples that use a one-to-one model.

Answer: In one to one thread model, there is one to one relationship between user level thread to a kernel level thread. Each user thread maps to a kernel thread.

Example: Windows NT, Windows 2000. Q. List any two examples of the many-to-many model.

Answer: In many to many model, many user level threads multiplex to the kernel thread of smaller or equal numbers.

Examples of Operating systems : Solaris 2, IRIX,etc.

Q. Define multiprocessor system.

Answer: A multiprocessor system, also known as a parallel processor system, is a computer system that uses more than one central processing unit (CPU) to perform tasks. In a multiprocessor system, multiple CPUs share the processing load and work together to execute programs and handle data.


Q. What is job queue?

Answer: It keeps all the processes in the system. As the process enters in the system, it enters the job queue. Q. What is the ready queue?

- It keeps a set of all processes residing in main memory, ready and waiting to execute. When a process is ready to execute, it is moved to the ready queue, and operating system's scheduler selects a process from the ready queue to run on CPU. Q. Write advantages of multiprocessor system.

Answer: Multiprocessor systems advantages are: 1) Increased Performance:By distributing the workload among multiple processors, tasks can be executed concurrently, leading to faster computation and reduced overall processing time. 2) Scalability: Multiprocessor systems are scalable, meaning that additional processors can be added to the system to handle increasing workloads. 3) Enhanced Reliability: Multiprocessor systems can be designed with redundancy and fault tolerance mechanisms. If one processor fails, the remaining processors can continue to operate. 4) Parallel Processing: Multiprocessor systems enable parallel processing, where multiple tasks can be executed simultaneously. 5) Cost-Effectiveness: In certain scenarios, a multiprocessor system can be more cost-effective than deploying multiple single-processor systems. Q. State scalability and responsiveness benefits of multithreading.

Answer: Scalability Benefits of Multithreading: 1) Efficient workload distribution across multiple threads. 2) Improved performance on multicore processors. 3) Better resource utilization, especially in parallelizable tasks. Responsiveness Benefits of Multithreading: 1) Enhanced user experience, particularly in GUIs. 3) Asynchronous operations prevent blocking and waiting. 4) Parallelism for heavy tasks maintains responsiveness. Q. What is context switching? State conditions when context switching occurs.

- Context switching is process of saving the state of a process in PCB and loading the state of another process from another PCB. It is used in multitasking operating system. It enables multiple processes to share a single CPU.

Conditions when it occurs:

1) Interrupt : When an interrupt occurs (Hardware- I/o Completion or Software: system call), a context swtich is performed to handle the interrupt.

2) Blocking/Waiting: When a process is waiting for an event, it gives up CPU, the operating system performs a context switch to allow another process to execute.

3) Process termination: When process completes its execution, context switch is performed to release its resources.

4) Process switching: When process requests to yield the CPU to switch to another process, a context switch occurs.

5) Priority Changes: If higher priority process becomes ready to run. Q. Explain any two benefits of multithreaded programming.

- Answer: Benefits of multi threaded programming are:

1) Improved responsiveness: Since multiple threads execute at a time, allowing system to continue processing tasks even when one thread is blocked or waiting for resources.

2) Increased throughput or performance: It allows leveraging of available resources more effectively. Multiple threads can take advantage of multiple CPU cores or processors, distributing work completion in parallel. Q. What is the primary function of the medium-term scheduler?

Answer: The medium-term scheduler is essential for maintaining a balance between the number of processes in main memory and the available resources. The medium-term scheduler is responsible for deciding which processes should be moved into or out of the main memory (RAM). When a process is in the "swapped out" state, it is stored on secondary storage (like the hard disk), and when it is "swapped in," it is brought back into the main memory for execution. 4 Mark Questions: Q. Explain many-to-many multithreaded model.

Answer: It is a threading model that combines features of both the one-to-one and many-to-one models. In this model multiple user level threads are mapped to an equal or smaller number of kernel level threads. The number of threads may be specific to either a particular application or particular machine. Q. Explain PCB with a proper diagram.

Answer: Each process in an operating system is represented by a Process Control Block (PCB) or Task Control Block (TCB).

-The operating system uses PCB data structure to manage information about a running process.

-It stores information about the process like its state, program counter, memory management, allocated resources, scheduling, etc. required to control and manage a particular process.


It consists of
- Process Number or Process ID: A unique identifier assigned to each process.
- Pointer: It has the address of next PCB, whose process state is ready.
- Process state: It specifies current state of process. It can be new, ready, running, waiting, terminated ,etc.
- Program Counter: It is a pointer indicates the address of next instruction to be executed for the process.
- CPU Registers: It consists of register values for process, like accumulators, stack pointers, general-purpose regsiters,etc.
- Memory Management/Allocation: It contains information such as process's memory usage, memory limit or segment table.
- File management: It consists of information of all open files, access rights , etc.

Q. Which scheduler controls the degree of multithreaded programming?
Answer: Thread schedular controls the degree of multithreaded programming. It is responsible for managing the execution of threads in a system with multiple threads. It decides which thread to run, when to switch between threads, and how to allocate CPU time to different threads. Q. What is a thread library?
Answer: Thread libraries typically provide functions for creating threads, synchronizing their execution, and handling communication and data sharing between threads. Q. Write a short note on process threads. -Process threads, also known as lightweight processes, are the smallest units of execution within a process. Unlike traditional heavyweight processes, threads within the same process share the same resources, such as memory space and file descriptors, while having their own registers and stack space. Threads enable parallel execution of tasks within a single process, allowing for more efficient utilization of resources and improved concurrency. Q. Write any two disadvantages of priority scheduling.
Answer: Priority scheduling is a process scheduling algorithm where each process is assigned a priority, and the process with the highest priority is selected for execution first. It's disadvantages are: Starvation: Priority scheduling may lead to starvation, where low-priority processes may be delayed indefinitely if higher-priority processes are always present. Priority Inversion: Priority inversion occurs when a higher-priority process is forced to wait for a lower-priority process to release a resource, impacting system efficiency and latency. Q. Compare preemptive and non-preemptive scheduling.

Preemptive vs. Non-Preemptive Scheduling

AspectPreemptive SchedulingNon-Preemptive Scheduling
InterruptsAllows forced interruption of running processes.Does not forcibly interrupt running processes.
Response TimeBetter for high-priority tasks due to immediate scheduling.May result in higher response times for high-priority tasks.
FairnessProvides fairness by preventing process monopolization.May be less fair as long-running tasks can monopolize the CPU.
ComplexityCan be more complex due to handling interruptions.Simple to implement without interruption handling.
Q. What is a process? State and explain in brief different types of process states.

Answer: A process is an instance of a computer program that is being executed by one or many threads. It is the basic unit of execution in a computer system. Each process has its own memory space, resources, and state.

Following are the types of process states:

1) New: It is the initial state when a process is first created. In this state, process is being initialized and not being executed.

2) Ready: It is the state when process is ready for execution, and loaded onto the main memory. It is waiting to be allocated a CPU to it.

3) Running : It is the state when process is selected for execution by the schedular. Now CPU has been allocated to it and it is currently being executed.

4) Blocked or Waiting: It is the state when the process has to wait for a particular event (such as user input) before it can proceed it's execution. Unless that event occurs, process cannot continue it's execution.

5) Terminated : It is the state when process has finished its execution. Now the process releases all of its allallocated resources, and operating system removes it from process table.

Q. Write a note on multilevel queue scheduling. Answer:

Q. What do you understand by a thread? Answer: It is the smallest unit of execution within a process in multi tasking operating system. It is a light weight sub process that shares same resources as its parent processs. They allow multiple operationgs to be performed concurrently within a single process. They are generally used in applications where parallel processing is beneficial like GUI network programming.


Q. Define independent and dependent processes.

Answer:

Independent Processes: Independent processes are those that do not rely on or affect the execution of other processes. Each independent process can run concurrently without any dependencies on the state or output of other processes. Dependent Processes: Dependent processes, on the other hand, have interdependencies, meaning that the execution or outcome of one process is contingent upon the behavior or results of another. Dependencies can involve sharing data, resources, or specific execution sequences. Q. Explain multilevel feedback queue scheduling with a diagram.

Answer: Q. Differentiate between user-level thread and kernel-level thread.

User-Level Threads vs. Kernel-Level Threads

AspectUser-Level ThreadsKernel-Level Threads
ManagementManaged by user-level threading library or runtime environment.Managed by the operating system kernel.
ResponsivenessMay be faster and more lightweight for thread creation and switching.Offers better responsiveness as the kernel can make scheduling decisions based on individual threads.
ConcurrencyLimited concurrency within a process since the kernel sees the entire process as a single-threaded entity.Enables true concurrency, allowing threads within the same process to execute in parallel.
Kernel AwarenessThe kernel is unaware of individual user-level threads.The kernel is aware of and manages individual threads within a process.
PortabilityHighly portable across different operating systems.May have variations in efficiency depending on the underlying operating system's threading support.
Resource UsageLess resource-intensive, but limited parallelism.May be more resource-intensive due to kernel involvement but allows for greater parallelism.
Q. Is the system in a safe state? If yes, find a safe sequence. Q. Explain the multithreading model in detail.
Answer: A multithreading model is a way of organizing and managing concurrent execution in a program by using multiple threads. Multithreading allows multiple threads to execute independently within the same process, sharing the same resources and memory space. Thread Creation: Threads are created using programming language constructs or operating system APIs. Thread Execution: Threads run concurrently within the same process, sharing resources and memory. Thread Synchronization: Mechanisms like locks and semaphores prevent conflicts over shared resources. Thread Communication: Threads exchange information using message passing, shared memory, or condition variables. Thread Lifecycle: Threads transition between states such as creation, ready, running, blocked, and termination. Thread Prioritization: Some models allow assigning priorities to influence thread scheduling. User-Level and Kernel-Level Threads: User-level threads are managed by a library, while kernel-level threads rely on the operating system. Benefits: Improved performance, better responsiveness, and development of concurrent applications. Challenges: Race conditions, deadlocks, and increased debugging complexity are challenges. Examples: Used in web servers, GUIs, video games, and scientific simulations for parallelism. Q. List the types of schedulers and also explain short-term schedulers in detail.

Answer:

Difference Table

CharacteristicShort-Term Scheduler (CPU Scheduler)Medium-Term SchedulerLong-Term Scheduler (Job Scheduler)
ScopeDeals with the selection of processes in the ready queue for execution.Handles the swapping of processes in and out of main memory.Selects processes to be admitted to the ready queue.
Frequency of ExecutionExecutes frequently, often in milliseconds or microseconds.Executes less frequently than the short-term scheduler.Executes the least frequently as it involves admitting processes.
Time HorizonVery short-term, focusing on the immediate execution of processes.Medium-term, involving the swapping of processes in and out of main memory.Long-term, considering overall system performance and resource availability.
Decision CriteriaBased on factors like burst time, priority, and fairness.Considers factors like process size, I/O intensity, and priority.Based on factors such as overall system workload, memory, and CPU utilization.
GoalTo optimize CPU utilization and throughput.To manage the degree of multiprogramming in the system, ensuring a balance between CPU and I/O-bound processes.To maintain a balance between system performance and fairness in admitting new processes.
ExampleShort-Term Scheduler (Round Robin, Priority Scheduling)Medium-Term Scheduler (Swapper)Long-Term Scheduler (Admission Control Algorithm)


Q. Write advantages of multiprocessor system. Answer:

A. Increased Processing Power:

Multiprocessor systems can handle multiple tasks simultaneously, boosting overall processing power.

B. Improved Throughput:

Tasks are executed concurrently, enhancing system throughput and responsiveness.

C. Scalability:

The system can grow in power by adding more processors, adapting to increasing workloads.

D. Enhanced Reliability:

Redundancy minimizes the impact of hardware failures, improving system reliability.

E. Better Resource Utilization:

Multiple processors efficiently use resources by working on independent tasks.

F. Parallel Processing:

Multiprocessors excel at parallel execution, providing significant speedup for certain computations.

G. Efficient Multitasking:

Different processors handle various tasks simultaneously, making multitasking smoother.

H. Cost-Effectiveness:

Shared resources and scalability often result in cost-effective solutions compared to single-processor systems.

Q. List any two advantages of multithreaded programming.