0% found this document useful (0 votes)
2 views58 pages

Chapter 3 Operating System New

The document covers the fundamentals of processes and threads in operating systems, detailing the components, states, and scheduling of processes. It explains interprocess communication (IPC) mechanisms, including shared memory and message passing, and discusses process creation and termination. Key concepts such as the Process Control Block (PCB), context switching, and multithreading models are also highlighted.

Uploaded by

henekkebede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views58 pages

Chapter 3 Operating System New

The document covers the fundamentals of processes and threads in operating systems, detailing the components, states, and scheduling of processes. It explains interprocess communication (IPC) mechanisms, including shared memory and message passing, and discusses process creation and termination. Key concepts such as the Process Control Block (PCB), context switching, and multithreading models are also highlighted.

Uploaded by

henekkebede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ECCS-4221: Operating Systems

Chapters 3: Processes and Threads

Credit :4-3-3-0
Instructor : Measho B.
2
Learning Objectives
• Understand the concept of a process and its
components
• Explain process scheduling and operations
• Describe interprocess communication (IPC)
mechanisms
• Differentiate between processes and threads
• Discuss multithreading models, thread libraries, and
threading issues

3
What is a process in operating system?
▪ A process is a program in execution.
▪ A process will need certain resources—such as
▪ CPU time,
▪ memory,
▪ files, and
▪ I/O devices to accomplish its task.
▪ These resources are typically allocated to the process while it is executing.
▪ A process is the unit of work in most systems.
▪ Systems consist of a collection of processes:
▪ operating-system processes execute system code, and
▪ user processes execute user code.
▪ All these processes may execute concurrently.
4
Processes
▪ An operating system executes a variety of programs that run as a process.
▪ Process – a program in execution;
▪ Process execution must progress in sequential fashion.
▪ No parallel execution of instructions of a single process in single CPU
▪ Process has Multiple parts
▪ The program code, also called text section
▪ Current activity including program counter, processor registers
▪ Stack containing temporary data
▪ Function parameters, return addresses, local variables
▪ Data section containing global variables
▪ Heap containing memory dynamically allocated during run time
▪ Program is passive entity stored on disk (executable file);
▪ Process is active
▪ Program becomes process when an executable file is loaded into memory
▪ Execution of program started via GUI mouse clicks, command line entry of its name, etc.
▪ One program can be several processes
▪ Consider multiple users executing the same program 5
Parts of Process

▪ The data field refers to initialized data,


and the bss field refers to uninitialized
data (BSS is a historical term meaning
Block Started by Symbol).
▪ The dec and hex values represent the
total size of the three sections (text,
data, and bss) in decimal and
hexadecimal, respectively.

6
Process State
• As a process executes, it changes state
• New: The process is being created
• Running: Instructions are being executed
• Waiting: The process is waiting for some event to occur
• Ready: The process is waiting to be assigned to a processor
• Terminated: The process has finished execution

7
Two programs (P1 and P2)
Program 1 Program 2
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int num1, num2, sum;// Ask user for input int num;// Input from user
printf("Enter first number: "); printf("Enter a number: ");
scanf("%d", &num1); scanf("%d", &num);// Increment by 2
printf("Enter second number: "); num = num + 2;// Display result
scanf("%d", &num2); printf("The number after incrementing by 2 is:
printf("You entered: %d and %d\n", num1, num2); %d\n", num);
printf("The sum is: %d\n", sum);
return 0;
return 0; }
}

8
Process Control Block (PCB)
▪ Information associated with each process(also
called task control block)
▪ Process state – running, waiting, etc.
▪ Program counter – location of instruction to next
execute
▪ CPU registers – contents of all process-centric
registers
▪ CPU scheduling information- priorities, scheduling
queue pointers
▪ Memory-management information – memory
allocated to the process
▪ Accounting information – CPU used, clock time
elapsed since start, time limits
▪ I/O status information – I/O devices allocated to
process, list of open files
9
Process Scheduling
• Process scheduler selects among available processes for next execution on CPU core
• Goal -- Maximize CPU use, quickly switch processes onto CPU core
• Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main memory, ready and waiting to execute
• Wait queues – set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues

