Top Banner

of 53

Record6-10

Jun 02, 2018

Download

Documents

sudharsonkumar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 Record6-10

    1/53

    INTERPROCESS COMMUNICATION

    INTRODUCTION

    Inter-Process Communication (IPC) is a set of techniques for the exchange of data among two or

    more threadsin one or more processes. Processes may be running on one or more computers connected by

    a network. IPC techniques are divided into methods for message passing, synchroniation, shared

    memory, and remote proced!re ca""s(PC). !he method of IPC used may vary based on the bandwidth

    and "atency of communication between the threads, and the type of data being communicated. IPC may a"so

    be referred to as inter-thread communicationand inter-application communication.IPC, on pair with the

    address spaceconcept, is the foundation for address space.

    PARENT C#I$D PROCESS

    IPC dea"s main"y with the techniques and mechanisms that faci"itate communication between

    processes. #et us start from something primitive. $e %now that some medium or other is required for

    communication between different processes. &imi"ar"y, when it comes to computer programs, we need some

    mechanism or medium for communication. Primari"y, processes can use the avai"ab"e memory to

    communicate with each other. 'ut then, the memory is comp"ete"y managed by the operating system.

    process wi"" be a""otted some part of the avai"ab"e memory for execution. !hen each process wi"" have its

    own unique user space. In no way wi"" the memory a""otted for one process over"ap with the memory

    a""otted for another process.

    !he operating systems %erne", which has access to a"" the memory avai"ab"e, wi"" act as the

    communication channe". !here are different IPC mechanisms which come into use based on the different

    requirements.

    %asic IPC

    !he IPC mechanisms can be c"assified into the fo""owing categories as given be"ow*

    pipes

    fifos

    shared memory

    mapped memory

    message queues

    soc%ets

    http://en.wikipedia.org/wiki/Thread_(computer_science)http://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Computer_networkhttp://en.wikipedia.org/wiki/Computer_networkhttp://en.wikipedia.org/wiki/Computer_networkhttp://en.wikipedia.org/wiki/Message_passinghttp://en.wikipedia.org/wiki/Message_passinghttp://en.wikipedia.org/wiki/Synchronizationhttp://en.wikipedia.org/wiki/Synchronizationhttp://en.wikipedia.org/wiki/Synchronizationhttp://en.wikipedia.org/wiki/Shared_memoryhttp://en.wikipedia.org/wiki/Shared_memoryhttp://en.wikipedia.org/wiki/Shared_memoryhttp://en.wikipedia.org/wiki/Remote_procedure_callhttp://en.wikipedia.org/wiki/Address_spacehttp://en.wikipedia.org/wiki/Thread_(computer_science)http://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Computer_networkhttp://en.wikipedia.org/wiki/Message_passinghttp://en.wikipedia.org/wiki/Synchronizationhttp://en.wikipedia.org/wiki/Shared_memoryhttp://en.wikipedia.org/wiki/Shared_memoryhttp://en.wikipedia.org/wiki/Remote_procedure_callhttp://en.wikipedia.org/wiki/Address_space
  • 8/10/2019 Record6-10

    2/53

  • 8/10/2019 Record6-10

    3/53

    In #inux, a pipe is imp"emented using two fle data structures which both point at the same

    temporary 1/& inode which itse"f points at a physica" page within memory. each fledata structure contains

    pointers to different fi"e operation routine vectors2 one for writing to the pipe, the other for reading from the

    pipe.

    !his hides the under"ying differences from the generic system ca""s which read and write to ordinary

    fi"es. s the writing process writes to the pipe, bytes are copied into the shared data page and when the

    reading process reads from the pipe, bytes are copied from the shared data page. #inux must synchroni3e

    access to the pipe. It must ma%e sure that the reader and the writer of the pipe are in step and to do this it

    uses "oc%s, wait queues and signa"s.

    IMP$EMENTATION

    !he first thing to do is see how we use pipes to send and receive data. $e create a pipe using the

    os.pipe()function which returns two ,i"e descriptors, one for each end of the pipe. $e can then use the

    os.read/writefunctions to send data a"ong the pipe. *

    import os

    #et us exp"ain a scenario where we can use the pipe system ca""* consider a %eyboard-reader

    program which simp"y exits after any a"pha-numeric character is pressed on the %eyboard. $e wi"" create

    http://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm#fdhttp://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm#fd
  • 8/10/2019 Record6-10

    4/53

    two processes2 one of them wi"" read characters from the %eyboard, and the other wi"" continuous"y chec%

    for a"pha-numeric characters.

    4ne ma0or feature of pipe is that the data f"owing through the communication medium is transient,

    that is, data once read from the read descriptor cannot be read again. "so, if we write data continuous"y into

    the write descriptor, then we wi"" be ab"e to read the data on"y in the order in which the data was written.

    &o, what happens when the pipesystem ca"" is invo%ed5 good "oo% at the manua" entry for pipesuggests that it creates a pair of fi"e descriptors. !his suggests that the %erne" imp"ements pipewithin the

    fi"e system. owever, pipedoes not actua""y exist as such - so when the ca"" is made, the %erne" a""ocates

    free inodes and creates a pair of fi"e descriptors as we"" as the corresponding entries in the fi"e tab"e which

    the %erne" uses. ence, the %erne" enab"es the user to use the norma" fi"e operations "i%e read, write, etc.

    which the user does through the fi"e descriptors. !he %erne" ma%es sure that one of the descriptors is for

    reading and another one it for writing.

    Spawning processes

    !he mechanism used for spawning a chi"d process is to use the os.ork()system ca"". !his returns

    different va"ues depending on whether you are in the origina", parent process or in the new chi"d process. In

    the origina" process the returned va"ue is the process I6 or pidof the chi"d process. If you are in the chi"d

    process then the return va"ue from orkis 3ero. !his means that in the code we wi"" have an if statement that

    tests the ork()return va"ue and if it is 3ero performs the chi"d functions and if non 3ero does the parent

    function. !o %eep things manageab"e it is usua""y best to put those functions into separate modu"es and ca""

    the functions as required.

    ANA$O-. Processes can communicate via pipes

    Parents can spawn a c"one of themse"ves using os.ork

    Chi"d processes need to be terminated or they wi"" run forever consuming va"uab"e computer

    resources. $e can terminate a chi"d process using the os.killfunction.

    Reading /rom and (riting to a Named Pipe

    eading from and writing to a named pipe are very simi"ar to reading and writing from or to a norma"

    fi"e. !he standard C "ibrary function ca""s read( )and write( )can be used for reading from and writing to

    a named pipe. !hese operations are b"oc%ing, by defau"t.

    !he fo""owing points need to be %ept in mind whi"e doing read7writes to a named pipe*

    named pipe cannot be opened for both reading and writing.

    !he process opening it must choose either read mode or write mode.

    !he pipe opened in one mode wi"" remain in that mode unti" it is c"osed.

    ead and write operations to a named pipe are b"oc%ing, by defau"t.

  • 8/10/2019 Record6-10

    5/53

    !herefore if a process reads from a named pipe and if the pipe does not have data in it, the reading

    process wi"" be b"oc%ed. &imi"ar"y if a process tries to write to a named pipe that has no reader, the writing

    process gets b"oc%ed, unti" another process opens the named pipe for reading. !his, of course, can be

    overridden by specifying the 484'#4C9 f"ag whi"e opening the named pipe. &ee% operations (via the

    &tandard C "ibrary function "see%) cannot be performed on named pipes.

    %ene,its o, Named Pipes amed pipes are very simp"e to use.

    mkfois a thread-safe function.

    o synchroni3ation mechanism is needed when using named pipes.

    $rite (using writefunction ca"") to a named pipe is guaranteed to be atomic. It is atomic even if the

    named pipe is opened in non-b"oc%ing mode.

    amed pipes have permissions (read and write) associated with them, un"i%e anonymous pipes

    !hese permissions can be used to enforce secure communication.

    $imitations o, Named Pipes

    amed pipes can on"y be used for communication among processes on the same host machine.

    amed pipes can be created on"y in the "oca" fi"e system of the host, that is, you cannot create a

    named pipe on the /& fi"e system.

    6ue to their basic b"oc%ing nature of pipes, carefu" programming is required for the c"ient and server

    in order to avoid dead"oc%s.

    amed pipe data is a byte stream, and no record identification exists.

    0' MESSA-E 1UEUE

    DE/INITION

    In programming, message queueing is a method by which process(or program instances) can

    exchange or pass data using an interface to a system-managed 2!e!eof messages. :essages can vary in

    "ength and be assigned different types or usages. message queue can be created by one process and used

    by mu"tip"e processes that read and7or write messages to the queue. /or examp"e, a ser3erprocess can read

    and write messages from and to a message queue created for c"ientprocesses. !he message type can be

    used to associate a message with a particu"ar c"ient process even though a"" messages are on the same

    queue.

    !he message queue is managed by the operating system(or kerne"). pp"ication programs

    (or their processes) create message queues and send and receive messages using an app"ication program

    interface (API). In Uni4 systems, the C programming "anguage msgget function is used with various

    parameters specifying the action requested, message queue I6, message type, and so forth.

    http://searchsoa.techtarget.com/definition/0,,sid9_gci212832,00.htmlhttp://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212852,00.htmlhttp://searchsoa.techtarget.com/definition/0,,sid9_gci212964,00.htmlhttp://searchwinit.techtarget.com/sDefinition/0,,sid1_gci211795,00.htmlhttp://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212714,00.htmlhttp://searchenterpriselinux.techtarget.com/sDefinition/0,,sid39_gci212439,00.htmlhttp://searchexchange.techtarget.com/sDefinition/0,,sid43_gci213778,00.htmlhttp://searchenterpriselinux.techtarget.com/sDefinition/0,,sid39_gci213253,00.htmlhttp://searchwinit.techtarget.com/sDefinition/0,,sid1_gci211723,00.htmlhttp://searchsoa.techtarget.com/definition/0,,sid9_gci212832,00.htmlhttp://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212852,00.htmlhttp://searchsoa.techtarget.com/definition/0,,sid9_gci212964,00.htmlhttp://searchwinit.techtarget.com/sDefinition/0,,sid1_gci211795,00.htmlhttp://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212714,00.htmlhttp://searchenterpriselinux.techtarget.com/sDefinition/0,,sid39_gci212439,00.htmlhttp://searchexchange.techtarget.com/sDefinition/0,,sid43_gci213778,00.htmlhttp://searchenterpriselinux.techtarget.com/sDefinition/0,,sid39_gci213253,00.htmlhttp://searchwinit.techtarget.com/sDefinition/0,,sid1_gci211723,00.html
  • 8/10/2019 Record6-10

    6/53

    !he maximum si3e of a message in a queue is "imited by the operating system and is typica""y

    ;, bytes.:essage queues a""ow one or more processes to write messages, which wi"" be read by one or

    more reading processes. #inux maintains a "ist of message queues, the ms!"evector2 each e"ement of

    which points to a ms!id#dsdata structure that fu""y describes the message queue. $hen message queues

    are created a new ms!id#dsdata structure is a""ocated from system memory and inserted into the vector.

    ANA$O-.

    Message 2!e!es? Information to be communicated is p"aced in a predefined message structure

    !he process generating the message specifies its type and p"aces the message in a system-maintained

    message queue. Processes accessing the message queue can use the message type to se"ecti3e"y read

    messages of specific types in a first in first out (/I/4) manner. :essage queues provide the user with a

    means of asynchronous"y mu"tip"exing data from mu"tip"e processes.

    O3er3iew

    :essage queues provide an asynchrono!scomm!nications protoco", meaning that the sender and

    receiver of the message do not need to interact with the message queue at the same time. :essages p"aced

    onto the queue are stored unti" the recipient retrieves them.

    :ost message queues have set "imits on the si3e of data that can be transmitted in a sing"e message. !hose

    that do not have such "imits are %nown as mai"boxes. :any imp"ementations of message queues function

    interna""y* within anoperating systemor within an app"ication. &uch queues exist for the purposes of that

    systemon"y.

    4ther imp"ementations a""ow the passing of messages between different computer systems

    potentia""y connecting mu"tip"e app"ications and mu"tip"e operating systems. !hese message queueing

    http://en.wikipedia.org/wiki/Asynchronous_communicationhttp://en.wikipedia.org/wiki/Communications_protocolhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Application_softwarehttp://en.wikipedia.org/wiki/Systemhttp://en.wikipedia.org/wiki/Asynchronous_communicationhttp://en.wikipedia.org/wiki/Communications_protocolhttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Application_softwarehttp://en.wikipedia.org/wiki/System
  • 8/10/2019 Record6-10

    7/53

    systems typica""y provide enhanced resi"iencefunctiona"ity to ensure that messages do not get @"ost@ in the

    event of a system fai"ure.

    USA-E5

    In a typica" message-queueing imp"ementation, a system administratorinsta""s and configures off-

    the-she"f message-queueing software (a 2!e!e manager), and defines a named message queue. n

    app"ication then registers a software routine that @"istens@ for messages p"aced onto the queue. &econd andsubsequent app"ications may connect to the queue and transfer a message onto it. !he queue-manager

    software stores the messages unti" a receiving app"ication connects and then ca""s the registered software

    routine. !he receiving app"ication then processes the message in an appropriate manner...

    !here are often numerous options as to the exact semantics of message passing, inc"uding*

    $"ra%ilit& (e.g. - whether or not queued data can be mere"y %ept in memory, or if it mustnt be "ost, and

    thus must be stored on dis%, or, more expensive sti"", it must be committed more re"iab"y to a D%MS)

    Security policies

    'essae p"rin poliies- queues or messages may have a !!# (!ime !o #ive)

    &ome systems support fi"tering data so that a subscriber may on"y see messages matching some pre-

    specified criteria of interest

    Delivery policies.

    o"tin poliies- in a system with many queue servers, what servers shou"d receive a message or a

    queues messages5

    Bathin poliies- shou"d messages be de"ivered immediate"y5 4r shou"d the system wait a bit and

    try to de"iver many messages at once5

    !hese are a"" considerations that can have substantia" effects on transaction semantics, system re"iabi"ity, and

    system efficiency.

    IMP$EMENTATION

    :essage queues are one of the three different types of &ystem 1 IPC mechanisms. !his mechanism

    enab"es processes to send information to each other asynchronous"y. !he word asynchronousin the present

    context signifies that the sender process continues with its execution without waiting for the receiver toreceive or ac%now"edge the information. 4n the other side, the receiver does not wait if no messages are

    there in the queue. !he queue being referred to here is the queue imp"emented and maintained by the %erne".

    #et us now ta%e a "oo% at the system ca""s associated with this mechanism.

    msgget5!his, in a way simi"ar to shmget, gets a message queue identifier. !he format is

    S.NTA6 5int msgget(%et8t %ey, int msgf"g)2

    http://en.wikipedia.org/wiki/Resiliencehttp://en.wikipedia.org/wiki/System_administratorhttp://en.wikipedia.org/w/index.php?title=Queue_manager&action=edithttp://en.wikipedia.org/wiki/DBMShttp://en.wikipedia.org/wiki/Resiliencehttp://en.wikipedia.org/wiki/System_administratorhttp://en.wikipedia.org/w/index.php?title=Queue_manager&action=edithttp://en.wikipedia.org/wiki/DBMS
  • 8/10/2019 Record6-10

    8/53

    !he first argument is a unique key, which can be generated by using ,toka"gorithm. !he second argument is

    the f"ag which can be IPC7CREAT, IPC7PRI8ATE, the permissions (read and7or write) are "ogica""y

    4ed with the f"ags. msggetreturns an identifier associated with the key. !his identifier can be used for

    further processing of the message queue associated with the identifier.

    msgct"5!his contro"s the operations on the message queue. !he format is

    S.NTA65int msgct"(int msqid, int cmd, struct msqid8ds Abuf)2ere ms2id is the message queue identifier returned by msgget. !he second argument is cmd, which

    indicates which action is to be ta%en on the message queue. !he third argument is a buffer of type str!ct

    ms2id7ds. Bach message queue has this structure associated with it2 it is composed of records for queues to

    be identified by the %erne". !his structure a"so defines the current status of the message queue. If one of the

    cmds is IPC7SET, some fie"ds in the ms2id7dsstructure (pointed by the third argument) wi"" be set to the

    specified va"ues. &ee the man page for the detai"s.

    msgsnd5!his is for sending messages. !he format is

    S.NTA65int msgsnd(int msqid, struct msgbuf Amsgp, si3e8t msgs3, int msgf"g)2

    !he first argument is the message queue identifier returned by msgget. !he second argument is a

    structure that the ca""ing process a""ocates. ca"" to msgsndappends a copy of the message pointed to by

    msgpto the message queue identified by ms2id. !he third argument is the si3e of the message text within

    the msg9!,structure. !he fourth argument is the f"ag that specify one of severa" actions to be ta%en as and

    when a specific situation arises.

    msgrc35!his is for receiving messages. !he format is,

    S.NTA65

    ssi3e8t msgrcv(int msqid, struct msgbuf Amsgp, si3e8t msgs3, "ong msgtyp, int msgf"g)2

    'esides the four arguments mentioned above for msgsnd, we a"so have msgtyp, which specifies the

    type of message requested. &ee the man page for the options.

    APP$ICATION

    commercia" app"ication of this %ind of message queueing so,tware (a"so %nown as Message Oriented

    Midd"eware) inc"ude I%Ms (e9Sphere M1 (former"y M1 Series), /ioranos :, Orac"e Ad3anced

    1!e!ing() within an Orac"e data9ase,and Microso,ts MSM1. !here is a :a3astandard ca""ed :a3a

    Message Ser3ice, which has, associated with it, a number of imp"ementations, both proprietary and open

    source.

    !here are a number of open source choices of messaging midd"eware systems, inc"uding :%oss Messaging

    :ORAM,Acti3eM1, Ra99itM1(an imp"ementation, in Er"ang,of AM1P), ISectd, Skytoo"s Pg1(runs

    atop PostgreS1$, created by Skype).

    http://en.wikipedia.org/wiki/Softwarehttp://en.wikipedia.org/wiki/Message_Oriented_Middlewarehttp://en.wikipedia.org/wiki/Message_Oriented_Middlewarehttp://en.wikipedia.org/wiki/IBMhttp://en.wikipedia.org/wiki/IBMhttp://en.wikipedia.org/wiki/WebSphere_MQhttp://en.wikipedia.org/wiki/MQ_Serieshttp://en.wikipedia.org/wiki/Fioranohttp://en.wikipedia.org/wiki/Fioranohttp://en.wikipedia.org/wiki/Oracle_Advanced_Queuinghttp://en.wikipedia.org/wiki/Oracle_Advanced_Queuinghttp://en.wikipedia.org/wiki/Oracle_databasehttp://en.wikipedia.org/wiki/Oracle_databasehttp://en.wikipedia.org/wiki/Microsofthttp://en.wikipedia.org/wiki/Microsofthttp://en.wikipedia.org/wiki/Microsoft_Message_Queuinghttp://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_Message_Servicehttp://en.wikipedia.org/wiki/Java_Message_Servicehttp://en.wikipedia.org/wiki/JBoss_Messaginghttp://joram.objectweb.org/http://en.wikipedia.org/wiki/ActiveMQhttp://en.wikipedia.org/wiki/ActiveMQhttp://www.rabbitmq.com/http://www.rabbitmq.com/http://en.wikipedia.org/wiki/Erlang_(programming_language)http://en.wikipedia.org/wiki/Erlang_(programming_language)http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocolhttp://isectd.sourceforge.net/http://pgfoundry.org/projects/skytools/http://en.wikipedia.org/wiki/PostgreSQLhttp://en.wikipedia.org/wiki/Skypehttp://en.wikipedia.org/wiki/Softwarehttp://en.wikipedia.org/wiki/Message_Oriented_Middlewarehttp://en.wikipedia.org/wiki/Message_Oriented_Middlewarehttp://en.wikipedia.org/wiki/IBMhttp://en.wikipedia.org/wiki/WebSphere_MQhttp://en.wikipedia.org/wiki/MQ_Serieshttp://en.wikipedia.org/wiki/Fioranohttp://en.wikipedia.org/wiki/Oracle_Advanced_Queuinghttp://en.wikipedia.org/wiki/Oracle_Advanced_Queuinghttp://en.wikipedia.org/wiki/Oracle_databasehttp://en.wikipedia.org/wiki/Microsofthttp://en.wikipedia.org/wiki/Microsoft_Message_Queuinghttp://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_Message_Servicehttp://en.wikipedia.org/wiki/Java_Message_Servicehttp://en.wikipedia.org/wiki/JBoss_Messaginghttp://joram.objectweb.org/http://en.wikipedia.org/wiki/ActiveMQhttp://www.rabbitmq.com/http://en.wikipedia.org/wiki/Erlang_(programming_language)http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocolhttp://isectd.sourceforge.net/http://pgfoundry.org/projects/skytools/http://en.wikipedia.org/wiki/PostgreSQLhttp://en.wikipedia.org/wiki/Skype
  • 8/10/2019 Record6-10

    9/53

    :ostRTOSes, such as84(orksand1N6operating systems encourage the use of message queueing as

    the primary IPC or inter-thread communication mechanism. !he resu"ting tight integration between

    message passing and CP+ schedu"ing is attributed as a main reason for the usabi"ity of !4&es for rea" time

    app"ications. Bar"y examp"es of commercia" !4&es that encouraged a message-queue basis to inter-thread

    communication a"so inc"ude 8RT6and pSOSD, both of which date to the ear"y

  • 8/10/2019 Record6-10

    10/53

    !o imp"ement the concept of shared memory between the c"ient and the server process.

    #ARD(ARE RE1UIREMENT

    P- II FEE :G speed, >EH' ard dis%, F :' :, J button mouse,

  • 8/10/2019 Record6-10

    11/53

    e"seSc"ose(fdKitE=L"oca"host MNO cc pipe.cK>itE=L"oca"host MNO .7a.out

    Bnter the string to enter into the pipe* India

    !he string retrieved from the pipe is India

    PRO-RAM5 itE=L"oca"host MNO vi shmem.c

    inc"udeQstdio.hRinc"udeQsys7shm.hRinc"udeQsys7ipc.hR

    int main()Sint chi"d,shmid,i2char Ashmptr2chi"dVfor%()2if(Wchi"d)SshmidVshmget(>EF,EZIPC8CB!)2shmptrVshmat(shmid,E,E)2printf(@TnParent waitingTn@)2for(iVE2iQ>E2iDD)

    SshmptrKiNVaDi2putchar(shmptrKiN)2wait(+##)2YYe"seSshmidVshmget(>EF,E)2shmptrVshmat(shmid,E,E)2

  • 8/10/2019 Record6-10

    12/53

    printf(@TnChi"d is readingTn@)2for(iVE2iQ>E2iDD)putchar(shmptrKiN)2shmdt(+##)2shmct"(shmid,IPC8:I6,+##)2Yreturn E2Y

    OUTPUT5

    K>itE=L"oca"host MNO cc shmem.cK>itE=L"oca"host MNO .7a.out

    Parent waitingabcdefghi0%"mnopqrst

    Chi"d is readingabcdefghi0%"mnopqrst

    RESU$T5

    !hus the concept of &hared :emory &egment [ pipes has been imp"emented.

    PRODUCER > CONSUMER PRO%$EM

    !he producer-consumer prob"em i""ustrates the need for synchroni3ation in systems where many

    processes share a resource. In the prob"em, two processes share a fixed-si3e buffer. 4ne process produces

    information and puts it in the buffer, whi"e the other process consumes information from the buffer. !hese

    processes do not ta%e turns accessing the buffer, they both wor% concurrent"y. erein "ies the prob"em. $hat

  • 8/10/2019 Record6-10

    13/53

    happens if the producer tries to put an item into a fu"" buffer5 $hat happens if the consumer tries to ta%e an

    item from an empty buffer5

    In order to synchroni3e these processes, we wi"" b"oc% the producer when the buffer is fu"", and we wi""

    b"oc% the consumer when the buffer is empty. &o the two processes, Producer and Consumer, shou"d wor%

    as fo""ows*

  • 8/10/2019 Record6-10

    14/53

    () !hen, it chec%s to see if the buffer is fu"". If it is, the producer wi"" put itse"f to s"eep unti" the consumer

    wa%es it up. @wa%eup@ wi"" come if the consumer finds the buffer empty.

    (J) ext, the producer puts the new widget in the buffer. If the producer goes to s"eep in step (>), it wi"" not

    wa%e up unti" the buffer is empty, so the buffer wi"" never overf"ow. (F) !hen, the producer chec%s to see if

    the buffer is empty. If it is, the producer assumes that the consumer is s"eeping, an so it wi"" wa%e the

    consumer. 9eep in mind that between any of these steps, an interrupt might occur, a""owing the consumer to

    run.

    'uffer&i3e V J2 count V E2 Producer() S int widget2 $I#B (true) S 77 "oop forever ma%e8new(widget)2 77 create a new widget to put in the buffer I/(countVV'uffer&i3e) &"eep()2 77 if the buffer is fu"", s"eep put8item(widget)2 77 put the item in the buffer count V count D

  • 8/10/2019 Record6-10

    15/53

  • 8/10/2019 Record6-10

    16/53

    E6'NO' ? PRODUCER CONSUMER PRO%$EM USIN- SEMAP#ORE

    AIM5

    !o create a producer consumer prob"em using semaphore.

    #ARD(ARE RE1UIREMENT5

    P-

  • 8/10/2019 Record6-10

    17/53

    S int ch2 smutex V semget((%ey8t)EAJ,E,IPC84$I!) VV -

  • 8/10/2019 Record6-10

    18/53

    mutV signa" (smutex)2 fu"" V signa"(sfu"")2 printf(@TnTt6uring &igna" 4peration@)2 printf(@TnTtmutex va"ue is UdTn@,mut)2 printf(@TnTtfu"" va"ue is UdTn@,fu"")2Yvoid consumer (void)S char itemK>EN2

    fu""Vwait(sfu"")2 mutVwait(smutex)2 printf(@TnTt6uring wait 4peration@)2 printf(@TnTtmutex va"ue is UdTn@,mut)2 printf(@TnTtfu"" va"ue is UdTn@,fu"")2 printf(@TnTt Consumer in critica" section@)2 if (msgrcv(qid,[item,>E,E,IPC84$I!) VV -itE=L"oca"host MNO cc prod.cK>itE=L"oca"host MNO .7a.out

    sumtex id is * =;JE] sempty id is *

  • 8/10/2019 Record6-10

    19/53

    empty va"ue is

    6uring wait 4peration mutex va"ue is =;JE

    fu"" va"ue is

  • 8/10/2019 Record6-10

    20/53

  • 8/10/2019 Record6-10

    21/53

  • 8/10/2019 Record6-10

    22/53

    Steps per,ormed 9y the MMU

    Hiven a 1, index into the P!(Page !ab"e) chec% if present, if so, put out phys

    address on the bus and "et the memory operation be performed what do you do if absent5 you

    have to go to dis% and get it. $e do not want to do this by hardware. &oftware shou"d gain

    contro" at this point (access contro"). ardware raises a page fau"t. !his fau"t is caught by the

    4&, the corresponding page is brought in from the dis% into a physica" page, the P! entry is

    updated. In the process, a victim may need to be thrown out and its entry inva"idated.

    Paging Iss!es

    Page tab"es are #arge*

    :apping :ust be /ast*

    :a%e use of the "oca"ity in typica" executions*

    &o"ution* !ransa"ation #oo%aside 'uffer

    Page sie 3ers!s page ta9"e sie

    system with a sma""er page si3e uses more pages, requiring a page ta9"ethat occupies more space

    /or examp"e, if a >J>virtua" address space is mapped to F9' (>bytes) pages, the number of virtua" pages

    is >>E(>E V J> - ). owever, if the page si3e is increased to J>9' (>

  • 8/10/2019 Record6-10

    23/53

    si3es mean that a !#' cache of the same si3e can %eep trac% of "arger amounts of memory, which avoids the

    cost"y !#' misses.

    Interna" ,ragmentation o, pages

    are"y do processes require the use of an exact number of pages. s a resu"t, the "ast page wi"" "i%e"y

    on"y be partia""y fu"", wasting some amount of memory. #arger page si3es c"ear"y increase the potentia" for

    wasted memory this way, as more potentia""y unused portions of memory are "oaded into main memory.&ma""er page si3es ensure a c"oser match to the actua" amount of memory required in an a""ocation.

    s an examp"e, assume the page si3e is

  • 8/10/2019 Record6-10

    24/53

    order, so the performance advantage of having a comp"ete"y sequentia" page fi"e is minima". owever, it is

    genera""y agreed that a "arge page fi"e wi"" a""ow use of memory-heavy app"ications, and there is no pena"ty

    except that more dis% space is used.

    DE/RA-MENTATION

    De,ragmenting the page fi"e is a"so occasiona""y recommended to improve performance when a

    $indows system is chronica""y using much more memory than its tota" physica" memory. "though this canhe"p s"ight"y, performance concerns are much more effective"y dea"t with by adding more physica" memory.

    Advantages of Paging

    o externa" fragmentation

    _ ny page can be p"aced in any frame in physica" memory

    _ /ast to a""ocate and free

    ` ""oc* o searching for suitab"e free space

    ` /ree* 6oesnt have to coa""esce with ad0acent free space

    ` ust use bitmap to show free7a""ocated page frames

    &imp"e to swap-out portions of memory to dis%

    _ Page si3e matches dis% b"oc% si3e

    _ Can run process when some pages are on dis%

    _ dd present bit to P!B

    Bnab"es sharing of portions of address space

    _ !o share a page, have P!B point to same frame

    Disadvantages of Paging

    Interna" fragmentation* Page si3e may not match si3e

    needed by process

    _ $asted memory grows with "arger pages

    _ !ension5

    dditiona" memory reference to "oo% up in page tab"e --R

    1ery inefficient

    _ Page tab"e must be stored imemory_ ::+ stores on"y base address of page tab"e

    &torage for page tab"es may be substantia"

    _ &imp"e page tab"e* equires P!B for a"" pages in address space

    ` Bntry needed even if page not a""ocated

    _ Prob"ematic with dynamic stac% and heap within address space

    http://en.wikipedia.org/wiki/Defragmenthttp://en.wikipedia.org/wiki/Defragmenthttp://en.wikipedia.org/wiki/Defragment
  • 8/10/2019 Record6-10

    25/53

    E6'NO'@a

    MEMOR. MANA-EMENT I

    SIMU$ATION O/ PA-IN-

    AIM5

    !o write a program to simu"ate the paging.

    #ARD(ARE RE1UIREMENT5

    P-itE=L"oca"host MNO vi paging.c

    inc"udeQstdio.hRinc"udeQmath.hRstruct pagetab"eSint pno,fno2

  • 8/10/2019 Record6-10

    26/53

    YpK

  • 8/10/2019 Record6-10

    27/53

  • 8/10/2019 Record6-10

    28/53

    0'SE-MENTATION

    &egmentation is one of the most common ways to achieve memory protection2 another common

    one is paging.

    In a computer system using segmentation, an instruction operand that refers to a memory "ocation

    inc"udes a va"ue that identifies a segment and an offset within that segment. segment has a set of

    permissions, and a "ength, associated with it. If the current"y running processis a""owed by the permissions

    to ma%e the type of reference to memory that it is attempting to ma%e, and the offset within the segment is

    within the range specified by the "ength of the segment, the reference is permitted2 otherwise, a hardware

    e4ceptionis de"ivered.

    In addition to the set of permissions and "ength, a segment a"so has associated with it information

    indicating where the segment is "ocated in memory. It may a"so have a f"ag indicating whether the segment

    is present in main memory or not2 if the segment is not present in main memory, an exception is de"ivered,

    and the operating systemwi"" read the segment into memory from secondary storage. !he information

    indicating where the segment is "ocated in memory might be the address of the first "ocation in the segment,

    or might be the address of a page ta9"efor the segment.

    http://en.wikipedia.org/wiki/Memory_protectionhttp://en.wikipedia.org/wiki/Paginghttp://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Exception_handlinghttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Page_tablehttp://en.wikipedia.org/wiki/Memory_protectionhttp://en.wikipedia.org/wiki/Paginghttp://en.wikipedia.org/wiki/Process_(computing)http://en.wikipedia.org/wiki/Exception_handlinghttp://en.wikipedia.org/wiki/Operating_systemhttp://en.wikipedia.org/wiki/Page_table
  • 8/10/2019 Record6-10

    29/53

    In the first case, if a reference to a "ocation within a segment is made, the offset within the segment

    wi"" be added to address of the first "ocation in the segment to give the address in memory of the referred-to

    item2 in the second case, the offset of the segment is trans"ated to a memory address using the page tab"e.

    IMP$EMENTATION

    Programmers view of memory is not usua""y as a sing"e "inear address space

    Programmer doesnt %now how "arge these wi"" be, or how they"" grow, and doesnt want to manage

    where they go in virtua" memory.

    SE-MENTATION (IT# PA-IN-

    Simi"arity5

    ddress space can exceed si3e of rea" memory.

    Di,,erences5

    Programmer is aware of segmentation. Paging is hidden.

  • 8/10/2019 Record6-10

    30/53

    &egmentation maintains multiple address spacesper process.

    Paging maintains one address space.

    &egmentation a""ows procedures and data to beseparately protected. !his is hard with paging.

    &egmentation easi"y permits tab"es whose si3e varies.

    &egmentationfacilitates sharing of procedures between processes. !his is hard with paging.

    Pure segmentation suffers from memory fragmentation.

    &egmentation and Paging can be used together.

    Programmer is aware of segments.

    Hains a"" the protection and sharing benefits.

    $ithin each segment, paging is used.

    voids externa" memory fragmentation

    +ses memory efficient"y.

  • 8/10/2019 Record6-10

    31/53

  • 8/10/2019 Record6-10

    32/53

    E6'NO'@9 SIMU$ATION O/ SE-MENTATION

    AIM5

    !o write a program to simu"ate segmentation.

    #ARD(ARE RE1UIREMENT5

    P-

  • 8/10/2019 Record6-10

    33/53

    inc"udeQstdio.hRinc"udeQmath.hRstruct segSint segno,base,"ength2YsegK

  • 8/10/2019 Record6-10

    34/53

    OUTPUT5

    K>itE=L"oca"host MNO cc segment.c

    K>itE=L"oca"host MNO .7a.out

    Bnter the no.of segments*F

    Bnter the segno(non E),base,"imit for each segment*< >JEE J

  • 8/10/2019 Record6-10

    35/53

    /IRST SITB %EST /ITB (ORST /IT

    INTRODUCTION

    !he rea" cha""enge of efficient"y managing memory is seen in the case of a system which has

    mu"tip"e processes running at the same time. &ince primary memory can be space-mu"tip"exed, the memory

    manager can a""ocate a portion of primary memory to each process for its own use. owever, the memory

    manager must %eep trac% of which processes are running in which memory "ocations, and it must a"so

    determine how to a""ocate and dea""ocate avai"ab"e memory when new processes are created and when o"d

    processes comp"ete execution. $hi"e various different strategies are used to a""ocate space to processes

    competing for memory, three of the most popu"ar are 'est fit, $orst fit, and /irst fit.

    %est ,it5!he a""ocator p"aces a process in the sma""est b"oc% of una""ocated memory in which it wi"" fit./or examp"e, suppose a process requests 9' of memory and the memory manager current"y has a "ist of

    una""ocated b"oc%s of 9',

  • 8/10/2019 Record6-10

    36/53

    Primary

    :emory'est fit $orst fit /irst fit

    otice in the diagram above that the 'est fit and /irst fit strategies both "eave a tiny segment of memory

    una""ocated 0ust beyond the new process. &ince the amount of memory is sma"", it is not "i%e"y that any newprocesses can be "oaded here. !his condition of sp"itting primary memory into segments as the memory is

    a""ocated and dea""ocated is %nown asfragmentation. !he $orst fit strategy attempts to reduce the prob"em

    of fragmentation by a""ocating the "argest fragments to new processes. !hus, a "arger amount of space wi""

    be "eft as seen in the diagram above.

    Memory Manager

    @!he memory manager is responsib"e for a""ocating primary memory to processes and for assisting the

    programmer in "oading and storing the contents of the primary memory. :anaging the sharing of primary

    memory and minimi3ing memory access time are the basic goa"s of the memory manager.@

    !he purpose of the memory manager is

    to a""ocate primary memory space to processes

    to mao the process address space into the a""ocated portion of the primary memory

    to minimi3e access times using a cost-effective amount of primary memory

    Memory Management A"gorithms

    In an environment that supports dynamic memory a""ocation, the memory manager must %eep a record of

    the usage of each a""ocatab"e b"oc% of memory. !his record cou"d be %ept by using a"most any data structure

    that imp"ements "in%ed "ists. n obvious imp"ementation is to define a free "ist of b"oc% descriptors, with

    each descriport containing a pointer to the next descriptor, a pointer to the b"oc%, and the "ength of the b"oc%.

    !he memory manager %eeps a free "ist pointer and inserts entries into the "ist in some order conducive to its

    a""ocation strategy. number of strategies are used to a""ocate space to the processes that are competing for

    memory.

    %est /it

  • 8/10/2019 Record6-10

    37/53

    !he a""ocator p"aces a process in the sma""est b"oc% of una""ocated memory in which it wi"" fit.

    Problems:

    It requires an expensive search of the entire free "ist to find the best ho"e.

    :ore important"y, it "eads to the creation of "ots of "itt"e ho"es that are not big enough to satisfy any requests

    !his situation is ca""edfragmentationBand is a prob"em for a"" memory-management strategies, a"though it

    is particu"ar"y bad for best-fit.Solution:4ne way to avoid ma%ing "itt"e ho"es is to give the c"ient a bigger b"oc% than it as%ed for. /or

    examp"e, we might round a"" requests up to the next "arger mu"tip"e of F bytes. !hat doesnt ma%e the

    fragmentation go away, it 0ust hides it.

    (orst /it

    !he memory manager p"aces process in the "argest b"oc% of una""ocated memory avai"ab"e. !he ides is that

    this p"acement wi"" create the "argest ho"e after the a""ocations, thus increasing the possibi"ity that, compared

    to best fit, another process can use the ho"e created as a resu"t of externa" fragmentation.

    /irst /it

    nother strategy is first fit, which simp"y scans the free "ist unti" a "arge enough ho"e is found.

    6espite the name, first-fit is genera""y better than best-fit because it "eads to "ess fragmentation.

    Problems5

    &ma"" ho"es tend to accumu"ate near the beginning of the free "ist, ma%ing the memory a""ocator search

    farther and farther each time.

    Solution:ext /it- !his /it is a variant of the first-fit strategy.!he prob"em of sma"" ho"es accumu"ating is

    so"ved with next fit a"gorithm, which starts each search where the "ast one "eft off, wrapping around to the

    beginning when the end of the "ist is reached (a form of one-way e"evator)

    COMPACTION

    Compaction attac%s the prob"em of fragmentation by moving a"" the a""ocated b"oc%s to one end of

    memory, thus combining a"" the ho"es. side from the obvious cost of a"" that copying, there is an important

    "imitation to compaction* ny pointers to a b"oc% need to be updated when the b"oc% is moved. +n"ess it is

    possib"e to find a"" such pointers, compaction is not possib"e. Pointers can stored in the a""ocated b"oc%s

    themse"ves as we"" as other p"aces in the c"ient of the memory manager. In some situations, pointers can

    point not on"y to the start of b"oc%s but a"so into their bodies. /or examp"e, if a b"oc% contains executab"e

    code, a branch instruction might be a pointer to another "ocation in the same b"oc%. Compaction is

    performed in three phases. /irst, the new "ocation of each b"oc% is ca"cu"ated to determine the distance the

    b"oc% wi"" be moved. !hen each pointer is updated by adding to it the amount that the b"oc% it is pointing

    (in)to wi"" be moved. /ina""y, the data is actua""y moved. !here are various c"ever tric%s possib"e to combine

    these operations.

  • 8/10/2019 Record6-10

    38/53

    E6'NO'a

    MEMOR. MANA-EMENT II

    /IRST /IT

    AIM5

    !o write a program for first fit.

    #ARD(ARE RE1UIREMENT5

    P-

  • 8/10/2019 Record6-10

    39/53

    inc"udeQstring.hRinc"udeQmath.hRstruct segmentSchar 0obidK

  • 8/10/2019 Record6-10

    40/53

    OUTPUT5

    K>itE=L"oca"host MNO cc ffit.cK>itE=L"oca"host MNO .7a.out

    +## ;J /BB+## ; /BB

    +## ]] /BB+## /BB+## F= /BB+## >< /BB

    !ota"memoryspaceJ]:aximum si3e* =J

    Bnter no of 0obs you want to store>

    Bnter the program id*pEBnter the program id*

    p>Bnter the si3e of the program

    ;E

    4' I6 &IGB &!!+&

    p< ;J 4CC+PIB6p> ; 4CC+PIB6

    +## ]] /BB+## /BB

    +## F= /BB+## >< /BB

    RESU$T5

    !hus the program for first fit has been executed.

  • 8/10/2019 Record6-10

    41/53

    E6'NO'9 %EST /IT

    AIM5

    !o write a program for best fit.

    #ARD(ARE RE1UIREMENT5

    P-

  • 8/10/2019 Record6-10

    42/53

    inc"udeQmath.hRstruct segmentSchar 0obidK

  • 8/10/2019 Record6-10

    43/53

  • 8/10/2019 Record6-10

    44/53

  • 8/10/2019 Record6-10

    45/53

    struct segmentSchar 0obidK

  • 8/10/2019 Record6-10

    46/53

  • 8/10/2019 Record6-10

    47/53

    Bnter si3e of program*>F

    '"oc% obid &i3e &tatus E =J 4CC+PIB6 < +## => /BB > +## ; /BB

    J +## ; /BB F +## ;J /BB X +## ]] /BB +## F= /BB ] +## JX /BB ; +## >< /BB = +##

  • 8/10/2019 Record6-10

    48/53

    /I$E S.STEM

    /i"e>System Str!ct!re

    /i"e structure #ogica" storage unit Co""ection of re"ated information

    /i"e system resides on secondary storage (dis%s) /i"e system organi3ed into "ayers /i"e contro" b"oc% ` storage structure consisting of information about a fi"e

    $ayered /i"e System

    A Typica" /i"e Contro" %"ock

    $inked A""ocation

    Bach fi"e is a "in%ed "ist of dis% b"oc%s* b"oc%s may be scattered anywhere on the dis%.

    pointerblock

    =

  • 8/10/2019 Record6-10

    49/53

    &imp"e ` need on"y starting address /ree-space management system ` no waste of space o random access :apping

    '"oc% to be accessed is the th b"oc% in the "in%ed chain of b"oc%s representing the fi"e.6isp"acement into b"oc% V D .

    $inked A""ocation

    $A&&

    1

    R

  • 8/10/2019 Record6-10

    50/53

    /i"e>A""ocation Ta9"e

    Re"ated Ur"Fs

  • 8/10/2019 Record6-10

    51/53

    E6'NO'&G

    /I$E A$$OCATION TEC#NI1UE

  • 8/10/2019 Record6-10

    52/53

    Yprintf(@Tn Bnter the fi"e name,starting b"oc%,"ength of fi"e*@)2scanf(@UsUdUd@,fn,[s,[")2for(iVs2iQV(sD"-VE'"oc% JV