3

The code takes a command as input and executes it. Pipes are also handled. The issue is that suppose if i enter ls | grep x as command. The process grep does not exit and so the program halts. Any ideas.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
void parse(int *cont,char *command,char **exarg)
{
    char *x=strchr(command,'&');
    if(x!=NULL)
    {
        *cont=1;
        *(--x)='\0';
    }
    x=strtok(command," ");
    while(x!=NULL)
    {
        *exarg++=x;
        x=strtok(NULL," ");
    }
    *exarg='\0';
}

int divide(char *cmd,char **cmdarr)
{
    int i=0;
    char *x;
    x=strtok(cmd,"|");
    while(x!=NULL)
    {
        i++;
        *cmdarr++=x;
        x=strtok(NULL,"|");
    }
    *cmdarr='\0';
    return i;
}

void main()
{
    pid_t pid;
    int cont;
    int i=0;
    int orgnum;
    char *cmdarr[50];
    char c[50];
    int p1[2];
    int p2[2];
    char *argument[50];
    if(pipe(p1)<0)
        printf("error\n");
    if(pipe(p2)<0)
        printf("error2\n");
    for(;;)
    {
        printf("Enter the command:");
        gets(c);
        if(!strcmp("exit",c))
            break;
        orgnum=divide(c,cmdarr);
        printf("%d\n",orgnum);
        for(i=0;i<orgnum;i++)
        {
            pid=fork();
            if(pid==0)
            {
                if(i==0)
                {
                    close(p1[0]);
                    close(p1[1]);
                    close(p2[0]);
                    if((i+1)!=orgnum)
                    {
                        dup2(p2[1],STDOUT_FILENO);
                        dup2(p2[1],STDERR_FILENO);
                    }
                    close(p2[1]);
                    parse(&cont,cmdarr[i],argument);
                    if((execvp(argument[0],argument))<0)
                    {
                        printf("Wrong cmd");
                        exit(0);
                    }
                }
                else if(i>0)
                {
                    if((i+1)%2==0)
                    {
                        close(p1[0]);
                        close(p2[1]);
                        dup2(p2[0],STDIN_FILENO);
                        close(p2[0]);
                        if((i+1)!=orgnum)
                        {
                            dup2(p1[1],STDOUT_FILENO);
                            dup2(p1[1],STDERR_FILENO);
                        }
                        close(p1[1]);
                    }
                    else if((i+1)%2==1)
                    {
                        close(p2[0]);
                        close(p1[1]);
                        dup2(p1[0],STDIN_FILENO);
                        close(p1[0]);
                        if((i+1)!=orgnum)
                        {
                            dup2(p2[1],STDOUT_FILENO);
                            dup2(p2[1],STDERR_FILENO);
                        }
                        close(p2[1]);
                    }
                    parse(&cont,cmdarr[i],argument);
                    if((execvp(argument[0],argument))<0)
                    {
                        printf("Wrng cmd");
                        exit(0);
                    }
                }
            }
            else if(pid>0){
                close(p1[0]);
                close(p1[1]);
                close(p2[0]);
                close(p2[1]);
                wait(NULL);}
        }
    }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
ZURA
  • 31
  • 2

1 Answers1

0

So you have cycle:

fork if child
exec
else if parent
close pipes
wait

When you have something like "ls | grep foo",

step 1: you exec ls in child, and close all pipes, and wait 'ls' termination in parent,

step 2: exec 'grep' in child, and use pipe as input, but you close them in step 1.


fghj
  • 8,898
  • 4
  • 28
  • 56
  • Yes because the pipes are not being used by the process in step 1. The second process 'grep' is a whole new child as the loop for(i=0;i – ZURA Nov 11 '12 at 04:24