Solved

problem with pipes and processes

Posted on 2003-11-16
3
378 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 112
if loop in java 3 162
conditional code and condition difference 9 85
Making an alias 7 114
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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