Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CPE334
- // PROBLEM SESSION 1: Process Creation & Pipes
- // Created by Warat Kaweepornpoj on 1/24/2560 BE.
- // 57070501038
- //
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <string.h>
- int main(int argc, const char * argv[]) {
- int fd[2];
- pid_t child_pid, pid;
- char send[30], recv[30];
- pipe(fd);
- child_pid = fork();
- pid = getpid();
- // Problem 1 - Hello from Child
- if(child_pid==0){
- printf("Parent %d\n", pid);
- read(fd[0], &recv, sizeof(recv));
- printf("%s\n", recv);
- } else {
- sprintf(send, "Hello from %d", pid);
- write(fd[1], &send, strlen(send)+1);
- printf("Child %d\n", pid);
- }
- close(fd[0]);
- close(fd[1]);
- // Problem 2 - Hello from Parent
- if(child_pid==0){
- printf("Parent %d\n", pid);
- sprintf(send, "Hello from %d", pid);
- write(fd[1], &send, strlen(send)+1);
- } else {
- printf("Child %d\n", pid);
- read(fd[0], &recv, sizeof(recv));
- printf("%s\n", recv);
- }
- close(fd[0]);
- close(fd[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement