mirror of
https://github.com/drasko/codezero.git
synced 2026-01-12 02:43:15 +01:00
Added a clone() test to test0.
This commit is contained in:
@@ -9,5 +9,6 @@ int mmaptest(void);
|
||||
int dirtest(void);
|
||||
int fileio(void);
|
||||
int fileio2(void);
|
||||
int clonetest(void);
|
||||
|
||||
#endif /* __TEST0_TESTS_H__ */
|
||||
|
||||
@@ -62,6 +62,9 @@ void main(void)
|
||||
printf("-- FAILED --\n");
|
||||
}
|
||||
|
||||
printf("Testing clone syscall...\n");
|
||||
clonetest();
|
||||
|
||||
while (1)
|
||||
wait_pager(0);
|
||||
#if 0
|
||||
|
||||
42
tasks/test0/src/clone.c
Normal file
42
tasks/test0/src/clone.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Clone test.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
|
||||
int my_thread_func(void *arg)
|
||||
{
|
||||
printf("Cloned child running...\n");
|
||||
printf("PID: %d\n", getpid());
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
int clonetest(void)
|
||||
{
|
||||
pid_t childid;
|
||||
void *child_stack;
|
||||
|
||||
if ((child_stack = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN, 0, 0)) == MAP_FAILED) {
|
||||
printf("MMAP failed.\n");
|
||||
_exit(1);
|
||||
} else {
|
||||
printf("Mapped area starting at %p\n", child_stack);
|
||||
}
|
||||
((int *)child_stack)[-1] = 5; /* Test mapped area */
|
||||
|
||||
printf("Cloning...\n");
|
||||
|
||||
if ((childid = clone(my_thread_func, child_stack,
|
||||
CLONE_PARENT | CLONE_FS | CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, 0)) < 0) {
|
||||
perror("CLONE failed.\n");
|
||||
} else {
|
||||
printf("Cloned a new thread with child pid %d\n", childid);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user