hi friends,
in this program, i m creating a pipe and creating two child process of main which will write and read in this pipe.
no error no warning, but when i m running the a.out, screen is blank, and waiting for somthing, for infinite time. ( its a gcc code)
Code:
1 // create pipe between two processes
2 #include<stdio.h>
3 #include<unistd.h>
4
5 main()
6 {
7 int fd[2];
8 int report[2];
9 int freturn ;
10 freturn = pipe(fd);
11 if(freturn < 0)
12 {
13 fprintf(stderr,"Cant create pipe..\n");
14 exit(1) ;
15 }
16 switch(fork())
17 {
18 case -1 : fprintf(stderr,"Cant fork..\n") ;
19 exit(2) ;
20 case 0 : // fork is successfull
21 close(1) ; //1 is writing fd
22 if(dup(fd[1]) == -1)
23 {
24 fprintf(stdout,"Can't dup..\n") ;
25 perror("dup");
26 exit(2);
27 }
28 if(execlp("ls","ls", "-l","-a", NULL) == -1)
29 {
30 fprintf(stderr,"Cant exec..\n") ;
31 exit(3) ;
32 }
33 break ;
34 }// end of switch1
35
36 switch(fork())
37 {
38 case -1 :fprintf(stderr,"Cant fork..\n") ;
39 exit(4) ;
40 case 0 : //fork is successfull
41 close(0);
42 if(dup(fd[0]) == -1)
43 {
44 fprintf(stderr,"Cant dup..\n") ;
45 perror("2nd dup") ;
46 exit(5) ;
47 }
48 if(execlp("wc","wc", NULL) == -1 )
49 {
50 fprintf(stderr,"Cant exec..\n") ;
51 exit(6) ;
52 }
53 break ;
54 } // end of switch 2
55
56 wait(&report[0]) ;
57 wait(&report[1]) ;
58 } // end of main
59