Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 3
Lecture 3 contents
Multi-Threading
Multi-Threading
Multi-threading refers to an application with multiple threads running within a process, while multi-processing refers to an application organised across multiple OS-level processes.
3: Processes
Multi-Threading
Need: Multiprogramming within a single application Using the same environment for performing different tasks concurrently
Multi-Threading
code registers data files user/kernel stacks process control block process control block code registers Thread control blocks user /kernel stacks data registers user /kernel stacks files registers user /kernel stacks
single threaded
multithreaded
Multi-Threading
The basic unit of CPU scheduling - threads: o program counter; register set; stack space Peer threads share resources like code section and data section A process is created with a single thread Multi-threaded tasks (processes) can have one thread running while another is blocked Good for applications that require sharing a common buffer by server threads A word processor can use three threads Updating the display (WYSIWYG) Interacting with the user (keyboard & mouse) Dealing with i/o to the disk
Unix Fork
A process is created by another process executing fork() function. The created process is named as child process which is created by the parent process the caller. Each process has its own process identifier PID.
Forking vs threading
Forking vs threading: both methods are creating sub-processes in the parent user process. There are differences between both the threads in Java and the forking concept in Unix. When you fork a child process (not thread) gets created. The child process will not share any resources with the parent process from which it got created through forking. But this is not the case with the Threads in Java. When you create threads, there share the same process resources. Whereas if a thread crashes, it takes down all of the other threads in the process, and if a thread has a buffer overrun, it opens up a security hole in all of the threads. Conversely, forking is more heavy-weight than threading, and have a higher start-up and shutdown cost. Inter-process communication (IPC) is also harder and slower than inter-thread communication. Actually threads really win the race when it comes to inter communication.
9
Multi-programming
Multi-programming (or multi-tasking) enables more than a single process to apparently execute simultaneously. How is this achieved on a uniprocoessor? Preemptive multitasking is achieved by rapidly context switching between processes. Each process is given a window of time to execute, called a 'timeslice'. The processor then gets interrupted by a timer, which causes it to context switch to another process, which will run until it blocks, or until the next tick. By switching rapidly enough, it creates the illusion that several processes are running simultaneously. Cooperative multitasking is acheived by having every process yield occasionally, such that other processes can run.
10
Advantage: User level threads are more configurable, as they may use any scheduling algorithm, and they can also run on any OS (probably even DOS) since they do not require kernel support for multiple threads. Diadvantage: The I/O must be non blocking, then require extra checking to deal with cases that would normally block. The Kernel is mulitplexing many user-level threads onto the single kernel thread, all managed within the process. So User level threads cannot take advantage of multiple cpu, because the OS only sees 1 process with 1 thread
11
12
Operating System
A collection of system programs which allow the user to run application software.
Manages processes and resources. Abstracts the real hardware of the system and presents the users and applications with a virtual machine. Has utilities for system administration tasks.
OS Mechanisms
System calls
System Calls
User programs are not allowed to access system resources directly. They must ask the OS to do that for them. OS provides a set of functions that can be called by user programs to request for OS services. These functions are called system calls
System calls: The mechanism used by an application program to request service from the operating system. System calls often use a special machine code instruction which causes the processor to change mode from user mode to kernel mode. When the system call finishes, processor returns to the user program and runs in user mode.
16
17
Processor Modes
Modern processors typically can operate in 2 modes: "user mode" and "kernel mode " .
User mode
Kernel mode
Kernel
The kernel is the core of the operating system. It consists of code and data structures that are protected and can be accessed only in the kernel mode.
In a processing of user program, the operating system operates in what is called a user mode. If an instruction in the user program tries to access the computer (hardwaresoftware) resources, this access is named as a system call, the operating system operates in what is called a Kernel mode. It is named as a kernel mode, where the operating system runs this instruction by itself to avoid harmful actions by direct user instructions. So, this method is applied for both, security and protection. After this system call finishes, the operating system return to the user mode again in the running the next instructions. A normal user instruction like this sentence (x = x+1;).
20
Process control: E.g., create, load, execute File operations: E.g., create, open, read, write Device manipulation: E.g., request, read, write
receive message
21
22
An API is a set of functions provided by an operating system or other system software. These functions are available to an application programmer, including the parameters that are passed to each function and the return values the programmer can expect.
An application program calls the functions to request the services from the operating system.
23
Portability: User programs that follow the APIs definition are portable. For example, the UNIX file system API is the same for all kinds of devices. Also Windows API has many implementations on different machine architectures.
Using an API allows upgrading system software without changing user programs. Actual system calls can often be more detailed and difficult to work with than he API available to an application programmer.
24
Examples of APIs
Three of the most common APIs available to application programmers are the win32 (for windows systems), POSIX (for all versions of unic, linux and mac OS) and java API that runs on the java virtual machine.
25
Virtual Machines
Processes provided with an interface virtually identical to the bare hardware Each process is provided with a (virtual) copy of the underlying computer Benefits:
Same hardware can run several different execution environments (that is, different operating systems. Host system is protected from VM. Where each VM is completely isolated from other VMs. And no direct sharing of resources.
26
Non-virtual Machine
Virtual Machine
27
Exception Mechanism
In Exception cases, The OS is Switching from user mode to kernel mode to perform system operations and return to user mode. Exception is caused by hardware interrupt (I/O) and software interrupt.
Some special instructions cause processor to switch between user and kernel modes: trap, return_from_trap.
Exception Handler When an exception occurs, an exception handler is called. Exception handler code is in kernel's protected memory Exception hander code for software traps are system calls
28
Exception Mechanism
Interrupt: An interrupt is a hardware mechanism that enables an external device, typically I/O devices, to send a signal to the CPU. An interrupt signal requests the CPU to interrupt its current activities and attend to the interrupting devices needs. A CPU will check interrupts only after it has completed the processing of one instruction and before it fetches a subsequent one. The basic interrupt mechanism works as follows: The CPU hardware has wire called the interrupt-request line that the CPU senses after executing
Exception Mechanism
What is the difference between a Trap, exception and Interrupt handler? Trap is a user program initiated and expected transfer of control to a special handler routine. An exception is an automatically a generated trap that occurs in response to some exceptional condition. Interrupts are program control interruption based on an external hardware event (external to the CPU).
30
create a new process execute a file, replace the process memory space with a new program. terminate proces wait for a child process to terminate
sbrk change the size of the space allocated in the heap data segment of the process (used by memory allocation functions, e.g. malloc)
31
Operating Systems
Lecture 3