• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_process.h"
32 
Thread(void * arg)33 static void *Thread(void *arg)
34 {
35     sleep(1);
36     return NULL;
37 }
38 
GroupProcess(void)39 static int GroupProcess(void)
40 {
41     int testPid;
42     int ret;
43     int status = 0;
44     pid_t pid, pid1;
45     int count = 500;
46     int waitProcess = 0;
47     pthread_attr_t a = { 0 };
48     pthread_t pthread[1000] = { 0 };
49     struct sched_param param = { 0 };
50     bool thread = false;
51 
52     int processCount = 0;
53 
54     pthread_attr_init(&a);
55     param.sched_priority = 31; // 31, set pthread priority.
56     pthread_attr_setschedparam(&a, &param);
57     for (int i = 0; i < 40; i++) { // 40, number of cycles
58         ret = pthread_create(&pthread[i], &a, Thread, NULL);
59         if (ret != 0) {
60             exit(10); // 10, exit args
61             break;
62         }
63     }
64 
65     for (int i = 0; i < count; i++) {
66         pid = fork();
67         if (pid == 0) {
68             pthread_create(&pthread[100], &a, Thread, NULL); // 100, pthread array subscript
69             pthread_create(&pthread[100], &a, Thread, NULL); // 100, pthread array subscript
70             pthread_create(&pthread[100], &a, Thread, NULL); // 100, pthread array subscript
71             usleep(1000 * 10 * 50); // 1000, 10, 50, Used to calculate the delay time.
72             exit(0);
73         } else if (pid < 0) {
74             if (errno != EAGAIN) {
75                 sleep(1);
76             }
77             ret = wait(&status);
78             if (ret > 0) {
79                 processCount--;
80             }
81             continue;
82         } else {
83             processCount++;
84             testPid = pid;
85             continue;
86         }
87     }
88 
89     ret = 0;
90     while (processCount > 0) {
91         ret = wait(&status);
92         if (ret > 0) {
93             processCount--;
94         } else {
95             sleep(1);
96         }
97     }
98 
99     exit(255); // 255, exit args
100 EXIT:
101     return 0;
102 }
103 
TestCase(void)104 static int TestCase(void)
105 {
106     int ret;
107     int status = 0;
108     pid_t pid, pid1;
109     pid = fork();
110     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, assert pid equal to this.
111 
112     if (pid == 0) {
113         prctl(PR_SET_NAME, "mainFork", 0UL, 0UL, 0UL);
114         GroupProcess();
115         printf("[Failed] - [Errline : %d RetCode : 0x%x\n", g_iCunitErrLineNo, g_iCunitErrCode);
116         exit(g_iCunitErrLineNo);
117     }
118 
119     ret = waitpid(pid, &status, 0);
120     ICUNIT_ASSERT_EQUAL(ret, pid, ret);
121     status = WEXITSTATUS(status);
122     ICUNIT_GOTO_EQUAL(status, 255, status, EXIT); // 255, assert status equal to this.
123 
124     return 0;
125 EXIT:
126     return 1;
127 }
128 
ItTestProcess041(void)129 void ItTestProcess041(void)
130 {
131     TEST_ADD_CASE("IT_POSIX_PROCESS_041", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
132 }
133