interview question

G

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
 
G

Grumble

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

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

Indentation is your friend ;-)
 
T

Taran

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
 
K

Keith Thompson

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();
}
}
}
 
G

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
 
C

CBFalconer

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

Ok.

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

Walter Roberson

: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 ;-) ]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top