Solved

code for a finite buffer?

Posted on 1998-10-22
10
263 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
Comment Utility
what os?  Could you give a little more clearity?  thanks
0
 

Author Comment

by:telmod
Comment Utility
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
Comment Utility
I still don't understand anything. To you want to transfer data from one programm to another? Which protokoll, via modem, which OS?
0
 

Author Comment

by:telmod
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:telmod
Comment Utility
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
Comment Utility
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
Comment Utility
Typo in receive function, should be
int receive(PACKET *q)
0
 
LVL 1

Expert Comment

by:newexpert
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now