10
Representation of Process Scheduling

▪ The process could issue an I/O request and then be placed in an I/O wait queue.
▪ The process could be removed forcibly from the core having its time slice expire and be put
back in the ready queue.
▪ The process could create a new child process and then be placed in a wait queue while it awaits
the child’s termination
▪ The process could be removed forcibly from the core, as a result of an interrupt
11
Context Switch
▪ A context switch occurs when the CPU switches from one process to another.
▪ When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
via a context switch
▪ Context of a process represented in the PCB
▪ Context-switch time is pure overhead
▪ The system does no useful work while switching
▪ The more complex the OS and the PCB ➔ the longer the context switch
▪ Time dependent on hardware support
▪ Some hardware provides multiple sets of registers per CPU ➔ multiple contexts loaded at once

12
Operations on Processes
▪ System must provide mechanisms for:
▪ Process creation
▪ Process termination
▪ Process Creation
▪ Parent process create children processes, which, in turn create other processes, forming a tree of processes
▪ Generally, process identified and managed via a process identifier (pid)
▪ Resource sharing options
▪ Parent and children share all resources
▪ Children share subset of parent’s resources
▪ Parent and child share no resources
▪ Execution options
▪ Parent and children execute concurrently
▪ Parent waits until children terminate

13
Process Creation (Cont.)
▪ Address space
▪ Child duplicate of parent
▪ Child has a program loaded into it
▪ UNIX examples
▪ fork() system call creates new process
▪ exec() system call used after a fork() to replace the process’
memory space with a new program
▪ Parent process calls wait()waiting for the child to terminate

A Tree of Processes in Linux (pstree Linux command generates the tree) 14


Parent-child processes
#include <sys/types.h> • Step-by-Step Execution
#include <sys/wait.h> • fork()
#include <stdio.h> • Creates a child process.
#include <unistd.h> • After fork(), there are two processes:
• Parent process (gets pid > 0)
int main(){ • Child process (gets pid = 0)
pid_t pid = fork(); /* fork a child process */ • If (gets pid < 0),error
if (pid < 0) { /* error occurred */ • In the Child Process
fprintf(stderr, "Fork Failed"); • pid == 0, so it executes:
• execlp("/bin/ls", "ls", NULL);
}
• This replaces the child’s code with the ls command,
else if (pid == 0) { /* child process */ listing the files in the current directory.
execlp("/bin/ls", "ls", NULL); • If execlp succeeds, it never returns to the next line in
the child.
} • In the Parent Process
else { /* parent process */ • The parent process runs the else block:
wait(NULL); /* parent will wait for the child to complete */ wait(NULL);
printf("Child Complete\n");
printf("Child Complete\n");
• The parent waits until the child finishes executing ls.
} • After the child terminates, the parent prints:
return 0; Child Complete
}
15
Process Termination
▪ Process executes last statement and then asks the operating system to delete it using the exit() system call.
▪ Returns status data from child to parent (via wait())
▪ Process’ resources are deallocated by operating system
▪ Parent may terminate the execution of children processes using the abort() system call. Some reasons for doing
so:
▪ Child has exceeded allocated resources
▪ Task assigned to child is no longer required
▪ The parent is exiting, and the operating systems does not allow a child to continue if its parent terminates
▪ Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all
its children must also be terminated.
▪ cascading termination. All children, grandchildren, etc., are terminated.
▪ The termination is initiated by the operating system.
▪ The parent process may wait for termination of a child process by using the wait()system call. The call returns
status information and the pid of the terminated process
pid = wait(&status);
▪ If no parent waiting (did not invoke wait()) process is a zombie
▪ If parent terminated without invoking wait(), process is an orphan
16
Question
#include <stdio.h> 1. How many processes are created?
#include <unistd.h>
2. How many child processes are
int main(){
created?
fork(); // Create a child process */
fork(); //Create another child process 3. How many times does "Operating
fork(); // and Create another */ Systems" print?
printf(“Operating Systems”); 4. Generate a generalized formula for
return 0; the above question. Assume that
} there are n fork() system calls in
the program.

