

#USING PTHREADS IN C FULL#
Spawning a new thread and stopping a thread takes less time than creating and stopping a full Process. Threads have the advantage that they are lightweight compared to Processes. | | Control Structures | | | | Control Structures | | | | Control Structures | | Control Structures | | Control Structures | | | | Thread 1 | | Thread 2 | | Thread n | | | Control Structures | | Control Structures | | Control Structures | | Resources | | Resources | | Resources | | One Thread | | One Thread | | One Thread | | Process 1 | | Process 2 | | Process n | If we have a single thread process, the process keeps track of only one set of control structures if we have a process with multiple threads each thread needs its own control structures. Control structures: We use control structures to keep track of where the stack pointer is currently at, to keep a reference to the instruction table, etcetera.Resources: memory allocated to the process, file descriptors, registers, etcetera.

The operating system creates a process and executes it.Īs we said before the process is a structure that represents executable code. We make a system call to a variant of the exec(3) command to specify the file we want to execute. When we want to execute a program, we ask our Operating System to do it for us. The process is the structure that contains it. So the process is not the executing code. In reality, a process is an abstract entity that encapsulates control structures and at least one execution thread. When we think of a process, we think of a program being executed.

Separate threads begin execution in a specified function, in a Pthreads program. The C program shown in below, demonstrates the basic Pthreads API for constructing a multithreaded program that calculates the summation of a nonnegative integer in a separate thread.

This specification may be implemented by Operating-system designers in any way they wish. Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization, which is a specification for thread behavior, not an implementation.
