Tuesday, April 10, 2012

Threads


  • A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers, ( and a thread ID. )


the difference between a Thread and a Process?

A process is an executing instance of an application.
For example, when you double-click the Microsoft Word icon, you start a process that runs Word.
A process is a collection of virtual memory space, code, data, and system resources.
A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the

primary thread.
A process can have multiple threads in addition to the primary thread
a process is an instance of a computer program that is being sequentially executed


A thread is a path of execution within a process.
a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.
A thread is code that is to be serially executed within a process
Thread – is stream of executable code within process. They are light weight process.
A single process may contain several executable programs (threads) that work together as a coherent whole



http://www.cs.jhu.edu/~yairamir/cs418/os2/index.htm
http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node99.html
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html





  • For example in a word processor, a background thread may check spelling and grammar while a foreground thread processes user input ( keystrokes ), while yet a third


thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited.

Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously, without having to service requests sequentially or to

fork off separate processes for every incoming request.
( The latter is how this sort of thing was done before the concept of threads was developed. A daemon would listen at a port, fork off a child for every incoming

request to be processed, and then go back to listening to the port. )

A single threaded process can only run on one CPU, no matter how many may be available, whereas the execution of a multi-threaded application may be split amongst

available processors.


There are two types of threads to be managed in a modern system: User threads and kernel threads.

User threads are supported above the kernel, without kernel support. These are the threads that application programmers would put into their programs.

Kernel threads are supported within the kernel of the OS itself. All modern OSes support kernel level threads, allowing the kernel to perform multiple simultaneous

tasks and/or to service multiple kernel system calls simultaneously.





Thread libraries provide programmers with an API for creating and managing threads.

There are three main thread libraries in use today:

POSIX Pthreads - may be provided as either a user or kernel library, as an extension to the POSIX standard.

Win32 threads - provided as a kernel-level library on Windows systems.

Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads depending on the system.



http://www2.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html







  • An application typically is implemented as a separate process with several threads of control.

A web browser might have one thread display images or text while another thread retrieves data from the network.

A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling

and grammar checking in the background.

Consider the Web server could be written in the absence of threads. The net result is that many fewer requests/sec can be processed.

The better approach would multithread the web-server process. Thus threads gain considerable performance.
The server would create a separate thread that would listen for client requests;
when a request was made, rather than creating another process, the server would create another thread to service the request.

Many OS kernels are now multithreaded; several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling.

http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node100.html




  • Three main thread libraries are in use today:


POSIX Pthreads. Pthreads, the threads extension of the POSIX standard, may be provided as either a user- or kernel-level library.

Win32. The Win32 thread library is a kernel-level library available on Windows systems.

Java. The Java thread API allows thread creation and management directly in Java programs. However, because in most instances the JVM is running on top of a host OS, the Java thread API is typically implemented using a thread library available on the host system.
http://siber.cankaya.edu.tr/OperatingSystems/ceng328/node106.html

  • concurrency is related to how an application handles multiple tasks it works on. An application may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently).

Parallelism on the other hand, is related to how an application handles each individual task. An application may process the task serially from start to end, or split the task up into subtasks which can be completed in parallel.
 As you can see, an application can be concurrent, but not parallel. This means that it processes more than one task at the same time, but the tasks are not broken down into subtasks.

An application can also be parallel but not concurrent. This means that the application only works on one task at a time, and this task is broken down into subtasks which can be processed in parallel.

Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at a time, and the task is never broken down into subtasks for parallel execution.
http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html

  • The Java Concurrency Utilities framework is a library of types that are designed to be used as building blocks for creating concurrent classes or applications. These types are thread-safe, have been thoroughly tested, and offer high performance.

Examples include semaphores, barriers, thread pools, and concurrent hashmaps.


https://www.javaworld.com/article/2078809/java-concurrency/java-concurrency-java-101-the-next-generation-java-concurrency-without-the-pain-part-1.html

  • Lesson: Concurrency

Computer users take it for granted that their systems can do more than one thing at a time. They assume that they can continue to work in a word processor, while other applications download files, manage the print queue, and stream audio. Even a single application is often expected to do more than one thing at a time. For example, that streaming audio application must simultaneously read the digital audio off the network, decompress it, manage playback, and update its display. Even the word processor should always be ready to respond to keyboard and mouse events, no matter how busy it is reformatting text or updating the display. Software that can do such things is known as concurrent software.
https://docs.oracle.com/javase/tutorial/essential/concurrency/

  • A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicing.

