03 Threads

19
Operating System thread [email protected]

description

thread sistem operasi

Transcript of 03 Threads

  • Operating System

    thread

    [email protected]

  • Multi (programming-tasking-processing-threading)

    Multiprogrammingreferstomultipleprogramsresidentinmainmemoryand(apparentlybutnotexactly)runningatthesametime

    MultitaskingreferstomultipleprocessesrunningsimultaneouslybysharingtheCPUtime

    MultiprocessingreferstomultipleCPUs

    Multithreadingisanexecutionmodelthatallowsasingleprocesstohavemultiplecodesegments(threads)runconcurrentlywithinthecontextof

    thatprocess

    MultiplethreadsofasingleprocesscansharetheCPUinasingleCPUsystemor(purely)runinparallelinamultiprocessingsystem

    operating system Threads 3

  • THREAD ?Threadisaflowofexecutionthroughtheprocesscode,withitsown

    programcounter,systemregistersandstack

    Threadsprovideawaytoimproveapplicationperformancethroughparallelism.

    Eachthreadbelongstoexactlyoneprocessandnothreadcanexistoutsideaprocess.Eachthreadrepresentsaseparateflowofcontrol.

    operating system Threads 4

    Threadshavebeensuccessfullyusedinimplementingnetwork

    serversandwebserver.

  • Process vs. Thread

    operating system Threads 5

  • Process vs. ThreadThreads are easier to create than processes since they don't require a

    separate address space.

    Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike

    threads, processes don't share the same address space.

    Threads are considered lightweight because they use far less resources than processes.

    Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other. This is really another

    way of stating #2 above.

    A process can consist of multiple threads.

    operating system Threads 6

  • Advantages of Thread

    Threadminimizescontextswitchingtime.

    Useofthreadsprovidesconcurrencywithinaprocess.

    Efficientcommunication.

    Economy Itismoreeconomicaltocreateandcontextswitchthreads.

    Utilizationofmultiprocessorarchitecturestoagreaterscaleandefficiency

    operating system Threads 7

  • User Level ThreadAdvantages

    1. ThreadswitchingdoesnotrequireKernelmodeprivileges.2. Userlevelthreadcanrunonanyoperatingsystem.3. Schedulingcanbeapplicationspecificintheuserlevelthread.4. Userlevelthreadsarefasttocreateandmanage

    operating system Threads 8

    Disadvantages1. Inatypicaloperatingsystem,

    mostsystemcallsareblocking.

    2. Multithreadedapplicationcannottakeadvantageofmultiprocessing.

  • Kernel Thread

    Advantages1. Kernelcansimultaneouslyschedulemultiplethreadsfromthe

    sameprocessonmultipleprocesses.2. Ifonethreadinaprocessisblocked,theKernelcanschedule

    anotherthreadofthesameprocess.3. Kernelroutinesthemselvescanmultithreaded.

    Disadvantages1. Kernelthreadsaregenerallyslowertocreateandmanagethan

    theuserthreads.2. Transferofcontrolfromonethreadtoanotherwithinsame

    processrequiresamodeswitchtotheKernel.

    operating system Threads 9

  • User v.s. Kernel Thread

    operating system Threads 10

  • Multithreading Models

    SomeoperatingsystemprovidesacombineduserlevelthreadandKernellevelthreadfacility.

    Multiplethreadswithinthesameapplicationcanruninparallelonmultipleprocessorsandablockingsystemcallneednot

    blocktheentireprocess.

    Therearethreetypes1. Manytomanyrelationship.2. Manytoonerelationship.3. Onetoonerelationship.

    operating system Threads 11

  • Many to One

    operating system Threads 12

  • One to One

    operating system Threads 13

  • Many to Many

    operating system Threads 14

  • Two Level Model

    operating system Threads 15

  • Java ThreadJavathreadsmaybecreatedby:

    ExtendingThreadclass ImplementingtheRunnable interface

    JavathreadsaremanagedbytheJVM.

  • LETS CODE

  • References

    1. AbrahamSilberschatz, PeterBaerGalvin,GregGagne,OperatingSystemConceptsWithJava,Wiley

    2. Tutorialpoint.com3. https://www.youtube.com/watch?v=E_Zp2d_STAo

    4. http://www.programmerinterview.com/index.php/operatingsystems/threadvsprocess/

    operating system Threads 18

  • Next Week Assignment(no submission just do it )

    http://www.programmerinterview.com/index.php/operating-systems/example-of-threading-and-synchronization-2/

    READ, UNDERSTAND, IMPLEMENT THE CODE, and ANALYZE

    operating system Threads 19

  • if you have spare time

    1. http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html

    2. Performancemonitor ResourcemonitoronWindows3. https://technet.microsoft.com/en

    us/library/bb896645.aspx4. http://www.cyberciti.biz/tips/toplinuxmonitoring

    tools.html5. http://www.nirsoft.net/utils/process_threads_view.html

    Onceagainifyouhavesparetime