SlideShare una empresa de Scribd logo
1 de 47
Processes and Threads
   in Windows Vista
Agenda
•   Fundamental Concepts
•   IPC
•   Synchronization
•   Implementation
    – Process Creation
    – Scheduling
Fundamental Concepts
• In window Vista processes are containers for
  programs.
• Each process includes:
  – Virtual address space
  – Handles to kernel-mode objects
  – Threads and resources to threads execution
• Each process have user-mode system data called
  the process environment block (PEB), includes:
  – List of loaded modules
  – The current working directory
  – Pointer to process’ heaps
Fundamental Concepts
• Threads are kernel’s abstraction for
  scheduling the CPU
• Threads can also be affinitized to only run on
  certain processors
• Each thread has two separate call stacks, one
  for execution in user-mode and one for
  kernel-mode
Fundamental Concepts
• There is also a threads environment block
  (TEB) that keeps user-mode data, includes:
  – Thread local storages
  – Fields
• Another data structure that kernel-mode
  shared is user shared data which is contains
  various form of time, version info, amount of
  physical memory, number of shared flags.
Fundamental Concepts
                    Process
• Process are created from section objects, each
  of which describes a memory object backed
  by a file on disk.
• Create process:
  – Modify a new process by mapping section
  – Allocating virtual memory
  – Writing parameters and environmental data
  – Duplicating file descriptors
  – Creating threads.
Fundamental Concepts
                     Jobs and Fibers
• Definition: Jobs is a group of processes.
• The main functions of a job is to constraints to the
  threads they contain such at:
   – Limiting resources
   – Prevents threads from accessing system objects by
     enforcing restricted token
• Once a process in a jobs, all process threads in
  those process create will also be in the job.
• Problems: one process can be in one job, there
  will be conflicts if many jobs attempt to manage
  the same process.
Fundamental Concepts
                         Fibers
• Definition: A fiber is a unit of execution that must
  be manually scheduled by the application
• Fibers are created by allocating a stack and a
  user-mode fiber data structure for storing
  registers and data can also be created
  independently of threads
• Fibers will not run until another running fiber in
  thread make explicitly call SwithToFiber.
• Advantage:
   – It easier and take fewer time to switch between fiber
     than threads
Fundamental Concepts
                  Jobs and Fibers

• Disadvantage: need a lot of synchronization
  to make sure fibers do not interface with each
  other.
• Solution: create only as many threads as
  there are processors to run them, and
  affinitize the threads to each run only on a
  distinct set of available processors.
Fundamental Concepts
      Jobs and Fibers
Fundamental Concepts
                     Threads
• Every process start out with one threads and can
  create threads dynamically.
• OS always selected threads to run not a process
• Every threads have state whereas processes do
  not have scheduling states.
• Each thread has an ID, which is taken from the
  same space as process IDs
• System use a handle table, which keep pointer
  field to lookup of a process and thread by ID.
Fundamental Concepts
                       Threads
• There are two types of threads: normal thread
  and system thread
• Normal thread:
  – A thread normally run in user-mode but when make
    a system call it switch to kernel-mode .
  – Each thread has two stacks: one for user-mode and
    one for kernel-mode.
• System thread:
  – Run only in kernel-mode
  – Not associated with any user process
Fundamental Concepts
                        Threads
• The thread CONTEXT includes the thread's set of
  registers, the kernel stack, a thread environment
  block, and a user stack in the address space of
  the thread's process.
• Running threads
   – Using access token of their containing process
   – Client/server
• Threads are also normal focal point for I/O.
• Any thread is able to access all the objects that
  belong to its process.
Fundamental Concepts
InterProcess Communication IPC
            Overview
 1 -How one process passes information to
       another in Windows Vista?

 2 – Six (6) main ways to pass information
       between process and process.
InterProcess Communication IPC
                         Windows Vista
   Windows Vista provides mechanisms for facilitating(thuận
    tiện) communications and data sharing between applications.
   Typically, applications can use IPC for categorized (phân
    loại) as client or server
    – Client: a application or process that requests a service from some other
      applications or processes.
    – Server: a application or process that responds client request
   As developers, we must choose suitable IPC methods to use
    for our applications.
