Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

code for a finite buffer?

Posted on 1998-10-22
10
269 Views
Last Modified: 2010-04-15
I would like to send packets (63 bytes) in finite buffer(size=80) and control the thruput.I don't know to code it in C, can any experts give me help.
Thanks very much in advance.
0
Comment
Question by:telmod
10 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1253722
what os?  Could you give a little more clearity?  thanks
0
 

Author Comment

by:telmod
ID: 1253723
Sorry, if I can't explain myself properly, it's only because
english is not my first language.

1) generate data_source (bits)

2) Create a finite buffer (size 80).

*one size=1 packet=63 bytes =63x8 bits
*so if the buffer is full we will get 63x8x80 bits.

3)The receiver

This receiver is take the measurment like:

*number of packet in output.
*delays
*State of the buffer at each time, (number of packet currently in the buffer)

4)We must consider.

At the beginning, there are nothing in the buffer.
if the buffer is full,don't fill anymore till you get some available space.

Thanks

0
 
LVL 10

Expert Comment

by:rbr
ID: 1253724
I still don't understand anything. To you want to transfer data from one programm to another? Which protokoll, via modem, which OS?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:telmod
ID: 1253725
Sorry, you can use only DOS system with the c compiler.
I want to create three function.

1) generate data.
2) fill buffer with the data
3) put out data from the buffer
4) collect the measurment from the buffer



0
 
LVL 3

Expert Comment

by:elfie
ID: 1253726
If you have some structure in your 'packet', you can use a c-structure. If there's no structure, just use a 63 byte character array.

If you then want 80 occurences of that  buffers you will end up using something like
struct packet_t packet[80];  or
char packet [80][63];

to fill the buffer  use assignment when using structs
(packet.fld1[i] = something), or use memcp/strcpy in teh latter case.

put data to buffer: if you mean put it on some kind of socket connection, use the appriopriate socket calls (send, write,...).


To collect data use something like read, recv.

If info doesn't satisfy, please specify what you want. Do you have 2 programs with communication, is it on 1 platform, are you talking to another system (protocal TCP, UDP, ...?)
0
 

Author Comment

by:telmod
ID: 1253727
Sorry, but I don't use external program and some kind of socket. Ii's only for example to generate traffic with mathematic function like poisson distribution.  

I don't tal with another system, it's only a small programm.

Maybe this figure will help you more.
-----------           --------     ----------  
|  source  | ------->| buffer |--->|receiver |
-----------           --------     ----------
0
 
LVL 1

Accepted Solution

by:
newexpert earned 100 total points
ID: 1253728
typedef struct { char datastart[63]; char *progress; } PACKET;
int endofpacket(PACKET *p) { return (p->progress - p->datastart) >= 63; }

char buf[80]; /* Circular buffer */
char *head = buf, *tail = buf;
char *addone(char *p) { return (p-buf+1) % 80) + buf; }
int full() { return addone(tail) == head; }
int empty() { return head == tail; }

int main(void) {
 PACKET p, q;
 int more = 1, pack_sent = 0, pack_rec = 0;
 q.progress = q.datastart;
 while (more) {
  if (pack_sent) {
   more = getmoredata(&p);
   p.progress = p.datastart; /* Get ready to send the packet */
   pack_sent = 0;
  }
  else pack_sent = send(&p);

  if (!pack_rec) pack_rec = receive(&q);
  else {
   dosomethingwith(&q);
   q.progress = q.datastart; /* Get ready for next packet */
   pack_rec = 0;
  }
 }
 return 0;
}

int getmoredata(PACKET *ptr_p) {
 /* Your code here */
 return /* 1 = supplied_more_data, 0 = no more data */;
}

int dosomethingwith(PACKET *ptr_q) {
 /* Your code here */
 return /* Anything */;
}

int send(const PACKET *p) {
 while (!full()) {
  if (!endofpacket(p)) {
     *tail = *(p.progress)++; tail = addone(tail);
  }
  else return 1; /* Packet is sent */
 }
 return 0; /* Blocked because buffer isfull */;
}

int receive(PACKET p) {
 while (!empty()) {
  if (!endofpacket(q)) {
    *(q.progress)++ = *head; head = addone(head);
  }
  else return 1; /* Packet is received */
 }
 return 0; /* Blocked because buffer is empty */
}
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1253729
Typo in receive function, should be
int receive(PACKET *q)
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1253730
And extra bracket in function addone() should be
char *addone(char *p) { return (p-buf+1) % 80 + buf; }

0
 
LVL 1

Expert Comment

by:newexpert
ID: 1253731
And an update due to typo:

int send(const PACKET *p) {
 while (!full()) {
  if (!endofpacket(p)) {
     *tail = *(p->progress)++; tail = addone(tail);
  }
  else return 1; /* Packet is sent */
 }
 return 0; /* Blocked because buffer isfull */;
}

int receive(PACKET p) {
 while (!empty()) {
  if (!endofpacket(q)) {
    *(q->progress)++ = *head; head = addone(head);
  }
  else return 1; /* Packet is received */
 }
 return 0; /* Blocked because buffer is empty */
}

0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question