1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/wait.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <stdio.h>
33
main(int argc,char * argv[])34 int main(int argc, char *argv[]) {
35
36 pid_t cpid;
37 int status;
38 struct rusage usage;
39
40 //----------------------------------------------------
41 //----------------- Wait(); System Call --------------
42 //----------------------------------------------------
43 printf("Testing Wait(); System Call\n");
44 printf("\n");
45
46 cpid = fork(); /* Creates fork */
47 if (cpid == -1) {
48 printf("For has failed, returned = %d\n", EXIT_FAILURE);
49 }
50
51 if (cpid == 0) { /* This is the child operation */
52 printf("Child Created\n");
53 printf("Child = %d\n", getpid());
54 printf("Parent = %d\n", getppid());
55 sleep(2);
56 exit(3);
57
58 } else { /* This is the parent operation */
59 printf("Waiting for child\n");
60 wait(&status);
61 printf("Waiting Complete\n");
62 printf("Child Exit Code: %d\n", WEXITSTATUS(status));
63 }
64
65 printf("\n"); /* Just console space */
66 //----------------------------------------------------
67 //-------------- Waitpid(); System Call --------------
68 //----------------------------------------------------
69 printf("Testing Waitpid(); System Call\n");
70 printf("\n");
71
72
73 cpid = fork(); /* Creates fork */
74 if (cpid == -1) {
75 printf("Fork has failed, returned = %d\n", EXIT_FAILURE);
76 }
77
78 if (cpid == 0) { /* This is the child operation */
79 printf("Child Created\n");
80 printf("Child = %d\n", getpid());
81 printf("Parent = %d\n", getppid());
82 sleep(2);
83 exit(3);
84
85 } else { /* This is the parent operation */
86 printf("Waiting for child %d\n", cpid);
87 waitpid(cpid, NULL, 0);
88 printf("Waiting Complete\n");
89 printf("Child Exit Code: %d\n", WEXITSTATUS(status));
90 }
91
92 printf("\n");
93 //----------------------------------------------------
94 //---------------- Wait3(); System Call --------------
95 //----------------------------------------------------
96 printf("Testing Wait3(); System Call\n");
97 printf("\n");
98
99 cpid = fork(); /* Creates fork */
100 if (cpid == -1) {
101 printf("For has failed, returned = %d\n", EXIT_FAILURE);
102 }
103
104 if (cpid == 0) { /* This is the child operation */
105 printf("Child Created\n");
106 printf("Child = %d\n", getpid());
107 printf("Parent = %d\n", getppid());
108 sleep(2);
109 exit(3);
110
111 } else { /* This is the parent operation */
112 printf("Waiting for child\n");
113 wait3(&status, 0, &usage);
114 printf("Waiting Complete\n");
115 printf("Child Exit Code: %d\n", WEXITSTATUS(status));
116 }
117
118 printf("\n");
119 sleep(1);
120 //----------------------------------------------------
121 //---------------- Wait4(); System Call --------------
122 //----------------------------------------------------
123 printf("Testing Wait4(); System Call\n");
124 printf("\n");
125
126 cpid = fork(); /* Creates fork */
127 if (cpid == -1) {
128 printf("For has failed, returned = %d\n", EXIT_FAILURE);
129 }
130
131 if (cpid == 0) { /* This is the child operation */
132 printf("Child Created\n");
133 printf("Child = %d\n", getpid());
134 printf("Parent = %d\n", getppid());
135 sleep(2);
136 exit(3);
137
138 } else { /* This is the parent operation */
139 printf("Waiting for child\n");
140 wait4(cpid, &status, 0, &usage);
141 //__wait4(cpid, &status, 0, &usage); // This function will work, the above which is delcared will not.
142 printf("Waiting Complete\n");
143 printf("Child Exit Code: %d\n", WEXITSTATUS(status));
144 }
145
146 printf("\n");
147 sleep(1);
148
149 return EXIT_SUCCESS;
150
151 }
152
153