Processes
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
Processes are often seen as synonymous with programs or applications. However, what the user sees as a single application may in fact be a set of cooperating processes. To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. IPC is used not just for communication between processes on the same system, but processes on different systems.


Threads
Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files

https://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

What difference is between prefix and postfix operator in java? ++a , a++

1) The prefix ++ operator first increments the value by one and then returns the new value.
while The postfix ++ operator first returns the value and then increments it.

2) prefix will increment first and assigns to the variable postfix will assing the value to the variable and then increments

Communication in Client-Server Systems

3.6 Communication in Client-Server Systems


  • 3.6.1 Sockets

A socket is an endpoint for communication.
Two processes communicating over a network often use a pair of connected sockets as a communication channel

Software that is designed for client-server operation may also use sockets for communication between two processes running on the same computer - For example, the UI for a database program may communicate with the back-end database manager using sockets.

A socket is identified by an IP address concatenated with a port number, e.g. 200.100.50.5:80.
Sockets are considered a low-level communications channel

Communication channels via sockets may be of one of two major forms,
1-Connection-oriented ( TCP, Transmission Control Protocol )
2-Connectionless ( UDP, User Datagram Protocol )



  • 3.6.2 Remote Procedure Calls, RPC

The general concept of RPC is to make procedure calls similarly to calling on ordinary local procedures, except the procedure being called lies on a remote machine.
Implementation involves stubs on either end of the connection.

The local process calls on the stub, much as it would call upon a local procedure.
The RPC system packages up ( marshals ) the parameters to the procedure call, and transmits them to the remote system.
On the remote side, the RPC daemon accepts the parameters and calls upon the appropriate remote procedure to perform the requested work.
Any results to be returned are then packaged up and sent back by the RPC system to the local system, which then unpackages them and returns the results to the local calling procedure.


  • 3.6.3 Pipes

Pipes are one of the earliest and simplest channels of communications between ( UNIX ) processes.
There are four key considerations in implementing pipes:

Unidirectional or Bidirectional communication?
Is bidirectional communication half-duplex or full-duplex?
Must a relationship such as parent-child exist between the processes?
Can pipes communicate over a network, or only on the same machine?


Unidirectional=operating or moving in one direction only; not changing direction
Bidirectional=capable of reacting or functioning in two, usually opposite, directions.
half-duplex=of or pertaining to the transmission of information in opposite directions but not simultaneously.
full-duplex=of or pertaining to the simultaneous, independent transmission of information in both directions over a two-way channel.






  • 3.6.3.1 Ordinary Pipes

Ordinary pipes are uni-directional, with a reading end and a writing end
Ordinary pipes in Windows are very similar. Windows terms them anonymous pipes



  • 3.6.3.2 Named Pipes

Named pipes support bidirectional communication, communication between non-parent-child related processes, and persistence after the process which created them exits.
Multiple processes can also share a named pipe, typically one reader and multiple writers




  • OLD 3.6.3 Remote Method Invocation, RMI ( Removed from 8th edition )


RMI is the Java implementation of RPC for contacting processes operating on a different Java Virtual Machine, JVM, which may or may not be running on a different physical machine.

There are two key differences between RPC and RMI, both based on the object-oriented nature of Java:
RPC accesses remote procedures or functions, in a procedural-programming paradigm. RMI accesses methods within remote Objects.
The data passed by RPC as function parameters are ordinary data only, i.e. ints, floats, doubles, etc. RMI also supports the passing of Objects.


