Solved

problem with pipes and processes

Posted on 2003-11-16
3
370 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

15 Experts available now in Live!

Get 1:1 Help Now