OS202
fork() Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.
Concurrency
concurrency is the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the final outcome.
This allows for parallel execution of the concurrent units, which can significantly improve overall speed of the execution in multi-processor and multi-core systems.
In more technical terms, concurrency refers to the decomposability property of a program, algorithm, or problem into order-independent or partially-ordered components or units.
Multithreading
Multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system.
This approach differs from multiprocessing. In a multithreaded application, the threads share the resources of a single or multiple cores, which include the computing units, the CPU caches, and the translation lookaside buffer (TLB).
pipe()
Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process.
In UNIX Operating System, Pipes are useful for communication between related processes(inter-process communication).
Benefits of Multithreads Programming
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.
Resoucrce Sharing Processes can share resources only through techniques such as shared memory and message passing.
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.
Scalability The beneits of multithreading can be even greater in a multiprocessor architecture, where threads may be running in parallel on different processing cores.
Data Parallelism
Data parallelismdistributes subsets of the same data across different computing cores and performs the same operation on each core.
Task parallelism distributes not data but tasks across multiple cores. Each task is running a unique operation.
Fork Join
The strategy for thread creation covered in Section 4.4 is often known as the fork-join model.
Recall that with this method, the main parent thread creates (forks) one or more child threads and then waits for the children to terminate and join with it, at which point it can retrieve and combine their results.
Difference between fork() and exec()
The fork() creates a new process by duplicating the calling process, The new process, referred to as child, is an exact duplicate of the calling process, referred to as parent.
The exec() family of functions replaces the current process image with a new process image. It loads the program into the current process space and runs it from the entry point.
Thread Libraries
A thread library provides the programmer with an API for creating and managing threads. There are two primary ways of implementing a thread library.
The first approach is to provide a library entirely in user space with no kernel support. All code and data structures for the library exist in user space.
The second approach is to implement a kernel-level library supported directly by the operating system.
Thread Local Storage
Threads belonging to a process share the data of the process. Indeed, this data sharing provides one of the benefits of multi threaded programming.
However, in some circumstances, each thread might need its own copy of certain data. Such data is called thread-local storage (or TLS).