InterProcess Communication IPC
                            Windows Vista
                                PIPES
 Two types (Pipes and Named pipes)
 Pipes have two modes:
  – Byte-mode: work the same ways as in UNIX – Allows a child process to inherit a
    communication channels from its parents: data written to one end of the pipe can be
    read at other.
  – To exchange data in both directions (duplex operation), you must create two pipes.
  – Message-mode: is somewhat similar but preserve message boundaries. To recognize the
    separated messages. If we send 4 writes of 128-byte, it will read as 4 message separated
    with 128-byte each, but not one message with 512-bytes.
 Named pipes: also have to modes as regular pipes. But named pipes can be used over a
  network.
  – Use for transfer data between unrelated process on different computers.
  – Exchange data by performing read and write operations on the pipe.
InterProcess Communication IPC
                            Windows Vista
                            MAILSLOTS
   Function of OS/2 operating system but implemented in
    Windows for capability.
   Similar to pipes, but not all.
   Is one-way communication.
   Two-way communication is possible.
    – A process can be both a mailslot server and a mailslot client
    Can broadcast a message to many receivers instead of one
    (if they have same mailslots name).
   Use over a network but not guarantee the delivery of
    message.
   Mailslots and named pipes are implemented as file system.
    – Allow them to access over the network using existed remote file system protocol.
InterProcess Communication IPC
                      Windows Vista
                        SOCKETS
   Connect processes on different machines.
   Generally used in networking context.
   2 ways of communication channels.
   Also can be used to connect processes on same machine,
    however it more complicated than pipes.
   READ/WRITE on socket is transferring data between 2
    processes.
   Windows Sockets are based on the sockets first popularized
    by Berkeley Software Distribution (BSD)..
InterProcess Communication IPC
                     Windows Vista
                  remote procedure calls
 RPC  enables applications to call functions remotely.
 Operates between processes on a single computer or
  on different computers on a network.
 Supports data conversion for different hardware
  architectures and for byte-ordering between
  dissimilar environments.
 In Windows, transport could be many ways:
  – TCP/IP sockets.
  – Named pipes.
  – Advanced Local Procedure Call (ALDC) – message-passing
    facility in the kernel mode.
InterProcess Communication IPC
                     Windows Vista
                     SHARE FILE
 Process   can share object
  –Includes section objects, which can be mapped into
   the virtual address space of different processes at
   the same time.
  –All writes done by one process then appear in the
   address spaces of the other processes.
SYNCHRONIZATION
Critical section
•   The simplest type of thread synchronization object
•   Can be used only by the threads of a single process (differ from Mutex)
•   Provide a slightly faster, more efficient mechanism for mutual-exclusion
    synchronization
•   There is no way to tell whether a critical section has been abandoned
•   Critical sections are not kernel-mode objects
•                     One writes to the list, and the other reads from it. To prevent
    the two threads from accessing the list at exactly the same time, you can
    protect the list with a critical section. The following example uses a globally
    declared CCriticalSection object to demonstrate how:



• // Global data
• CCriticalSection g_cs;

• .
• .
• //Thread A                                         //Thread B
• g_cs.Lock ();                                      g_cs.Lock();
• //Write to the linked list.                              Read from
  the llinked list
• g_cs.Unlock();                                     g_cs.Unlock();
• Figure 17-3. Protecting a shared resource with
  a critical section
mutex
• Mutexes are kernel mode
• To gain exclusive access to a resource shared by two or more
  threads
• Can be used to synchronize threads running in the same
  process or in different processes
• Suppose two applications use a block of shared memory to
  exchange data. Inside that shared memory is a linked list that
  must be protected against concurrent thread accesses. A
  critical section won't work because it can't reach across
  process boundaries, but a Mutex will do the job nicely. Here's
  what you do in each process before reading or writing the
  linked li
• // Global data
• CMutex g_mutex (FALSE, _T ("MyMutex"));
• g_mutex.Lock ();
• // Read or write the linked list.
• g_mutex.Unlock ();
•   The first parameter passed to the CMutex constructor specifies whether the mutex
    is initially locked (TRUE) or unlocked (FALSE).
•    The second parameter specifies the mutex's name, which is required if the mutex
    is used to synchronize threads in two different processes.
•   You pick the name, but both processes must specify the same name so that the
    two CMutex objects will reference the same mutex object in the Windows kernel.
•   Naturally, Lock blocks on a mutex locked by another thread, and Unlock frees the
    mutex so that others can lock it
