Skip to content

Commit 2f26323

Browse files
xianglycxiaoxiang781216
authored andcommitted
arch/libc: Integrate vfork into fork, and vfork directly call up_fork
Signed-off-by: liwenxiang1 <liwenxiang1@xiaomi.com>
1 parent 67c9a7a commit 2f26323

4 files changed

Lines changed: 65 additions & 84 deletions

File tree

libs/libc/unistd/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ endif()
8585

8686
if(CONFIG_ARCH_HAVE_FORK)
8787
list(APPEND SRCS lib_fork.c)
88-
if(CONFIG_SCHED_WAITPID)
89-
list(APPEND SRCS lib_vfork.c)
90-
endif()
9188
endif()
9289

9390
target_sources(c PRIVATE ${SRCS})

libs/libc/unistd/Make.defs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ endif
5353

5454
ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
5555
CSRCS += lib_fork.c
56-
ifeq ($(CONFIG_SCHED_WAITPID),y)
57-
CSRCS += lib_vfork.c
58-
endif
5956
endif
6057

6158
# Add the unistd directory to the build

libs/libc/unistd/lib_fork.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include <unistd.h>
3232
#include <stdio.h>
33+
#include <sys/wait.h>
34+
#include <errno.h>
35+
#include <debug.h>
3336

3437
#if defined(CONFIG_ARCH_HAVE_FORK)
3538

@@ -170,4 +173,66 @@ pid_t fork(void)
170173
return pid;
171174
}
172175

176+
#if defined(CONFIG_SCHED_WAITPID)
177+
178+
/****************************************************************************
179+
* Public Functions
180+
****************************************************************************/
181+
182+
/****************************************************************************
183+
* Name: vfork
184+
*
185+
* Description:
186+
* The vfork() function is implemented based on fork() function, on
187+
* vfork(), the parent task need to wait until the child task is performing
188+
* exec or running finished.
189+
*
190+
* Returned Value:
191+
* Upon successful completion, vfork() returns 0 to the child process and
192+
* returns the process ID of the child process to the parent process.
193+
* Otherwise, -1 is returned to the parent, no child process is created,
194+
* and errno is set to indicate the error.
195+
*
196+
****************************************************************************/
197+
198+
pid_t vfork(void)
199+
{
200+
int status = 0;
201+
int ret;
202+
pid_t pid;
203+
204+
#ifdef CONFIG_PTHREAD_ATFORK
205+
atfork_prepare();
206+
#endif
207+
pid = up_fork();
208+
209+
#ifdef CONFIG_PTHREAD_ATFORK
210+
if (pid == 0)
211+
{
212+
atfork_child();
213+
}
214+
else
215+
{
216+
atfork_parent();
217+
}
218+
#endif
219+
220+
if (pid != 0)
221+
{
222+
/* we are in parent task, and we need to wait the child task
223+
* until running finished or performing exec
224+
*/
225+
226+
ret = waitpid(pid, &status, WNOWAIT);
227+
if (ret < 0)
228+
{
229+
serr("ERROR: waitpid failed: %d\n", get_errno());
230+
}
231+
}
232+
233+
return pid;
234+
}
235+
236+
#endif /* CONFIG_SCHED_WAITPID */
237+
173238
#endif /* CONFIG_ARCH_HAVE_FORK */

libs/libc/unistd/lib_vfork.c

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)