CS604 Assignment 02 Solution idea 2 Spring 2013

No Comments

hi,
The day I got the assignment, solved same day. It is easy little understanding is needed. I can give you some guide lines. This is not solution but some idea
define MAX 80

void child();
void parent();
void childtwo();
char * getli();
void printline(char *buffer, int count);
char * convertCase(char *str);

int pipe1[2];
int pipe2[2];
int main(int argc, char **argv)
{
  pipe(pipe1);
  pipe(pipe2);
  if(fork()){
    if(fork()){
      printf("1st\n");
      parent();
          exit(0);
    }
    else{
      printf("3rd\n");
      childtwo();
      exit(0);
    } 
  }
  else{
    printf("2nd\n");
    child();
    exit(0);
  }
}

void child(){
  char *buf;
  int count = 0;
  close(pipe1[1]);
  close(pipe2[0]); 
  while(1){
  buf = (char *) malloc(sizeof(char)*MAX);
  read(pipe1[0], buf, MAX);
  if (strcmp(buf,"quit")== 0){
    printf("Child is leaving\n");
    free(buf);
    break;
  }
  else{
    printf("Child: ");
    printline(buf,strlen(buf));
    write(pipe2[1],buf, strlen(buf)+1);
    free(buf);
        }
  close(pipe2[1]);
  close(pipe1[0]);
  exit(0);
  }
}

void childtwo()
{
  char *buf;
  int count = 0;
  close(pipe2[1]);
  buf = (char *) malloc(sizeof(char)*MAX);
  while(1){
    buf = (char *) malloc(sizeof(char)*MAX);
    read(pipe2[0], buf, MAX);
    if (strcmp(buf,"quit")== 0){
      printf("Childtwo is leaving\n");
      free(buf);
      break;
    }
    else{
      printf("Childtwo:");
      printline(buf,strlen(buf));
      free(buf); 
    }
  }

Next PostNewer Post Previous PostOlder Post Home

0 comments

Post a Comment