• C

code for a finite buffer?

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.
telmodAsked:
Who is Participating?
 
newexpertConnect With a Mentor Commented:
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
 
kellyjjCommented:
what os?  Could you give a little more clearity?  thanks
0
 
telmodAuthor Commented:
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
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
rbrCommented:
I still don't understand anything. To you want to transfer data from one programm to another? Which protokoll, via modem, which OS?
0
 
telmodAuthor Commented:
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
 
elfieCommented:
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
 
telmodAuthor Commented:
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
 
newexpertCommented:
Typo in receive function, should be
int receive(PACKET *q)
0
 
newexpertCommented:
And extra bracket in function addone() should be
char *addone(char *p) { return (p-buf+1) % 80 + buf; }

0
 
newexpertCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.