Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University.

Similar presentations


Presentation on theme: "Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University."— Presentation transcript:

1 Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University

2 SILICON VALLEY UNIVERSITY CONFIDENTIAL2 Threads Overview  Threads Process Characteristics: Thread of Execution User-Level Threads and Kernel-Level Threads Multithreading Models  Many-to-One Model  One-to-One Model  Many-to-Many Model Benefit of Multithreading  Multicore Programming  Symmetric Multiprocessor (SMP) Design Issues Thread Libraries  Linux POSIX Threads  Win32 Threads Threading Issues Thread-Specific Data Process Vs Threads  Operating System Structures Monolithic Operating System Microkernel Operating System Copyright @ 2009 John Wiley & Sons Inc.

3 SILICON VALLEY UNIVERSITY CONFIDENTIAL3 Threads Basic unit of CPU utilitzation:  Process Process Characteristics:  Process image in Virtual Address Space (Program, Data, Stack and Attributes in Process Control Block).  Resource Ownership Characteristic: Controls Resources, such as memory, I/O Channels, I/O Devices, and Files. Referred to as the “process” or “task”.  Scheduling/Execution Characteristic: Follows an execution path (trace) through one or more program. Execution State (Running, Ready, etc.) and dispatch priority. Referred to as the “thread” or “lightweight process”. Copyright @ 2009 John Wiley & Sons Inc. RegistersStack FilesDataCode Thread The thread or scheduling characteristic of a process. The resource ownership characteristic of a process.

4 SILICON VALLEY UNIVERSITY CONFIDENTIAL4 Threads Execution Thread  Single thread of execution per process. Copyright @ 2009 John Wiley & Sons Inc. Int main ( ) … Proc1( ) … return Int Proc1( ) … Proc2( ) … return Int Proc2( ) … … return Execution of a process follows an execution path through one or more programs.

5 SILICON VALLEY UNIVERSITY CONFIDENTIAL5 Threads Execution Thread  Multithreading is the ability of an OS to support multiple, concurrent paths of execution within a single process. Copyright @ 2009 John Wiley & Sons Inc. Int main ( ) … return Int Proc1( ) … return Int Proc2( ) … … return Execution of a thread follows an execution path, but over separate processors. Concurrent execution of different threads of a process on separate processor.

6 SILICON VALLEY UNIVERSITY CONFIDENTIAL6 Threads Basic unit of CPU utilitzation Thread is flow of control within process. Multithreaded Process  Operating System supports multiple, concurrent paths of execution within a single process.  Thread has: Thread ID. Program Counter. Register Set. Stack.  Shares with other threads: Code Section. Data Section. Operating System Resources ( files and signals ). Copyright @ 2009 John Wiley & Sons Inc. RegistersStack FilesDataCode Thread FilesDataCode Registers Stack Thread

7 SILICON VALLEY UNIVERSITY CONFIDENTIAL7 Threads Thread Functionality Thread, similar to process, has execution states for synchronization and dispatching Operations associated with thread state changes:  Spawn: New process spawned, a thread in the new process is created. Has Program Counter, Register Context, Stack Space.  Block: Waiting for an event, thread will block and save its User Registers, Program Counter, and Stack Pointers.  Unblock: Event occurs, thread will be moved to the Ready Queue for dispatching.  Finish: Thread exits, Register Context,Stack Space deallocated. Benefits of multithreaded programming can be broken down into four major categories:  Responsiveness.  Resource Sharing.  Economy.  Scalability. Copyright @ 2009 John Wiley & Sons Inc.

8 SILICON VALLEY UNIVERSITY CONFIDENTIAL8 Threads Multithread Process Benefits  Responsiveness Allow a program to continue, even if it is blocked or executing lengthy operation. Web Server can create separate thread listening for connections. When new connection is made, Web Server will create a new thread to service the new connection. Less time to create new thread in an existing process than to create a brand-new process. Thread creation is 10X faster than process creation. Less time to terminate a thread than a process. Less time to switch between two threads within the same process than to switch between processes. Copyright @ 2009 John Wiley & Sons Inc. ClientServerthread 1) Request 2) Create new thread to service the request. 3) Resume listening for additional client request.

