Βάνα Καλογεράκη Τµήµα Πληροφορικής Οικονοµικό Πανεπιστήµιο Αθηνών 1 Lecture 3: Threads (Νήµατα)
The Case for Cooperating Processes Independent process: cannot affect or be affected by the execution of another process Cooperating process: can affect or be affected by the execution of another process What are the reasons for process cooperation? Information sharing (e.g., interested in the same file) Computation speed-up (break a big task in smaller sub-tasks) Modularity (e.g., divide the system functions into separate processes or threads) Convenience (many concurrent tasks e.g., editing, printing) 2
Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions without sharing the same address space Message-Passing System: processes communicate with each other without the need to resort to shared variables 3
Interprocess Communication (cont.) IPC facility provides two operations: send(message) message size is fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive operations 4
Direct Communication Processes that want to communicate must name each other explicitly Message Primitives: send (P, message) send a message to process P receive (Q, message) receive a message from process Q Properties Processes must name each other explicitly must know each other s id One link between each pair of communicating processes The link may be unidirectional, but is usually bi-directional 5
Indirect Communication Messages are directed and received from mailboxes Each mailbox has a unique id Processes can communicate only if they share a mailbox Properties Link established only if processes share a common mailbox A link may be associated with many processes; a pair of processes may share several communication links. Link may be unidirectional or bi-directional. 6
Interprocess Communication Mechanisms Pipes Signals Message queues Semaphores Shared memory Sockets 7
Threads Overview Thread States Processes and Threads Multithreading Models Threads in Java Today s Outline 8
Multithreading vs. Single threading Multithreading: when the OS supports multiple threads of execution within a single process The basic idea is that if a process has multiple threads of control, it can do more than one tasks at a time Single threading: when the OS does not recognize the concept of a thread 9
Processes (Διεργασίες) A process is a program in execution A process consists of: An executable program Associated data (variables, buffers,..) Process state (processor registers) A process needs resources (CPU time, memory, files) to accomplish a task Allocated when a process is created or.. While the process is executing Address space: set of all objects (usually in memory) accessible by the process 10
Process Control Block (PCB) Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information PCB 11
What is a Thread (Νήµα)? Is the basic unit of CPU utilization Also known as lightweight process (LWP) Αποτελείται από: χωριστή στοίβα (stack), καταχωρητές (register set), µετρητής προγράµµατος (program counter ), thread id Μοιράζεται µε άλλα threads στην ίδια διεργασία: κώδικα (code section), δεδοµένα (data section,) αρχεία (open files), signals 12
Single and Multithreaded Processes 13
Διεργασίες και Νήµατα Each process has Virtual address space that holds the process image Access to resources (files, I/O resources..) and other processes (for interprocess communication) Within a process, there may be one or more threads, each with the following An execution state (running, ready, etc.) An execution stack for each thread Some per-thread static storage for local variables Access to memory and resources of its process, shared with all threads in that process 14
Ερωτήσεις Εάν ένα thread µεταβάλει την τιµή µια µεταβλητής στη µνήµη, µπορούν τα άλλα threads να δουν το αποτέλεσµα αυτής της µεταβολής όταν διαβάσουν τη µεταβλητή? Εάν ένα thread ανοίξει ένα αρχείο για διάβασµα (read privileges), µπορούν τα άλλα νήµατα της ίδιας διεργασίας να διαβάσουν από το ίδιο αρχείο? 15
Multithreaded Applications Παραδείγµατα: Web servers, word processors, P2P applications Αξιοποίηση στους πελάτες Απόκρυψη καθυστερήσεων του δικτύου Παράδειγµα: ανάγνωση ιστοσελίδας (webpage) Εµφάνιση κειµένου προς ανάγνωση Πολλά νήµατα µεταφοράς εικόνων Αξιοποίηση στους εξυπηρετητές Διαχείριση πολλών πελατών Αξιοποίηση πολλών επεξεργαστών Παράδειγµα: εξυπηρετητής αρχείων Πολλά νήµατα για εξυπηρέτηση αιτήσεων Ένα νήµα για διανοµή αιτήσεων Κάθε νήµα λειτουργεί και εµποδίζεται αυτόνοµα 16
How Multithreading works in a Web Server Application? Web client A Assume that you have N Web clients. How many threads should the Web Server create? Web server Database Web client B Μοντέλο διανοµέα/εργατών: εξυπηρετητής ιστοσελίδων Όλα τα νήµατα-εργάτες είναι παρόµοια Εξυπηρέτηση αιτήσεων από οποιοδήποτε νήµα 17
Thread States Three thread states: running, ready, blocked No suspend state because all threads within the same process share the same address space Process termination: terminates all threads within the process 18
Thread Descriptor Data structure where the OS keeps all information it needs to manage a thread State Thread s current state (running, ready, blocked) Execution statistics Time accumulation, start time,.. Process List of related threads Stack Other resources A reference to the process descriptor of the thread s associated process A reference to a list of parent/child/sibling threads The location of the base thread s stack in main memory Reference to the thread-specific resources 19
Πλεονεκτήµατα Παράλληλη εκτέλεση πολλών βηµάτων Χωριστά νήµατα για κλήσεις εισόδου/εξόδου Ταυτόχρονη επικοινωνία µε χρήστη και επεξεργασία Ένα thread µπορεί να εξακολουθεί να εκτελείται ακόµα κι αν µέρος της διεργασίας είναι µπλοκαρισµένη (blocked) ή εκτελεί µακρόχρονη λειτουργία Αξιοποίηση εγγενούς παραλληλισµού εφαρµογής Χρήση όλων των διαθέσιµων επεξεργαστών (πχ. παράλληλη αρχιτεκτονική) Γρήγορη επικοινωνία µέσω κοινής µνήµης 20
Πλεονεκτήµατα (συνέχεια) Επίδοση: χρειάζεται λιγότερος χρόνος να δηµιουργήσουµε ή να καταστρέψουµε threads (πχ. σε σύγκριση µε τις processes) Takes less time to create a new thread in an existing process than to create a new process Takes less time to terminate a thread than a process Takes less time to switch between two threads in the same process Because threads within the same process share memory and files, they can communicate with each other without invoking the kernel 21
Thread Operation Latencies Operation User-Level Threads Kernel-Level Threads Processes Null Fork 34 948 11,300 Signal Wait 37 441 1,840 22
Any Challenges? Synchronization between threads that share the same resources Concurrent execution may be difficult 23
Example of an inconsistent view using threads 2 shared variables: A, B Thread T1 and T2 T1 transfers amount X from A to B T1 must do: A=A-X and B=B+X A+B is unchanged T2 computes C=A+B Schedule 1: Schedule 2: Schedule 3: T1: A=A-X T1: A=A-X T2: C=A+B T2: C=A+B T1: B=B+X T1: A=A-X T1: B=B+X T2: C=A+B T1: B=B+X 24
Thread Categories User-level threads Implemented by a thread library at the user level (e.g., POSIX Pthreads, Solaris 2 UI-threads) All thread creation and scheduling done in user space without the need for kernel intervention Kernel-level threads Supported by the OS (e.g., Windows NT/2000, Solaris 2..) Thread creation, scheduling and management done in kernel space Usually slower to create and manage than user threads 25
Questions What happens if the kernel is single-threaded and a user-level thread performs a blocking system call? What happens if a kernel-level thread performs a blocking system call? 26
Many-to-One Multithreading Model Many user-level threads mapped to a single kernel thread Used on systems that do not support kernel threads The entire process blocks if a thread makes a blocking system call 27
One-to-One Multithreading Model 28 Each user-level thread maps to a kernel thread Multiple threads run in parallel on multiprocessors despite blocking system calls Examples Windows NT/2000 OS/2
Many-to-Many Multithreading Model 29 Many user-level threads mapped to a smaller or equal number of kernel threads (not 1-1 mapping necessary) Examples Solaris 2, HP-UX, IRIX
Advantages and disadvantages of using userlevel threads Advantages Thread switching does not involve the kernel Scheduling can be application specific: choose the best algorithm User-level threads can run on any OS. Need to provide a thread library (e.g., Posix threads package, Java threads package) Disadvantages If system calls are blocking then the kernel blocks processes. So all threads within the process will be blocked The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors with user-level threads 30
Advantages and disadvantages of using kernel-level threads Advantages The kernel can simultaneously schedule many threads of the same process on many processors Blocking is done on a thread level Kernel itself and its internal routines can be multithreaded Disadvantages Thread switching within the same process involves the kernel Results in poor performance 31
Νήµατα στην Java Κάθε εφαρµογή ξεκινά µε ένα νήµα Εκτελεί τη µέθοδο main της εφαρµογής Το νήµα αυτό µπορεί να δηµιουργήσει άλλα Διάφοροι τρόποι διακοπής εκτέλεσης Αναµονή εισόδου / εξόδου Αναµονή χρονοµέτρου (sleep) Αναµονή γεγονότος (wait) Προτεραιότητες νηµάτων Κάθε νήµα έχει προτεραιότητα 1 (χαµηλή) έως 10 (υψηλή) Η προτεραιότητα κληρονοµείται από τον γονέα Χρονοπρογραµµατισµός ανάλογα µε την προτεραιότητα Διακοπτόµενος ή µη διακοπτόµενος χρονοπρογραµµατισµός 32
Νήµατα στην Java Νήµατα: υποστηρίζονται µέσω της τάξης Thread Thread (String όνοµα_νήµατος) Κατασκευάζει ένα νήµα µε συγκεκριµένο όνοµα Thread () Κατασκευάζει ένα νήµα µε όνοµα "Thread-#" void start() Ξεκινά την εκτέλεση ενός νέου νήµατος (µέθοδος run()) Τα δύο νήµατα εκτελούνται παράλληλα void run () Η µέθοδος που περιέχει τον κώδικα του νήµατος Κάθε thread ορίζει τη δική της run() void setname (String όνοµα) / String getname () Θέτει/επιστρέφει το όνοµα του νήµατος 33
Νήµατα στην Java 34 static Thread currentthread () Επιστρέφει µία αναφορά προς το εκτελούµενο νήµα void sleep (int διάστηµα) Αποκοιµίζει το νήµα για διάστηµα msec void yield () Παραχωρεί τον επεξεργαστή σε άλλα νήµατα void interrupt () Στέλνει στο νήµα ένα σήµα διακοπής σ ένα νήµα Αν το νήµα είναι σε κοιµισµένο ή σε αναµονή ξυπνάει Αλλιώς το νήµα πρέπει να ελέγχει ρητά αν έχει λάβει διακοπές boolean isinterrupted () Επιστρέφει true αν το νήµα έχει διακοπεί
Νήµατα στην Java 35 boolean isalive () Επιστρέφει true αν το νήµα εκτελείται void setpriority (int προτεραιότητα) / int getpriority () Θέτει/επιστρέφει την προτεραιότητα του νήµατος void join (long διάστηµα) Περιµένει τον τερµατισµό του νήµατος για διάστηµα msec Αν το διάστηµα είναι 0, αναµονή για πάντα void join (): Ισοδύναµη µε join(0) void checkaccess () Εξετάζει αν έχουµε πρόσβαση στο νήµα Εξαίρεση αν δεν έχουµε, επιστροφή αν έχουµε
Νήµατα στην Java 36 ThreadGroup getthreadgroup () Επιστρέφει την οµάδα του νήµατος int activecount () Επιστρέφει το πλήθος ενεργών νηµάτων της οµάδας int enumerate (Thread[] πίνακας) Αντιγράφει κάθε ενεργό νήµα της οµάδας στον πίνακα Αν ο πίνακας είναι µικρός, δεν αποθηκεύονται όλα String tostring () Επιστρέφει όνοµα, προτεραιότητα και οµάδα του νήµατος void dumpstack () Εµφανίζει τη στοίβα κλήσεων των µεθόδων του νήµατος Χρήσιµη για εκσφαλµάτωση εφαρµογών
A Simple Example The TwoThreadsTest class provides a main() method that creates two SimpleThread threads: one is named "Jamaica" and the other is named "Fiji The main() method also starts each thread immediately following its construction by calling the start() method 37 class TwoThreadsTest { public static void main (String args[]) { } } new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start();
A Simple Example 38 class SimpleThread extends Thread { // Thread class provided by the java.lang package public SimpleThread(String str) { // constructor that takes as argument the name of thread super(str); } public void run() { // the heart of any thread for (int i = 0; i < 10; i++) { //in each iteration it prints iteration number + thread name System.out.println(i + " " + getname()); try { sleep((int)(math.random() * 1000)); // sleeps between 0..1 second } catch (InterruptedException e) {} } } } System.out.println("DONE! " + getname()); // when it finishes it prints DONE
A Simple Example 39 0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 3 Jamaica 4 Jamaica Notice that the output from each thread is intermingled with the output from the other. This is due to the fact that both SimpleThread threads are running concurrently thus both run() methods are running at the same time and each thread is displaying its output at the same time as the other. 4 Fiji 5 Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7 Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9 Jamaica DONE! Jamaica
Summary Today: Threads Multithreading Concepts Thread States Threads and Processes Multithreading Model Threads in Java 40