The ‘thread’ abstraction A look at the distinction between the idea of a ‘process’ and the concept of a ‘thread’

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Advertisements

The ‘process’ abstraction
Computer Systems/Operating Systems - Class 8
Introduction to Operating Systems CS-2301 B-term Introduction to Operating Systems CS-2301, System Programming for Non-majors (Slides include materials.
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Processes CSCI 444/544 Operating Systems Fall 2008.
“Virtual” Memory How does the OS kernel provide “private” and “shared” memory areas to multiple concurrent processes?
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
The kernel’s task list Introduction to process descriptors and their related data-structures for Linux kernel version
1/28/2004CSCI 315 Operating Systems Design1 Operating System Structures & Processes Notice: The slides for this lecture have been largely based on those.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
CS4315A. Berrached:CMS:UHD1 Operating System Structures Chapter 3.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Operating System A program that controls the execution of application programs An interface between applications and hardware 1.
Operating Systems CMPSC 473 Threads September 16, Lecture 7 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Threads, Thread management & Resource Management.
Implementing Processes and Process Management Brian Bershad.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Processes and OS basics. RHS – SOC 2 OS Basics An Operating System (OS) is essentially an abstraction of a computer As a user or programmer, I do not.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Slide 3-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 3.
Threads G.Anuradha (Reference : William Stallings)
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
CS 346 – Chapter 2 OS services –OS user interface –System calls –System programs How to make an OS –Implementation –Structure –Virtual machines Commitment.
Operating System Principles And Multitasking
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Threads, Thread management & Resource Management.
Assoc. Prof. Dr. Ahmet Turan ÖZCERİT.  What Operating Systems Do  Computer-System Organization  Computer-System Architecture  Operating-System Structure.
What is a Process ? A program in execution.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 2.
CS4315A. Berrached:CMS:UHD1 Introduction to Operating Systems Chapter 1.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Introduction to Operating Systems Concepts
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Kernel Design & Implementation
Process Management Process Concept Why only the global variables?
OPERATING SYSTEMS CS3502 Fall 2017
Process Management Presented By Aditya Gupta Assistant Professor
Threads & multithreading
Processes in Unix, Linux, and Windows
Chapter 4: Threads.
Threads, SMP, and Microkernels
Chapter 15, Exploring the Digital Domain
Chapter 4: Threads.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Process Description and Control
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
CS510 Operating System Foundations
Process Management -Compiled for CSIT
Processes David Ferry, Chris Gill, Brian Kocoloski
Presentation transcript:

The ‘thread’ abstraction A look at the distinction between the idea of a ‘process’ and the concept of a ‘thread’

Recall the ‘process’ notion A process is “a program in execution” Modern systems allow ‘multiprogramming’ (i.e., several processes exist concurrently) Each process requires an allocation of cpu time, in order to make forward progress The OS must do ‘scheduling’ of cpu time Each process also needs the use of other system facilities (memory, files, devices)

Process Control Blocks An operating system represents a process by means of a kernel data-structure known generically as a ‘process control block’ (or ‘task control block’) which keeps a record of a program’s current state, the cpu time consumed and available, and the system resources being accessed or requested Linux calls this record a ‘task_struct’ and maintains a dynamic linked-list of them all

‘task_struct’ memory-map open files pid next_task prev_task gid processor register-values (EIP, ESP, EFLAGS, etc) signal-handlers timers task-statetask-priority task-name terminal process execution aspects resource ownership aspects

Dual task components A process has two distinct aspects –Its ‘execution’ (forward progression of states) –Its ‘ownership’ (share of system’s resources) and these may be usefully disentangled The word ‘thread’ refers to the execution aspect of a process (i.e., the entity which gets ‘scheduled’ by an Operating System for a share of the available cpu time

‘multi-threading’ It is possible for an operating system to support a concept of ‘process’ in which more than one execution-thread exists (with process-resources being shared) one process with one thread one process with multiple threads

Advantages of ‘threads’ All the ‘threads’ that belong to a process can access the same memory, the same files, the same terminal, the same timers and the same signal-handlers Thus the system ‘overhead’ involved in managing a multithreaded task is more efficient (less time-consuming to set up and keep track of) than if each of those threads had individualized ownerships

Easier software development A complex program is easier to write (and debug) if it can be broken down into a set of simpler pieces The programmer doesn’t have to think of ‘the big picture’ all the time, but can focus on each separate piece one at a time

The job of compiling a computer program involves a succession of distinct steps: These steps could be written as separate execution-threads within a single process Example: program compilation preprocessing lexical analysis parsing assemblinglinking

Example: matrix multiplication Each entry in a matrix-product is gotten by summing up the products of numbers from two equal-size lists (a row and a column) Separate ‘threads’ could be working on the individual matrix-entries concurrently: Row 1 times column 1 Row 1 times column 2 Row 2 times column 1 Row 2 times column 2

pipe Process-communication The operating system ‘isolates’ processes by giving them different memory-maps and preventing simultaneous access to files or peripheral devices (e.g. printers, terminals) Special mechanisms (e.g., “pipes”) allow data-communication between processes process A process B process C

Thread communication Since the threads in a process share the same memory, no special mechanism is needed for them to communicate data All these threads can read or write the same memory-locations process

Multiprocessor systems cpu 0 cpu 2 main memory system bus I/O cpu 3 I/O cpu 1 A mult-threaded application may benefit from scheduling its separate threads for simultaneous execution on different CPUs But note that a new hardware issue arises: cache coherency

How Linux supports ‘threads’ Linux uses a refinement of ‘fork()’ to create the illusion of separate ‘threads’ executing within a process (this function is ‘clone()’) It creates a new process (i.e., puts a new ‘task_struct’ into the task-list) but the caller can request that the ‘resources’ should be copies (e.g., same map of virtual memory, same timers, same signal-handlers, etc.)

New ‘scheduler’ not needed By treating ‘processes’ and ‘threads’ in a uniform manner (insofar as scheduling is concerned), Linux avoids extra complexity while still getting most ‘thread’ advantages An additional field in the ‘task_struct’ takes notice of the ‘thread-group’ relationship, so cpu time can be appropriately apportioned among threads (no process monopolizes)

Details of ‘clone()’ The ‘clone()’ library-function takes 4 args –A function that the new thread will execute –A memory-region for use as thread’s stack –Flags (telling which resources to be copied) –A pointer to a data-area for communications It returns the new thread’s process-ID (pid)

Demo: ‘trythread.cpp’ We wrote a demo-program illustrating use of the ‘clone()’ function with different flags First a new thread is created which will be executing within the same memory space (so it can directly access it’s parent’s data) Then another thread is created, but it gets a copy of the memory space, so it doesn’t access the same memory as its parent

In-class exercise Last week we wrote several modules that created pseudo-files (mm, vma, threadlist) showing current information in the kernel Now we can use those modules to study the relationships and differences between processes and threads (in a Linux context) Revise ‘mmfork.cpp’ so it displays the mm and vma info for parent-and-child threads