• Note that:
       There is one other difference between mutexes
and critical sections:
        If a thread locks a critical section and terminates
without unlocking it, other threads waiting for the
critical section to come free will block indefinitely.
However, if a thread that locks a mutex fails to unlock it
before terminating, the system deems the mutex to be
"abandoned" and automatically frees the mutex so that
waiting threads can resume.
EVENTS
• Events are a way of signaling one thread
  from another, allowing one thread to wait or
  sleep until it’s signaled by another thread.
•     The producing thread, Thread A
  generates some data and puts it in a shared
  working space. In this example, the
  consuming thread, Thread B is sleeping on
  the event (waiting for the event to trigger):
Shared working space
           Thread A                                 Thread B
                      data      data      data



                                  Event




10/18/12                                                       32
•      Once the producing thread has finished
    writing data, it triggers the event.


                  Shared working space
     Thread A                             Thread B
                  data      data   data



                         Event
• This signals the consuming thread, Thread B,
  thereby waking it up

                  Shared working space
     Thread A                             Thread B
                  data      data   data



                         Event
• Once the consuming thread, Thread B has
  woken up, it starts doing work. The
  assumption is that the producing thread will
  no longer touch the data.
               Shared working space
  Thread A                                   Thread B
                 data       data      data



                    Event
•                    Here's an example involving one thread (thread A)
    that fills a buffer with data and another thread (thread B) that does
    something with that data. Assume that thread B must wait for a
    signal from thread A saying that the buffer is initialized and ready
    to go. An autoreset event is the perfect tool for the job:
•   // Global data
•   CEvent g_event; // Autoreset, initially nonsignaled
•   .
•   .
•   // Thread A
•   InitBuffer (&buffer); // Initialize the buffer.
•   g_event.SetEvent (); // Release thread B.
•   .
•   .
•   // Thread B
•   g_event.Lock (); // Wait for the signal.
SEMAPHORE
•   A semaphore is a mutex that multiple threads can access
•   Events, critical sections, and mutexes are "all or nothing" objects in the sense that
    Lock blocks on them if any other thread has them locked. Semaphores are
    different.
•   Maintain resource counts representing the number of resources available
•   Locking a semaphore decrements its resource count, and unlocking a semaphore
    increments the resource count
•   Can be used to synchronize threads within a process or threads that belong to
    different processes.
•               To limit the time that Lock will wait for the
    semaphore's resource count to become nonzero, you
    can pass a maximum wait time (in milliseconds, as
    always) to the Lock function.




• Figure 17-6. Using a semaphore to guard a shared
  resource.
Dynamic-link Library: thư viện cấp phát động mà các ứng
dụng dùng chung.
API: giao diện lập trình ứng dụng, giúp cho sự giao tiếp giữa
các tiến trình
Subsystem: cung cấp môi trường hoạt động cho các ứng dụng
Section object: phần đại diện cho vùng bộ nhớ được chia sẻ
Process object: dùng để kiểm tra và điều khiển process
Thread object: dùng để kiểm soát thread
Process Environment Block (PEB): chứa dữ liệu hoạt động
trong suốt quá trình hoạt động của một process
Thread Environment Block (TEB): chưa dữ liệu của các threads
đang hoạt động
A new process is created when another process make
a Win32 CreateProcess call.
There are 7 step in creating a new process:

1.Converse from Win32 pathname to NT pathname.
2.Open EXE and create Section object.
3.Create Process object.
4.Create Thread object.
5.Checking
6.Shim the program application if necessary.
Scheduling
                        Content
1. Overview
2. Scheduling Priorities
3. Scheduling Algorithm
4. Priority Boosting
5. Priority Inversion
Scheduling
                               Overview
• Windows schedules threads, not processes.

• The Scheduler is called when:

   –   The currently running thread blocks on a semaphore, mutex, event, I/O, …
   –   The thread signals an object (e.g., does an up on a semaphore)
   –   The quantum expires
   –   An I/O operation completes
   –   A timed wait expires
Scheduling
                                  Overview
•    Scheduling is preemptive, priority-based, and round-robin at the highest priority.
•    Scheduler tries to keep a thread on its ideal processor in order to improve
     performance




      (a) Symmetric Multiprocessing (SMP)
      (b) Non-Uniform Memory Access (NUMA)


    • Processes/Threads can specify affinity mask to run only on certain processors:
      SetProcessAffinityMask(), SetThreadAffinityMask(), …
