Reputation: 79
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
int pfd[2];
int status,i;
char input[1024];
int rfds[n]; //Hold the file IDs of each pipe fd[0]
int wfds[n]; //Holds the file IDs of each pipe fd[1]
int pids[n]; //Holds the list of child process IDs
while(fgets(input,sizeof(input),stdin)){
for (i=0;i<6;i++){
if(pipe(pfd) < 0)
{
printf("Failed to create pipe!\n");
return 1;
}
//Store the pipe ID
rfds[i] = pfd[0];
wfds[i] = pfd[1];
if((pids[i] = fork())<0){
printf("Failed to fork!\n");
return 1;
}
if (pids[i]==0) {
close(wfds[i]);
if(read(rfds[i],input,strlen(input)) > 0)
{
printf("process #%d (%d) relaying message:%s",i,getpid(),input);
}
close(rfds[i]);
}
else
{
close(rfds[i]);
if((write(wfds[i], input, strlen(input))) ==-1)
{
printf("Failed to write!\n");
return 1;
}
close(wfds[i]);
wait(&status);
}
}
}
return 0;
}
I code this to transmits messages from process to process. but i want to make the last process connect to the first process. i.e., what it output is like
process #0 (47652) sending message: MD
process #1 (47653) relaying message: MD
process #2 (47654) relaying message: MD
process #3 (47655) relaying message: MD
process #4 (47656) relaying message: MD
process #5 (47657) relaying message: MD
process #6 (47658) relaying message: MD
What i need is the last process is done in the process with process id 47651 rather than 47658
Upvotes: 0
Views: 1912
Reputation: 863
You will have to create one end pipe before you start the forking. Because this is needed for the last process to connect to the first.
I have some code which you can create children with.
void createChildren(int size)
{
int i = 0;
int end_pipe[2];
int incomming[2];
int outgoing[2];
/* create the end pipe */
pipe(end_pipe);
for(i = 0; i < size - 1; ++i)
{
pipe(outgoing);
/* parent process */
if (fork() != 0)
{
break;
}
/* incomming pipe of the child is the outgoing of the parent */
incomming[0] = outgoing[0];
incomming[1] = outgoing[1];
}
/**
* If master then the incomming pipe is the end pipe. Glue the end to the beginning
*/
if (i == 0)
{
incomming[0] = end_pipe[0];
incomming[1] = end_pipe[1];
}
/**
* If master then the ougoing pipe is the end pipe. Glue the end to the beginning
* Initial write to the ring
*/
if (i == size - 1)
{
int buffer = 0;
outgoing[0] = end_pipe[0];
outgoing[1] = end_pipe[1];
write(outgoing[1], &buffer, sizeof(int));
}
runClient(i, size, incomming, outgoing);
}
Upvotes: 1
Reputation: 646
Just create n pipes (you could use an array), where n is the number of processes. Then every time write to the pipe of the next one and read from you own pipe. At the end of the loop, just use a modulo to stay within the array of pipes.
Does this help? I could give you some code, but it should not be too hard.
Upvotes: 0