pipe example problem - Linux

This is a discussion on pipe example problem - Linux ; /* test pipe */ #include #include #include #define MAXLINE 80 int main() { int n,fd[2]; pid_t pid; char line[MAXLINE]; if(pipe(fd) perror(strerror(errno)); } if((pid=fork()) perror(strerror(errno)); }else if (pid>0){ close(fd[0]); write(fd[1],"hello, world\n",13); }else{ close(fd[1]); n=read(fd[0],line,MAXLINE); write(STDOUT_FILENO,line,n); } return 0; } a. In ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: pipe example problem

  1. pipe example problem

    /* test pipe */

    #include
    #include
    #include

    #define MAXLINE 80

    int main()
    {
    int n,fd[2];
    pid_t pid;
    char line[MAXLINE];

    if(pipe(fd)<0){
    perror(strerror(errno));
    }

    if((pid=fork())<0){
    perror(strerror(errno));
    }else if (pid>0){
    close(fd[0]);
    write(fd[1],"hello, world\n",13);
    }else{
    close(fd[1]);
    n=read(fd[0],line,MAXLINE);
    write(STDOUT_FILENO,line,n);
    }

    return 0;
    }

    a. In bash it displays the string, but in eshell in emacs it doesn't. Why?

    b. I haven't include string.h, where is the declaration of strerror?

  2. Re: pipe example problem

    Ronald wrote:
    > /* test pipe */
    >
    > #include
    > #include
    > #include
    >
    > #define MAXLINE 80
    >
    > int main()
    > {
    > int n,fd[2];
    > pid_t pid;
    > char line[MAXLINE];
    >
    > if(pipe(fd)<0){
    > perror(strerror(errno));
    > }
    >
    > if((pid=fork())<0){
    > perror(strerror(errno));
    > }else if (pid>0){
    > close(fd[0]);
    > write(fd[1],"hello, world\n",13);
    > }else{
    > close(fd[1]);
    > n=read(fd[0],line,MAXLINE);
    > write(STDOUT_FILENO,line,n);
    > }
    >
    > return 0;
    > }
    >
    > a. In bash it displays the string, but in eshell in emacs it doesn't. Why?


    Try some error checking on your read and write.
    Don't know anything about eshell. Does it draw over the screen once your
    program is finished?

    >
    > b. I haven't include string.h, where is the declaration of strerror?


    string.h

  3. Re: pipe example problem

    Joe Beanfish wrote:
    > Ronald wrote:
    >> /* test pipe */
    >>
    >> #include
    >> #include
    >> #include
    >>
    >> #define MAXLINE 80
    >>
    >> int main()
    >> {
    >> int n,fd[2];
    >> pid_t pid;
    >> char line[MAXLINE];
    >>
    >> if(pipe(fd)<0){
    >> perror(strerror(errno));
    >> }
    >>
    >> if((pid=fork())<0){
    >> perror(strerror(errno));
    >> }else if (pid>0){
    >> close(fd[0]);
    >> write(fd[1],"hello, world\n",13);
    >> }else{
    >> close(fd[1]);
    >> n=read(fd[0],line,MAXLINE);
    >> write(STDOUT_FILENO,line,n);
    >> }
    >>
    >> return 0;
    >> }
    >> I haven't include string.h, where is the declaration of strerror?

    >
    > string.h


    How can it be work?
    I checked the included head files, no one include string.h inside.

  4. Re: pipe example problem

    2006-12-15 <4582FA0D.7060206@163.com>,
    Ronald wrote:
    > Joe Beanfish wrote:
    >> Ronald wrote:
    >>> /* test pipe */
    >>>
    >>> #include
    >>> #include
    >>> #include
    >>>
    >>> #define MAXLINE 80
    >>>
    >>> int main()
    >>> {
    >>> int n,fd[2];
    >>> pid_t pid;
    >>> char line[MAXLINE];
    >>>
    >>> if(pipe(fd)<0){
    >>> perror(strerror(errno));
    >>> }
    >>>
    >>> if((pid=fork())<0){
    >>> perror(strerror(errno));
    >>> }else if (pid>0){
    >>> close(fd[0]);
    >>> write(fd[1],"hello, world\n",13);
    >>> }else{
    >>> close(fd[1]);
    >>> n=read(fd[0],line,MAXLINE);
    >>> write(STDOUT_FILENO,line,n);
    >>> }
    >>>
    >>> return 0;
    >>> }
    >>> I haven't include string.h, where is the declaration of strerror?

    >>
    >> string.h

    >
    > How can it be work?
    > I checked the included head files, no one include string.h inside.


    perror(strerror(errno)) is a bad usage of it anyway. perror prints this:

    (argument to perror): (result of strerror(errno))\n

    so you should be doing like perror("program") , to get this output:
    program: Insufficient memory.

    strerror is for if you want to format your own error messages instead of
    putting them to stderr in the above format

  5. Re: pipe example problem

    Random832 wrote:
    > 2006-12-15 <4582FA0D.7060206@163.com>,
    > Ronald wrote:
    >> Joe Beanfish wrote:
    >>> Ronald wrote:
    >>>> /* test pipe */
    >>>>
    >>>> #include
    >>>> #include
    >>>> #include
    >>>>
    >>>> #define MAXLINE 80
    >>>>
    >>>> int main()
    >>>> {
    >>>> int n,fd[2];
    >>>> pid_t pid;
    >>>> char line[MAXLINE];
    >>>>
    >>>> if(pipe(fd)<0){
    >>>> perror(strerror(errno));
    >>>> }
    >>>>
    >>>> if((pid=fork())<0){
    >>>> perror(strerror(errno));
    >>>> }else if (pid>0){
    >>>> close(fd[0]);
    >>>> write(fd[1],"hello, world\n",13);
    >>>> }else{
    >>>> close(fd[1]);
    >>>> n=read(fd[0],line,MAXLINE);
    >>>> write(STDOUT_FILENO,line,n);
    >>>> }
    >>>>
    >>>> return 0;
    >>>> }
    >>>> I haven't include string.h, where is the declaration of strerror?
    >>> string.h

    >> How can it be work?
    >> I checked the included head files, no one include string.h inside.

    >
    > perror(strerror(errno)) is a bad usage of it anyway. perror prints this:
    >
    > (argument to perror): (result of strerror(errno))\n
    >
    > so you should be doing like perror("program") , to get this output:
    > program: Insufficient memory.
    >
    > strerror is for if you want to format your own error messages instead of
    > putting them to stderr in the above format

    Realized, the info is duplicated. I think the two functions (perror and
    strerror) are coupling. However, convenient.
    I still don't know why I needn't include "string.h".

  6. Re: pipe example problem

    Ronald wrote:
    > Random832 wrote:
    >
    >> 2006-12-15 <4582FA0D.7060206@163.com>,
    >> Ronald wrote:
    >>
    >>> Joe Beanfish wrote:
    >>>
    >>>> Ronald wrote:
    >>>>
    >>>>> /* test pipe */
    >>>>>
    >>>>> #include
    >>>>> #include
    >>>>> #include
    >>>>>
    >>>>> #define MAXLINE 80
    >>>>>
    >>>>> int main()
    >>>>> {
    >>>>> int n,fd[2];
    >>>>> pid_t pid;
    >>>>> char line[MAXLINE];
    >>>>>
    >>>>> if(pipe(fd)<0){
    >>>>> perror(strerror(errno));
    >>>>> }
    >>>>>
    >>>>> if((pid=fork())<0){
    >>>>> perror(strerror(errno));
    >>>>> }else if (pid>0){
    >>>>> close(fd[0]);
    >>>>> write(fd[1],"hello, world\n",13);
    >>>>> }else{
    >>>>> close(fd[1]);
    >>>>> n=read(fd[0],line,MAXLINE);
    >>>>> write(STDOUT_FILENO,line,n);
    >>>>> }
    >>>>>
    >>>>> return 0;
    >>>>> }
    >>>>> I haven't include string.h, where is the declaration of strerror?
    >>>>
    >>>> string.h
    >>>
    >>> How can it be work?
    >>> I checked the included head files, no one include string.h inside.

    >>
    >>
    >> perror(strerror(errno)) is a bad usage of it anyway. perror prints this:
    >>
    >> (argument to perror): (result of strerror(errno))\n
    >>
    >> so you should be doing like perror("program") , to get this output:
    >> program: Insufficient memory.
    >>
    >> strerror is for if you want to format your own error messages instead
    >> of putting them to stderr in the above format

    >
    > Realized, the info is duplicated. I think the two functions (perror and
    > strerror) are coupling. However, convenient.
    > I still don't know why I needn't include "string.h".


    Because your compiler did not require you to. It probably defaulted to
    int perror (char *) or some such. However, your linker still
    found an object to link to. In early C (like K&R), there was no
    requirement to prototype procedures as long as you were willing to live
    with the default. What compiler are you using?
    Tim Keck

  7. Re: pipe example problem

    > What compiler are you using?
    gcc, I think I should have the declaration ahead for safety.
    Thanks.



  8. Re: pipe example problem

    Ronald wrote:
    > Random832 wrote:
    >
    >> 2006-12-15 <4582FA0D.7060206@163.com>,
    >> Ronald wrote:
    >>
    >>> Joe Beanfish wrote:
    >>>
    >>>> Ronald wrote:
    >>>>
    >>>>> /* test pipe */
    >>>>>
    >>>>> #include
    >>>>> #include
    >>>>> #include
    >>>>>
    >>>>> #define MAXLINE 80
    >>>>>
    >>>>> int main()
    >>>>> {
    >>>>> int n,fd[2];
    >>>>> pid_t pid;
    >>>>> char line[MAXLINE];
    >>>>>
    >>>>> if(pipe(fd)<0){
    >>>>> perror(strerror(errno));
    >>>>> }
    >>>>>
    >>>>> if((pid=fork())<0){
    >>>>> perror(strerror(errno));
    >>>>> }else if (pid>0){
    >>>>> close(fd[0]);
    >>>>> write(fd[1],"hello, world\n",13);
    >>>>> }else{
    >>>>> close(fd[1]);
    >>>>> n=read(fd[0],line,MAXLINE);
    >>>>> write(STDOUT_FILENO,line,n);
    >>>>> }
    >>>>>
    >>>>> return 0;
    >>>>> }
    >>>>> I haven't include string.h, where is the declaration of strerror?


    > Realized, the info is duplicated. I think the two functions (perror and
    > strerror) are coupling. However, convenient.
    > I still don't know why I needn't include "string.h".


    "string.h" contains the prototype for strerror():

    extern char *strerror (int __errnum) __THROW;

    If you do not include "string.h", the compiler will not know what
    arguments strerror() expects nor which type of value it will return,
    assuming "int" for the latter.

    The former is of little consequence, but the latter is important:

    On x86_64, an int is 32 bits, so without "string.h", the return value of
    strerror() will be truncated:

    without string.h:
    call strerror
    movl %eax, %esi
    with string.h:
    call strerror
    movq %rax, %rsi

    ALso, on a number of architectures, *all* return values are put into the
    same location, irrespective of the fact whether it is a pointer or not.
    On some architectures, however, e.g. on Motorola 68k, the compiler may
    return pointers in one type of register (e.g. A0) and numerics in
    another type of register (e.g. D0).

    When you have no prototype of strerror, the compiler will pick up the
    return value from the wrong location or it will pick up too little.

    So, for your program to run correctly on x86_64 (and maybe on M68k
    derivates), you *must* include "string.h".

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


+ Reply to Thread