Ring of processes (litle test of comunication) !!!

This is a discussion on Ring of processes (litle test of comunication) !!! within the C Programming forums, part of the General Programming Boards category; Hello, I have this ring of process. Code: if (pipe (fd) == -1) { perror("Could not create pipe"); exit(1); } ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    France
    Posts
    6

    Ring of processes (litle test of comunication) !!!

    Hello,

    I have this ring of process.

    Code:
       if (pipe (fd) == -1) {
          perror("Could not create pipe");
          exit(1);
       }
       if ((dup2(fd[0], STDIN_FILENO) == -1) ||
           (dup2(fd[1], STDOUT_FILENO) == -1)) {
          perror("Could not dup pipes");
          exit(1);
       }
       if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
          perror("Could not close extra descriptors");
          exit(1);
       } 
            /* create the remaining processes with their connecting pipes */
       for (i = 1; i < nprocs;  i++) {
          if (pipe (fd) == -1) {
             fprintf(stderr,"Could not create pipe %d: %s\n",
                i, strerror(errno));
             exit(1);
          }  
          if ((childpid = fork()) == -1) {
             fprintf(stderr, "Could not create child %d: %s\n",
                i, strerror(errno));
             exit(1);
          }  
          if (childpid > 0)        /* for parent process, reassign stdout */
              error = dup2(fd[1], STDOUT_FILENO);
          else
              error = dup2(fd[0], STDIN_FILENO);
          if (error == -1) {
             fprintf(stderr, "Could not dup pipes for iteration %d: %s\n",
                     i, strerror(errno));
             exit(1);
          }  
          if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
             fprintf(stderr, "Could not close extra descriptors %d: %s\n",
                    i, strerror(errno));
             exit(1);
          }
          if (childpid)
             break;
       }
      
       
       if (i!=nprocs) while (( wait(&status)==-1)&&(errno==EINTR));   
                                     
       fprintf(stderr, "This is process %d with ID %d and parent id %d\n",
          i, (int)getpid(), (int)getppid());
    
    exit (0);
    }
    I want to test the comunication between them, so i am trying to full-up a matrix of thier process IDs, I tried to write the IDs in several ways but I didn't succeed.

    This part of the code is to be inserted before the wait system call.

    Code:
       int status,  int next_ID,*ID;
    
       ID=calloc(nprocs,sizeof(int));   
       
       ID[0]=(int)getpid();
       next_ID=ID[0];  
       
       for (j=1;j<nprocs;j++){
       	
       		char * temp_buf;
       		temp_buf=malloc(100);   		
       		
       		sprintf(temp_buf,"%d",next_ID);
       		fwrite(temp_buf,sizeof(int),1,stdout);   		
       		
       		fread(temp_buf,sizeof(int),1,stdin);   		
       		next_ID=atoi(temp_buf);
       		
       		//fprintf(stdout,"%d",next_ID);
       		//fscanf(stdin,"%d",&next_ID);
       		//printf("%d",next_ID);
       		//scanf("%d",&next_ID);
       		
       		ID[j]=next_ID;
       		   		
       		free(temp_buf);
       }

    This to see if each process have the IDs of the others.

    Code:
    	for(j=0;j<nprocs ;j++){
       		fprintf(stderr, "		The element %d in the matrix is %d\n",j,ID[j]);
    The problem is that the program blocking and I don't know why. I thought that may be i should delay the process ID exchanges until all the ring is done but i didn't succeed also.



    Any idea of why this is not working ????

  2. #2
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    you have way to much unreadable code going on and your variable nameing convetions need alot of work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  2. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  3. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 03:57 PM
  4. process ring
    By gregulator in forum C++ Programming
    Replies: 0
    Last Post: 02-28-2005, 08:21 PM
  5. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21