Process, Thread, Program - Yash-777/LearnJava GitHub Wiki

Process

In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

A computer program is a passive collection of instructions, while a process is the actual execution of those instructions. Several processes may be associated with the same program; for example, opening up several instances of the same program often means more than one process is being executed. Each of the instances are termed as a process.

Process will be created by the Operating System by allocating memory | address space for the required resources from secondary storage device (hard disk drive, CD-ROM, etc.) into main memory.

Computer Data Storage

Example: Reading data from java console - IO Operation, will change the state of an process to waiting state, After completing the event success | Failure process will terminate.

Process p1 = Runtime.getRuntime().exec("cmd.exe /c start java -jar "+JARFile+" "+args1+" "+args2);
InputStream is = p1.getInputStream();
int i = 0;
while( (i = is.read() ) != -1) {
	System.out.print((char)i);
}
System.out.println("Process will terminate. « https://www.youtube.com/watch?v=ljxHH7i7u3Q");

A process consists of multiple threads. A thread is a smallest part of the process that can execute concurrently with other parts(threads) of the process.

A thread is a subset(part) of the process which shares same memory | address space allotted to its process by the kernel.
It is termed as a ‘lightweight process’.

Multitasking is a method to allow multiple processes to share processors (CPUs) and other system resources. Each CPU executes a single task at a time. However, multitasking allows each processor to switch between tasks that are being executed without having to wait for each task to finish.

Multitasking

Multithreading

Multithreading is mainly found in multitasking operating systems. Multithreading is a widespread programming and execution model that allows multiple threads to exist within the context of one process. These threads share the process's resources, but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. Multithreading can also be applied to one process to enable parallel execution on a multiprocessing system.

  • Advantages: Responsiveness, Faster execution, Lower resource consumption, Better system utilization, Simplified sharing and communication, Parallelization.
  • Drawbacks: Synchronization, Thread crashes a process: an illegal operation performed by a thread crashes the entire process; therefore, one misbehaving thread can disrupt the processing of all the other threads in the application. CPU will maintain the thread's state via a single program counter, and set of registers

Threads vs. processes

Threads differ from traditional multitasking operating system processes in that:

  • processes are typically independent, while threads exist as subsets of a process
  • processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources
  • processes have separate address spaces, whereas threads share their address space
  • processes interact only through system-provided inter-process communication mechanisms
  • context switching between threads in the same process is typically faster than context switching between processes.

Programming paradigms

Programming paradigms are a way to classify programming languages based on the features of th languages. Languages can be classified into multiple paradigms.

A programming language is a formal language that specifies a set of instructions that can be used to produce various kinds of output. Programming languages generally consist of instructions for a computer. Programming languages can be used to create programs that implement specific algorithms.

An instruction set, with its instruction set architecture (ISA), is the interface between a computer's software and its hardware, and thereby enables the independent development of these two computing realms; it defines the valid instructions that a machine may execute.

It is the part of the computer architecture related to programming, including the native data types, instructions, registers, addressing modes, memory architecture, interrupt and exception handling, and external I/O. An ISA includes a specification of the set of opcodes (machine language), and the native commands implemented by a particular processor.

Computer programming

Computer programming (often shortened to programming) is a process that leads from an original formulation of a computing problem to executable computer programs. Programming involves activities such as analysis, developing understanding, generating algorithms, verification of requirements of algorithms including their correctness and resources consumption, and implementation (commonly referred to as coding) of algorithms in a target programming language.

Computer program

A computer program is a collection of instructions that performs a specific task when executed by a computer. A computer requires programs to function and typically executes the program's instructions in a central processing unit(CPU).

  • A computer program is usually written by a computer programmer in a programming language called source code.
  • In computing, source code is any collection of computer instructions, possibly with comments written by a human in plain text (i.e., human readable alphanumeric characters) Files. These files are stored on a computer's hard disk; usually these files are carefully arranged into a directory tree, known as a source tree. The source code for a particular piece of software may be contained in a single file or many files. Though the practice is uncommon, a program's source code can be written in different programming languages.
  • The purpose of programming is to find a sequence of instructions that will automate performing a specific task or solving a given problem.
  • Final program must satisfy some fundamental properties. like Reliability, Robustness, Usability, Portability, Maintainability, Efficiency/performance
  • Source code may be converted into an executable image by a compiler or executed immediately with the aid of an interpreter.

Compiler « Javac is Java Compiler -- Compiles your Java code into Byte-code.

Interpreter « Interpreters are used to execute source code from a programming language line-by-line. Interpreting code is slower because the interpreter must decode each statement and then perform it.

JIT « Java virtual machine Hotspot contains a Just In Time Compiler which selectively compiles[Runs/Interprets/translates] Java byte-code into machine code - but only code which Hotspot predicts is likely to be used many times.

Either compiled or interpreted programs might be executed in a batch process without human interaction.

executable image « machine code consists of the central processing unit's native instructions, ready for execution.

Instruction code:

An Instruction code is a group of bits that instruct the computer to perform a specific operation.

  ___________________________________________
 | Operation  | Address - Specifies operands,|
 |  Code      |  registers (or)_memory word  |
 |____________|______________________________|

Object-oriented programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

In class-based programming, objects are created from classes by subroutines called constructors, and destroyed by destructors.

Objects and classes

Languages that support object-oriented programming typically use inheritance for code reuse and extensibility in the form of either classes or prototypes. Those that use classes support two main concepts:

  • Class - collection of common features of group of objects, which can be considered as plan for creating objects. i.e. classes contains the data members and member functions
  • Object - physical entity an instance of a class that requires memory {state[v] - behavior[m]}. objects are entities that combine state (i.e. data), behavior (i.e. methods) and identity (unique existence among all other objects). The structure and behavior of an object are defined by a class, which is a definition, or blueprint, of all objects of a specific type. An object must be explicitly created based on a class and an object thus created is considered to be an instance of that class.

method area + heap area = premgem space

java.exe is the command where it waits for application to complete untill it takes the next command. javaw.exe is the command which will not wait for the application to complete.

The difference is in the subsystem that each executable targets.

  • java.exe targets the CONSOLE subsystem.
  • javaw.exe targets the WINDOWS subsystem.