Article View: es.comp.os.linux.programacion
Article #2Re: controlar un programa desde otro
From: J.A. Gutierrez
Date: Tue, 08 Oct 2002 12:08
Date: Tue, 08 Oct 2002 12:08
107 lines
3585 bytes
3585 bytes
In es.comp.os.linux.programacion Claudio S. <claudio@ningun.sitio.es> wrote:
:>
:> Usando pipe() genero un par de descriptores (in[0] e in[1]), de
:> forma que yo leo de in[0] lo que el programa escribe en in[1].
:>
:> De igual forma, con otro pipe() hago out[0] y out[1] de forma que
:> lo que yo escribo en out[1] lo lee el programa en out[0].
si te entendido bien, lo que necesitas es el comando "69" :
-8<--- 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);
}
-8<------------------------------------------------------------------------
--
PGP and other useless info at \
http://www.cps.unizar.es/~spd/ \
finger://daphne.cps.unizar.es/spd \ Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/ \ (Virgilio)
Technical Details
Message-ID:
<anuhra$7o6$2@news.unizar.es>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!propagator2-maxim!news-in.spamkiller.net!news.telia-iberia.com!news.rediris.es!news.unizar.es!not-for-mailReferences:
<Pine.LNX.4.44.0210021725480.3140-100000@lorien.local> <slrnapmmki.ece.claudio@nntp.tralari.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f4463,f557a5b44ea0d9c4
X-Google-Attributes: gidf4463,public
X-Google-Thread: f86c8,f557a5b44ea0d9c4
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2002-10-08 04:29:10 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!propagator2-maxim!news-in.spamkiller.net!news.telia-iberia.com!news.rediris.es!news.unizar.es!not-for-mail
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Newsgroups: es.comp.os.linux.programacion,es.comp.hackers
Subject: Re: controlar un programa desde otro
Date: 8 Oct 2002 12:08:10 GMT
Organization: Rank
Lines: 107
Message-ID: <anuhra$7o6$2@news.unizar.es>
References: <Pine.LNX.4.44.0210021725480.3140-100000@lorien.local> <slrnapmmki.ece.claudio@nntp.tralari.com>
NNTP-Posting-Host: daphne.cps.unizar.es
X-Trace: news.unizar.es 1034078890 7942 155.210.152.20 (8 Oct 2002 12:08:10 GMT)
X-Complaints-To: usenet@news.unizar.es
NNTP-Posting-Date: 8 Oct 2002 12:08:10 GMT
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (IRIX/6.2 (IP22))
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (IRIX/6.2 (IP22))