Smartphone Operating System (779883), страница 16
Текст из файла (страница 16)
Scheduling requires working with many aspects of theoperating system many times over. Chapter 5 deals with the intricacies ofscheduling in more detail.Implementation conceptsFigure 4.1 shows that processes must be created to enter the system.When it is created, a process is given a unique identifier, a process ID.The process ID, usually an integer, makes this process accessible in thetable of processes running on the system.A process must be created by the operating system – operating inkernel mode – or by another process. This relationship between processesis often characterized as a parent–child relationship. Parents create childprocesses, which go on to create other child processes, and so on. Theentire collection forms a kind of family tree.This hierarchical relationship is exploited in a number of ways.
Forexample, it is typical that if a process receives an interrupt, then itschildren also receive that interrupt. It is also typical that a parent processcannot terminate until its children have terminated (while this behaviorcan be changed by system calls, it is the default behavior).AN OVERVIEW OF THE PROCESS MODEL67A zombie process is ready to terminate but for some reason cannotinform its parent of its termination. This could happen if the parent processwas aborted for some reason. The child process tries to inform the parentof its termination and waits for the parent to respond.
Since no response isforthcoming, the child process stays forever in the waiting state. It cannotterminate, but it cannot move to the ready state to be executed. Suchprocesses are not unusual on large systems with much activity.ThreadsAs an executing program, a process has a rather large ‘footprint’ on acomputer system. It commands a system’s resources and acts like it is theonly program being run on a computer. Its PCB might take up quite a bitof process-table space and it might take a lot of time to move the processinto and out of the running state. One of the components of a processis the thread of control – the set of instructions being executed on behalfof the program. This thread is orchestrated by the program counter andrepresents an executing program.Consider the situation where a process has multiple threads of control.The other parts of the process remain the same: one PCB governingthe process’s information, one memory space, one set of accountinginformation, and so forth.
Now, within a single structure, a process couldrun multiple tasks at one time with these multiple threads of execution.These tasks share the resources represented in a process’s structureand would have to do so carefully. Such multithreaded processes havepotential for executing faster than processes with a single thread.Not all components of a process are shared. Threads do have somecomponents they keep private (see Figure 4.3). For multiple threads tohave their own execution space, each thread must command its ownprogram counter and its own set of registers. Each thread becomes anentity that can be scheduled and has a set of interrupts that it responds to.Multithreading communicationsLet’s say that you are using the Internet and begin to browse someweb pages. Your browser program executes on your behalf by starting aprocess and moving that process through process states until it begins toshare the CPU with the other processes on the system.
You type a URLinto the ‘Address’ field in the browser and click the ‘Go’ button. You nowexpect your browser to go and fetch a web page from the Internet anddisplay it.68PROCESSES AND THREADSFile SpaceInput/OutputFile SpaceRegistersProgram StackFigure 4.3RegistersRegistersRegistersRegistersProgramStackProgramStackProgramStackProgramStackThread #4Input/OutputThread #3Data SegmentThread #2Code SegmentThread #1Data SegmentSingle ThreadCode SegmentSinglethreaded and multithreaded processesConsider what would happen at this point if your browser used a singlethread of control.
The browser interface would freeze while the processfetched and displayed the web page you requested. In fact, because webpages often result in many files to be fetched (for example, image and textfiles), the page would render quite slowly, because only one file wouldbe fetched, then rendered, at a time.Most web browser implementations are multithreaded.
You candemonstrate this for yourself. Try viewing a complex website – onewith many graphics – and use the menu system on your browser. Trydownloading a web page then download it again when the first time isonly halfway through. Even simple things show multithreading: roll yourmouse over a link in a web page while it is downloading and you willusually see the cursor change. All of these activities would not be possiblewithout a multithreaded implementation.Consider the web server that answers the requests that web browsersmake for web pages. If a web server used only a single thread, only oneweb page request would be serviced at a time. Requestors would haveto wait a long time while the components of a single web page weredelivered.After considering these illustrations, you might think that multiplethreads are the best way to program in applications.
This is usually true.AN OVERVIEW OF THE PROCESS MODEL69However, there are times when a single thread is more beneficial. This isusually the case when a resource, such as a device or special sections ofmemory, is shared but must be accessed carefully – usually by one threadat a time.
To do otherwise would corrupt the resource or the data derivedfrom it. We discuss concurrency in operating systems more in Chapter 6.Benefits of multithreadingProcesses benefit from multithreading in a number of ways:• Sharing of resources : threads share the memory space, the operatingsystem resources and even the code of the process to which theybelong.• Saving resources : because threads share resources amongst themselves and with the process that spawned them, expensive andtime-consuming allocation of new resources is not required for eachthread; threads often require the same kind of attention that processesdo but they require fewer resources which makes operations such ascreating and scheduling a thread much faster.• Interacting with users : multithreaded applications can interact withusers while doing other things; if there are operations that requirewaiting or computing for a long period of time, multiple threads allowan application to attend to these long operations while still makingthe user feel as if she is in control.
Because multiple operations canbe going on at the same time, applications feel quicker and moreresponsive.• Accessing multiple processors : if a computing system has multipleprocessors, multithreaded application can take advantage of this;multiple threads, like multiple processes, can each be scheduled torun on a separate processor. True parallelism can be achieved whenseparate threads of a single application run on separate processors.User and kernel threadsWhen an application is multithreaded, it is composed of user threads.These threads run on behalf of an application, which is itself runningat the user level.
Threads that run on behalf of users at the user levelconsume user resources and interact with the system at the user level.The kernel is not needed until system calls are made.70PROCESSES AND THREADS(a)Figure 4.4 Kernel thread models(c)kernel threaduser threadkernel threaduser threadkernel threaduser threadkernel threaduser thread(b)user threadkernel threadkernel threaduser threaduser threadkernel threaduser threaduser threaduser threaduser threaduser threadkernel threadIf an operating system supports user-level multithreading, the threadingof the kernel affects how multithreaded applications execute. As ananalogy, consider a multilane highway that merges to a single lane as itgoes through a small town.
When the highway is busy, merging all thoselanes of traffic onto the single road causes huge backups as cars in eachlane wait their turn to enter the single road.In a similar manner, some kernels that support multithreading at theuser level support only a single thread at the kernel level. This is calleda many-to-one model of kernel threading (see Figure 4.4a). Many userthreads compete for a single thread of kernel control. As with the highwayexample, the result is a backlog of kernel requests and much waiting onbehalf of user threads.In a many-to-one system, applications still get some user-level benefitfrom multithreading but when they interact with the kernel there is noAN OVERVIEW OF THE PROCESS MODEL71additional benefit. In fact, many systems that use singlethreaded kernels(for example, Microsoft Windows NT) restrict the number of user threadsthat can be used at any one time.