chap04

21
Threads • Separate two ideas: – Process: Ownership of memory, files, other resources – Thread: Unit of execution we use to dispatch • Multithreading – Allow multiple threads per process • Thread – Individual execution state – Each thread has a control block, with a state (Running/Blocked/etc.), saved registers, instruction pointer – Separate stack – Shares memory and files with other threads that are in that process – Faster to create a thread than a process

Upload: mbadubai

Post on 08-Nov-2015

215 views

Category:

Documents


0 download

DESCRIPTION

OS SLIDES

TRANSCRIPT

  • ThreadsSeparate two ideas:Process: Ownership of memory, files, other resourcesThread: Unit of execution we use to dispatchMultithreadingAllow multiple threads per processThread Individual execution stateEach thread has a control block, with a state (Running/Blocked/etc.), saved registers, instruction pointerSeparate stackShares memory and files with other threads that are in that processFaster to create a thread than a process

  • Using ThreadsFigure 4.2 Example of multiple threads in a single processSeparate control blocks for the process and each threadCan quickly switch between threadsCan communicate without invoking the kernelFour ExamplesForeground/BackgroundAsynchronous Processing Backing up in backgroundFaster Execution Read one set of data while processing another setOrganization For a word processing program, may allow one thread for each file being edited

  • ThreadsThread operationsSpawn Creating a new threadBlock Waiting for an eventUnblock Event happened, start newFinish This thread is completedGenerally a thread can block without blocking the remaining threads in the processAllow the process to start two operations at once, each thread blocks on the appropriate eventMust handle synchronization between threads (see Chap 5, 6)System calls or local subroutinesThread generally responsible for getting/releasing locks, etc.

  • Managing ThreadsUser-level threadsApplication creates/manages all threads using a librarySystem schedules the process as a unitScheduling, etc. is all in user space (faster)Scheduling can be application specificDoes not require O.S. supportHas problems with blocking system callsCan avoid by polling a non-blocking callCannot support multiprocessingKernel-level threadsKernel handles managing threadsEasier to support multiple processorsKernel itself may be multithreadedNeed user/kernel mode switch to change threadsOther ApproachesEach kernel thread may have multiple user threads

  • Threads and Processes1 to 1Each process has one thread (Unix)Many to 1Each process can have multiple threads (NT, Solaris, OS/2, Mach)Very common1 to ManySeen in some distributed operating systemsThread can move between machines and address spacesMany to ManyCan switch to a different domain (I/O) to perform key operationsHelps create a structured program

  • Thread Example (Win95)Echo program - get data from a socket, echo it back to the senderNote: Some details not directly associated with threads have been omitted

    SOCKET msock, ssock;// master & slave server sockets

    int main(int argc, char *argv[])// main - Concurrent TCP server for ECHO service{char *service = "echo";// service name or port numberstruct sockaddr_in fsin;// the address of a clientint alen;// length of client's addressWSADATA wsadata;if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n");msock = passiveTCP(service, 5);while (1) {alen = sizeof(fsin);ssock = accept(msock, (struct sockaddr *)&fsin, &alen);if (ssock == INVALID_SOCKET)errexit("accept: error number\n", GetLastError());if (_beginthread((void (*)(void *))TCPechod, 8000, (void *)ssock) < 0)errexit("_beginthread: %s\n", strerror(errno));}return 1;// not reached}

    int TCPechod(SOCKET fd)// TCPechod - echo data until end of file{char buf[BUFSIZE];intcc;cc = recv(fd, buf, sizeof buf, 0);while (cc != SOCKET_ERROR && cc > 0) {if (send(fd, buf, cc, 0) == SOCKET_ERROR) { break; }cc = recv(fd, buf, sizeof buf, 0);}if (cc == SOCKET_ERROR)fprintf(stderr, "echo recv error: %d\n", GetLastError());closesocket(fd);return 0;}

  • Parallel ProcessorsSISD Single instruction, single data (standard processor)SIMD Single instruction, multiple data (vector processor, MMX)MISD Multiple instruction, single data (never implemented)MIMD Multiple instruction, multiple dataTightly coupled (shared memory)Master/SlaveKernel, I/O on one processorOthers do user programsSymmetric MultiprocessorProcessors share Kernel, I/OLoosely coupled (distributed mem.)Cluster (chapter 13)

  • SMP OrganizationGenerally each processor has its own cache, share memory and I/ODesign issuesSimultaneous concurrent processes or threadsKernel routines must be reentrant to allow multiple treads to execute themScheduling (Chap 10)Must avoid conflictsMay be able to run threads concurrentlySynchronization (Chap 5)Mutual exclusion, event orderingMemory management (Chap 7, 8)Deal with multiport memoryHave a unified paging schemeReliability and fault toleranceSolutions similar to normal case

  • MicrokernelsPopularized by use in Mach O.S.Monolithic O.S. Built as a single large program, any routine can call any other routineUsed in most early systemsLayered O.S. (fig 4.10a)Based on modular programmingMajor changes still had wide-spread effects on other layersMicrokernel (fig 4.10b)Only essential functions in the kernelFile System, Device drivers, etc., are now external subsystems/processesProcesses interact through messages passed through the kernel

  • Microkernel BenefitsUniform InterfaceSame message for user/system servicesExtensibilityEasy to add new servicesModifications need only change directly affected componentsCould have multiple file servicesFlexibilityCan customize system by omitting servicesPortabilityIsolate nearly all processor-specific code in the kernelChanges tend to be in logical areas

  • Microkernel Benefits 2ReliabilityEasy to rigorously test kernelFewer system calls to masterLess interaction with other components Distributed System SupportJust as easy to send a message to another machine as this machineNeed system-wide unique IdsProcesses dont have to know where a service residesObject-Orientated O.S.Lends discipline to the kernelSome systems (NT) incorporate OO principles into the design

  • PerformanceSending a message generally slower than simple kernel callDepends on size of the microkernelFirst generation systems slowerThen tried to include critical system items into kernel (Mach)Fewer user/system mode switchesLose some microkernel benefitsTrying approach of very small kernelL4 - 12K code, 7 system callsSpeed seems to match Unix

  • Microkernel DesignVary according to the systemPrimitive Memory ManagementKernel handles virtualphysical mapping, rest is a user mode processV.M. module can decide what pages to move to/from diskModule can allocate memoryThree microkernel operationsGrant Grant pages to someone elseGrantor gives up access to pagesMap Map pages in another spaceBoth processes can access pageFlush Reclaim pages granted or mappedInterprocess CommunicationBased on messagesI/O and InterruptsHandle interrupts as messages

  • Win 2000 ThreadsCharacteristics of ProcessesImplemented as objectsMay contain one or more threadsBoth processes and threads have built-in synchronization capabilitiesSee Figure 4.12, page 180

  • Process/Thread ObjectsProcess Object (Fig 4.13a, Table 4.3, page 181)Thread Object (Fig 4.13b, Table 4.4)Each process must contain at least one threadMultithreadingThreads in the same process can execute concurrentlySMP SupportAny thread (including kernel threads) can run on any processorSoft affinity Try to reschedule a thread on the same processorHard affinity Restrict a thread to certain processors

  • Win 2000 ThreadsThread States (Fig 4.14, pg 183)Ready Able to runStandby Scheduled to runRunningWaiting Blocked or suspendedTransition Not blocked, but cant run (paged out of memory)TerminatedSupport for O.S. SubsystemProcess creationBegins with request from applicationGoes to protected subsystemPassed to executive, returns handleWin32, OS/2 use handle to create threadReturn process/thread informationWin2000 - client requests a new threadThread inherits limits, etc. from parent

  • Solaris ThreadsFour thread-related conceptsProcess Normal Unix processUser-level Thread Thread libraryLightweight Process Mapping between ULTs and Kernel ThreadsKernel Thread Fundamental kernel scheduling objectAlso used for system functionsFigure 4.15, page 185

  • Threads 2MotivationULTs allow logical parallelism and speed of user-level threadsLWPs handle situation of blocking system callsULTs can share/not share LWPsProcess StructureProcess has:Signal dispatch tableMemory MapFile DescriptorsList of LWPs for this processEach LWP hasLWP IDSaved RegistersPriorityKernel StackSignal MaskResource usageKernel Thread PtrProcess Ptr

  • Thread ExecutionULTs have four states (Fig 4.17a):Active Has a LWP assignedSleepingStoppedRunnableEvents that could happen:Wait due to synchronizationSuspension Until woken up by another threadPreemption Higher-priority thread able to runYielding Yield to same-priority thread (if one exists)

  • More on ThreadsLWP States (fig 4.17b)RunningRunnableStoppedBlocked As far as ULT library is concerned, that tread is still activeInterruptsConverted into kernel threads, each with its own priority, context, stackControl access to data structures using synchronization primitivesGiven higher priorities than normal threadsInterrupted thread pinned to that processor, will stay on that processorReduce time spent with interrupts off.

  • Linux ThreadsTask structure maintained for each process/threadState (executing, ready, zombie, etc.)Scheduling informationProcess, user, group identifiersInterprocess communication infoLinks to parent, siblings, childrenTimers (time used, interval timer)File system Pointers to open filesVirtual memoryProcessor-specific contextThreads are implemented as processes that share files, virtual memory, signals, etc.Clone system call to create a thread library provides more user-friendly thread support