MEMon
06-03-2008, 14:12
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #define TRUE 1
6 #define FALSE 0
7 #define ERROR -1
8 #define PERM 0644 //Definisce i permessi per la creazione dei file:rw per l'user e r per il gruppo e gli altri
9 #define N_ARGS 2 //Numero di argomenti atteso
10 #define MSGSIZE 20
11
12 void error(char * error, int ex_value){
13 perror(error);
14 exit(ex_value);
15 }
16
17 int checkArgs(int n){
18 if(n-1!=N_ARGS) return ERROR;
19 return TRUE;
20 }
21
22
23 main(int argc, char *argv[]){
24
25 int nr,wr,piped[2];
26 int pid, ppid;
27 int cycle;
28 char buffer[MSGSIZE];
29
30 if((checkArgs(argc))<0) error("Numero argomenti inatteso", 1);
31 if(pipe(piped)<0) error("Errore pipe: ", 2);
32 if((pid=fork())<0) error("Errore fork: ",3);
33 cycle=atoi(argv[2])+1;
34 if(pid==0){
35 //------- FIGLIO --------
36 ppid=getppid();
37 close(piped[1]); //Figlio: chiusura lato scrittura
38 while((nr=read(piped[0],buffer,MSGSIZE))>0){
39 printf("Ricevuto: %s\n", buffer);
40 }
41 printf("PIPE senza scrittore: exit\n");
42 exit(0);
43 }
44 else{
45 //------- PADRE ---------
46 close(piped[0]); //Padre: chiusura lato lettura
47 while(cycle--) write(piped[1],argv[1],MSGSIZE);
48 close(piped[1]);
49 exit(0);
50 }
51 }
In teoria il programma dovrebbe terminare quando ANCHE il padre chiude il lato di scrittura della pipe. In quel momento la read dovrebbe "sbloccarsi".
Funziona, cioà mi stampa a video "PIPE senza scrittore: exit" ma sembra però che non termini realmente finchè non premo INVIO.
Da cosa è dato questo comportamento? C'è qualcosa di strano nel codice?
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #define TRUE 1
6 #define FALSE 0
7 #define ERROR -1
8 #define PERM 0644 //Definisce i permessi per la creazione dei file:rw per l'user e r per il gruppo e gli altri
9 #define N_ARGS 2 //Numero di argomenti atteso
10 #define MSGSIZE 20
11
12 void error(char * error, int ex_value){
13 perror(error);
14 exit(ex_value);
15 }
16
17 int checkArgs(int n){
18 if(n-1!=N_ARGS) return ERROR;
19 return TRUE;
20 }
21
22
23 main(int argc, char *argv[]){
24
25 int nr,wr,piped[2];
26 int pid, ppid;
27 int cycle;
28 char buffer[MSGSIZE];
29
30 if((checkArgs(argc))<0) error("Numero argomenti inatteso", 1);
31 if(pipe(piped)<0) error("Errore pipe: ", 2);
32 if((pid=fork())<0) error("Errore fork: ",3);
33 cycle=atoi(argv[2])+1;
34 if(pid==0){
35 //------- FIGLIO --------
36 ppid=getppid();
37 close(piped[1]); //Figlio: chiusura lato scrittura
38 while((nr=read(piped[0],buffer,MSGSIZE))>0){
39 printf("Ricevuto: %s\n", buffer);
40 }
41 printf("PIPE senza scrittore: exit\n");
42 exit(0);
43 }
44 else{
45 //------- PADRE ---------
46 close(piped[0]); //Padre: chiusura lato lettura
47 while(cycle--) write(piped[1],argv[1],MSGSIZE);
48 close(piped[1]);
49 exit(0);
50 }
51 }
In teoria il programma dovrebbe terminare quando ANCHE il padre chiude il lato di scrittura della pipe. In quel momento la read dovrebbe "sbloccarsi".
Funziona, cioà mi stampa a video "PIPE senza scrittore: exit" ma sembra però che non termini realmente finchè non premo INVIO.
Da cosa è dato questo comportamento? C'è qualcosa di strano nel codice?