Article View: es.comp.os.linux.programacion
Article #18243Re: Pipes
From: J.A. Gutierrez
Date: Thu, 23 Sep 1999 00:00
Date: Thu, 23 Sep 1999 00:00
114 lines
3731 bytes
3731 bytes
Juan Salvador Perez Garcia <jspgxx@teleline.es> wrote:
: ADnoctum <adnoctum@graffiti.net> escribi๏ฟฝ en el mensaje de noticias
: 7rh37t$vj6$1@batcher.rediris.es...
:> En el shel yo puedo hacer algo como
:>
:> $ miprograma | less
:>
: Buff!, tendre que desempolvar mis conocimientos sobre S.O. Creo que era algo
: asi:
: /* Desde tu programa */
: int pid; /* Para el fork -> Identificador de proceso */
: int fd[2]; /* Para el pipe -> Cada entero es un identificador de fichero. 0
: es para leer y 1 es para
: escribir ( o viceversa, ya te he dicho que no me acuerdo
: muy bien */
Un ejemplo curioso que hice en tiempos:
-69.c----------------------------------------------------------------------
/*
* (c) SPDsoft 1994
* 24 julio 1994
*
* Launch two processes:
* output cmd[0] -> input cmd[1]
* input cmd[0] <- output cmd[1]
*/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
main(int argc, char **argv)
{
pid_t pid;
int pipefd[2][2];
int i;
char **cmd[2];
extern int errno;
char strerr[256];
cmd[0] = &argv[1];
cmd[1] = NULL;
for(i=1; ( i<argc)&&(strcmp(argv[i],";")!=0); i ++)
;
if ( i < argc -1 )
{
argv[i] = (char*) 0;
cmd[1] = &argv[i+1];
}
if (( argc < 4 ) || ( cmd[1] == NULL ))
{
sprintf(strerr,
"%s: SPDsoft 1994: Sixty-nine two processes\n"
"Usage: %s cmd1 [args] ; cmd2 [args]\n",
argv[0] , argv[0]);
write(2, strerr, strlen(strerr));
exit(-1);
}
pipe (pipefd[0]);
pipe (pipefd[1]);
if ((pid = fork()) == (pid_t)0)
{
close(0);
close(1);
dup (pipefd[1][0]);
dup (pipefd[0][1]);
close (pipefd[0][0]);
close (pipefd[1][1]);
execvp(*cmd[0], cmd[0]);
sprintf(strerr,"%s: Can't execvp %s (%s)\n",
argv[0], *cmd[0], strerror(errno));
write(2, strerr, strlen(strerr));
exit(errno);
}
else if (pid > (pid_t)0)
{
close(0);
close(1);
dup (pipefd[0][0]);
dup (pipefd[1][1]);
close (pipefd[1][0]);
close (pipefd[0][1]);
execvp(*cmd[1], cmd[1]);
sprintf(strerr,"%s: Can't execvp %s (%s)\n",
argv[0], *cmd[1], strerror(errno));
write(2, strerr, strlen(strerr));
exit(errno);
}
else
perror("fork");
exit(errno);
}
---------------------------------------------------------------------------
--
PGP and other useless info at \ Oh, show us the way
http://www.cps.unizar.es/~spd/ \ to the next whisky bar
finger://daphne.cps.unizar.es/spd \ Weill/Bretch
Lo que si que es cierto es que la felicidad no da el dinero...
Technical Details
Message-ID:
<7scqc0$n59$2@posta2.unizar.es>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7rh37t$vj6$1@batcher.rediris.es> <7sacpi$1vn$1@talia.mad.ttd.net>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,5846027b06c115db
X-Google-Attributes: gidf86c8,public
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Subject: Re: Pipes
Date: 1999/09/23
Message-ID: <7scqc0$n59$2@posta2.unizar.es>
X-Deja-AN: 528574373
Content-Transfer-Encoding: 8bit
References: <7rh37t$vj6$1@batcher.rediris.es> <7sacpi$1vn$1@talia.mad.ttd.net>
Content-Type: text/plain; charset=ISO-8859-1
X-Complaints-To: usenet@news.unizar.es
X-Trace: posta2.unizar.es 938077376 23721 155.210.29.12 (23 Sep 1999 09:02:56 GMT)
Organization: Rank
Mime-Version: 1.0
User-Agent: tin/pre-1.4-19990413 ("Endemoniada") (UNIX) (IRIX/6.2 (IP22))
NNTP-Posting-Date: 23 Sep 1999 09:02:56 GMT
Newsgroups: es.comp.os.linux.programacion