2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Threads Section 2.2. Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Threads CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Jonathan Walpole Computer Science Portland State University
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Share Memory Program Example int array_size=1000 int global_array[array_size] main(argc, argv) { int nprocs=4; m_set_procs(nprocs); /* prepare to launch.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
OPERATING SYSTEM LESSON 5 THREADS. 2 The process model is based on two independent concepts: resource grouping and execution. A process is the way to.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Threads. Progressing With Parallel Processing(PWPP?) eWeek (09/18/06) Vol. 23, No. 37, P. D5 Multithreading skills are becoming essential as parallel.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Process Tables; Threads
Processes and Threads Processes and their scheduling
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Threads.
Chapter 4: Threads.
Operating Systems Threads.
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Process Tables; Threads
Thread Programming.
CS510 Operating System Foundations
Operating System Concepts
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
What is a Thread? A thread is similar to a process, but it typically consists of just the flow of control. Multiple threads use the address space of a.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads: single address space + many threads of execution  Shared global variables, stack address space, open files, signals, child processes, etc.

Threads  Process – used to group resources together  Threads – entities scheduled for execution on CPU  Sometimes called lightweight processes  Multithreading – multiple threads in the same process

Thread functions  Create threads: #include #include int pthread_create ( pthread_t* thread, pthread_attr_t* attr, void* (*start_routine)(void *), void* arg );

Thread termination void pthread_exit ( void* retval ); int pthread_cancel ( pthread_t thread );

Why threads?  Easier to create a thread than a process  Share resources  Compute and I/O within a single process on a single processor can be overlapped  Compute and compute within a single process on a multiprocessor can be overlapped

Example: word processor  Multiple threads: 1.User interaction 2.Document reformatting 3.Automatic saving/backups  Alternative is everything else stops when (2) or (3) occurs.

Example: web server  Dispatcher – handles incoming requests  Workers – performs request  Checks cache  Reads from disk if necessary  Adds to cache

Threads and alternatives  Threads retain the simple sequential, blocking model while allowing for parallelism.  The single threaded server retains the simple sequential, block model but performance suffers (no parallelism).  Finite state machine = each computation has a saved state and there exists some set of events that can occur to change the state  High performance through parallelism  Uses nonblocking calls and interrupts (not simple)  May be in user space or kernel.

Threads  Thread types:  Detached  Joinable  (also popup)  Implementations:  User space  Kernel  Hybrid

#include #include bool err = false; void* t1 ( void* p ) { … err = true; if (err) puts( “hello” ); //may never get here! …} void* t2 ( void* p ) { … err = false; …} int main ( int argc, char* argv[] ) { … if (err) puts( “something bad happened.” ); …} Pitfall: global variables

Pitfalls: global variables

Pitfall: global variables  Solution: don’t use ‘em.  Solution: create thread-wide globals (using your own libraries)  Solution: (other mechanisms that we will explore in the future)

Other pitfalls  Libraries may not be reentrant  Solution: rewrite the library  Solution: wrap each library call with a wrapper  Signals, alarms, and I/O may not be thread specific.

Other pthread functions  int thread_yield()  int pthread_join( pthread_t th, void** thread_return )  void pthread_exit( void* retval )  Many, many others

Multithreaded example