Codice: Seleziona tutto
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
static void consumer (int *pipeIN, int *pipeOUT);
static void producer (int *pipeIN, int *pipeOUT);
int main () {
pid_t pidProd, pidCons;
int fd1[2], fd2[2];
if (signal (SIGUSR1, signalHandler) == SIG_ERR) {
fprintf (stderr, "Signal Handler Error.\n");
return 1;
}
fflush (stdout);
pidCons = fork ();
pipe (fd1);
pipe (fd2);
fflush (stdin);
fflush (stdout);
if (pidCons == 0) {
consumer (fd1, fd2);
} else {
pidProd = fork ();
if (pidProd == 0) {
producer (fd2, fd1);
}
}
wait ((int *) 0);
wait ((int *) 0);
remove (TMPFILE);
return EXIT_SUCCESS;
}
static void producer (int *pipeIN, int *pipeOUT) {
char buffer[1024];
int flag;
flag = 1;
close (pipeIN[1]);
close (pipeOUT[0]);
while (flag) {
fprintf (stdout, "Producer: ");
fflush (stdout);
scanf ("%s", buffer);
write (pipeOUT[1], buffer, strlen (buffer)+1);
read (pipeIN[0], &flag, sizeof (int));
}
close (pipeIN[0]);
exit (0);
}
static void consumer (int *pipeIN, int *pipeOUT) {
char buffer[1024];
int flag, i;
flag = 1;
close (pipeIN[1]);
close (pipeOUT[0]);
read (pipeIN[0], buffer, 1024);
while (flag) {
if (strcmp (buffer, "end") == 0) {
flag = 0;
close (pipeIN[0]);
} else {
for (i = 0; i < strlen (buffer); i++) {
buffer[i] = toupper (buffer[i]);
}
fprintf (stdout, "Consumer: %s\n", buffer);
fflush (stdout);
}
write (pipeOUT[1], &flag, sizeof (int));
read (pipeIN[0], buffer, 1024);
}
exit (0);
}
Codice: Seleziona tutto
Consumer: T
Producer:
E ovviamente non funziona.
Mi sapete dare una mano?