17
Cont.
#include <sys/types.h> 1. When will LINE J be reached?
#include <stdio.h>
#include <unistd.h> a) If fork() system call successfully
int main(){ creates a child process (pid == 0)
pid t pid= fork(); b) If fork() system call successfully not
if (pid < 0) { /* error occurred */ creates a child process (pid < 0)
fprintf(stderr, "Fork Failed"); c) If fork() system call successfully
return 1; creates a parent process (pid == 0)
}
else if (pid == 0) { /* child process */ 2. If a parent process terminates
execlp("/bin/ls","ls",NULL); unexpectedly, its child process becomes
printf("LINE J"); an:
} a) Super process with enhanced abilities.
else { /* parent will wait for the child to complete */ b) Orphan process adopted by the init
wait(NULL); process.
printf("Child Complete");
} c) Zombie process (different scenario).
return 0; d) It simply disappears without a trace.
}
18
Interprocess Communication
• Processes within a system may be independent or cooperating
• Cooperating process can affect or be affected by other processes,
including sharing data
• Reasons for cooperating processes:
• Information sharing
• Computation speedup
• Modularity
• Convenience
• Cooperating processes need interprocess communication (IPC)
• Two models of IPC
• Shared memory
• Message passing
19
Communications Models
(a) Shared memory. (b) Message passing.

20
IPC
Producer-Consumer Problem IPC – Shared Memory
• Paradigm for cooperating processes: • An area of memory shared among the
• producer process produces information processes that wish to communicate
that is consumed by a consumer process
• The communication is under the
• Two variations: control of the users processes not the
• unbounded-buffer places no practical operating system.
limit on the size of the buffer:
• Producer never waits
• Major issues is to provide mechanism
• Consumer waits if there is no buffer to that will allow the user processes to
consume synchronize their actions when they
• bounded-buffer assumes that there is a access shared memory.
fixed buffer size
• Producer must wait if all buffers are full
• Consumer waits if there is no buffer to
consume 21
Bounded-Buffer – Shared-Memory Solution
• Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
▪ The shared buffer is implemented as a circular array with two logical pointers: in and out.
▪ The variable in points to the next free position in the buffer;
▪ out points to the first full position in the buffer.
▪ The buffer is empty when in ==out;
▪ The buffer is full when ((in + 1) % BUFFER_SIZE) == out.
▪ The producer process has a local variable next_produced in which the new item to be produced is
stored.
▪ The consumer process has a local variable next-consumed in which the item to be consumed is stored

22
IPC – Shared Memory (Cont.)
Producer Process:
item next_produced;
while (true) { /* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}

Consumer Process:
item next_consumed;
while (true) {
while (in == out) ; /* do nothing */
next_consumed =buffer[out];
out = (out + 1) % BUFFER_SIZE;/* consume the item in next consumed */
} 23
What about Filling all the Buffers?
• Suppose we want to provide a solution to the producer–
consumer problem that allows all buffers to be filled.
• We can achieve this by using an integer counter that keeps track
of the number of full buffers.
• Initially, the counter is set to 0.
• The producer increments the counter each time it produces a
new item, and the consumer decrements the counter each time
it consumes an item.
• However, we will discuss this in more detail under the
synchronization topic in Chapter 4.

24
Producer Consumer

while (true) {/* produce an item in next produced */ while (true) {


while (counter == BUFFER_SIZE) ; /* do nothing */ while (counter == 0); /* do nothing */
buffer[in] = next_produced; next_consumed =buffer[out];
in = (in + 1) % BUFFER_SIZE; out = (out + 1) % BUFFER_SIZE;
counter++; counter--; /* consume the item in next consumed */
} }

