Hello,
I have this ring of process.
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.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); }
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.
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.Code:for(j=0;j<nprocs ;j++){ fprintf(stderr, " The element %d in the matrix is %d\n",j,ID[j]);
Any idea of why this is not working ????



LinkBack URL
About LinkBacks


