-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_test.c
More file actions
147 lines (132 loc) · 3.77 KB
/
fork_test.c
File metadata and controls
147 lines (132 loc) · 3.77 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
/* Fork test with tracker change
int main(void)
{
pid_t pid;
int rv;
int tracker = 5;
switch(pid = fork()) {
case -1:
perror("fork");
exit(1);
case 0:
printf("CHILD: This is the child process!\n");
printf("CHILD: My PID is %d.\n", getpid());
printf("CHILD: My parent's PID is %d.\n", getppid());
printf("CHILD: My tracker is %d.\n", tracker);
printf(" CHILD: Enter my exit status (make it small): ");
scanf(" %d", &rv);
printf(" CHILD: I'm outta here!\n");
exit(rv);
default:
printf("PARENT: This is the parent process!\n");
printf("PARENT: My PID is %d\n", getpid());
printf("PARENT: My child's PID is %d\n", pid);
printf("PARENT: My tracker is %d.\n", tracker);
printf("PARENT: I'm now waiting for my child to exit()...\n");
wait(&rv);
tracker = WEXITSTATUS(rv);
printf("PARENT: My tracker is now %d.\n", tracker);
printf("PARENT: My child's exit status is: %d\n", WEXITSTATUS(rv));
printf("PARENT: I'm outta here!\n");
}
return 0;
}
* Use child_process(), parent_process() to separate the two.
*/
/*
int main(void)
{
int i;
int fd;
int result;
int *map; *//* mmapped array of int's */
/* Open a file for writing.
* - Creating the file if it doesn't exist.
* - Truncating it to 0 size if it already exists. (not really needed)
*
* Note: "O_WRONLY" mode is not sufficient when mmaping.
*/
/*
fd = open("test", O_RDWR);
if (fd == -1) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
*//* Stretch the file size to the size of the (mmapped) array of ints
*/
/*
result = lseek(fd, FILESIZE-1, SEEK_SET);
if (result == -1) {
close(fd);
perror("Error calling lseek() to 'stretch' the file");
exit(EXIT_FAILURE);
}
*/
/* Something needs to be written at the end of the file to
* have the file actually have the new size.
* Just writing an empty string at the current file position will do.
*
* Note:
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - An empty string is actually a single '\0' character, so a zero-byte
*/
#define NUMINTS (1000)
#define FILESIZE (NUMINTS * sizeof(int))
int main(void)
{
int fd;
char* map;
fd = open("test", O_RDWR | O_CREAT);
//File must be stretched to a suitable size for the mmap array.
lseek(fd, FILESIZE-1, SEEK_SET);
//Something must be written to end of file to give it its new size.
write(fd, "", 1);
map = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
pid_t pid;
int rv;
int val1, val2, val3;
switch(pid = fork()) {
case -1:
perror("fork() has not worked!");
exit(1);
case 0:
printf("CHILD: I am the child!\n");
printf("CHILD: My PID is %d.\n", getpid());
printf("CHILD: First value for mmap?\n");
scanf(" %d", &val1);
printf("CHILD: Second value for mmap?\n");
scanf(" %d", &val2);
printf("CHILD: Third value for mmap?\n");
scanf(" %d", &val3);
map[0] = val1;
map[1] = val2;
map[2] = val3;
printf("CHILD: Enter my return status: ");
scanf(" %d", &rv);
printf("CHILD: I'm done!\n");
break;
default:
printf("PARENT: I am the parent!\n");
printf("PARENT: My PID is %d.\n", getpid());
wait(&rv);
printf("PARENT: My child's return value was %d.\n", rv);
printf("PARENT: My child set the following values:\n");
printf("PARENT: map[0] is %d.\n", map[0]);
printf("PARENT: map[1] is %d.\n", map[1]);
printf("PARENT: map[2] is %d.\n", map[2]);
printf("PARENT: I'm done!\n");
break;
}
return 0;
}