1 / 41

Processes and Threads Case studies: Windows and Linux

Processes and Threads Case studies: Windows and Linux. Lecture 6 ~ Fall, 2007 ~. Contents. Windows 2000 process management Linux process management. Windows processes Fundamental concepts. Jobs Each job has one or more processes Processes Each process has one or more threads Threads

skah
Download Presentation

Processes and Threads Case studies: Windows and Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Processes and ThreadsCase studies: Windows and Linux Lecture 6 ~ Fall, 2007 ~

  2. Contents • Windows 2000 process management • Linux process management TUCN. Operating Systems. Lecture 6

  3. Windows processesFundamental concepts • Jobs • Each job has one or more processes • Processes • Each process has one or more threads • Threads • Kernel threads - each thread has one or more fibers • Fiber • User space threads TUCN. Operating Systems. Lecture 6

  4. Windows processesJobs • A collection of one or more processes managed as a single unit • Each job object has quotas and resource limits associated with it • maximum number of processes • CPU time available per process and per job • maximum memory usage per process and per job • security restrictions imposed on processes • Win32 API • CreateJobObject • AssignProcessToJobObject • SetInformationJobObject • QueryInformationJobObject TUCN. Operating Systems. Lecture 6

  5. Windows processesProcesses • Containers for resources • A 4GB address space • the bottom 2GB or 3GB = user space • the rest = OS space • Information associated with a process • a unique process ID • a list of handles • an access token holding security information • Each process has at least one thread • the first thread is created at process creation • Win32 API • CreateProcess, CreateProcessAsUser, CreateProcessWithLogonW • ExitProcess, TerminateProcess, GetExitCodeProcess • GetCurrentProcessId, GetEnvironmentStrings TUCN. Operating Systems. Lecture 6

  6. Windows processesThreads • Describes an independent execution within a process • Threads form the basis of CPU scheduling • Information associated to a thread • a state (ready, running, blocked etc.) • two stacks for user and kernel execution mode • a unique thread ID • an access token • a context used to save its state • a private area for its own local variables • There are some kernel threads • perform administrative tasks • Win32 API • CreateThread, CreateRemoteThread • ExitThread, GetExitCodeThread, TerminateThread • SetThreadPriority, GetThreadPriority, GetCurrentThreadId TUCN. Operating Systems. Lecture 6

  7. Windows processesFibers • Similar with threads, but scheduled entirely in user space • The context switch is not so expensive as with threads – does not need trap to kernel • Called lightweight threads • Each thread can have multiple fibers • The OS is not aware of thread’s fibers • Win32 API • ConvertThreadToFiber, ConvertFiberToThread • CreateFiber, DeleteFiber TUCN. Operating Systems. Lecture 6

  8. Windows processesRelationship – processes, threads and fibers TUCN. Operating Systems. Lecture 6

  9. Windows processesSynchronization mechanisms • Semaphores – are kernel objects • down () = WaitForSingleObject() • up () = ReleaseSemaphore() • Mutexes (locks) – are kernel objects • lock () = WaitForSingleObject() • unlock () = ReleaseMutex() • Critical sections – similar to mutexes, but local to a process • EnterCriticalSection() • ExitCriticalSection() • Events – are kernel objects • manual-reset and auto-reset events • WaitForSingleObject() • SetEvent(), ResetEvents(), PulseEvent() • All of them work on threads, not processes TUCN. Operating Systems. Lecture 6

  10. Windows processesA list of API Calls TUCN. Operating Systems. Lecture 6

  11. Windows processesAPI Calls Examples (1) TUCN. Operating Systems. Lecture 6

  12. Windows processesAPI Calls Examples (2) TUCN. Operating Systems. Lecture 6

  13. Windows processesScheduling • There is no central scheduling thread • Windows 2000 is fully preemptive • thread switches can occur at any time • A thread runs (in kernel mode) the scheduler code when: • it blocks on a semaphore, mutex, I/O event etc. • it signals an object • its quantum expires (usually 20 msec) • The scheduler is also called when • an I/O operation completes • a timed wait expires TUCN. Operating Systems. Lecture 6

  14. Windows processesScheduling algorithm (1) • Set the process (all threads) priority class • SetPriorityClass() • the allowed values: real time, high, above normal, normal, below normal, and idle • Set the relative priority of a thread within its own process • SetThreadPriority() • the allowed values: time critical, highest, above normal, normal, below normal, lowest, and idle • The system has 32 priorities • the 42 possible priority classes are mapped onto the 32 system priories • real time (16-31), user (1-15), zero (0), and idle (-1) • Works with threads not processes and every thread has associated • base priority • current priority ( >= base priority ) TUCN. Operating Systems. Lecture 6

  15. Windows processesScheduling algorithm (2) • The highest priority thread is chosen • Real-time priorities are fixed • User priorities are dynamic • A process get a boost when it is woken up because • an I/O operation completes • the amount of boost: 1 for disk, 2 for serial line, 6 for keyboard ,8 for sound card etc. • A semaphore is signaled (up) • the amount of boost is 2 • When a process consumes its entire quantum, it looses a point from its priority TUCN. Operating Systems. Lecture 6

  16. Windows processesMapping of scheduling priorities TUCN. Operating Systems. Lecture 6

  17. Windows processesScheduling priority classes TUCN. Operating Systems. Lecture 6

  18. Linux processesThe support • Processes • an instance of a program in execution • a collection of data structure fully describes how far the execution has progressed • Lightweight processes • can share some resources (memory, file descriptors, etc.) • Linux support for multithreading • Threads • many independent execution flows in the same process • examples of POSIX-compliant pthread libraries • LinuxThreads • Next GenerationPosix Threading Package (NGPT) TUCN. Operating Systems. Lecture 6

  19. Linux processesProcesses description • Each process has a unique identifier (pid) • returned by getpid() • the maximum PID number allowed on Linux is 32,767 • A process is created by another process • using the fork() system call => parent-child relationship • the child process is a copy of the parent, but with its own identity and resources • group of processes • Processes can communicate or cooperate • pipes, signals, shared memory, message queues, semaphores • Background processes = daemons TUCN. Operating Systems. Lecture 6

  20. Linux processesProcesses information – ps command (1) TUCN. Operating Systems. Lecture 6

  21. Linux processesProcesses information – ps command (2) TUCN. Operating Systems. Lecture 6

  22. Linux processesSystem calls for processes TUCN. Operating Systems. Lecture 6

  23. Linux processesThe way the shell works TUCN. Operating Systems. Lecture 6

  24. Linux processesProcess descriptor (1) • A data structure containing all the information related to a process • Process state • Flags • Scheduling information (priority etc.) • File descriptors • Pointers to the allocated memory areas • Each process, even lightweight processes, has its own process descriptor TUCN. Operating Systems. Lecture 6

  25. Linux processesProcess descriptor (2) TUCN. Operating Systems. Lecture 6

  26. Linux processesProcess state • Running • currently executed on a CPU or waiting to be executed • Interruptible • suspended (sleeping) until some conditions become true • Uninterruptible • similar with the one above, but not responsive to signals • Stopped • process execution has been stopped • Zombie • process execution is terminated, but the parent process has not issued a wait() for the terminated child process TUCN. Operating Systems. Lecture 6

  27. Linux processesProcess Usage Limits • Maximum CPU time • signal SIGXCPU sent when limit exceeded • Maximum file size allowed • signal SIGXFSZ sent when limit exceeded • Maximum heap size • Maximum number of processes a user can own • Maximum number of open files • Maximum size of process address space TUCN. Operating Systems. Lecture 6

  28. Linux processesProcess creation • Traditional way • Resources own by the parent are duplicated, and a copy is granted to the child • Modern kernels • copy-on-write technique • clone() system call => lightweight processes • address space • root and working directory • file descriptors table • signal handlers • process identifier (pid) • vfork() system call • Processes share the same address space • Parent process is blocked until the child finishes TUCN. Operating Systems. Lecture 6

  29. Linux processesSystem calls for threads TUCN. Operating Systems. Lecture 6

  30. Linux processesThreads implementation • Based on lightweight processes • Thread group • A collection of lightweight processes that share the same pid • The fork() semantics • only the currently executed thread is activated in the child process • atfork() function can be used • Signal handling TUCN. Operating Systems. Lecture 6

  31. Linux processesScheduling policy (1) • The set of rules used to determine when and how selecting a new process to run • Linux scheduling is based on • process preemption • time-sharing • ranking processes according to their priority • The value of processes quantum • a good compromise between efficient CPU use and good system responsiveness • choose a quantum duration as long as posible, while keeping good system response time TUCN. Operating Systems. Lecture 6

  32. Linux processesScheduling policy (2) • Priorities • static – real time processes (between 1 - 99) • dynamic – conventional processes • Implicitly favor I/O-bound processes over CPU-bound ones • Scheduling classes • real-time FIFO • real-time Round Robin • conventional time-sharing • Always chooses the highest priority process to be executed TUCN. Operating Systems. Lecture 6

  33. Linux processesScheduling algorithm – kernel 2.4 (1) • Divide CPU time into epochs • an epoch is the time between all runnable processes begin with a new time slice and the time all runnable processes have used up their time slices • every process has a specified quantum whose duration is computed when the epoch begins • Each process has a base quantum (base priority) • the quantum assigned by the scheduler to the process if it has exhausted its quantum in previous epoch • about 210 ms • can be modified with nice() or setpriority() system calls • Dynamic priority of conventional processes • base priority + number of ticks of CPU time left to the process before its quantum expires in the current epoch TUCN. Operating Systems. Lecture 6

  34. Linux processesScheduling algorithm – kernel 2.4 (2) • At process creation • the number of CPU ticks left to the parent are split in two halves between it and its child • Direct invocation • The current process must be blocked • Lazy invocation • quantum expired • a process with a greater priority than the current process is woken up • sched_setscheduler() or sched_yield() system call is issued TUCN. Operating Systems. Lecture 6

  35. Linux processesScheduling algorithm – kernel 2.4 (3) • At the beginning of a new epoch • quantum = quantum/2 + base_priority • give preference to I/O bound processes • never become larger then 2*(base_priority) • How good is a runnable process (goodness) TUCN. Operating Systems. Lecture 6

  36. Linux processesPerformance of sched. alg. – kernel 2.4 • The algorithm does not scale well • O(n) complexity • The predefined quantum is too large for high system loads • I/O-bound processes boosting strategy is not optimal • Support for real-time application is weak TUCN. Operating Systems. Lecture 6

  37. Linux processesScheduling algorithm – kernel 2.6 (1) • O(1) complexity • use a ready queue organized as a stack of priorities queues – called priority array • use two priority arrays • Active tasks –have not yet consumed entirely their time slice in the current epoch • Expired – have consumed their time slice in the current epoch • a bitmap is used to find quickly the highest priority thread • A bit 1 indicates a non empty priority list in the priority array • O(1) complexity • a new time slice for a task is computed when it is inserted in the expired priority array • switch between epochs = switch between the two priority arrays • O(1) complexity TUCN. Operating Systems. Lecture 6

  38. Linux processesScheduling algorithm – kernel 2.6 (2) • Range of priorities – 0 ÷ 140 • Real-time (RT) tasks : 0 ÷ 99 • Normal tasks: 100 ÷ 140 • Non RT tasks • Average time slice – 100 ms • All tasks have a static priority called nice value (-20 ÷ 19) – never changed by scheduler • Dynamic priority – add to or subtract from static priority • Reward I/O tasks • Punish CPU-bound tasks • Maximum priority bonus is 5 • Maximum priority penalty is 5 • Sleep_avg < MAX_SLEEP_AVG • add the total sleep time • subtract the total runtime TUCN. Operating Systems. Lecture 6

  39. Linux processesScheduling algorithm – kernel 2.6 (3) • Dynamic priority bonus(p) = CURRENT_BONUS(p) - MAX_BONUS / 2; prio(p) = static_prio(p) – bonus(p); CURRENT_BONUS(p) = sleep_avg * MAX_BONUS / MAX_SLEEP_AVG; MAX_BONUS = 10; • Time slice • calculated by simply scaling a task’s static priority onto the possible time slice range and making sure a certain minimum and maximum time-slice is enforced time_slice = (MIN_TIMESLICE + ((MAX_TIMESLICE - MIN_TIMESLICE) * (MAX_PRIO - 1 - static_prio) / (MAX_USER_PRIO - 1))) TUCN. Operating Systems. Lecture 6

  40. Linux processesScheduling related system calls TUCN. Operating Systems. Lecture 6

  41. Bibliography [Tann01] Andrew Tannenbaum, “Modern Operating Systems”, second edition, Prentice Hall, 2001, pgs. 690 – 708, pgs. 796 – 809. [BC01] D. Bovet, M. Cesati, “Understanding Linux Kernel”, O’Reilly, 2001, pgs. 65 – 96, pgs. 277 – 298. [JA05] Josh Aas, “Understanding the Linux 2.6 CPU Scheduler”, Silicon Graphics, February 2005. TUCN. Operating Systems. Lecture 6

More Related