Thread: Need desperate help on this ring/node in C, I'm lost at writing the 2 functions

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Need desperate help on this ring/node in C, I'm lost at writing the 2 functions

    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(){
    
    }
    
    



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    It might help if you could post the contents of your key.dat file.
    I'm assuming that it's a text file, even though you've chosen to open it in "rb" mode.

    To be honest, if you're struggling to even read a file, then pipes and processes are a big ask.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Hey Mr Salem, the key.dat file is just any text file input of random numbers. Would you like me to post the code guideline here, I've been stuck on this for hours. Is there a way I can contact you or email you? too long to type stuff on here. Thank you.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    You can send other members a Private Message but generally Email's
    are not given out via public forums. It is also generally a good learning
    experience for other members to read posted code by other people to
    study it, So public posting of code where possible is generally encouraged.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate help: functions and looping
    By krisvern93 in forum C Programming
    Replies: 11
    Last Post: 11-27-2010, 11:55 PM
  2. Really Lost - Functions
    By nexja in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2007, 02:27 PM
  3. Replies: 3
    Last Post: 08-03-2007, 04:11 AM
  4. HELP!! NEw to Arrays/Functions.. LOST
    By felixgun in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2006, 01:20 PM
  5. Beginner who is lost - functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-07-2002, 08:27 PM

Tags for this Thread