RMI is implemented using stubs ( on the client side ) and skeletons ( on the server's side), whose responsibility is to package ( marshall ) and unpack the parameters and return values being passed back and forth


http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/3_Processes.html


  • Socket


A socket is a transport mechanism. Sockets are like applying procedural networking to object-oriented environment.
Sockets-based network programming can be laborious.

RMI

RMI uses sockets. RMI is object-oriented. Methods can be invoked on the remote objects running on a separate JVM.
RMI provides a convenient abstraction over raw sockets. Can send and receive any valid Java object utilizing underlying object serialization without having to worry about using data streams.

http://www.cloudsopedia.com/interviewquestions/j2ee/j2ee-rmi.php


context switch

context switch

Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process.
This task is known as a context switch.
When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run.

http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node91.html

Process Scheduling

3.2 Process Scheduling

The two main objectives of the process scheduling system are to keep the CPU busy at all times and to deliver "acceptable" response times for all programs, particularly for interactive ones


http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/3_Processes.html

Processes


  • Processes

A process is an instance of a program in execution.

Process in the memory is divided into four sections

The text section comprises the compiled program code, read in from non-volatile storage when the program is launched.
The data section stores global and static variables, allocated and initialized prior to executing main.
The heap is used for dynamic memory allocation, and is managed via calls to new, delete, malloc, free, etc.
The stack is used for local variables

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/3_Processes.html


  • Processes - Part I

http://www.youtube.com/watch?v=TIa2mhKCeYo&feature=related

what is a process?
an instance of a computer program in execution
can have several threads
each process has its own state,address and space

process in memory
-stack(local function variables)
-heap(memory allocated directly by a program)
-shared variables
-global variables
-text(program code converted to machine instructions)

process states
-new
-ready
-running
-waiting
-terminated



  • Processes - Part II

http://www.youtube.com/watch?v=_5EV7isUJ6k&feature=relmfu

  • process are created by system call fork() on unix-like systems

exec system call
fork system call

child process that run in the background after parent process terminates are

In Linux we can set guidelines for the CPU to follow when it is looking at all the tasks it has to do. These guidelines are called niceness or nice value. The Linux niceness scale goes from -20 to 19. The lower the number the more priority that task gets. If the niceness value is high number like 19 the task will be set to the lowest priority and the CPU will process it whenever it gets a chance. The default nice value is zero.

By using this scale we can allocate our CPU resources more appropriately. Lower priority programs that are not important can be set to a higher nice value, while high priority programs like daemons and services can be set to receive more of the CPU’s focus. You can even give a specific user a lower nice value for all of his/her processes so you can limit their ability to slow down the computer’s core services.

https://www.nixtutor.com/linux/changing-priority-on-linux-processes/called demon processes

  • Linux Kernel schedules the process and allocates CPU time accordingly for each of them. But, when one of your process requires higher priority to get more CPU time, you can use nice and renice command
A nice value of -20 represents highest priority, and a nice value of 19 represent least priority for a process.
By default when a process starts, it gets the default priority of 0.
https://www.thegeekstuff.com/2013/08/nice-renice-command-examples/?utm_source=tuicool
  • A Vanilla Kernel is the official Kernel released on http://www.kernel.org/. It is a standard release, ?gzipped tar kernel file, from kernel.org.
https://wiki.debian.org/vanilla

  • The kernels at www.kernel.ORG are vanilla kernels.
Each different distribution takes these vanilla kernels and adds their own type of flavoring. They may fix particular bugs in the code (Debian is very good at doing this), and they may add their own patches to do fancy things (SuSE adds a patch to do a boot screen animation), and they may add patches to enhance the facilities of the kernel (often using the source from the developmental branch of the kernel eg for very new USB devices).Thus, although all kernels are derived from the same kernel source, not all kernels are equal.
https://www.linuxquestions.org/questions/linux-general-1/what-is-vanilla-kernel-79388/


Virtual Machines

2.8 Virtual Machines
The concept of a virtual machine is to provide an interface that looks like independent hardware, to multiple different OSes running simultaneously on the same physical hardware. Each OS believes that it has access to and control over its own CPU, RAM, I/O devices, hard drives, et

2.8.3 Simulation
An alternative to creating an entire virtual machine is to simply run an emulator, which allows a program written for one OS to run on a different OS.
For example, a UNIX machine may run a DOS emulator in order to run DOS programs, or vice-versa.

2.8.4 Para-virtualization
Para-virtualization is another variation on the theme, in which an environment is provided for the guest program that is similar to its native OS, without trying to completely mimic it
Solaris 10 uses a zone system, in which the low-level hardware is not virtualized, but the OS and its devices ( device drivers ) are.

2.8.6 Examples
2.8.6.1 VMware
2.8.6.2 The Java Virtual Machine



http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/2_Structures.html

Modules

2.7.4 Modules
Modern OS development is object-oriented, with a relatively small core kernel and a set of modules which can be linked in dynamically.

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/2_Structures.html

Microkernels

2.7.3 Microkernels
The basic idea behind micro kernels is to remove all non-essential services from the kernel, and implement them as system applications instead, thereby making the kernel as small and efficient as possible.
Security and protection can be enhanced, as most services are performed in user mode, not kernel mode


http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/2_Structures.html

System Calls

2.3 System Calls
System calls provide a means for user or application programs to call upon the services of the operating system.
Generally written in C or C++, although some are written in assembly for optimal performance.


2.4 Types of System Calls

2.4.1 Process Control
2.4.2 File Management
2.4.3 Device Management
2.4.4 Information Maintenance
2.4.5 Communication
2.4.6 Protection

http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/2_Structures.html