interview question

Discussion in 'C Programming' started by Guest, Mar 21, 2005.

  1. Guest

    Guest Guest

    q:
    void producer()
    {
    while(1)
    {
    GeneratePacket();
    PutPacketIntoBuffer();
    Signal(customer);
    }
    }

    void customer()
    {
    while(1)
    {
    WaitForSignal();
    if(PacketInBuffer>10)
    {
    ReadAllPackets();
    ProcessPackets();
    }
    }
    }

    1 how to improve efficiency?
    2 achieve it without signal

    ps: sorry my english is so poor ! i wish you can understand what i post:)

    my answer

    1
    void producer()
    {

    while(1)
    {
    GeneratePacket();
    PutPacketIntoBuffer();
    if(PacketInBuffer>10)
    Signal(customer);
    } // is it right? i wait for the better

    2 i don't know , i guess it is PV or arithmetic skill
     
    Guest, Mar 21, 2005
    #1
    1. Advertising

  2. Guest

    Grumble Guest

    chong19782002 at hotmail.com wrote:

    > void producer()
    > {
    > while(1)
    > {
    > GeneratePacket();
    > PutPacketIntoBuffer();
    > Signal(customer);
    > }
    > }
    >
    > void customer()
    > {
    > while(1)
    > {
    > WaitForSignal();
    > if(PacketInBuffer>10)
    > {
    > ReadAllPackets();
    > ProcessPackets();
    > }
    > }
    > }


    Indentation is your friend ;-)
     
    Grumble, Mar 21, 2005
    #2
    1. Advertising

  3. Guest

    Guest Guest

    "Grumble" <> ??????:d1m1se$eq8$...
    > chong19782002 at hotmail.com wrote:
    >
    > > void producer()
    > > {
    > > while(1)
    > > {
    > > GeneratePacket();
    > > PutPacketIntoBuffer();
    > > Signal(customer);
    > > }
    > > }
    > >
    > > void customer()
    > > {
    > > while(1)
    > > {
    > > WaitForSignal();
    > > if(PacketInBuffer>10)
    > > {
    > > ReadAllPackets();
    > > ProcessPackets();
    > > }
    > > }
    > > }

    >
    > Indentation is your friend ;-)


    tks
    i cann't understand. can you give me a example
     
    Guest, Mar 22, 2005
    #3
  4. Guest

    Taran Guest

    wrote:
    > q:
    > void producer()
    > {
    > while(1)
    > {
    > GeneratePacket();
    > PutPacketIntoBuffer();
    > Signal(customer);
    > }
    > }
    >
    > void customer()
    > {
    > while(1)
    > {
    > WaitForSignal();
    > if(PacketInBuffer>10)
    > {
    > ReadAllPackets();
    > ProcessPackets();
    > }
    > }
    > }
    >
    > 1 how to improve efficiency?


    > 2 achieve it without signal


    You can use semaphores and mutexes if not signals.

    > ps: sorry my english is so poor ! i wish you can understand what i

    post:)
    >
    > my answer
    >
    > 1
    > void producer()
    > {
    >
    > while(1)
    > {
    > GeneratePacket();
    > PutPacketIntoBuffer();
    > if(PacketInBuffer>10)
    > Signal(customer);
    > } // is it right? i wait for the better
    >
    > 2 i don't know , i guess it is PV or arithmetic skill


    Also make sure that producer does not start filling in the buffer
    before the consumer has finished reading it. so you need a second
    signal.

    HTH
    --
    Taran
     
    Taran, Mar 22, 2005
    #4
  5. <> writes:
    > "Grumble" <> ??????:d1m1se$eq8$...
    >> chong19782002 at hotmail.com wrote:
    >>
    >> > void producer()
    >> > {
    >> > while(1)
    >> > {
    >> > GeneratePacket();
    >> > PutPacketIntoBuffer();
    >> > Signal(customer);
    >> > }
    >> > }
    >> >
    >> > void customer()
    >> > {
    >> > while(1)
    >> > {
    >> > WaitForSignal();
    >> > if(PacketInBuffer>10)
    >> > {
    >> > ReadAllPackets();
    >> > ProcessPackets();
    >> > }
    >> > }
    >> > }

    >>
    >> Indentation is your friend ;-)

    >
    > tks
    > i cann't understand. can you give me a example


    In other words, your code would be easier to read if it looked like
    this:

    void producer()
    {
    while(1)
    {
    GeneratePacket();
    PutPacketIntoBuffer();
    Signal(customer);
    }
    }

    void customer()
    {
    while(1)
    {
    WaitForSignal();
    if(PacketInBuffer>10)
    {
    ReadAllPackets();
    ProcessPackets();
    }
    }
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 22, 2005
    #5
  6. Guest

    Guest Guest

    thanks ! i change it

    q:
    void producer()
    {
    while(1)
    {
    GeneratePacket();
    PutPacketIntoBuffer();
    Signal(customer);
    }
    }

    void customer()
    {
    while(1)
    {
    WaitForSignal();
    if(PacketInBuffer>10)
    {
    ReadAllPackets();
    ProcessPackets();
    }
    }
    }

    1 how to improve efficiency?
    2 achieve it without signal

    ps: sorry my english is so poor ! i wish you can understand what i post:)

    my answer:

    1
    void producer()
    {
    while(1)
    {
    GeneratePacket();
    PutPacketIntoBuffer();
    if(PacketInBuffer>10)
    Signal(customer);
    } // is it right? i wait for the better

    2 i don't know , i guess it is PV or arithmetic skill
     
    Guest, Mar 22, 2005
    #6
  7. Guest

    CBFalconer Guest

    wrote:
    > "Grumble" <> wrote:
    >> chong19782002 at hotmail.com wrote:
    >>

    .... snip ...
    >>>
    >>> void customer()
    >>> {
    >>> while(1)
    >>> {
    >>> WaitForSignal();
    >>> if(PacketInBuffer>10)
    >>> {
    >>> ReadAllPackets();
    >>> ProcessPackets();
    >>> }
    >>> }
    >>> }

    >>
    >> Indentation is your friend ;-)

    >
    > tks i cann't understand. can you give me a example


    Ok.

    void customer()
    {
    while (1) {
    WaitForSignal();
    if (PacketInBuffer > 10) {
    ReadAllPackets();
    ProcessPackets();
    }
    }
    }

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Mar 22, 2005
    #7
  8. In article <d1l8mb$bf7$>, <> wrote:
    :q:
    :void producer() {
    : while(1) { GeneratePacket(); PutPacketIntoBuffer(); Signal(customer); }
    :}

    :void customer() {
    : while(1) { WaitForSignal(); if(PacketInBuffer>10) { ReadAllPackets(); ProcessPackets(); } }
    :}

    :1 how to improve efficiency?

    Insufficient information. Are these two different functions in the same
    process, or are they in different processes? Are they connected by a
    pipe, a shared memory segment, a semaphore, a socket of type 'unix',
    a STREAM, a network socket?

    Are GeneratePacket() and PutPacketInBuffer() sufficiently simple that it
    makes sense to interleave them into one routine?

    Is GeneratePacket() something that is subject to efficiency increases?
    e.g., if it is a spending most of its timne waiting for user or network
    input, then there might not be any great increase that can be made.

    : 2 achieve it without signal

    You have to know the connection between the two. There must be -some-
    connection because PutPacketIntoBuffer() and PacketInBuffer need to
    work together [unless we assume that WaitForSignal() increments
    PacketInBuffer blindly..]. In any case, PutPacketIntoBuffer() and
    ReadAllPackets() have to work together.. but we don't know how.

    We also aren't given any information about whether the signal is
    crucial to the packet transfer protocol -- there are transfer protocols
    that work by stuffing data into a common buffer and then signalling,
    and the end of the buffer is determined by what's there at the time of
    the signal rather than there being a nice packet framing mechanism
    suitable for packing several packets into a single buffer and later
    unpacking them. The Signal() / WaitForSignal() sequence could also be
    crucial in emptying scace packet memory to ready it for placing the
    next packet in. It's nice to say "Duh, just generate 10 packets before
    signalling", but if one supposes that there is a network or serial
    connection between the two, then it could be that there is only enough
    memory in the packet buffers on each end for a single packet.

    In the degenerate case, consider that a "packet" could consist of a
    single byte, that PutPacketInBuffer() could consist of writing the byte
    to a parallel I/O port, and that the Signal() routine could consist of
    strobing the write line on the I/O port. The WaitForSignal() routine
    would then be waiting to see the write line on the I/O port, and would
    then read the byte and save it as a "packet" and increment the count.
    In such a case, the way to "improve the efficiency" would be to
    impliment a ring buffer for output, so that one could drop the byte in
    at the user level and return to the user level, with the device driver
    level taking care of sending the bytes as quickly as practical -- that
    would be more efficient than waiting until each byte/packet was
    transmitted. This is, you might recognize, a completely different
    interpretation of the problem than the suggestions to signal once per
    10 packets.

    Another thing -- we aren't told that the consumer and
    producer are the only such entities around. There could be multiple
    producers or multiple consumers, so with the information we have,
    we don't know that optimizations such as internal buffering
    at the producer level is practical or correct. Put packet in
    buffer, yeah, but we aren't told that it is the -same- buffer
    each time, or that no-one else is going to be attempting to
    write to the buffer.

    Ah, and something else to consider: if the two are running in
    different processes, then one needs to put in interlocks so
    that the producer knows to hold-off while the consumer is
    emptying the buffer. The interlock process can be expensive,
    which could be a reason why the code is implimented the
    way it is, rather than a produce-one / consume-one cycle.


    Have you ever heard the quip that, "An expert is someone who tells
    you all the reasons why something can't be done?" Just so -- the
    information given in the problem is insufficient for a meaningful
    answer other than "It depends".


    And it's always good to keep in mind that one should usually
    refrain from trying to optimize something unless it is known
    that the existing implimentation is inefficient. Now, sometimes
    the process of thinking about optimizations leads to wonderful
    clarity of the problem, so it can be worth-while -- but if you
    have code that is already quite straight-forward then chances
    are that optimization will just complicate the code and make
    it harder to maintain, and most of the time won't end up
    speeding anything up anyhow. Optimize where there's a proven
    problem [and ugly existing code is a proven problem ;-) ]
    --
    Feep if you love VT-52's.
     
    Walter Roberson, Mar 22, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gopal Krish

    Interview question

    Gopal Krish, Oct 22, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    682
    John Timney \(Microsoft MVP\)
    Oct 23, 2004
  2. Digital Puer
    Replies:
    17
    Views:
    4,120
    Andrew Thompson
    Dec 27, 2003
  3. Jerry

    An interview question

    Jerry, May 27, 2005, in forum: Java
    Replies:
    22
    Views:
    945
    Brooks Hagenow
    Jun 12, 2005
  4. Replies:
    9
    Views:
    463
    Andrey Tarasevich
    Jan 22, 2005
  5. reema
    Replies:
    0
    Views:
    293
    reema
    Aug 26, 2008
Loading...

Share This Page