Re: Help on pipe

"Ardhan Madras" <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
My demo can run. i use -Wall flag, the "const char *argv[2] = {
"sort", NULL }" as you said warned with:

pipe.c:35: warning: passing argument 2 of 'execve' from incompatible
pointer type.

but it's perfect for  "char * const argv[2] = { "sort", NULL }"

and i define "int ret" for both process, but i undestand forking
mechanism on GNU/Linux.

sorry i don't write the code cleanly.. My point is only to make the
"sort" working, im now understand that "sort" will always wait for
input until it's end (peer closed the input).

Thank you very much.



2009/7/10 MichaÅ‚ Nazarewicz <[email protected]>:
> On Fri, 10 Jul 2009 08:22:54 +0200, Ardhan Madras <[email protected]>
> wrote:
>
>> #include <sys/types.h>
>> #include <sys/wait.h>
>> #include <unistd.h>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main(void)
>> {
>>  pid_t pid;
>>  int fds[2], ret;
>>  const char *sort = "/usr/bin/sort";
>
> "static const char sort[]" would be better IMO.
>
>>  char *argv[2] = { "sort", NULL };
>
> "static const char *argv[]" is definitly better here and does not produce
> warnings.  Your code discards "const" from pointer (a string literal).
>
>>  ret = pipe(fds);
>>  if (ret == -1) {
>>    perror("pipe");
>>    return -1;
>>  }
>>  pid = fork();
>>  if (pid == -1) {
>>    perror("fork");
>>    return -1;
>>  }
>
> Personally I preffer something like:
>
>  switch (fork()) {
>  case -1: /* error */  break;
>  case  0: /* child */  break;
>  default: /* parent */
>  }
>
> but suit yourself.
>
>>  if (pid == 0) {
>>    int ret;
>
> No need to define this variable here.  You already have such variable.
>
>>
>>    close(fds[1]);
>>    dup2(fds[0], STDIN_FILENO);
>>
>>    ret = execve(sort, argv, NULL);
>>    if (ret == -1) {
>>      perror("execve");
>>    }
>>    close(fds[0]);
>>    _exit(0);
>
> No need to close(), and _exit(0) should be _exit(1) (or -1 if you will)
> since the chiled finished with error.
>
>>  }
>>  else {
>>    FILE *stream;
>>
>>    close(fds[0]);
>>    stream = fdopen(fds[1], "w");
>>    if (!stream) {
>>      perror("fdopen");
>>      return -1;
>>    }
>>    fprintf(stream, "this\n");
>>    fprintf(stream, "means\n");
>>    fprintf(stream, "war\n");
>>    fflush(stream);
>
> And here, replace fflush() with fclose().  It seems, data is buffered on
> kernel level, not only libc level and fflush() cannot handle that.
>
>>    waitpid(pid, &ret, 0);
>>    close(fds[1]);
>
> No need to close.
>
>>  }
>>  return 0;
>> }
>
> --
> Best regards,                                            _     _
>  .o. | Liege of Serenly Enlightened Majesty of         o' \,=./ `o
>  ..o | Computer Science,  Michał "mina86" Nazarewicz      (o o)
>  ooo +-<[email protected]>-<[email protected]>-ooO--(_)--Ooo--
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html




_____________________________________________________________
Listen to KNAC, Hit the Home page and Tune In Live! ---> http://www.knac.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.