Processes
• Recall that a process includes many things
• An address space (defining all the code and data pages)
• OS resources (e.g., open files) and accounting information
• Execution state (PC, SP, regs, etc.)
• Creating a new process is costly because of all of the data
structures that must be allocated and initialized
• Recall struct proc in Solaris
• …which does not even include page tables, perhaps TLB
flushing, etc.
• Communicating between processes is costly because most
communication goes through the OS
• Overhead of system calls and copying data
Parallel Programs
• To execute these programs we need to
• Create several processes that execute in parallel
• Cause each to map to the same address space to share data
• They are all part of the same computation
• Have the OS schedule these processes in parallel
• This situation is very inefficient
• Space: PCB, page tables, etc.
• Time: create data structures, fork and copy addr space, etc.
• Solutions: possible to have more efficient, yet cooperative
“processes”?
Rethinking Processes
• What is similar in these cooperating processes?
• They all share the same code and data (address space)
• They all share the same privileges
• They all share the same resources (files, sockets, etc.)
• What don’t they share?
• Each has its own execution state: PC, SP, and registers
• Key idea: Why don’t we separate the concept of a process from
its execution state?
• Process: address space, privileges, resources, etc.
• Execution state: PC, SP, registers
• Exec state also called thread of control, or thread
Threads
• Modern OSes (Mac, Windows, modern Unix) separate
the concepts of processes and threads
• The thread defines a sequential execution stream within a
process (PC, SP, registers)
• The process defines the address space and general process
attributes (everything but threads of execution)
• A thread is bound to a single process
• Processes, however, can have multiple threads
• Threads become the unit of scheduling
• Processes are now the containers in which threads execute
Analogy:
• Process:
• Hire 4 software engineers to work on 4 projects
• Thread:
• Have one engineer to work on 4 projects
The thread model
• Shared information
• Processor info: parent process, time, etc
• Memory: segments, page table, and stats, etc
• I/O and file: communication ports, directories and file descriptors, etc
• Private state
• Registers
• Program counter
• Execution stack
• State (ready, running and blocked)
• Why?
• Each thread execute separately
Kernel-Level Threads
• We have taken the execution aspect of a process and
separated it out into threads
• To make concurrency cheaper
• As such, the OS now manages threads and processes
• All thread operations are implemented in the kernel
• The OS schedules all of the threads in the system
• OS-managed threads are called kernel-level threads or
lightweight processes
• Windows: threads
• Solaris: lightweight processes (LWP)
• POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM
Kernel-level Thread
Limitations
• Kernel-level threads make concurrency much cheaper than
processes
• Much less state to allocate and initialize
• However, for fine-grained concurrency, kernel-level threads
still suffer from too much overhead
• Thread operations still require system calls
• Ideally, want thread operations to be as fast as a procedure call
• For such fine-grained concurrency, need even “cheaper”
threads
User-Level Threads
• To make threads cheap and fast, they need to be
implemented at user level
• Kernel-level threads are managed by the OS
• User-level threads are managed entirely by the run-time system
(user-level library)
• User-level threads are small and fast
• A thread is simply represented by a PC, registers, stack, and
small thread control block (TCB)
• Creating a new thread, switching between threads, and
synchronizing threads are done via procedure call
• No kernel involvement
• User-level thread operations 100x faster than kernel threads
• pthreads: PTHREAD_SCOPE_PROCESS
User-level Thread Limitations
• But, user-level threads are not a perfect solution
• As with everything else, they are a tradeoff
• User-level threads are invisible to the OS
• They are not well integrated with the OS
• As a result, the OS can make poor decisions
• Scheduling a process with idle threads
• Blocking a process whose thread initiated an I/O, even though the
process has other threads that can execute
• Solving this requires communication between the kernel and
the user-level thread manager
Kernel- vs. User-level Threads
• Kernel-level threads
• Integrated with OS (informed scheduling)
• Slow to create, manipulate, synchronize
• User-level threads
• Fast to create, manipulate, synchronize
• Not integrated with OS (uninformed scheduling)
• Understanding the differences between kernel- and
user-level threads is important
• For programming (correctness, performance)
• For test-taking
Kernel- and User-level
Threads
• Or use both kernel- and user-level threads
• Can associate a user-level thread with a kernel-level thread
• Or, multiplex user-level threads on top of kernel-level
threads
• Java Virtual Machine (JVM)
• Java threads are user-level threads
• On older Unix, only one “kernel thread” per process
• Multiplex all Java threads on this one kernel thread
• On Windows NT, modern Unix
• Can multiplex Java threads on multiple kernel threads
• Can have more Java threads than kernel threads
Implementing Threads
• Implementing threads has a number of issues
• Interface
• Context switch
• Preemptive vs. Non-preemptive
• What do they mean?
• Scheduling
• Synchronization (next lecture)
• Focus on user-level threads
• Kernel-level threads are similar to original process
management and implementation in the OS
Sample Thread Interface
• thread_create(procedure_t, arg)
• Create a new thread of control
• Start executing procedure_t
• thread_yield()
• Voluntarily give up the processor
• thread_exit()
• Terminate the calling thread; also thread_destroy
• thread_join(target_thread)
• Suspend the execution of calling thread until target_thread
terminates
Thread Scheduling
• For user-level thread: scheduling occurs entirely in user-space
• The thread scheduler determines when a thread runs
• It uses queues to keep track of what threads are doing
• Just like the OS and processes
• But it is implemented at user-level in a library
• Run queue: Threads currently running (usually one)
• Ready queue: Threads ready to run
• Are there wait queues?
Sample Thread Interface
• thread_create(procedure_t, arg)
• Create a new thread of control
• Start executing procedure_t
• thread_yield()
• Voluntarily give up the processor
• thread_exit()
• Terminate the calling thread; also thread_destroy
• thread_join(target_thread)
• Suspend the execution of calling thread until target_thread
terminates
Thread Context Switch
• The context switch routine does all of the magic
• Saves context of the currently running thread (old_thread)
• Push all machine state onto its stack
• Restores context of the next thread
• Pop all machine state from the next thread’s stack
• The next thread becomes the current thread
• Return to caller as new thread
• This is all done in assembly language
• See arch/mips/mips/switch.S in OS161 (kernel thread
implementation)
Wait a minute
• Non-preemptive threads have to voluntarily give up
CPU
• Only voluntary calls to thread_yield(), or thread_exit()
causes a context switch
• What if one thread never release the CPU (never calls
thread_yield())?
• We need preemptive user-level thread scheduling
Preemptive Scheduling
• Preemptive scheduling causes an involuntary context switch
• Need to regain control of processor asynchronously
• How?
• Use timer interrupt
• Timer interrupt handler forces current thread to “call”
thread_yield
• How
Process vs. threads
• Multithreading is only an option for “cooperative tasks”
• Trust and sharing
• Process
• Strong isolation but poor performance
• Thread
• Good performance but share too much
• Example: web browsers
• Safari: multithreading
• one webpage can crash entire Safari
• Google Chrome: each tab has its own process
Threads Summary
• The operating system as a large multithreaded program
• Each process executes as a thread within the OS
• Multithreading is also very useful for applications
• Efficient multithreading requires fast primitives
• Processes are too heavyweight
• Solution is to separate threads from processes
• Kernel-level threads much better, but still significant overhead
• User-level threads even better, but not well integrated with OS
• Now, how do we get our threads to correctly cooperate with
each other?
• Synchronization…
กระบวนการ•จำได้ว่าเป็นกระบวนการที่มีหลายสิ่งหลายอย่างงาน•พื้นที่ที่อยู่(กำหนดรหัสทั้งหมดและหน้าข้อมูล) •ทรัพยากร OS (เช่นไฟล์ที่เปิด) และข้อมูลบัญชี•การดำเนินการของรัฐ(PC, SP, Regs ฯลฯ ) •การสร้าง กระบวนการใหม่เป็นค่าใช้จ่ายเพราะข้อมูลทั้งหมดโครงสร้างที่จะต้องได้รับการจัดสรรและเริ่มต้น• proc struct จำใน Solaris • ... ซึ่งไม่ได้รวมตารางหน้าอาจ TLB ล้าง ฯลฯ•การสื่อสารระหว่างกระบวนการเป็นค่าใช้จ่ายเพราะส่วนใหญ่การสื่อสารผ่านไประบบปฏิบัติการ•ค่าใช้จ่ายของสายระบบและข้อมูลการคัดลอกโปรแกรมขนาน•ในการรันโปรแกรมเหล่านี้เราต้อง•สร้างกระบวนการหลายอย่างที่ดำเนินการในแบบคู่ขนาน•สาเหตุแต่ละแมปไปยังพื้นที่ที่อยู่เดียวกันในการใช้ข้อมูลร่วมกัน•พวกเขาเป็นส่วนหนึ่งของการคำนวณเดียวกัน•มีระบบปฏิบัติการกำหนดเวลากระบวนการเหล่านี้ในแบบคู่ขนาน•สถานการณ์เช่นนี้จะไม่มีประสิทธิภาพมาก•พื้นที่: PCB ตารางหน้า ฯลฯ•เวลา: สร้างโครงสร้างข้อมูลส้อมและคัดลอกพื้นที่ addr ฯลฯ• Solutions: เป็นไปได้ที่จะมีประสิทธิภาพมากขึ้น แต่ สหกรณ์"กระบวนการ"? กระบวนการทบทวน•อะไรคือสิ่งที่คล้ายกันในการให้ความร่วมมือในกระบวนการเหล่านี้หรือไม่•พวกเขาร่วมกันทุกรหัสเดียวกันและข้อมูล(พื้นที่ที่อยู่) •พวกเขาร่วมกันทุกสิทธิประโยชน์เดียวกัน•พวกเขาทั้งหมดร่วมกันทรัพยากรเดียวกัน (แฟ้มซ็อกเก็ต ฯลฯ ) •อะไรไม่ได้ที่พวกเขาร่วมกัน? •แต่ละคนมีการดำเนินการของรัฐของตัวเอง: PC, SP และลงทะเบียน•ความคิดที่สำคัญ: ทำไมเราไม่แยกแนวคิดของกระบวนการจากรัฐการดำเนินการของตนหรือไม่•กระบวนการ: พื้นที่ที่อยู่สิทธิ์ ทรัพยากร ฯลฯ •การดำเนินการของรัฐ: PC, SP, ลงทะเบียนรัฐ• Exec เรียกว่าด้ายควบคุมหรือกระทู้กระทู้•โมเดิร์นระบบปฏิบัติการ(Mac, Windows, Unix ที่ทันสมัย) แยกแนวคิดของกระบวนการและหัวข้อ•ด้ายกำหนดลำดับกระแสการดำเนินการภายในกระบวนการ (PC, SP ทะเบียน) •ขั้นตอนการกำหนดพื้นที่ที่อยู่และกระบวนการทั่วไปแอตทริบิวต์ (ทุกอย่าง แต่หัวข้อของการดำเนินการ) •ด้ายถูกผูกไว้กับขั้นตอนเดียว•กระบวนการแต่สามารถมีหลายหัวข้อ•กระทู้กลายเป็นหน่วยของการตั้งเวลา•กระบวนการอยู่ในขณะนี้ภาชนะที่กระทู้ดำเนินการเปรียบเทียบ: •กระบวนการ: •เช่า 4 วิศวกรซอฟต์แวร์ในการทำงานกับ 4 โครงการ•กระทู้: •มีวิศวกรคนหนึ่งในการทำงานกับ 4 โครงการรูปแบบด้าย•ข้อมูลที่ใช้ร่วมกัน•โปรเซสเซอร์ข้อมูล: การปกครองที่, เวลา, ฯลฯ•หน่วยความจำ: ส่วนโต๊ะหน้าและสถิติ ฯลฯ• I / O และไฟล์: พอร์ตสื่อสารไดเรกทอรีและอธิบายไฟล์ ฯลฯ•รัฐภาคเอกชน•ลงทะเบียน•โปรแกรมเคาน์เตอร์•การดำเนินการสแต็ค•รัฐ( พร้อมทำงานและถูกปิดกั้น) •ทำไม? •แต่ละเธรดรันแยกกระทู้เคอร์เนลระดับ•เรามีการดำเนินการด้านการดำเนินการของกระบวนการและแยกมันออกไปในหัวข้อ•เพื่อให้เห็นพ้องด้วยราคาถูก•เช่นระบบปฏิบัติการตอนนี้จัดการกระทู้และกระบวนการ•การดำเนินงานทั้งหมดด้ายจะดำเนินการใน kernel •ตาราง OS ทุกหัวข้อในระบบ•หัวข้อOS ที่มีการจัดการที่เรียกว่าหัวข้อเคอร์เนลระดับหรือกระบวนการที่มีน้ำหนักเบา•หน้าต่าง: หัวข้อ• Solaris วิธีการที่มีน้ำหนักเบา (LWP) • POSIX กระทู้ ( pthreads): PTHREAD_SCOPE_SYSTEM เคอร์เนลระดับกระทู้ข้อ จำกัด •หัวข้อเคอร์เนลระดับให้พร้อมกันมากราคาถูกกว่ากระบวนการ•รัฐน้อยมากที่จะจัดสรรและเริ่มต้น•อย่างไรก็ตามสำหรับการเห็นพ้องละเอียดหัวข้อเคอร์เนลระดับยังคงทุกข์ทรมานจากค่าใช้จ่ายมากเกินไป•การดำเนินงานของกระทู้ยังคงต้องใช้สายระบบ•เป็นการดีที่ต้องการให้การดำเนินงานของด้ายที่จะเป็นเร็วที่สุดเท่าที่เรียกขั้นตอน•สำหรับการเห็นพ้องละเอียดดังกล่าวต้องแม้แต่"ถูกกว่า" หัวข้อระดับผู้ใช้กระทู้•เพื่อให้หัวข้อที่ถูกและเร็วพวกเขาจะต้องมีการดำเนินการที่ระดับผู้ใช้•หัวข้อเคอร์เนลระดับมีการจัดการโดยระบบปฏิบัติการ•หัวข้อระดับผู้ใช้มีการจัดการอย่างสิ้นเชิงโดยระบบเวลาทำงาน(ห้องสมุดระดับผู้ใช้) •หัวข้อระดับผู้ใช้ที่มีขนาดเล็กและรวดเร็ว•ด้ายเป็นตัวแทนเพียงโดยคอมพิวเตอร์ทะเบียนสแต็คและบล็อกควบคุมด้ายขนาดเล็ก (TCB) •การสร้างหัวข้อใหม่สลับไปมาระหว่างหัวข้อและตรงกันกระทู้จะทำผ่านการเรียกขั้นตอน•ไม่มีส่วนร่วมเคอร์เนลการดำเนินงานด้าย•ผู้ใช้ระดับ100x เร็วกว่าหัวข้อเคอร์เนล• pthreads: PTHREAD_SCOPE_PROCESS ระดับผู้ใช้ข้อ จำกัด กระทู้• แต่หัวข้อระดับผู้ใช้ไม่ได้เป็นโซลูชั่นที่สมบูรณ์แบบ•เช่นเดียวกับทุกสิ่งทุกอย่างที่พวกเขาจะถ่วงดุลอำนาจ•หัวข้อระดับผู้ใช้จะมองไม่เห็นระบบปฏิบัติการ•พวกเขาจะไม่บูรณาการได้ดีกับระบบปฏิบัติการ•เป็นผลให้ระบบปฏิบัติการสามารถทำให้การตัดสินใจที่ยากจน•ตั้งเวลากระบวนการที่มีหัวข้อไม่ได้ใช้งาน•การปิดกั้นกระบวนการที่มีด้ายเริ่มI / O แม้ว่ากระบวนการมีหัวข้ออื่นๆ ที่สามารถดำเนินการ•การแก้นี้ต้องใช้การสื่อสารระหว่างเคอร์เนลและด้ายระดับผู้ใช้ผู้จัดการKernel- กับหัวข้อที่ผู้ใช้ระดับ•หัวข้อเคอร์เนลระดับ•บูรณาการกับระบบปฏิบัติการ(การตั้งเวลาแจ้ง) •ช้าในการสร้าง, จัดการ, ประสาน•หัวข้อที่ผู้ใช้ระดับ•รวดเร็วในการสร้าง, จัดการ, ประสาน•บูรณาการไม่ได้อยู่กับระบบปฏิบัติการ(ไม่รู้ การจัดตารางเวลา) •การทำความเข้าใจความแตกต่างระหว่าง kernel- และหัวข้อระดับผู้ใช้เป็นสิ่งสำคัญ•สำหรับการเขียนโปรแกรม(ถูกต้องประสิทธิภาพ) •สำหรับการทดสอบการKernel- และผู้ใช้ระดับกระทู้•หรือใช้ทั้งสองหัวข้อkernel- และระดับผู้ใช้•สามารถเชื่อมโยงหัวข้อที่ใช้ในระดับเดียวกับด้ายเคอร์เนลระดับ•หรือหัวข้อหลายระดับผู้ใช้ที่ด้านบนของเมล็ดระดับหัวข้อ•โปรแกรมJava Virtual Machine (JVM) •หัวข้อ Java เป็นหัวข้อระดับผู้ใช้•บนระบบปฏิบัติการยูนิกซ์เก่าเพียงหนึ่ง"เคอร์เนล กระทู้ "ต่อกระบวนการ•มัลติเธรดJava ทั้งหมดในหัวข้อนี้เคอร์เนลหนึ่ง•ในWindows NT, Unix ทันสมัย•สามารถmultiplex หัวข้อ Java บนหัวข้อเคอร์เนลหลาย•สามารถมีหัวข้อJava มากกว่าหัวข้อเคอร์เนลการใช้ด้าย•การใช้กระทู้มีจำนวนของปัญหา• อินเตอร์เฟซ•สวิทช์บริบท• Preemptive เทียบกับที่ไม่ใช่มาตรการ•สิ่งใดที่พวกเขาหมายถึงอะไร•การจัดตารางเวลา•การประสาน(บรรยายถัดไป) •มุ่งเน้นหัวข้อระดับผู้ใช้•หัวข้อเคอร์เนลระดับที่มีความคล้ายคลึงกับกระบวนการเดิมการจัดการและการดำเนินงานในระบบปฏิบัติการกระทู้ตัวอย่างอินเตอร์เฟซ• thread_create (procedure_t, หาเรื่อง) •สร้างหัวข้อใหม่ในการควบคุม•เริ่มต้นการดำเนิน procedure_t • thread_yield () •สมัครใจให้ขึ้นโปรเซสเซอร์• thread_exit () •ยุติด้ายเรียก; ยัง thread_destroy • thread_join (target_thread) •ระงับการดำเนินการของการเรียกด้ายจนกว่า target_thread ยุติกระทู้ตั้งเวลา•สำหรับด้ายระดับผู้ใช้: การตั้งเวลาที่เกิดขึ้นทั้งหมดในผู้ใช้พื้นที่•กำหนดการด้ายกำหนดเมื่อด้ายทำงาน•จะใช้คิวที่จะติดตามสิ่งที่หัวข้อกำลังทำ•เช่นเดียวกับระบบปฏิบัติการและกระบวนการ• แต่จะดำเนินการในระดับผู้ใช้ในห้องสมุด•คิวเรียกใช้: กระทู้ทำงานอยู่ในปัจจุบัน (ปกติ) •คิวพร้อม: กระทู้พร้อมที่จะทำงาน•มีรอคิว? กระทู้ตัวอย่าง อินเตอร์เฟซ• thread_create (procedure_t, หาเรื่อง) •สร้างหัวข้อใหม่ในการค%
การแปล กรุณารอสักครู่..
