Solved

problem with pipes and processes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 89
lucky13 challenge 11 124
java ^ examples 8 62
sumHeights  challenge 17 66
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…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now