Problem in creating two pipes

Discussion in 'Programmer's Corner' started by learneros, Nov 15, 2007.

  1. learneros

    Thread Starter Member

    Nov 2, 2007
    14
    0
    Hi,
    I am working in pipes.I have created a pipe then create a child process by fork.Child process is wrinting into the pipe and

    then parent read frm the pipe and displayed it.This code is working alright


    int main()
    {
    int fd[2];
    int fd2[2];
    int pp,pp2;
    int status;
    char *string = "This is my first Pipe creation",buff[1024];

    int p_id;
    pp=pipe(fd);

    if(pp<0)
    printf("Pipe creation failed");
    else
    {
    p_id = fork();
    if(p_id < 0)
    printf("Fork Failed");
    if(p_id == 0)
    {
    close(fd[0]);
    write(fd[1],string,strlen(string));
    read(fd2[0],buff2,strlen(string2));

    exit(7);
    }
    else
    {
    wait(&status);
    close(fd[1]);
    read(fd[0],buff,strlen(string));
    printf("BUFFER : ");
    printf(buff);

    }


    }

    return 0;
    }


    What i want is to create a full duplex pipe where both child and parent process can read and write.For tht im creating two
    pipes.When i executes the below program with two pipes it get stuck on the command line.Plz tell me where im goin wrong in

    the below code.

    int main()
    {
    int fd[2];
    int fd2[2];
    int pp,pp2;
    int status;
    char *string = "This is my first Pipe creation",buff[1024];
    char *string2 = "Parent writing",buff2[1024];
    int p_id;
    pp=pipe(fd);
    pp2=pipe(fd2);
    if(pp<0)
    printf("Pipe creation failed");
    else
    {
    p_id = fork();
    if(p_id < 0)
    printf("Fork Failed");
    if(p_id == 0)
    {
    close(fd[0]);
    write(fd[1],string,strlen(string));
    read(fd2[0],buff2,strlen(string2));
    printf("buff2");
    exit(7);
    }
    else
    {
    wait(&status);
    close(fd[1]);
    read(fd[0],buff,strlen(string));
    printf("BUFFER : ");
    printf(buff);
    write(fd2[1],string2,strlen(string2));
    }


    }

    return 0;
    }
     
  2. learneros

    Thread Starter Member

    Nov 2, 2007
    14
    0
    I am using cygwin and this is how im passing command line argument

    ./myprog inputfile.txt 4

    And in my program,i am recieving it like
    int main(int argc,char *argv[])
    {

    char *argument1[20];
    argument1 = atoi(argv[2]);
    return 0;
    }

    Im trying to recieve passed file through command line in a char*.But its showing an error.How can i get
    the file name in a char*.Also i dont want to pass the length in char* array as user can input file name of any length.
    Anyone can help ?
     
  3. learneros

    Thread Starter Member

    Nov 2, 2007
    14
    0
    Another issue im facing is,im trying to open and read a file with the following code:

    int main()
    {
    int fp; /* create a pointer to the file */
    char buf[15];
    fp = open("c://cygwin/home/faraz/input.txt", "r");

    read(fp,buf,10);
    printf("descriptor %d ",buf);
    return 0;
    }


    The file path is correct bt each time it shows 2280640 instead of showing content of file.File has
    intergers 123456789 bt what this program output is totally different.I am unable to understand frm where
    these integers coming from.Plz let me knw how can i read a file and store it in a buffer.
     
Loading...