Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > interview question

Reply
Thread Tools

interview question

 
 
Guest
Posts: n/a
 
      03-21-2005
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


 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      03-21-2005
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
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      03-22-2005

"Grumble" <(E-Mail Removed)> ??????:d1m1se$eq8$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Taran
Guest
Posts: n/a
 
      03-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-22-2005
<(E-Mail Removed)> writes:
> "Grumble" <(E-Mail Removed)> ??????:d1m1se$eq8$(E-Mail Removed)...
>> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Guest
Posts: n/a
 
      03-22-2005
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



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-22-2005
(E-Mail Removed) wrote:
> "Grumble" <(E-Mail Removed)> 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


 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-22-2005
In article <d1l8mb$bf7$(E-Mail Removed)>, <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP Interview Questions ASP Interview Questions reema ASP General 0 08-26-2008 11:57 AM
.NET Interview Question, C#, ASP.NET Interview Questions dotnetuncle Javascript 0 10-30-2007 03:08 PM
An interview question Jerry Java 22 06-12-2005 03:55 PM
Interview question Gopal Krish ASP .Net 10 10-23-2004 10:18 AM
interview question (toughest bug you've fixed)? Digital Puer Java 17 12-27-2003 09:35 AM



Advertisments