Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* test.c */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main()
- {
- pid_t child;
- int status;
- int pfd[2];
- if (pipe(pfd) != -1)
- {
- char pipe_readend[12]; /* put file descriptor into c-string to be passed */
- sprintf(pipe_readend, "%i", pfd[0]);
- char * arg[2] = {pipe_readend, NULL};
- char message[2] = {'h', 'i'};
- switch(child=fork())
- {
- case -1:
- printf("Error with fork");
- break;
- case 0:
- /* child actions */
- close(pfd[1]); /* close write end */
- execve("test2", arg, NULL); /* start new process */
- break;
- default:
- /* parent actions */
- close(pfd[0]); /* close read end */
- write(pfd[1], message, 2);
- close(pfd[1]); /* close write end */
- waitpid(child, &status, 0); /* wait for child to exit */
- break;
- }
- }
- return 0;
- }
- /* test2.c */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main(int argc, char * argv[])
- {
- int read_end = atoi(argv[0]);
- char message[2];
- read(read_end, message, 2);
- printf("%s", message);
- close(read_end);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement