Test 3 OS Flashcards

1
Q

Define “forking”. How many processes are created as a result of a fork()

A
  • Forking is a mechanism in operating systems that creates a new process from an existing one.
  • Number of Processes Created: A single fork() system call creates two processes:
    • The parent process continues execution after the fork().
    • The child process is a copy of the parent, but with a different process ID (PID) to distinguish them.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define argc and argv[]

A
  • In C and C++, argc and argv[] are arguments passed to the main() function of a program:
    • argc (argument count): An integer value that indicates the number of arguments passed on the command line when the program is executed.
    • argv (argument vector): An array of character pointers (char*[]) that stores the actual arguments passed. argv[0] is always the name of the program itself, and subsequent elements (argv[1], argv[2], etc.) hold the command-line arguments.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define time quantum

A

In multitasking operating systems, a time quantum (or time slice) is a predefined amount of CPU time allocated to a process. When a process is in the running state and its time quantum expires, the OS preempts (interrupts) it and gives the CPU to another ready process. This ensures fair sharing of CPU resources among multiple running processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What system call is used to start a new thread of execution?

A
  • The system call used to create a new thread of execution depends on the operating system:
    • POSIX systems (Linux, macOS, etc.): pthread_create()
    • Windows: CreateThread()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define context switching

A

Context switching is the process of saving the state of the currently running process (CPU registers, program counter, etc.) and loading the state of a new process or thread that will be scheduled to run on the CPU next. This allows the OS to efficiently switch between multiple processes or threads, giving the illusion of multitasking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define CPU bound process and compare/contrast it with I/O bound processes

A
  • CPU bound process: A process that spends most of its time performing calculations and requires significant CPU time to complete its task. Examples include scientific computing, encryption, and video processing.
  • I/O bound process: A process that spends most of its time waiting for data transfer from or to external devices (e.g., disk, network). Examples include web browsing (downloading files), database access, and printing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the difference states of a process?

A
  • New: The process has been created but not yet admitted to the ready queue.
  • Ready: The process is ready to run and is waiting for the CPU.
  • Running: The process is currently executing on the CPU.
  • Waiting: The process is waiting for an event (e.g., I/O completion, synchronization) before it can proceed.
  • Terminated: The process has finished execution and its resources have been released.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does execv() do?

A
  • execv() (or family functions like execl(), execlp()) is a system call used to replace the current process image with a new program.
  • It takes two arguments:
    • The path to the new program to execute.
    • An array of strings (argv[]) containing the arguments to pass to the new program.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does it mean for an application to be multithreaded?

A
  • An application is considered multithreaded if it utilizes multiple threads of execution to perform concurrent tasks.
  • A multithreaded application is a program that creates and manages multiple threads of execution within a single process. These threads share the same memory space and resources of the process but can execute concurrently. This allows for:
    • Improved responsiveness: Threads within the application can handle different tasks simultaneously, making it feel more responsive to the user.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do the following function(s) do: pthread_yield()

A

pthread_yield() (deprecated): Causes the calling thread to yield the processor to another thread.

This function is considered deprecated in many systems. It tells the thread to voluntarily relinquish the CPU and allow another thread a chance to run. However, modern OS scheduling algorithms usually make this unnecessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do the following function(s) do: pthread_create()

A

pthread_create(): Creates a new thread of execution.

This function creates a new thread of execution within a process. It takes arguments specifying the thread’s function, arguments to that function, and attributes for the thread (e.g., stack size).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do the following function(s) do: pthread_join()

A

pthread_join(): Waits for a specific thread to terminate.

This function allows a thread (usually the main thread) to wait for another thread to finish execution. It takes the target thread’s ID and optionally a pointer to retrieve the value returned by the exiting thread with pthread_exit().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do the following function(s) do: pthread_exit()

A

pthread_exit(): Terminates the calling thread and returns a value

This function allows a thread to terminate its execution and optionally return a value to the thread that joined it using pthread_join().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a race condition?

A
  • A race condition occurs in a multithreaded or multiprocessing environment when the outcome of a program depends on the unpredictable timing of events, such as the order in which threads access shared data. This can lead to unexpected behavior and potential errors.
  • For example, if two threads are updating a shared counter without proper synchronization, one might read the old value, increment it, and then another thread might read the same old value before the first thread’s update is reflected. This results in an incorrect final value for the counter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the sleeping barber problem.

A
  • The sleeping barber problem is a classic synchronization problem in concurrency control. It describes a scenario with a barber, a waiting area with chairs, and customers arriving for haircuts.
  • The Problem:
    • The barber sleeps when there are no customers.
    • A customer arriving to an empty waiting area wakes the barber up.
    • If all chairs are full, the customer leaves.
  • Synchronization is needed to ensure:
    • The barber doesn’t try to cut hair while sleeping.
    • Customers don’t wake the barber if all chairs are occupied.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are two problems with the sleeping barber?

A
  • Waking Multiple Customers(Deadlock): If a customer wakes the barber and another customer arrives simultaneously, barber finishes the haircut and goes to the waiting room, which he finds empty (because the customer walks slowly or went to the restroom) and thus goes to sleep in the barber chair
  • Starvation: A continuously arriving stream of customers might prevent existing customers from ever getting a haircut.
  • when two customers arrive at the same time when there is only one empty seat in the waiting room and both try to sit in the single chair; only the first person to get to the chair will be able to sit.
