-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_Unnamed_Pipe_Data_Transfer.c
67 lines (54 loc) · 1.4 KB
/
11_Unnamed_Pipe_Data_Transfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Q. Create an unnamed pipe for inter-process communication.
// Accept a string input in the child process and then pass the data
// to parent process to print it.
/*
Note:
pipe():
pipe() function creates a unidirectional pipe for IPC.
On success it return two file descriptors pipefd[0] and pipefd[1].
fd[0] is the reading end of the pipe. So, the process which will receive the data should use this file descriptor.
fd[1] is the writing end of the pipe. So, the process that wants to send the data should use this file descriptor.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int f, fd[2], len;
char data[50], buf[50];
pipe(fd);
f = fork();
// The creation of the process was unsuccessful
if (f < 0)
{
printf("Error occurred!\n");
}
// Child process
else if (f == 0)
{
// For runtime input
char data[50];
printf("Enter the input string: ");
scanf("%[^\n]s", data);
write(fd[1], data, sizeof(data));
printf("Child: Passing the data to Parent.\n");
}
// Parent process
else
{
wait(NULL);
printf("Parent: Data received.\n");
read(fd[0], buf, 50);
printf("Parent saying: %s\n", buf);
}
}
/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 11_Unnamed_Pipe_Data_Transfer.c
s4shibam@SHIBAM:~/OS$ ./a.out
Enter the input string: I am the Data!
Child: Passing the data to Parent.
Parent: Data received.
Parent saying: I am the Data!
s4shibam@SHIBAM:~/OS$
*/