9 SILICON VALLEY UNIVERSITY CONFIDENTIAL9 Threads Multithread Process Benefits  Resource Sharing Threads share the memory and resources of the parent process. Application can have several different threads of activity within the same address space. Word processor can have thread displaying graphics, thread responding to user keystrokes, thread for performing spelling and grammer checking in the background. Threads enchance efficiency in communication between different executing programs.  Threads within same process share memory and files, threads can communicate with each other without involving the kernel (i.e. protection and Message Queues.  Economy Threads share the resources of the parent process. More economical to create and context-switch threads. Process creation and process context-switch slower than thread creation. Copyright @ 2009 John Wiley & Sons Inc.

10 SILICON VALLEY UNIVERSITY CONFIDENTIAL10 Threads Multithread Processing Benefits  Scalability Threads may run in parallel in multiprocessor architecture. Multithreading on a multi-CPU machine increases parallelism: Uses of threads simplifies a program that logically has different functions.  Shared memory: File Server process receives file request and opens a thread to service the request. Data sharing between threads handling requests faster than using kernel message queues.  Foreground/Background: Spreadsheet process could display menus and read user input. Another thread execute commands and updates the spreadsheet.  Asynchronous: Word processor writes its buffer to disk once every minute. A thread can schedule itself with kernel and periodically awaken to backup its buffer.  Speed of execution: Multi-threaded process can read and block for data, while another thread can process the data.  Modularity: Processes that has separate activities or variety of sources and destinations can implement these activities as threads. Copyright @ 2009 John Wiley & Sons Inc.

11 SILICON VALLEY UNIVERSITY CONFIDENTIAL11 Threads Multithread Processing Uniprocessor Copyright @ 2009 John Wiley & Sons Inc. Single-threaded process, the results from two Remote Procedure Calls (RPC) are obtained in sequence. Process has to wait for a response from each server. Multi-threaded process has separate thread for each RPC results in a performance improvement. Once one thread blocks waiting for RPC response, the other thread will execute and block for its RPC response. Both the RPC request and RPC response will execute concurrently.

12 SILICON VALLEY UNIVERSITY CONFIDENTIAL12 Threads Multithread Models User-level Threading  Many user-level threads supported by one kernel thread.  User-level threads managed by Thread Manager in user space.  Advangages: User-level threads managed by user-space thread library. Does not require Kernel mode privileges and does not need to switch to Kernel mode for Thread Management. User Thread Manager scheduler does not affect System Kernel level processes. User Thread environment in the Threads Library, not in Kernel: User Thread Library runnable under different Operating Systems. Copyright @ 2009 John Wiley & Sons Inc. K User-level Threads Kernel-level Threads Threads Library Kernel Space User Space

13 SILICON VALLEY UNIVERSITY CONFIDENTIAL13 Threads Multithread Models User-level Threading  Disadvantages: Since user-level threads managed in user space, user-threads cannot be scheduled by kernel scheduler for multiprocessor systems. When user-level thread makes a blocking system call and kernel- level thread blocks, all user-level threads are blocked.  Systems: Solaris Copyright @ 2009 Pearson Education Inc. Null Fork: Time in Usec to create, schedule, execute, and complete forking a process or thread. Signal Wait: Time in Usec to signal a wait process or thread and then wait on a condition (synchronizing two process or thread).

14 SILICON VALLEY UNIVERSITY CONFIDENTIAL14 Threads Multithread Models User-level Threading Copyright @ 2009 Pearson Education Inc. Process B is executing in its Thread 2. Kernel is UNAWARE of the User-level Threads managed in User Space. Thread 2 makes an I/O Request, Process B is in the Blocked state. NOTE: Thread 2 state is still “Running” state. Process B has completed its quantum, and is placed in the Ready State. NOTE: Thread 2 state is still “Running” state. Thread 2 waiting on Thread 1 semaphore. Thread 2 is Blocked and Thread 1 is in Running State. NOTE: Process B state is “Running” state.

15 SILICON VALLEY UNIVERSITY CONFIDENTIAL15 Threads Multithread Models Kernel-level Threading  One user-level thread supported by one kernel thread.  Kernel-level threads scheduled by Kernel scheduler.  Advangages: Each user-level thread has kernel-level thread and user call of blocking system calls does not affect other user-level threads. Threads scheduled by kernel scheduler to different CPUs in multiprocessor system.  Disadvantages: Creating user-level thread always creates kernel-level thread. Overhead creating kernel-level thread high.  Systems: Linux, Solaris Copyright @ 2009 John Wiley & Sons Inc. K User-level Threads Kernel-level Threads KKK Kernel Space User Space

16 SILICON VALLEY UNIVERSITY CONFIDENTIAL16 Threads Multithread Models Hybrid-level Threading  Multiplex many user-level threads to smaller or equal number of kernel-level threads.  Advantages: With multiple kernel-level threads, user call of blocking system calls does not affect other user-level threads. Threads scheduled by kernel scheduler to different CPUs in multiprocessor system.  Disadvantages: Complex.  Systems: Linux 2.4, Solaris (older) Copyright @ 2009 John Wiley & Sons Inc. K User-level Threads Kernel-level Threads KKK

17 SILICON VALLEY UNIVERSITY CONFIDENTIAL17 Threads Multithread Processing Multicore Programming  On system with multicore, concurrency means that the threads can run in parallel, as the system assigns a separate thread to each core.  Areas present challenges in programming multicore systems: Dividing Activities: Application must find areas that can divided into separate concurrent tasks. Balance: Concurrent tasks must perform equal work of equal value. Data Splitting: Data accessed and manipulated by concurrent tasks must be divided to run on separate cores. Data Dependency: Concurrent tasks must be synchronized to accommodate data dependency. Testing and Debugging: Testing and debugging concurrent tasks are inherently more difficult than testing and debugging single- threaded tasks. Copyright @ 2009 John Wiley & Sons Inc.

18 SILICON VALLEY UNIVERSITY CONFIDENTIAL18 Threads Multithread Processing Multicore Programming Copyright @ 2009 John Wiley & Sons Inc. T1T4T3T2T1T4T3T2T1... Single Core T1T3T1T3T1T3T1T3T1... Core 1 T2T4T2T4T2T4T2T4T2... Core 2 Single Core Concurrency: Execution of the threads interleaved over time. Multiple Core Concurrency: Threads run in parallel, Operating System assigns separate thread for each core.

19 SILICON VALLEY UNIVERSITY CONFIDENTIAL19 Threads Multithread Processing Category of Parallel Processor Systems Dedicated Memory Multiprocessor  Processors has dedicated memory, each processor is in a self- contained server.  Interprocess communication through Networking facility.  Clusters: Group of interconnected computers working as unified computing resource (Distributed Database, or RAID). Shared Memory Multiprocessor  Processors share a Common Memory.  Interprocess communication through Shared Memory.  General Classification: Master/Slave Architecture: OS Kernel runs on particular processor.  Master (OS Kernel) schedules processes or threads.  Slave sends request to Master for service (I/O Request). Symmetric Multiprocessor ( SMP): OS Kernel runs on any processor.  Each processor does self-scheduling.  OS Kernel executes in parallel. Copyright @ 2009 John Wiley & Sons Inc.

20 SILICON VALLEY UNIVERSITY CONFIDENTIAL20 Threads Multithread Processing Symmetric Multiprocessor (SMP) Design Issues:  Manages processor and other computer resources similar to multiprogramming uni-processor system. User applications multiprogramming same between uni-processor or multiple processors.  Simultaneous scheduling concurrent processes or threads: Kernel must be reentrant. Kernel tables and structures must be protected and avoid deadlocks. Multiple threads from same process simultaneously executes on multiple processors.  Sychronization: Mutual exclusion and event ordering.  Memory Management: Paging mechanism on different processors must be coordinated when page or segment are shared.  Reliability and Fault Tolerance: Scheduler must detect failed processor and restructure Kernel tables. Copyright @ 2009 John Wiley & Sons Inc.

21 SILICON VALLEY UNIVERSITY CONFIDENTIAL21 Threads Multithread Processing Symmetric Multiprocessor (SMP) Organization  Multiple Processors: Control Unit, ALU, Registers, Memory Caches. Shared Main Memory and Shared I/O Devices through Shared Bus. Copyright @ 2009 John Wiley & Sons Inc. Processor L1 Cache L2 Cache Processor L1 Cache L2 Cache Processor L1 Cache L2 Cache Main Memory I/O Adapter System Bus I/O Subsystem Kernel can execute on any processor. Each processor is self- scheduling from available processes or threads. Kernel written as multiple processes or threads.

22 SILICON VALLEY UNIVERSITY CONFIDENTIAL22 Threads Thread Libraries Linux POSIX Threads ( Pthreads )  Monolithic Operating System: Layered approach, but all layers executed in kernel space.  User-level Library Library Code/Data in user space ( no system calls ).  Kernel-level Library Library Code/Data in kernel space ( use system calls ). Current Linux supports mapping User-level Threads to Kernel-level Threads. Linux does not recognize distinction between Process and Threads. Processes and Threads share resources such as files and memory, but cannot share user stack. Minimizes context switch when kernel switches from one process/thread to another process/thread.  Kernel checks page directory with the next process/thread.  If the same, then the memory space is shared, the context switch is jump from one location of code to another location of code. Copyright @ 2009 John Wiley & Sons Inc.

23 SILICON VALLEY UNIVERSITY CONFIDENTIAL23 Threads Linux POSIX Threads ( Pthreads ) Copyright @ 2009 John Wiley & Sons Inc. Process halted and can only resume by action from another process (process in debut mode and placed in “Stopped” state. Running process state is either executing or it is ready to execute. Process has terminated, but waiting for parent process to acknowledge. Process is in Blocked state, but process is waiting for hardware condition and will not handle any other signal or event. Process is in Blocked state, but can accept other signal or event. Linux Process/Thread Execution State.

24 SILICON VALLEY UNIVERSITY CONFIDENTIAL24 Threads Thread Libraries Win32 Threads  Kernel-level Library  Windows process design supports variety of OS subsystem environments (OS/2, POSIX, Win32).  Hybrid Microkernel: Kernel space native process structures and services are simple and general purpose, each OS subsystem emulates a particular process structure in User Space.  Windows object-oriented structure. Window Process represented as an object type, that owns resources, such as memory and files. Window Process must contain one thread object type to execute: thread is a dispatchable unit of work that executes sequentially and is interruptible. Copyright @ 2009 John Wiley & Sons Inc. Process Object Type controls resources through Object Table.

25 SILICON VALLEY UNIVERSITY CONFIDENTIAL25 Threads Thread Libraries Win32 Threads  Windows Process represented by an object type. Copyright @ 2009 Pearson Education, Inc. Windows Process represented by an Object Type. Each process defined by a number of attributes and encapsulates a number of actions, or services, that it may perform.

26 SILICON VALLEY UNIVERSITY CONFIDENTIAL26 Threads Thread Libraries Win32 Threads Copyright @ 2009 Pearson Eduction, Inc. Windows is a hybrid microkernel rather than monolithic kernel: a)Subsystems run in user-mode server processes. b)Most system components run in same address space as the kernel.