17
Q

Describe how spin locks work?What are the problems?

A
  • A spin lock is a simple synchronization mechanism used in multithreaded programming. It’s a flag in memory that indicates whether a resource is currently being used.
  • How it Works:
    1. A thread trying to acquire the lock (access the resource) continuously checks the lock’s state.
    2. If the lock is free (not set), the thread sets it and proceeds to use the resource.
    3. If the lock is already set (resource in use), the thread keeps looping (spinning) and checking the lock until it becomes free.
  • Problems with Spin Locks:
    • Busy Waiting: Threads waste CPU cycles by constantly checking the lock instead of doing productive work while waiting. This can be inefficient for heavily contended resources.
    • Priority Inversion: A low-priority thread waiting on a spin lock held by a high-priority thread can starve the high-priority thread of the CPU.
18
Q

What are the problems with spin lock?

A
  • Problems with Spin Locks:
    • Busy Waiting: Threads waste CPU cycles by constantly checking the lock instead of doing productive work while waiting. This can be inefficient for heavily contended resources.
    • Priority Inversion: A low-priority thread waiting on a spin lock held by a high-priority thread can starve the high-priority thread of the CPU.
19
Q

How does spin lock work?

A
  • A spin lock is a simple synchronization mechanism used in multithreaded programming. It’s a flag in memory that indicates whether a resource is currently being used.
  • How it Works:
    1. A thread trying to acquire the lock (access the resource) continuously checks the lock’s state.
    2. If the lock is free (not set), the thread sets it and proceeds to use the resource.
    3. If the lock is already set (resource in use), the thread keeps looping (spinning) and checking the lock until it becomes free.
20
Q

List 3 items specific to processes

A
  • Separate address spaces: Each process has its own memory space, protecting them from interfering with each other’s data.
  • Independent execution: Processes are independent entities that can be scheduled and run by the OS at any time.
  • Higher overhead: Creating and destroying processes is a more expensive operation compared to threads.

Items Specific to Processes:
- Process ID (PID)
- Address space
- Process control block (PCB)

21
Q

List 3 items specific to threads

A
  • Shared address space: Threads within a process share the same memory space, allowing for faster communication and data sharing.
  • Lightweight creation and destruction: Creating and destroying threads is much faster than processes.
  • Limited independence: Threads rely on the parent process for resources and cannot exist independently.

Items Specific to Threads:
- Thread ID (TID)
- Stack
- Register set

22
Q

Processes vs Threads?

A

Processes:
- Each process has its own address space.
- Processes are heavyweight in terms of resource consumption.
- Communication between processes is typically slower.

Threads:
- Threads within the same process share the same address space.
- Threads are lighter in terms of resource consumption compared to processes.
- Communication between threads is faster as they share memory.

23
Q

What are threads?

A

Threads are the entities within a process
scheduled for execution on the CPU.

24
Q

How many programs does a process have? How many threads do each program has?

A

Every process has exactly one
program and every running program has at
least one thread of execution.

25
Q

What are the possible thread transitions?

A
  1. Thread blocks for input
  2. Scheduler picks another thread
  3. Scheduler picks this thread
  4. Thread becomes able to run
    again
26
Q

What is a solution to sleeping barber problem

A
  • mutex
  • Think of a Mutex as a “hall pass” in this scenario.
  • There is only 1 hall pass.
  • The barber must acquire it before checking for
    customers and release it when he begins to
    sleep or cut hair.
  • Customers must acquire it before entering the
    shop to check on the barber and before checking
    on the waiting room. The customer must release
    it when he is sitting in the barber’s chair, in the
    waiting room or when he leaves.
    Wikipedia
27
Q

What is metal exclusion?

A
  • if one process is using a shared resource, all
    other processes will be excluded from doing the
    same
  • to avoid race conditions, we need a way of assuring
    that if one process is using a shared resource, all
    other processes will be excluded from doing the
    same. This is called mutual exclusion.
28
Q

what is critical region or critical section

A

Any section of code where a process is accessing a
shared resource in a way that could lead to a race
condition is called a critical region or critical
section of code.

29
Q

what is a mutex?

A
  • Mutex = mutual exclusion object (our virtual “hall pass”)
  • You can “lock” the mutex (acquire the hall pass). You
    are said to “own” it if you successfully acquire it.
  • You can “unlock” the mutex (release the hall pass), if
    you own it.
  • If you try to acquire the mutex while it is locked by
    another process/thread, you are put to sleep and
    automatically awakened when the mutex is unlocked.
    This is called sleeping on the mutex.
30
Q

what is a atomic process?

A

Locking a mutex is an atomic process (assisted
by the hardware)

31
Q

what are the requirements to avoid race condition?

A
  1. No two processes may be simultaneously inside
    their critical regions.
  2. No assumptions may be made about speeds or
    the number of CPUs.
  3. No process running outside its critical region
    may block other processes.
  4. No process should have to wait forever to enter
    its critical region.