Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include <sys/types.h>
- #include <strings.h>
- int main (void)
- {
- int pid1, pid2, nbytes, descriptor, flag, cond;
- int d_pipe[2];
- char buffer[80];
- char readbuffer[80];
- pipe(d_pipe);
- pid1 = fork();
- if(pid1 == -1)
- {
- printf("ERROR 404");
- exit(EXIT_FAILURE);
- }
- if(pid1 == 0)
- {
- close(d_pipe[0]);
- if ((descriptor = open ("algunnombre.text", O_CREAT, 0777)) < 0)
- {
- printf("ERROR OPEN");
- exit(EXIT_FAILURE);
- }
- while ((flag = read(descriptor, &buffer, 6)) != 0)
- {
- write(d_pipe[1], buffer, flag);
- //flag = read(descriptor, &buffer, 6);
- //printf("%d", flag);
- }
- sprintf(buffer,"%s\n","fin");
- write(d_pipe[1], buffer, flag);
- close(d_pipe[1]);
- close(descriptor);
- exit(EXIT_SUCCESS);
- }
- else
- {
- pid2 = fork();
- if(pid2 == -1)
- {
- printf("EEROROOROARFSADF");
- exit(EXIT_FAILURE);
- }
- if(pid2 == 0)
- {
- close(d_pipe[1]);
- while ((nbytes = read(d_pipe[0], &readbuffer, 6)) == 6)
- {
- printf("Mira recibiste algo: %s con %d bytes\n", readbuffer,nbytes);
- //nbytes = read(d_pipe[0], readbuffer, sizeof(readbuffer));
- //printf("%d", nbytes);
- }
- printf("Mira recibiste algo: %s con %d bytes\n", readbuffer,nbytes);
- printf("TTTEERRMMIINNEE!!\n");
- close(d_pipe[0]);
- //NO SALE DEL WHILE
- exit(EXIT_SUCCESS);
- }
- waitpid(pid1);
- waitpid(pid2);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement