Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <unistd.h>
- void checker(bool stmt, const char *usrmsg, const char *file, const char *func, const int line){
- if(!stmt){
- perror(usrmsg);
- fprintf(stderr, "%s %s %d\n", file, func, line);
- exit(EXIT_FAILURE);
- }
- }
- #define osAssert(stmt, usrmsg) checker(stmt,usrmsg,__FILE__, __func__, __LINE__);
- #define PIPE_RD (0)
- #define PIPE_WR (1)
- #define MAX_LINE (128)
- int main(){
- int pipeFD[2];
- osAssert(-1 != pipe(pipeFD), "pipe creation failed");
- pid_t child = fork();
- osAssert(-1 != child, "fork failed");
- if(child>0){
- //parrent
- close(pipeFD[PIPE_RD]);
- printf("Parrent: ");
- char *lineRead = NULL;
- size_t lineReadLen = 0;
- osAssert(-1 != getline(&lineRead, &lineReadLen, stdin), "user read failed");
- osAssert(-1 != write(pipeFD[PIPE_WR], lineRead, lineReadLen), "write failed");
- osAssert(-1 != wait(NULL), "wait failed");
- close(pipeFD[PIPE_WR]);
- } else {
- //kid
- close(pipeFD[PIPE_WR]);
- char buf[MAX_LINE];
- osAssert(-1 != read(pipeFD[PIPE_RD], buf, sizeof buf), "read failed");
- close(pipeFD[PIPE_RD]);
- printf("Child: %s\n", buf);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement