mardi 4 octobre 2016

Having trouble creating this function in C program, confused by two parts by most, involves pipes and ID nodes

I'm stuck at creating this function right, addnode, my program will start by calling the routine to add one node.

The "addNode" is a routine will know that no nodes have been created. So, it will fork and the child process will consider itself to be the first node.Each "addNode" call will send a addNode message through the initial pipe. addNoe includes a node ID to be used. The receiving process will either (a) accept the addNoe message and insert a new node with the specified ID after itself or (b) pass the message along until the correct process receives it.

For background of this program, this code is chord simulation of distributed hast table, the program will read an initial set of key values from a text file of random numbers. There's one process for each node in ring. Each node (process) will communicate with its successor node (process) using a pipe between the two processes. So basically it's using pipe instead of linked list. The program starts with a single node (with randomly assigned ID). When a new node is created, a new process is forked, pipes created and/or rearranged, and keys properly assigned, by passing data through the network of pipes to get the data items to the correct node.

So here are my two question for my function:

1) What is the right way to create nodeID that pass through my function? Each node is assigned a random number in the range 0-63 when it is created (using the appropriate Linux syscall for random numbers). For my node id part, I just declared my nodeID. That's definitely wrong, how should I generate a random node ID?

2) How do I use pipe replace the linked list, I'm not sure how to make sure a key is stored when the message is accepted. Right I just used the basic pipe concept to use a string to create a message. I'm not sure that's correct way to do it. How do I pass or insert without a linked list in the pipe?

This is my program so far, in really bad shape:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>


typedef struct node
{
   int data;
   int key;
   int node;
   int nodeID;
};

void addNode(int nodeID)
{


    int     fd[2], nbytes;
    pid_t   childpid;
    char    string[] = "node received \n";
            char    readbuffer[80];

            pipe(fd);

            if((childpid = fork()) == -1)
            {
                    perror("fork");
                    exit(1);
            }

            if(childpid == 0)
            {

                    close(fd[0]);

                    // Send "string" through the output side of pipe */
                    write(fd[1], string, (strlen(string)+1));
                    exit(0);
            }
            else
            {

                    close(fd[1]);


                    nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                    printf("Received string: %s", readbuffer);
            }

            return(0);

}









void addKey (int key)
{
    //send the message STORE key into the ring;
    //lost on this still
}




Aucun commentaire:

Enregistrer un commentaire