Solved

problem with pipes and processes

Posted on 2003-11-16
3
379 Views
Last Modified: 2013-12-26
Hi

My question refers to the problem already posted by someone:

http://www-level3.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_20760089.html

However as I can see they want the whole answer. That is they want their homework done by the experts. Well this is the same homework I'm doing. However I've almost finished it without any outside help. But I'm stuck now. While my program can read an input, send it to another process which reverses the string and then another process makes it uppercase, I cannot connect the third process to the first one. Below is my code. It will compile and run. Also I don't need it to run in a loop. once is enough. Will it be possible to connect the first and the third process without modifying this code too much. What am I doing wrong?

#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAXWORD 6

char *reverse( char *s);
char *uppercase( char *m);


main()
{
  char word[MAXWORD], buf[MAXWORD];
  int m[2],p[2];
  int i,j,k,len;
  char c='a', *newbuf;
  pid_t pid1,pid2;
  newbuf=&c;

  if (pipe(p)==-1)
     {
     printf("Error creating pipes\n");
     exit(1);
}

switch(pid1=fork())
{
  case -1 :
       printf("Error creating forks\n");
       exit(2);
  case 0 :

      if (pipe(m)==-1)
           {
             printf("Error creating pipes\n");
             exit(5);
      }
      switch(pid2=fork())
      {
        case -1 :
       printf("Error creating forks\n");
       exit(4);
       case 0:

             close(p[0]);
       close(p[1]);
       close(m[0]);
      printf("Please enter a word:\n");
        scanf("%s",word);
        write(m[1], word, MAXWORD);
      exit(6);


     default:
        close (m[1]);
      read(m[0], buf, MAXWORD);
      newbuf=reverse(buf);
      write(p[1], newbuf,MAXWORD);
      exit(8);
      }


      default:
      close (p[1]);
      read(p[0], buf, MAXWORD);
      newbuf=uppercase(buf);
      printf("The reversed and uppercased word is %s\n", newbuf);
      close(m[0]);
      write(m[1],newbuf,MAXWORD);
        }

exit(0);
}

char *reverse( char *s)
 {char *buf;
 int i,len;
 len = strlen(s); /* allocate memory */
 buf = malloc(len+1);
 for (i=0;i<len;i++)
 buf[i] = s[len-i-1];
 buf[i]='\0';
 strcpy(s,buf);
  free(buf);
  return(s);}


  char *uppercase( char *m)
 {char *buf;
 int i,len;
 len = strlen(m); /* allocate memory */
 buf = malloc(len+1);
 for (i=0;i<len;i++)
 buf[i] = toupper(m[i]);
 buf[i]='\0';
 strcpy(m,buf);
  free(buf);
  return(m);}



0
Comment
Question by:muhid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9761856
I provide a detailed explanation of the solution here:

http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_20763547.html

basically, the third process needs to inherit an open pipe from the first process.

0
 
LVL 1

Accepted Solution

by:
TriShakti earned 500 total points
ID: 9785342
int main(void)
{

    char ip[6] ;
    int rv=0;

    int communication_line[2] ;


    pid_t child1=0, child2=0 ;

    memset(ip,0,WORDLEN);

    rv = pipe( communication_line ) ;
    if( rv < 0 )
    {
        printf( "Errro creating communication line.\n");
        return(-1);
    }


    child1=fork();
    if( child1 < 0 )
    {
        printf( "fork1 failed\n" );
        exit(-1);
    }

    if( child1 != 0 )
    {
        acceptstring(ip);
        write(communication_line[1], ip, WORDLEN );

        rv=waitpid(child1, (int*)0, 0) ;
        read(communication_line[0], ip, WORDLEN);

        printf( "The final string is [%s]\n", ip);

        close(communication_line[1]);

        close(communication_line[0]);
        return(0);
    }


    if( child1 == 0 )
    {
        child2=fork();
        if( child2 < 0 )
        {
            printf( "Error creating second child\n" );
            exit(-1);
        }

        if( child2 != 0 )
            {
             read(communication_line[0], ip, WORDLEN);
             reversestring(ip);
             write(communication_line[1], ip, WORDLEN);
             rv = waitpid(child2, (int*)0, 0);
             close(communication_line[0]);
             close(communication_line[1]);
              exit(0);
            }

        if( child2 == 0 )
        {
            read(communication_line[0], ip, WORDLEN);
            capitalstring(ip);
            write(communication_line[1], ip, WORDLEN );
            close(communication_line[0]);
            close(communication_line[1]);
            exit(0);
        }
   }
    return(0);
}



Hi,
 The WORLEN is 6.
I have not provided the functios for uppercase and reversing, as you already have them.

I hope the code will make sens e without explanation also.
I no just email me. I will do the needful.

Regards and thanks,
Karkra Jaikumar


0
 

Author Comment

by:muhid
ID: 9796271
To Karkra

Ok, I was not asking for a full solution but what is wrong with my code  and how my code can be modified so that it works. However your code  do make sense and at least helped me to see what modifications can be made for my code to work. So I'm accepting your solution

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question