All pastes #1698255 Raw Edit

Ring process

public c v1 · immutable
#1698255 ·published 2009-12-02 12:25 UTC
rendered paste body
/* Program 4.1 */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>/* Sample C program for generating a unidirectional ring of processes.Invoke this programwith a command-line arg umentindicating the number of processes on the ring. Communicationis done via pipes that connect the standard output of a process to the standard input ofits successor on the ring. After the ring is created, each process identifies itself withits process ID and the process ID of its parent. Each process then exits. */void main(int argc, char *argv[ ]){int i; /* number of this process (starting with 1) */int childpid; /* indicates process should spawn another */int nprocs; /* total number of processes in ring */int fd[2]; /* file descriptors returned by pipe */int error; /* return value from dup2 call *//* check command line for a valid number of processes to generate */if ( (argc!= 2) || ((nprocs=atoi(argv[1])) <= 0) ) {fprintf(stderr, "Usage: %snprocs\n",argv[0]);exit(1);}/* connect std input to std output via apipe */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, reassignstdout*/error = dup2(fd[1], STDOUT_FILENO);elseerror = 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;}/* say hello to the world */fprintf(stderr,"This is process %d with ID %d and parent id %d\n",i, (int)getpid(), (int)getppid());exit (0);} /* end of main program here */