blob: bb59d8d2b367bec239aeadf29318ac6de8379c43 (
plain) (
blame)
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
68
69
70
71
72
|
// -----------------------------------------------------------------------------
// Pipes
//
// Write a program that creates a child process and pipes the string
// "hello child" to the child to be printed. The parent should wait for the
// child to terminate
//
// Template: Implement the TODOs
//
// Expected output:
// Parent Sending: hello child
// Child Received: hello child
// -----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
// Open pipe
// TODO
int ends[2];
pipe(ends);
char string_to_send[] = "hello child";
int bytes_to_send_recv = strlen(string_to_send) + 1;
// Create child process
pid_t pid = fork();
if (pid < 0) {
// Error
fprintf(stderr, "ERROR: Failed to fork\n");
}
else if (pid > 0) {
// Parent
printf("Parent Sending: %s\n", string_to_send);
// Parent doesn't need read file descriptor
// TODO
close(ends[0]);
// Write the string to the pipe
// TODO
write(ends[1], string_to_send, bytes_to_send_recv);
// Done writing
// TODO
close(ends[1]);
// Wait for child to terminate
wait(NULL);
}
else {
// Child
char *recv_buffer = malloc(bytes_to_send_recv);
// Child doesn't need write file descriptor
// TODO
close(ends[1]);
// Read the string from the pipe
// TODO
read(ends[0], recv_buffer, bytes_to_send_recv);
// Done reading
// TODO
close(ends[0]);
// Print result
printf("Child Received: %s\n", recv_buffer);
free(recv_buffer);
}
return 0;
}
|