C++ in a UNIX environment Create a one-directional circular buffer (ring) of pip
ID: 665583 • Letter: C
Question
C++ in a UNIX environment
Create a one-directional circular buffer (ring) of pipes for N processes (N>=1)
Create a one-directional circular buffer (ring) of pipe for N threads
Create a one-directional cicular buffer (ring) of socket for N hosts (or all in one)
You may run N processes in one host.
Each process has two designated ports - one for sending and the other for receiving.
Each program for each process will be run with an argument telling which process (Nth process)
You may assume that each process has a table of process with 2 ports as following:
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
When N processes (or threads) in a ring are up and ready, Process 0 sends a message to be received by Process 1, and so on, to back to Process 0 and done. Whenever a process sends or receives a message, it prints its process # (N), "the message", and "sent" or "received".
Process i
IP address
Port# to send
Port# to receive
0
127.0.0.1
9000
9001
1
127.0.0.1
9100
9101
2
127.0.0.1
9200
9201
and so on
Explanation / Answer
one-directional circular buffer (ring) of pipes for N processes (N>=1)
#define SIZE 10
struct pipe
{
int c[SIZE];
unsigned head, tail;
};
void writec(pipe_t *p, int c)
{
while(p->tail - p->head == SIZE)
{
schedule();
}
p->c[p->tail % SIZE] = c;
++p->tail;
}
int readc(pipe_t *p) {
while(p->head == p->tail)
{
schedule();
}
int c = p->c[p->head%SIZE];
++p->head;
return c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.