• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include "test.h"
23 
24 /**
25  * @tc.name      : waitpid_0100
26  * @tc.desc      : Test the waitpid function to waitpid for the child process
27  * @tc.level     : Level 0
28  */
waitpid_0100(void)29 void waitpid_0100(void)
30 {
31     pid_t pid = fork();
32     if (pid > 0) {
33         int status = 0;
34         int options = 0;
35         pid_t waitpid_for_pind = waitpid(pid, &status, options);
36         if (waitpid_for_pind != pid) {
37             t_error("%s waitpid get pid is %d are not want %d\n", __func__, waitpid_for_pind, pid);
38         }
39         if (status != 0) {
40             t_error("%s waitpid get status is %d are not 0\n", __func__, status);
41         }
42     } else if (pid == 0) {
43         sleep(1);
44         exit(0);
45     } else {
46         t_error("%s waitpid fork error\n");
47     }
48 }
49 
50 /**
51  * @tc.name      : waitpid_0200
52  * @tc.desc      : Test the result of the waitpid function when the exit code of the child process is not 0
53  * @tc.level     : Level 1
54  */
waitpid_0200(void)55 void waitpid_0200(void)
56 {
57     pid_t pid = fork();
58     if (pid > 0) {
59         int status = 0;
60         int options = 0;
61         pid_t waitpid_for_pind = waitpid(pid, &status, options);
62         if (waitpid_for_pind != pid) {
63             t_error("%s waitpid get pid is %d are not want %d\n", __func__, waitpid_for_pind, pid);
64         }
65         if (status == 0) {
66             t_error("%s waitpid get status is 0\n", __func__);
67         }
68     } else if (pid == 0) {
69         sleep(1);
70         exit(66);
71     } else {
72         t_error("%s waitpid fork error\n");
73     }
74 }
75 
main(int argc,char * argv[])76 int main(int argc, char *argv[])
77 {
78     waitpid_0100();
79     sleep(2);
80     waitpid_0200();
81     return t_status;
82 }