Scheduling
                            Scheduling Priorities
•   Threads are scheduled to run based on their scheduling priority.
    Each thread is assigned a scheduling priority.
•   The priority levels range from zero (lowest priority) to 31 (highest priority),
    correspondingly associated with 32 queues.
     – Priorities are divided into 6 priority classes that are applied to processes.
     – In each class, there are 7 priority levels that are applied to threads in that process.
•   Base priority (of a thread) = F(priority class, priority level) = constant.
•   Dynamic priority = Base priority + Boost Amount, is used to determine which
    thread to execute.
Scheduling
                    Scheduling Algorithm
• The system treats all threads with the same priority as equal.

• The system assigns time slices in a round-robin fashion to all
  threads with the highest priority.

   – If none of these threads are ready to run, the system assigns time slices in a
     round-robin fashion to all threads with the next highest priority.

   – If a higher-priority thread becomes available to run, the system ceases to
     execute the lower-priority thread (without allowing it to finish using its time
     slice), and assigns a full time slice to the higher-priority thread.
Scheduling
    Scheduling Algorithm




Figure 11-28. Thread priorities in Windows Vista
Scheduling
                             Priority Boosting
•   Only applied to non-realtime threads
    that belong to:
     – GUI foreground
       (GUI input, mouse message …)
     – Waking for event
       (disk operation completion +1, keyboard
       input +6, sound +8...)


•   After raising a thread's dynamic
    priority, the scheduler reduces that
    priority by one level each time the
    thread completes a time slice, until
    the thread drops back to its base
    priority.
Scheduling
                                 Priority Inversion
•   Priority inversion occurs when a
    mutex or critical section held by a                 1
    lower-priority thread delays the
    running of a higher-priority thread
    when both are contending for the
    same resource.                                      2
     – Thread 1 has high priority.
     – Thread 2 has medium priority.
     – Thread 3 has low priority.
                                                        3
•   Win32 Solution: randomly boosting the priority of the ready threads (in this
    case, the low priority lock-holders).
     – The low priority threads run long enough to exit the critical section, and the high-priority
       thread can enter the critical section.
     – If the low-priority thread does not get enough CPU time to exit the critical section the first
       time, it will get another chance during the next round of scheduling.
Summary
•   Fundamental Concepts
•   IPC
•   Synchronization
•   Implementation
    • Process Creation
    • Scheduling              Q&A

Más contenido relacionado

La actualidad más candente

Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process SchedulingShipra Swati
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 
cpu scheduling
cpu schedulingcpu scheduling
cpu schedulinghashim102
 
Query processing in Distributed Database System
Query processing in Distributed Database SystemQuery processing in Distributed Database System
Query processing in Distributed Database SystemMeghaj Mallick
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rulesHarini Balamurugan
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory managementrprajat007
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationMohd Tousif
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OSMsAnita2
 
Open mp library functions and environment variables
Open mp library functions and environment variablesOpen mp library functions and environment variables
Open mp library functions and environment variablesSuveeksha
 
Multithreading
Multithreading Multithreading
Multithreading WafaQKhan
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzGiulianoRanauro
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptErenJeager20
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)Prakhar Maurya
 

La actualidad más candente (20)

6.distributed shared memory
6.distributed shared memory6.distributed shared memory
6.distributed shared memory
 
Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process Scheduling
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
cpu scheduling
cpu schedulingcpu scheduling
cpu scheduling
 
Query processing in Distributed Database System
Query processing in Distributed Database SystemQuery processing in Distributed Database System
Query processing in Distributed Database System
 
Bankers
BankersBankers
Bankers
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rules
 
Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
Open mp library functions and environment variables
Open mp library functions and environment variablesOpen mp library functions and environment variables
Open mp library functions and environment variables
 
Lecture 2 process
Lecture 2   processLecture 2   process
Lecture 2 process
 
Multithreading
Multithreading Multithreading
Multithreading
 
Operating system
Operating systemOperating system
Operating system
 
Operating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatzOperating systems chapter 5 silberschatz
Operating systems chapter 5 silberschatz
 
Chapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.pptChapter 2 Operating System Structures.ppt
Chapter 2 Operating System Structures.ppt
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 

Destacado

CPU scheduling algorithms in OS
CPU scheduling algorithms in OSCPU scheduling algorithms in OS
CPU scheduling algorithms in OSharini0810
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Conceptssgpraju
 
Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithmBinal Parekh
 
Thread scheduling in Operating Systems
Thread scheduling in Operating SystemsThread scheduling in Operating Systems
Thread scheduling in Operating SystemsNitish Gulati
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingguesta40f80
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Presentation on Android operating system
Presentation on Android operating systemPresentation on Android operating system
Presentation on Android operating systemSalma Begum
 

Destacado (11)

CPU scheduling algorithms in OS
CPU scheduling algorithms in OSCPU scheduling algorithms in OS
CPU scheduling algorithms in OS
 
OS Process and Thread Concepts
OS Process and Thread ConceptsOS Process and Thread Concepts
OS Process and Thread Concepts
 
Os..
Os..Os..
Os..
 
SSL Layer
SSL LayerSSL Layer
SSL Layer
 
Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithm
 
Thread scheduling in Operating Systems
Thread scheduling in Operating SystemsThread scheduling in Operating Systems
Thread scheduling in Operating Systems
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
The Windows Scheduler
The Windows SchedulerThe Windows Scheduler
The Windows Scheduler
 
Presentation on Android operating system
Presentation on Android operating systemPresentation on Android operating system
Presentation on Android operating system
 

Similar a Processes and Threads in Windows Vista

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdf
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdfInter-Process-Communication (or IPC for short) are mechanisms provid.pdf
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdfaesalem06
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxAmanuelmergia
 
EMBEDDED OS
EMBEDDED OSEMBEDDED OS
EMBEDDED OSAJAL A J
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdfAmanuelmergia
 
06 - OSI Model.ppt
06 - OSI Model.ppt06 - OSI Model.ppt
06 - OSI Model.pptssuserf7cd2b
 
06 - OSI Model.ppt
06 - OSI Model.ppt06 - OSI Model.ppt
06 - OSI Model.pptssuserf7cd2b
 
Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Akhil Nadh PC
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2mona_hakmy
 

Similar a Processes and Threads in Windows Vista (20)

OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdf
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdfInter-Process-Communication (or IPC for short) are mechanisms provid.pdf
Inter-Process-Communication (or IPC for short) are mechanisms provid.pdf
 
4 threads
4 threads4 threads
4 threads
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
EMBEDDED OS
EMBEDDED OSEMBEDDED OS
EMBEDDED OS
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdf
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
 
Os
OsOs
Os
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Thread
ThreadThread
Thread
 
Pthread
PthreadPthread
Pthread
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
06 - OSI Model.ppt
06 - OSI Model.ppt06 - OSI Model.ppt
06 - OSI Model.ppt
 
06 - OSI Model.ppt
06 - OSI Model.ppt06 - OSI Model.ppt
06 - OSI Model.ppt
 
Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2
 