27 SILICON VALLEY UNIVERSITY CONFIDENTIAL27 Threads Thread Libraries Win32 Threads Copyright @ 2009 Pearson Education, Inc. Ready: May be scheduled for execution. Standby: Thread selected to run next on a particular processor. Thread waits until the processor is made available. Running: Standby thread switched into Running State and begins execution until preempted by higher priority thread, exhausts time slice, blocks, or terminates. Transition: After Waiting State and ready to execute, but resources not available (thread’s stack paged out of memory. Waiting: Thread blocked on event (I/O), waiting for sync (semaphore), suspended. When condition satisfied, moves to Ready State when resources are available. Terminated: Thread terminate itself, by another thread, or parent terminates.

28 SILICON VALLEY UNIVERSITY CONFIDENTIAL28 Threads Thread Libraries Java Threads  Java Thread APIs managed directly in Java programs.  Java Virtual Machine and Interpreter on top of local Operating System. Windows Operating System (i.e. XP):  Win32 Kernel-level Library. UNIX/Linux Operating System, MAC OS X, Solaris 9:  Pthreads User-level Library or Kernel-level Library.  Java Thread API implemented using the Thread Library available on the Host System. Copyright @ 2009 John Wiley & Sons Inc.

29 SILICON VALLEY UNIVERSITY CONFIDENTIAL29 Threads Thread Libraries Windows / Linux Comparision Copyright @ 2009 Pearson Education Inc. WindowsLinux Processes are containers for the user-mode address space, a general handle mechanism for referencing kernel objects, and threads; Threads run in a process and are schedulable entities. Process are both containers and the schedulable entities; processes can share address space and system resources, making processes effectively usable as threads. Processes created by discrete steps and container for a new process created. Processes created as virtual copies with fork() and over-written with exec() for a new process. Process uses table to uniformly reference kernel objects (threads, memory, sync, I/O devices). Kernel objects referenced by Kernel Library APIs and mechanisms (File Descriptors, PIDs for open files and sockets). 16 million kernel objects per process.64 open files/sockets supported per process. Kernel fully multi-threaded with kernel preemption.Kernel processes used with kernel preemption. Many system services implemented using a client/server computing, including the OS personality subsystems (Win32, POSIX, OS/2) and communicate with Remote-Procedure Calls. Most services are implemented in the kernel, with exception of networking functions.

30 SILICON VALLEY UNIVERSITY CONFIDENTIAL30 Threads Copyright @ 2009 John Wiley & Sons Inc. #include pthread.h … Int main (int argc, char *argv[ ]) { … /* get the default attributes */ pthread_attr_init (&attr); /* create the thread */ pthread_create(&tid, &attr, runner, argv[ 1 ]); /* wait for the thread to exit */ pthread_join(tid, NULL); … } … /* The thread will begin control in this function */ void (*runner(void *param)... pthread_exit( 0 ); … Linux Pthreads #include windows.h … DWORD WINAPI Summation(LPVOID Param) … return( 0 ); … Int main (int argc, char *argv[ ]) { … Threadhandle = CreateThread (…Summation … If (Threadhandle != NULL) { /* now wait for the thread to finish */ WaitForSingleObject(ThreadHandle, INFINITE); /* close the thread handle */ CloseHandle(ThreadHandle); … Windows Win32 API

31 SILICON VALLEY UNIVERSITY CONFIDENTIAL31 Threads Thread Libraries Solaris ( UNIX ) Operating System Multithread Architecture concepts:  Process: UNIX process with User’s address space, Stack, PCB.  User-level Threads ( ULT ): Using Threads Library in User space, invisible to Operating System. A ULT is a user-created unit of execution within a process.  Lightweight Processes ( LWP ): Mapping between ULTs and Kernel Threads. Always one Kernel Thread for each LWP. A LWP is scheduled by kernel independently and executes in parallel for concurrency on multiprocessors.  Kernel Threads: Fundamental entities that are scheduled and dispatched to run on one of the system processes. Kernel threads associated with LWP, but kernel can also create Kernel Threads to execute system functions within kernel. Interrupts handled as high priority Kernel Threads.  Synchronizes access to data structures using mutual exclusion.  Interrupt threads has higher priority than other Kernel Threads. Copyright @ 2009 Pearson Education Inc.

32 SILICON VALLEY UNIVERSITY CONFIDENTIAL32 Threads Thread Libraries Solaris ( UNIX ) Operating System Solaris Process Structure: Copyright @ 2009 Pearson Education Inc. User-level thread (ULT) supported by Lightweight Process (LWP). Each LWP is mapped to a Kernel Thread.

33 SILICON VALLEY UNIVERSITY CONFIDENTIAL33 Threads Thread Libraries Solaris ( UNIX ) Operating System Multithread Architecture concepts: Copyright @ 2009 Pearson Education Inc. Solaris maintains similar process structure as UNIX, but the Processor State structure has a linked list of LWP structures. There is at least one LWP for each Solaris process.

34 SILICON VALLEY UNIVERSITY CONFIDENTIAL34 Threads Thread Libraries Solaris ( UNIX ) Operating System Solaris Thread States: Copyright @ 2009 Pearson Education Inc. Pinned: Interrupt pins the interrupted thread to the processor and suspended until interrupt is processed. Run: Thread is runnable (thread is ready to execute). Onproc: Thread is executing on a processor. Sleep: Thread is blocked. Stop: Thread is stopped. Zombie: Thread was terminated and waiting for parent process to retrieve status. Free: Thread resources are released and waiting for removal from OS thread data structure.

35 SILICON VALLEY UNIVERSITY CONFIDENTIAL35 Threads Threading Issues  fork( ) and exec( ) fork( ) duplicates all threads of parent process? exec( ) replaces the calling process, including all threads? Thread Cancellation  Terminating a thread before it has completed and release all resources. Web page being loaded and stop button is pressed.  Asynchronous Cancellation: The “target” thread is immediately terminated. Immediate cancellation might leave unreleased resources.  Deferred Cancellation: Cancellation points allow process to check for cancellation and release all resources (Pthreads). Copyright @ 2009 John Wiley & Sons Inc.

36 SILICON VALLEY UNIVERSITY CONFIDENTIAL36 Threads Threading Issues Signal Handling  Operating System notifies a process a signal has occurred. Synchronous Signals: Process execution creates signal ( i.e. illegal memory reference ). Asynchronous Signals: Signal created by external event ( i.e. ).  Default Signal Handler: Kernel execute.  User-Defined Signal Handler: Called within process ( Overrides Default Signal Handler ).  Delivering signal in multithreaded process. Deliver signal to the thread to which signal applies.  Synchronous Signals. Deliver signal to every thread in the process.  Entry. Deliver signal to certain threads in the process.  Delivered to thread that is not blocking the signal. kill ( pid_t pid, int signal ): Delivering “signal” to “pid”. pthread_kill ( pthread_t tid, int signal ) Copyright @ 2009 John Wiley & Sons Inc.

37 SILICON VALLEY UNIVERSITY CONFIDENTIAL37 Threads Threading Issues Problems with Creating Kernel-level Threading  Creating one-to-one User Thread / Kernel Thread overhead.  No bounds on number of concurrent Kernel Threads. Thread Pools  Process startup, create a pool of Kernel Threads.  When User Thread created, link with Kernel Thread from pool or wait until Kernel Thread becomes available. Servicing User Thread with existing Kernel thread eliminates Kernel thread creation overhead. Pool of Kernel Threads automatically creates bound on concurrent Kernel Threads.  Number of Kernel Threads based on number of CPUs, amount of physical memory, statistical average of expected number of concurrent threads. Copyright @ 2009 John Wiley & Sons Inc.

38 SILICON VALLEY UNIVERSITY CONFIDENTIAL38 Threads Threading Issues Thread-Specific Data  Sharing memory could cause privacy problem.  Each thread declare thread-specfic global data.  Compiler Support __thread char *variable;  Function Support Keys or index to identify thread unique data area. Max 128 keys or unique data areas. pthread_key_create ( ): Prepare a key for use. pthread_setspecific ( ): Use key to set a value to a thread-specific data. pthread_getspecific ( ): Use key to retrieve the current value of thread- specific data. pthread_key_delete ( ): Delete a thread-specific data key previously returned by pthread_key_create ( ). Copyright @ 2009 John Wiley & Sons Inc.

39 SILICON VALLEY UNIVERSITY CONFIDENTIAL39 Threads Threading Issues Thread-Specific Data Copyright @ 2009 John Wiley & Sons Inc. typedef struct { int threadSpecific1; int threadSpecific2; } threadSpecific_data_t; typedef struct { int threadspecific1; int threadspecific2; } threadspecific_data_t; … pthread_key_t threadSpecificKey; … void *theThread(void *param) threadSpecific_data_t *gData; … gData = (threadSpecific_data_t *)parm; rc = pthread_setspecific(threadSpecificKey, gData); … Int main (int argc, char **argv) threadSpecific_data_t *gData; … rc = pthread_create(&thread[ I ], NULL, theThread, gData) … rc = pthread_join( thread[ I ], NULL); … pthread_key_delete( threadSpecificKey); Structure used in thread-specific data. Key used to reference the thread- specific data item. Thread with thread-specific data. Save the thread-specific data in a thread. Thread created in the main process. Delete the key after thread has exited.

40 SILICON VALLEY UNIVERSITY CONFIDENTIAL40 Threads Windows Operating System Threads Win32 API: Uses One-to-One Model  Windows 95, 98, NT, 2000, XP Windows XP  Many-to-Many Model Fiber Library Context of the thread  ETHREAD – Executive Thread Block Thread start address,  KTHREAD – Kernel Thread Block. Scheduling and Information, Kernel Stack.  TEB – Thread Environment Block. Thread Identifier, User Stack, Thread-Local Storage Copyright @ 2009 John Wiley & Sons Inc. Kernel stack KTHREAD Scheduling and synchronization information... ETHREAD Thread start address Pointer to parent process... Thread-local storage TEB User stack... Thread identifier User Space Kernal Space

41 SILICON VALLEY UNIVERSITY CONFIDENTIAL 41 Fall 2015 Threads Linux Operating System Threads  Linux Threading: clone() fork(): process  CLONE_CHILD_CLEARTID: erase child thread ID when child exits.  CLONE_CHILD_SETTID: store child thread ID.  SIGCHLD: Termination signal sent to parent when child dies. pthread_create(): thread  CLONE_FILES: share the same file descriptor table.  CLONE_VM: share the same memory space.  CLONE_FS: share file system information  CLONE_SIGHAND: share same table of signal handlers.  CLONE_THREAD: share the same thread group as the calling process.  CLONE_SYSVSEM: share a single list of System V semaphores.  CLOSE_SETTLS: Set Thread Local Storage descriptor.  CLONE_PARENT_SETTID: store child thread ID.  CLONE_CHILD_CLEARTID: erase child thread ID when child exits.

42 SILICON VALLEY UNIVERSITY CONFIDENTIAL 42 Fall 2015 Threads Linux Process Vs Threads int main() { … pid_t pID = fork(); If (pID == 0) { sIdent = “Child Process: “; … } else if (pID < 0) { /* Failed to fork. */ exit(1); } else { sident = “Parent Process:”; } … } fork: Spawn a new child process which is an identical process to the parent, except that it has a new system process ID. The process is copied in memory from the parent. - Copy-on-write memory pages created. - Duplicate parent’s page tables. - Create unique task_struct for the child. - Return value = 0 indicates child’s process. child PID in the parent’s process. -1 error, no child process created.

43 SILICON VALLEY UNIVERSITY CONFIDENTIAL 43 Fall 2015 Threads Linux Process Vs Threads int func1() { /* Thread function. */ … } int main() { … iret1 = pthread_create( &tid1, NULL, func1, (void *)message1); … pthread_join( tid1, NULL); … return 0; } pthread_create: Creates a new thread with attributes in the call or NULL for default attributes. - tid1 recieves the thread ID. - NULL uses the default thread attributes. - func1 is the pointer to the function to be threaded. - message1 is the pointer to the argument. pthread_join: Waits for termination of another thread. - tid1 is the thread the main process is waiting to terminate. NULL specifies return value not needed.

44 SILICON VALLEY UNIVERSITY CONFIDENTIAL 44 Fall 2015 Threads Summary Thread is a flow of control within a process. Multithreading Benefits:  Responsiveness to the user.  Resource sharing within the process.  Economy.  Scalability, more efficient use of multiple cores. User Level Threads  Thread Manager and Thread Library in User Space.  Many-to-One, One-to-One, and Many-to-Many User Level and Kernel Level Threads Mapping. Thread Library  Linux POSIX Pthreads Library  Windows Win32 Threads Library Fork() and exec() System Calls Thread Cancellation, Signal Handling, Thread-Specific Data.

45 SILICON VALLEY UNIVERSITY CONFIDENTIAL45 Threads Operating System Structure Monolithic Operating System  Modular Programming Techniques.  Layered Operating System: Functions organized hierarchically and interaction only takes place between adjacent layers.  Most or all of the layers execute in Kernel Mode.  Performance: Better than Microkernel: Messaging between “Kernel” services takes longer than single service call on Monolithic OS. Performance more important is OS than Microkernel’s modularity.  Problems: Major changes in one layer affects above and below layers. Tailored version of OS difficult to implement. Security difficult to build because of interactive. Copyright @ 2009 John Wiley & Sons Inc.

46 SILICON VALLEY UNIVERSITY CONFIDENTIAL46 Threads Operating System Structure Microkernels: Essential OS functions are in the Kernel (Kernel Mode), while less essentail services and applications execute in User Mode.  Decouples Kernel and Server development: Servers customized to specific applications or environments.  Simplifies Kernel implementation. Address Space Management ( Map Virtual Page to Physical Frame). Process and Thread Management ( Scheduling ). Inter-process Communication ( IPC ).  Traditional OS Functions moved to User Mode. Device Drivers.Windowing System. Protocol Stacks.Security Services. File Systems.Virtual Memory Manager (Page Replacement Algo).  Flexibility. OS has common core, changes to Device Drivers, Protocol Stacks, File Systems, and other low-level systems developed in User Space. Since these functions or services in User Space allowed server development without the complexity managing of Kernel Space services.  Performance problems forced User Space services back to Kernel. Copyright @ 2009 John Wiley & Sons Inc.

47 SILICON VALLEY UNIVERSITY CONFIDENTIAL47 Threads Operating System Structure Microkernels: Copyright @ 2009 John Wiley & Sons Inc.


Download ppt "Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL1 Operating Systems Dr. Jerry Shiao, Silicon Valley University."

Similar presentations


Ads by Google