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 <errno.h> 17#include <stdlib.h> 18#include <unistd.h> 19#include <sys/wait.h> 20#include "functionalext.h" 21 22/** 23 * @tc.name : fork_0100 24 * @tc.desc : The function call is successful and the process can be created 25 * @tc.level : Level 0 26 */ 27void fork_0100() 28{ 29 pid_t fpid; 30 int count = 0; 31 int options = 0; 32 const char *sign_r = "1"; 33 const char *sign_f = "0"; 34 char list1[2]; 35 char list2[2]; 36 37 fpid = fork(); 38 if (fpid < 0) { 39 t_error("%s error in fork!", __func__); 40 } else if (fpid == 0) { 41 FILE* fp = fopen("test1.txt", "w+"); 42 if (!fp) { 43 t_error("failed to open test1.txt for writing"); 44 } 45 fwrite(sign_r, sizeof(char), strlen(sign_r), fp); 46 fclose(fp); 47 _exit(EXIT_SUCCESS); 48 } else { 49 FILE* fp = fopen("test2.txt", "w+"); 50 if (!fp) { 51 t_error("failed to open test2.txt for writing"); 52 } 53 fwrite(sign_r, sizeof(char), strlen(sign_r), fp); 54 fclose(fp); 55 waitpid(fpid, NULL, options); 56 57 FILE *fp1 = fopen("test1.txt", "r"); 58 if (!fp1) { 59 t_error("failed to open test1.txt for reading"); 60 } 61 FILE *fp2 = fopen("test2.txt", "r"); 62 if (!fp2) { 63 t_error("failed to open test2.txt for reading"); 64 } 65 fread(list1, sizeof(list1), 1, fp1); 66 fread(list2, sizeof(list2), 1, fp2); 67 EXPECT_EQ("fork_0100", list1[0], '1'); 68 EXPECT_EQ("fork_0100", list2[0], '1'); 69 70 fclose(fp1); 71 fclose(fp2); 72 remove("test1.txt"); 73 remove("test2.txt"); 74 } 75} 76 77int main(int argc, char *argv[]) 78{ 79 fork_0100(); 80 return t_status; 81} 82