Último

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Último (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Processes and Threads in Windows Vista

  • 1. Processes and Threads in Windows Vista
  • 2. Agenda • Fundamental Concepts • IPC • Synchronization • Implementation – Process Creation – Scheduling
  • 3. Fundamental Concepts • In window Vista processes are containers for programs. • Each process includes: – Virtual address space – Handles to kernel-mode objects – Threads and resources to threads execution • Each process have user-mode system data called the process environment block (PEB), includes: – List of loaded modules – The current working directory – Pointer to process’ heaps
  • 4. Fundamental Concepts • Threads are kernel’s abstraction for scheduling the CPU • Threads can also be affinitized to only run on certain processors • Each thread has two separate call stacks, one for execution in user-mode and one for kernel-mode
  • 5. Fundamental Concepts • There is also a threads environment block (TEB) that keeps user-mode data, includes: – Thread local storages – Fields • Another data structure that kernel-mode shared is user shared data which is contains various form of time, version info, amount of physical memory, number of shared flags.
  • 6. Fundamental Concepts Process • Process are created from section objects, each of which describes a memory object backed by a file on disk. • Create process: – Modify a new process by mapping section – Allocating virtual memory – Writing parameters and environmental data – Duplicating file descriptors – Creating threads.
  • 7. Fundamental Concepts Jobs and Fibers • Definition: Jobs is a group of processes. • The main functions of a job is to constraints to the threads they contain such at: – Limiting resources – Prevents threads from accessing system objects by enforcing restricted token • Once a process in a jobs, all process threads in those process create will also be in the job. • Problems: one process can be in one job, there will be conflicts if many jobs attempt to manage the same process.
  • 8. Fundamental Concepts Fibers • Definition: A fiber is a unit of execution that must be manually scheduled by the application • Fibers are created by allocating a stack and a user-mode fiber data structure for storing registers and data can also be created independently of threads • Fibers will not run until another running fiber in thread make explicitly call SwithToFiber. • Advantage: – It easier and take fewer time to switch between fiber than threads
  • 9. Fundamental Concepts Jobs and Fibers • Disadvantage: need a lot of synchronization to make sure fibers do not interface with each other. • Solution: create only as many threads as there are processors to run them, and affinitize the threads to each run only on a distinct set of available processors.
  • 10. Fundamental Concepts Jobs and Fibers
  • 11. Fundamental Concepts Threads • Every process start out with one threads and can create threads dynamically. • OS always selected threads to run not a process • Every threads have state whereas processes do not have scheduling states. • Each thread has an ID, which is taken from the same space as process IDs • System use a handle table, which keep pointer field to lookup of a process and thread by ID.
  • 12. Fundamental Concepts Threads • There are two types of threads: normal thread and system thread • Normal thread: – A thread normally run in user-mode but when make a system call it switch to kernel-mode . – Each thread has two stacks: one for user-mode and one for kernel-mode. • System thread: – Run only in kernel-mode – Not associated with any user process
  • 13. Fundamental Concepts Threads • The thread CONTEXT includes the thread's set of registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. • Running threads – Using access token of their containing process – Client/server • Threads are also normal focal point for I/O. • Any thread is able to access all the objects that belong to its process.
  • 15. InterProcess Communication IPC Overview 1 -How one process passes information to another in Windows Vista? 2 – Six (6) main ways to pass information between process and process.
  • 16. InterProcess Communication IPC Windows Vista  Windows Vista provides mechanisms for facilitating(thuận tiện) communications and data sharing between applications.  Typically, applications can use IPC for categorized (phân loại) as client or server – Client: a application or process that requests a service from some other applications or processes. – Server: a application or process that responds client request  As developers, we must choose suitable IPC methods to use for our applications.
  • 17. InterProcess Communication IPC Windows Vista PIPES  Two types (Pipes and Named pipes)  Pipes have two modes: – Byte-mode: work the same ways as in UNIX – Allows a child process to inherit a communication channels from its parents: data written to one end of the pipe can be read at other. – To exchange data in both directions (duplex operation), you must create two pipes. – Message-mode: is somewhat similar but preserve message boundaries. To recognize the separated messages. If we send 4 writes of 128-byte, it will read as 4 message separated with 128-byte each, but not one message with 512-bytes.  Named pipes: also have to modes as regular pipes. But named pipes can be used over a network. – Use for transfer data between unrelated process on different computers. – Exchange data by performing read and write operations on the pipe.
  • 18. InterProcess Communication IPC Windows Vista MAILSLOTS  Function of OS/2 operating system but implemented in Windows for capability.  Similar to pipes, but not all.  Is one-way communication.  Two-way communication is possible. – A process can be both a mailslot server and a mailslot client  Can broadcast a message to many receivers instead of one (if they have same mailslots name).  Use over a network but not guarantee the delivery of message.  Mailslots and named pipes are implemented as file system. – Allow them to access over the network using existed remote file system protocol.
  • 19. InterProcess Communication IPC Windows Vista SOCKETS  Connect processes on different machines.  Generally used in networking context.  2 ways of communication channels.  Also can be used to connect processes on same machine, however it more complicated than pipes.  READ/WRITE on socket is transferring data between 2 processes.  Windows Sockets are based on the sockets first popularized by Berkeley Software Distribution (BSD)..
  • 20. InterProcess Communication IPC Windows Vista remote procedure calls  RPC enables applications to call functions remotely.  Operates between processes on a single computer or on different computers on a network.  Supports data conversion for different hardware architectures and for byte-ordering between dissimilar environments.  In Windows, transport could be many ways: – TCP/IP sockets. – Named pipes. – Advanced Local Procedure Call (ALDC) – message-passing facility in the kernel mode.
  • 21. InterProcess Communication IPC Windows Vista SHARE FILE  Process can share object –Includes section objects, which can be mapped into the virtual address space of different processes at the same time. –All writes done by one process then appear in the address spaces of the other processes.
  • 23. Critical section • The simplest type of thread synchronization object • Can be used only by the threads of a single process (differ from Mutex) • Provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization • There is no way to tell whether a critical section has been abandoned • Critical sections are not kernel-mode objects
  • 24. One writes to the list, and the other reads from it. To prevent the two threads from accessing the list at exactly the same time, you can protect the list with a critical section. The following example uses a globally declared CCriticalSection object to demonstrate how: • // Global data • CCriticalSection g_cs; • . • . • //Thread A //Thread B • g_cs.Lock (); g_cs.Lock(); • //Write to the linked list. Read from the llinked list • g_cs.Unlock(); g_cs.Unlock();
  • 25. • Figure 17-3. Protecting a shared resource with a critical section
  • 26. mutex • Mutexes are kernel mode • To gain exclusive access to a resource shared by two or more threads • Can be used to synchronize threads running in the same process or in different processes • Suppose two applications use a block of shared memory to exchange data. Inside that shared memory is a linked list that must be protected against concurrent thread accesses. A critical section won't work because it can't reach across process boundaries, but a Mutex will do the job nicely. Here's what you do in each process before reading or writing the linked li • // Global data • CMutex g_mutex (FALSE, _T ("MyMutex")); • g_mutex.Lock (); • // Read or write the linked list. • g_mutex.Unlock ();
  • 27. The first parameter passed to the CMutex constructor specifies whether the mutex is initially locked (TRUE) or unlocked (FALSE). • The second parameter specifies the mutex's name, which is required if the mutex is used to synchronize threads in two different processes. • You pick the name, but both processes must specify the same name so that the two CMutex objects will reference the same mutex object in the Windows kernel. • Naturally, Lock blocks on a mutex locked by another thread, and Unlock frees the mutex so that others can lock it
  • 28. • Note that: There is one other difference between mutexes and critical sections: If a thread locks a critical section and terminates without unlocking it, other threads waiting for the critical section to come free will block indefinitely. However, if a thread that locks a mutex fails to unlock it before terminating, the system deems the mutex to be "abandoned" and automatically frees the mutex so that waiting threads can resume.
  • 29. EVENTS • Events are a way of signaling one thread from another, allowing one thread to wait or sleep until it’s signaled by another thread. • The producing thread, Thread A generates some data and puts it in a shared working space. In this example, the consuming thread, Thread B is sleeping on the event (waiting for the event to trigger):
  • 30. Shared working space Thread A Thread B data data data Event 10/18/12 32
  • 31. Once the producing thread has finished writing data, it triggers the event. Shared working space Thread A Thread B data data data Event
  • 32. • This signals the consuming thread, Thread B, thereby waking it up Shared working space Thread A Thread B data data data Event
  • 33. • Once the consuming thread, Thread B has woken up, it starts doing work. The assumption is that the producing thread will no longer touch the data. Shared working space Thread A Thread B data data data Event
  • 34. Here's an example involving one thread (thread A) that fills a buffer with data and another thread (thread B) that does something with that data. Assume that thread B must wait for a signal from thread A saying that the buffer is initialized and ready to go. An autoreset event is the perfect tool for the job: • // Global data • CEvent g_event; // Autoreset, initially nonsignaled • . • . • // Thread A • InitBuffer (&buffer); // Initialize the buffer. • g_event.SetEvent (); // Release thread B. • . • . • // Thread B • g_event.Lock (); // Wait for the signal.
  • 35. SEMAPHORE • A semaphore is a mutex that multiple threads can access • Events, critical sections, and mutexes are "all or nothing" objects in the sense that Lock blocks on them if any other thread has them locked. Semaphores are different. • Maintain resource counts representing the number of resources available • Locking a semaphore decrements its resource count, and unlocking a semaphore increments the resource count • Can be used to synchronize threads within a process or threads that belong to different processes.
  • 36. To limit the time that Lock will wait for the semaphore's resource count to become nonzero, you can pass a maximum wait time (in milliseconds, as always) to the Lock function. • Figure 17-6. Using a semaphore to guard a shared resource.
  • 37. Dynamic-link Library: thư viện cấp phát động mà các ứng dụng dùng chung. API: giao diện lập trình ứng dụng, giúp cho sự giao tiếp giữa các tiến trình Subsystem: cung cấp môi trường hoạt động cho các ứng dụng Section object: phần đại diện cho vùng bộ nhớ được chia sẻ Process object: dùng để kiểm tra và điều khiển process Thread object: dùng để kiểm soát thread Process Environment Block (PEB): chứa dữ liệu hoạt động trong suốt quá trình hoạt động của một process Thread Environment Block (TEB): chưa dữ liệu của các threads đang hoạt động
  • 38. A new process is created when another process make a Win32 CreateProcess call. There are 7 step in creating a new process: 1.Converse from Win32 pathname to NT pathname. 2.Open EXE and create Section object. 3.Create Process object. 4.Create Thread object. 5.Checking 6.Shim the program application if necessary.
  • 39. Scheduling Content 1. Overview 2. Scheduling Priorities 3. Scheduling Algorithm 4. Priority Boosting 5. Priority Inversion
  • 40. Scheduling Overview • Windows schedules threads, not processes. • The Scheduler is called when: – The currently running thread blocks on a semaphore, mutex, event, I/O, … – The thread signals an object (e.g., does an up on a semaphore) – The quantum expires – An I/O operation completes – A timed wait expires
  • 41. Scheduling Overview • Scheduling is preemptive, priority-based, and round-robin at the highest priority. • Scheduler tries to keep a thread on its ideal processor in order to improve performance (a) Symmetric Multiprocessing (SMP) (b) Non-Uniform Memory Access (NUMA) • Processes/Threads can specify affinity mask to run only on certain processors: SetProcessAffinityMask(), SetThreadAffinityMask(), …
  • 42. Scheduling Scheduling Priorities • Threads are scheduled to run based on their scheduling priority. Each thread is assigned a scheduling priority. • The priority levels range from zero (lowest priority) to 31 (highest priority), correspondingly associated with 32 queues. – Priorities are divided into 6 priority classes that are applied to processes. – In each class, there are 7 priority levels that are applied to threads in that process. • Base priority (of a thread) = F(priority class, priority level) = constant. • Dynamic priority = Base priority + Boost Amount, is used to determine which thread to execute.
  • 43. Scheduling Scheduling Algorithm • The system treats all threads with the same priority as equal. • The system assigns time slices in a round-robin fashion to all threads with the highest priority. – If none of these threads are ready to run, the system assigns time slices in a round-robin fashion to all threads with the next highest priority. – If a higher-priority thread becomes available to run, the system ceases to execute the lower-priority thread (without allowing it to finish using its time slice), and assigns a full time slice to the higher-priority thread.
  • 44. Scheduling Scheduling Algorithm Figure 11-28. Thread priorities in Windows Vista
  • 45. Scheduling Priority Boosting • Only applied to non-realtime threads that belong to: – GUI foreground (GUI input, mouse message …) – Waking for event (disk operation completion +1, keyboard input +6, sound +8...) • After raising a thread's dynamic priority, the scheduler reduces that priority by one level each time the thread completes a time slice, until the thread drops back to its base priority.
  • 46. Scheduling Priority Inversion • Priority inversion occurs when a mutex or critical section held by a 1 lower-priority thread delays the running of a higher-priority thread when both are contending for the same resource. 2 – Thread 1 has high priority. – Thread 2 has medium priority. – Thread 3 has low priority. 3 • Win32 Solution: randomly boosting the priority of the ready threads (in this case, the low priority lock-holders). – The low priority threads run long enough to exit the critical section, and the high-priority thread can enter the critical section. – If the low-priority thread does not get enough CPU time to exit the critical section the first time, it will get another chance during the next round of scheduling.
  • 47. Summary • Fundamental Concepts • IPC • Synchronization • Implementation • Process Creation • Scheduling Q&A

Notas del editor

  1. Manipulate thao tác
  2. Manipulate thao tác
  3. Manipulate thao tác
  4. Manipulate thao tác
  5. Manipulate thao tác
  6. Manipulate thao tác
  7. Manipulate thao tác
  8. Manipulate thao tác