25
IPC – Message Passing
• Processes exchange information with one another without using shared variables.
• IPC facility provides two operations:
• send(message)
• receive(message)
• The message size is either fixed or variable
• If processes P and Q wish to communicate, they need to:
• Establish a communication link between them
• Exchange messages via send/receive
• Implementation issues:
• How are links established?
• Can a link be associated with more than two processes?
• How many links can these be between every pair of communicating processes?
• What is the capacity of a link?
• Is the size of a message that the link can accommodate fixed or variable?
• Is a link unidirectional or bi-directional?

26
Implementation of Communication Link
• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect (Naming)
• Synchronous or asynchronous (Synchronization)
• Automatic or explicit
Naming
1. Direct Communication
• Processes must name each other explicitly:
• send (P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q
• Properties of communication link
• Links are established automatically
• A link is associated with exactly one pair of communicating processes
• Between each pair there exists exactly one link
• The link may be unidirectional, but is usually bi-directional 27
Cont.
2. Indirect Communication
• Messages are directed and received from mailboxes (also referred to as
ports)
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
• Properties of communication link
• Link established only if processes share a common mailbox
• A link may be associated with many processes
• Each pair of processes may share several communication links
• Link may be unidirectional or bi-directional
• Operations
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
• Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
28
Indirect Communication (Cont.)
• Mailbox sharing
• P1, P2, and P3 share mailbox A
• P1, sends; P2 and P3 receive
• Who gets the message?
• Solutions
• Allow a link to be associated with at most two processes
• Allow only one process at a time to execute a receive operation
• Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.

29
Synchronization
Message passing may be either blocking or non-blocking
1. Blocking is considered synchronous
• Blocking send -- the sender is blocked until the message is received
• Blocking receive -- the receiver is blocked until a message is
available
2. Non-blocking is considered asynchronous
• Non-blocking send -- the sender sends the message and continue
• Non-blocking receive -- the receiver receives:
• A valid message, or
• Null message
• Different combinations possible
• If both send and receive are blocking, we have a rendezvous

30
Buffering
• Queue of messages attached to the link.
• Implemented in one of three ways
1. Zero capacity – no messages are queued on a link.
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits

31
Threads and Concurrency
▪ A thread is a basic unit of CPU utilization;
▪ It represents an independent path of execution within a process.
▪ A thread is a lightweight process that allows multiple tasks to be executed
concurrently within a single program.
▪ It comprises
• Thread ID,
• Program counter (PC)
• Register set,
• Stack .
▪ It shares with other threads belonging to the same process its code section,
data section, and other operating-system resources, such as open files and signals.
▪ A traditional process has a single thread of control.
▪ If a process has multiple threads of control, it can perform more than one task at a
time.
32
Single and Multithreaded Processes

•Processes execute sequentially, one at •Processes can have multiple threads of


a time. execution within them.
•Only one thread of execution per •Threads share the same address space
process (single-threaded). and resources within a process.
•If a process is performing I/O or waiting •Threads within a process can execute
for an event, it blocks and prevents other concurrently on multiple CPU cores (if
processes from executing. available). 33
Multithreaded Server Architecture

Benefits of Multithreaded Programming


• Responsiveness – may allow continued execution if part of process is blocked,
especially important for user interfaces
• Resource Sharing – threads share resources of process, easier than shared memory or
message passing
• Economy – cheaper than process creation, thread switching lower overhead than
context switching
• Scalability – process can take advantage of multicore architectures
34
Multicore Programming
• Multicore or multiprocessor systems puts pressure on
programmers, challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
• Parallelism implies a system can perform more than one task
simultaneously(N Cores)
• Concurrency supports more than one task making progress
• Single processor / core, scheduler providing concurrency

35
Concurrency vs. Parallelism
▪ Concurrent execution on single-core system:

▪ Parallelism on a multi-core system:

36
Multicore Programming
• Types of parallelism
• Data parallelism – distributes subsets of the same data across multiple cores,
same operation on each
• Task parallelism – distributing threads across cores, each thread performing
unique operation

37
Amdahl’s Law
• Identifies performance gains from adding additional cores to an
application that has both serial and parallel components
• S is serial portion
• N processing cores

• That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores
results in speedup of 1.6 times
• As N approaches infinity, speedup approaches 1 / S

Serial portion of an application has disproportionate effect on


performance gained by adding additional cores
38
User Threads and Kernel Threads
• User threads - management done by
user-level threads library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the Kernel
• Examples – virtually all general-purpose
operating systems, including:
• Windows
• Linux
• Mac OS X
• iOS
• Android 39
Multithreading Models
▪ Many-to-One
▪ One-to-One
▪ Many-to-Many
▪ Two-level Model

1. Multithreading Models
1. Many-to-One
• Many user-level threads mapped to single kernel
thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on
multicore system because only one may be in
kernel at a time
• Few systems currently use this models

40
2. One-to-One
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Number of threads per process sometimes restricted due to overhead
• Examples
• Windows
• Linux

41
3. Many-to-Many Model
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel
threads
• Windows with the ThreadFiber package
• Otherwise not very common

42
4. Two-level Model
• Similar to M:M, except that it allows a user thread to be bound
to kernel thread

43
Thread Libraries and Pthreads
Thread library
• provides programmer with API for creating and managing threads
• Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS

Pthreads
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
• Specification, not implementation
• API specifies behavior of the thread library, implementation is up to development of the
library
• Common in UNIX operating systems (Linux & Mac OS X)
44
Threading Issues
• Semantics of fork() and exec() system calls
• Signal handling
• Synchronous and asynchronous
• Thread cancellation of target thread
• Asynchronous or deferred
• Thread-local storage
• Scheduler Activations

45
1. Semantics of fork() and exec()
• Does fork() duplicate only the calling thread or all threads?
• Some UNIXes have two versions of fork
• exec() usually works as normal – replace the running process including all
threads

46
2. Signal Handling
• Signals are used in UNIX systems to notify a process that a particular event has
occurred.
• A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
• Every signal has default handler that kernel runs when handling signal
• User-defined signal handler can override default
• For single-threaded, signal delivered to process
• Where should a signal be delivered for multi-threaded?
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the processs

47
3. Thread Cancellation
• Terminating a thread before it has finished
• Thread to be canceled is target thread
• Two general approaches:
• Asynchronous cancellation terminates the target thread immediately
• Deferred cancellation allows the target thread to periodically check if it should
be cancelled
• Pthread code to create and cancel a thread:

48
Thread Cancellation (Cont.)
• Invoking thread cancellation requests cancellation, but actual cancellation
depends on thread state

• If thread has cancellation disabled, cancellation remains pending until thread


enables it
• Default type is deferred
• Cancellation only occurs when thread reaches cancellation point
• i.e., pthread_testcancel()
• Then cleanup handler is invoked
• On Linux systems, thread cancellation is handled through signals

49
4. Thread-Local Storage
• Thread-local storage (TLS) allows each thread to have its own copy of data
• Useful when you do not have control over the thread creation process (i.e., when
using a thread pool)
• Different from local variables
• Local variables visible only during single function invocation
• TLS visible across function invocations
• Similar to static data
• TLS is unique to each thread

50
5. Scheduler Activations
• Both M:M and Two-level models require communication to
maintain the appropriate number of kernel threads allocated
to the application
• Typically use an intermediate data structure between user
and kernel threads – lightweight process (LWP)
• Appears to be a virtual processor on which process can schedule
user thread to run
• Each LWP attached to kernel thread
• How many LWPs to create?
• Scheduler activations provide upcalls - a communication
mechanism from the kernel to the upcall handler in the
thread library
• This communication allows an application to maintain the
correct number kernel threads

51
Questions
1. Which of the following is NOT typically a 3. How does the operating system manage memory
allocation for the heap in a process?
section of a process's memory layout?
a) It reserves a fixed size at the start of the process's
a) Text Segment memory space.
b) Data Segment b) It maintains a free memory pool and allocates
c) Stack chunks as requested by the process
d) Register File c) It automatically expands the heap size whenever
needed.
2. What is the primary purpose of the data d) The heap is not managed by the operating system
segment in a process's memory layout? and grows uncontrolled.
a) To store program instructions 4. What happens when a process experiences a stack
overflow?
b) To hold dynamically allocated memory
a) The operating system increases the stack size
c) To store global and static variables with defined automatically.
values
b) The process continues execution normally.
d) To manage function calls and local variables c) The process crashes due to exceeding the available
stack space.
d) The process exits gracefully and cleans up its
resources.
52
Cont.
5. A process is initially created in which state? 8. When a process finishes execution and releases
a) Running all its resources, it enters which state?
b) Ready a) Suspended
c) New b) Terminated
d) Terminated c) Idle
6. A process is waiting for an I/O operation to complete. d) Orphaned
What state is it most likely in?
a) Running
9. A Windows executable ([Link]) is
launched on a Linux system without
b) Ready compatibility layers like WINE. At which process
c) Blocked(Waiting) state does Linux typically reject the program due
d) Exited to API/format incompatibility?
7. A process is currently executing instructions a) Ready
on the CPU. What state is it in? b) Running
a) Running c) New
b) Ready d) Terminated
c) Waiting e) Waiting
d) Terminated
53
Cont.
10. What is the primary reason for a context switch in a 12. What information is typically saved during a
multitasking operating system? context switch?
a) To allow a single program to execute multiple a) Only the program counter (PC) of the
instructions simultaneously. currently running process.
b) To share the CPU among multiple processes, b) The program counter (PC), registers, and
giving each process a chance to run. process state (running, waiting, etc.).
c) To automatically terminate processes that are c) The entire memory space of the currently
using too much memory. running process
d) To improve the performance of a single long- d) Only the data segment of the process for
running process. faster restoration.
11. What happens to the CPU registers when 13. What is the potential downside of
a context switch occurs? frequent context switches?
a) They are automatically cleared and reset to a) It allows more processes to run concurrently,
default values. leading to better utilization.
b) The values in the registers are discarded and b) It can introduce overhead due to saving and
lost. restoring process context.
c) The values in the registers are saved so the c) It simplifies memory management for the
process can resume execution from the same operating system.
point later. d) It improves the responsiveness of the system
d) The registers are shared between all processes for interactive users.
54
without any context switching needed.
Cont.
14. a 14. b
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
int main() { int main() {
int i; int i;
for (i = 0; i < 3; i++){ for (i = 0; i < 3; i++)
fork(); fork();
printf("Department of CSE\n"); printf("Department of CSE\n");
} return 0;
return 0; }
}
I. How many processes are created in question
A. How many processes are created in question 14.b?
14.a?
II. How many child processes are created?
B. How many child processes are created ?
III. How many times does " Department of CSE "
C. How many times does " Department of CSE " print? print?
D. Generate a generalized formula for the above IV. Generate a generalized formula for the above
question. Assume that there are n fork() system question. Assume that there are n fork() system
calls in the program. calls in the program. 55
Cont.
15. What is the primary reason for a context switch in a 18. What information is typically saved during a
multitasking operating system? context switch?
a) To allow a single program to execute multiple a) Only the program counter (PC) of the currently
instructions simultaneously. running process.
b) To share the CPU among multiple processes, b) The program counter (PC), registers, and
giving each process a chance to run. process state (running, waiting, etc.).
c) To automatically terminate processes that are c) The entire memory space of the currently
using too much memory. running process
d) To improve the performance of a single long- d) Only the data segment of the process for faster
running process. restoration.
16. What happens to the CPU registers when a context 19. What is the potential downside of frequent context
switch occurs? switches?
a) They are automatically cleared and reset to a) It allows more processes to run concurrently,
default values. leading to better utilization.
b) The values in the registers are discarded and b) It can introduce overhead due to saving and
lost. restoring process context.
c) The values in the registers are saved so the c) It simplifies memory management for the
process can resume execution from the same operating system.
point later. d) It improves the responsiveness of the system
d) The registers are shared between all processes for interactive users.
without any context switching needed.
56
Cont.
20. What is Interprocess communication? 25. Which of the following are TRUE for direct communication?
a) allows processes to communicate and synchronize their actions when a) A communication link can be associated with N number of process (N = max. number of
using the same address space processes supported by system)
b) allows processes to communicate and synchronize their actions b) A communication link is associated with exactly two processes
c) allows the processes to only synchronize their actions without c) Exactly N/2 links exist between each pair of processes (N = max. number of processes
supported by system)
communication
d) Exactly two link exists between each pair of processes
d) none of the mentioned
26. In indirect communication between processes P and Q __________
[Link] passing system allows processes to __________ a) there is another process R to handle and pass on the messages between P and Q
a) communicate with each other without sharing the same address space b) there is another machine between the two processes to help communication
b) communicate with one another by resorting to shared data c) there is a mailbox to help communication between P and Q
c) share data d) none of the mentioned
d) name the recipient or sender of the message 27. In the non-blocking send __________
22. Which of the following two operations are provided by the IPC facility? a) the sending process keeps sending until the message is received
a) write & delete message b) the sending process sends the message and resumes operation
b) delete & receive message c) the sending process keeps sending until it receives a message
c) send & delete message d) none of the mentioned
d) receive & send message 28. In the Zero capacity queue __________
[Link] sent by a process __________ a) the queue can store at least one message
a) have to be of a fixed size b) the sender blocks until the receiver receives the message
c) the sender keeps sending and the messages don’t wait in the queue
b) have to be a variable size
d) none of the mentioned
c) can be fixed or variable sized
29. The Zero Capacity queue __________
d) none of the mentioned a) is referred to as a message system with buffering
[Link] link between two processes P and Q to send and receive messages is b) is referred to as a message system with no buffering
called __________ c) is referred to as a link
a) communication link d) none of the mentioned
b) message-passing link 30. Bounded capacity and Unbounded capacity queues are referred to as __________
c) synchronization link a) Programmed buffering
d) all of the mentioned b) Automatic buffering
c) User defined buffering
d) No buffering
57
Cont.
31. Which one of the following is not shared by threads? 35. Termination of the process terminates ___________
a) program counter a) first thread of the process
b) stack b) first two threads of the process
c) both program counter and stack c) all threads within the process
d) none of the mentioned d) no thread within the process
32. A process can be ___________ 36. Which one of the following is not a valid state of a
a) single threaded thread?
b) multithreaded a) running
c) both single threaded and multithreaded b) parsing
d) none of the mentioned c) ready
d) blocked
33. If one thread opens a file with read privileges the
__________ 37. The register context and stacks of a thread are
a) other threads in the another process can also read deallocated when the thread?
from that file a) terminates
b) other threads in the same process can also read from b) blocks
that file c) unblocks
c) any other thread can not read from that file d) spawns
d) all of the mentioned
38. Thread synchronization is required because
34. The time required to create a new thread in an existing ___________
process is ___________ a) all threads of a process share the same address space
a) greater than the time required to create a new process b) all threads of a process share the same global
b) less than the time required to create a new process variables
c) equal to the time required to create a new process c) all threads of a process can share the same files
d) all of the mentioned

58

You might also like