<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=Esercizio_1%2C_prova_pratica_21.02.2015</id>
	<title>Esercizio 1, prova pratica 21.02.2015 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=Esercizio_1%2C_prova_pratica_21.02.2015"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizio_1,_prova_pratica_21.02.2015&amp;action=history"/>
	<updated>2026-05-05T05:33:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizio_1,_prova_pratica_21.02.2015&amp;diff=949&amp;oldid=prev</id>
		<title>Stefano.zaniboni: Created page with &quot;&lt;source lang=&quot;text&quot;&gt; Scrivere un programma C di nome filepipe che abbia come unico parametro il pathnae di un file di testo. Questo file contiene due comandi con I rispettivi ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Esercizio_1,_prova_pratica_21.02.2015&amp;diff=949&amp;oldid=prev"/>
		<updated>2015-04-02T15:08:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt; Scrivere un programma C di nome filepipe che abbia come unico parametro il pathnae di un file di testo. Questo file contiene due comandi con I rispettivi ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Scrivere un programma C di nome filepipe che abbia come unico parametro il pathnae di un file di testo.&lt;br /&gt;
Questo file contiene due comandi con I rispettivi parametri, uno per riga.&lt;br /&gt;
Il programma deve mettere in esecuzione concorrente I due comandi in modo che l'output del primo venga fornito come input del secondo usando una pipe.&lt;br /&gt;
Il programma deve terminare quando entrambi I comandi sono terminati.&lt;br /&gt;
Esempio: se il file ffff contiene:&lt;br /&gt;
ls -l&lt;br /&gt;
tac&lt;br /&gt;
il comando:&lt;br /&gt;
      filepipe ffff&lt;br /&gt;
deve restituire lo stesso output del comando:&lt;br /&gt;
ls -l | tac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
==Soluzione di Stefano.zaniboni==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;error.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
   int mypipe[2];&lt;br /&gt;
   FILE *stream;&lt;br /&gt;
   char *line=NULL;&lt;br /&gt;
   size_t len=0;&lt;br /&gt;
   ssize_t read;&lt;br /&gt;
&lt;br /&gt;
   /*controllo il numero degli argomenti passati*/&lt;br /&gt;
   if(argc != 2){&lt;br /&gt;
      fprintf(stderr, &amp;quot;no such arguments \n&amp;quot;);&lt;br /&gt;
      exit(1);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   /*apro il file in sola lettura*/&lt;br /&gt;
   stream=fopen(argv[1], &amp;quot;r&amp;quot;);&lt;br /&gt;
   if(stream==NULL){&lt;br /&gt;
      exit(EXIT_FAILURE);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   /*apro la pipe*/&lt;br /&gt;
   if(pipe(mypipe) == -1){&lt;br /&gt;
      fprintf(stderr, &amp;quot;Error pipe\n&amp;quot;);&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   if(fork()==0){&lt;br /&gt;
      close(1); /*chiudo lo stdout*/&lt;br /&gt;
      dup(mypipe[1]); /*rimpiazzo lo stdout con la pipe*/&lt;br /&gt;
      close(mypipe[0]); /*chiudo la read della pipe*/&lt;br /&gt;
      close(mypipe[1]);&lt;br /&gt;
&lt;br /&gt;
      read=getline(&amp;amp;line, &amp;amp;len, stream);&lt;br /&gt;
&lt;br /&gt;
      char *lines[2]={line, NULL};&lt;br /&gt;
      execvp(argv[1], lines);&lt;br /&gt;
      perror(&amp;quot;execvp failed&amp;quot;);&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   if(fork()==0){&lt;br /&gt;
      close(0); /*chiudo lo stdin*/&lt;br /&gt;
      dup(mypipe[0]); /*rimpiazzo lo stdin con la pipe read*/&lt;br /&gt;
      close(mypipe[1]); /*chiudo la write della pipe*/&lt;br /&gt;
      close(mypipe[0]);&lt;br /&gt;
&lt;br /&gt;
      read=getline(&amp;amp;line, &amp;amp;len, stream);&lt;br /&gt;
      execvp(argv[1], &amp;amp;line);&lt;br /&gt;
      perror(&amp;quot;execvp 2 failed&amp;quot;);&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   close(mypipe[0]);&lt;br /&gt;
   close(mypipe[1]);&lt;br /&gt;
   wait(0);&lt;br /&gt;
   wait(0);&lt;br /&gt;
   free(line);&lt;br /&gt;
   fclose(stream);&lt;br /&gt;
   exit (0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il codice ho provato a testarlo ma la shell mi restituisce sempre su entrambe le exec &amp;quot;Permission Denied&amp;quot;.&lt;/div&gt;</summary>
		<author><name>Stefano.zaniboni</name></author>
	</entry>
</feed>