penso che ti serva questo =)
codice:/* * Esercizio: scrivere un programma che esegua il comando di shell “ls| sort| grep<pat>” * con tre processi distinti. */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h> #include<errno.h> #define READ 0 #define WRITE 1 //AVENDO DEFINITO LE pipe ALL' INIZIO OGNI PROCESSO ANCHE SE NN LE UTILIZZA DEVE CHIUDERLE int main(int argc, char *argv[]) { pid_t pid_0, pid_1, pid_2; int fd[2], fd2[2]; pipe(fd); // CREO UNA PIPE TRA PID0 e PID1 pipe(fd2); // CREO UNA PIPE TRA PID1 e PID2 if (pid_0 = fork() == 0) { // Creo pid 0 è fa "LS" close(fd2[READ]); close(fd2[WRITE]); close(fd[READ]); dup2(fd[WRITE], 1); close(fd[WRITE]); execlp("ls", "ls", NULL); } if (pid_1 = fork() == 0) { // Creo pid 1 è fa "SORT" close(fd[WRITE]); dup2(fd[READ], 0); close(fd[READ]); close(fd2[READ]); dup2(fd2[WRITE], 1); close(fd2[WRITE]); execlp("sort", "sort", NULL); } if (pid_2 = fork() == 0) { // Creo pid 2 è fa "Grep" close(fd[READ]); close(fd[WRITE]); close(fd2[WRITE]); dup2(fd2[READ], 0); close(fd2[READ]); execlp("grep", "grep", argv[1], NULL); } close(fd[READ]); close(fd2[READ]); close(fd[WRITE]); close(fd2[WRITE]); printf("Programma terminato con successo\n"); exit(0); }

Rispondi quotando