I'm really having hard time with code right now, basically it should be a node ring code that takes input and then fork and pipe each time it creates a node. The concept is the code will read an initial set of key values from the file key.dat in the working directory.There will be one process for each node in the logical ring making up the DHT. Each node (process) will communicate with its successor node (process) using a pipe between the two processes. 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.
1) right now I'm not sure my input reading part is right , how should I add it, inside the main function?
2) I'm having hard time writing this addnode function, the program will start by calling the routine to add one node.The "addNode" routine will know that no nodes have been created. So, it will fork and the child process will consider itself to be the initial node. The parent and child processes will set up a one-way pipe from the original program to the child. Each "addNode" call will send a ADDNODE message through the initial pipe. ADDNODE includes a node ID to be used. The receiving process will either (a) accept the ADDNODE message and insert a new node with the specified ID after itself or (b) pass the message along until the correct process receives it.
3) For my addkey function, Each "addKey" call will send an ADDKEY message through the initial pipe. ADDKEY includes a key value to be stored. The receiving process will either (a) accept the ADDKEY message and store the specified key or (b) pass the message long until the correct process receives it.The original program will then call a routine named "listData". This routine will send a LISTDATA message through the initial pipe. The receiving process will list its node ID and the keys that it stores, then pass the message along. Finally, the original program will call a routine named "exitRing". This routine will send an EXITRING message through the initial pipe. The receiving process will pass the message along, then exit. Node IDs and key values are integers from 0 to 63.This is what I have so far, please help me or comment! Thank you!
Code:Code:#include<errno.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> int main(int argc,char*argv[]){ pid_t childpid;// indicates process should spawn another */ int error;//not sure needed int fd[2];// file descriptors returned by pipe */ int n;// number of this process (starting with 1) */ FILE * pFile; pFile=fopen ("key.dat","rb"); if (pFile==NULL) perror ("The following error occurred"); else fclose (pFile); return 0; if(pipe (fd)==-1){// connect std input to a pipe perror("Failed to create starting pipe"); return1; } if((dup2(fd[0], STDIN_FILENO)==-1)|| (dup2(fd[1], STDOUT_FILENO)==-1)){ perror("Failed to connect pipe"); return1; } fprintf(stderr,"This is process %d with ID %ld and parent id %ld\n", n,(long)getpid(),(long)getppid()); return0; } void addNode(int*pid){ int fd[2]; pipe (fd); *pid = fork(); if(*pid >0) dup2(fd[1], STDOUT_FILENO); else dup2(fd[0], STDIN_FILENO); close(fd[0]); close(fd[1]); //Where is the node ID parameter? } void addKey(){ }