Solved

problem with pipes and processes

Posted on 2003-11-16
3
376 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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
Need a Shell script to start a service checking the port 6 52
sum13 challenge 24 122
Unix Command -- Challenging  question 7 96
How to convert MFC::CString to UTF8 wchar_t* 10 278
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 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