Test 3 OS Flashcards
Define “forking”. How many processes are created as a result of a fork()
- 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.
Define argc and argv[]
- 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.
Define time quantum
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.
What system call is used to start a new thread of execution?
- 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()
Define context switching
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.
Define CPU bound process and compare/contrast it with I/O bound processes
- 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.
What are the difference states of a process?
- 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.
What does execv() do?
- 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.
What does it mean for an application to be multithreaded?
- 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.
What do the following function(s) do: pthread_yield()
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.
What do the following function(s) do: pthread_create()
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).
What do the following function(s) do: pthread_join()
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().
What do the following function(s) do: pthread_exit()
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().
What is a race condition?
- 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.
Describe the sleeping barber problem.
- 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.