Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BUFF_SIZE 255
- int main(int ac, char* av[])
- {
- if(ac <3)
- {
- printf("Please enter all required arguments!n");
- exit(0);
- }
- int pfd[2];
- int pipeCreated;
- char readFile[50];
- char writePipe[20];
- pid_t child_pid_read;
- pid_t child_pid_write;
- pipeCreated = pipe(pfd);
- if(pipeCreated == -1)
- {
- printf("An error occurred when trying to create a pipen");
- exit(0);
- }
- strcpy(readFile, av[1]);
- sprintf(writePipe,"%d", pfd[1]);
- child_pid_read = fork();
- char writeFile[50];
- char readPipe[20];
- //Handling the read()
- switch(child_pid_read)
- {
- //Error in case forfk() failed
- case -1:
- perror("fork failed");
- return 1;
- //Handle child processes
- case 0:
- if(close(pfd[0]) == -1)
- {
- printf("An error occurred while closing the pipen");
- exit(0);
- }
- if(execle("./read.out", "./read.out", readFile, writePipe, (char*)0, NULL) == -1)
- {
- printf("Child: Error creating read.n");
- exit(0);
- }
- default:
- wait(&child_pid_read);
- strcpy(writeFile, av[2]);
- sprintf(readPipe,"%d", pfd[0]);
- child_pid_write = fork();
- break;
- }
- //Handling the write
- switch(child_pid_write)
- {
- //Error in case fork() failed
- case -1:
- perror("fork failed");
- return 1;
- //Handle child processes
- case 0:
- if(close(pfd[1]) == -1)
- {
- printf("An error occurred while closing the pipen");
- exit(0);
- }
- if(execle("./write.out", "./write.out", writeFile, readPipe, (char*)0, NULL) == -1)
- {
- printf("Child: Error creating read.n");
- exit(-1);
- }
- break;
- default:
- wait(&child_pid_write);
- break;
- }
- printf("Write completed!");
- return 0;
- }
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define BUFF_SIZE 16
- int main(int ac, char* av[])
- {
- char buffer[BUFF_SIZE];
- int fd;
- int pid;
- if(ac > 1)
- {
- fd = open(av[1], O_RDONLY);
- if(fd == -1)
- {
- printf("error: Could Not Open Filen");
- exit(0);
- }
- pid = atoi(av[2]);
- }
- int num_read = 1;
- while(1)
- {
- num_read = read(fd, buffer, BUFF_SIZE);
- if(num_read == -1)
- {
- printf("Error reading filen");
- exit(0);
- }
- if(num_read == 0)
- {
- break;
- }
- if(write(pid, buffer, num_read) != num_read)
- {
- printf("Error writing to pipen");
- break;
- }
- }
- close(fd);
- return 1;
- }
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define BUFF_SIZE 1
- int main(int ac, char* av[])
- {
- char buffer[BUFF_SIZE];
- int fd = open(av[1], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
- int pid = atoi(av[2]);
- int num_read = 1;
- while(1)
- {
- num_read = read(pid, buffer, BUFF_SIZE);
- printf("num_read: %dn", num_read);
- if(num_read == -1)
- {
- printf("Error reading pipen");
- break;
- }
- if(write(fd, buffer, num_read) != num_read)
- {
- printf("Error writing to filen");
- break;
- }
- if(num_read == EOF)
- {
- break;
- }
- }
- close(fd);
- return 1;
- }
- default:
- if(close(pfd[0]) == -1)
- {
- printf("An error occurred while closing the pipen");
- exit(0);
- }
- wait(&child_pid_write);
- break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement