Added minimal fork() test app.

Enhanced exec_test.c to use some process functions as well.
Switched to load_image() instead of using the old _kern_create_team() syscall.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9359 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2004-10-14 17:52:41 +00:00
parent dc0f47e390
commit 4496669366
10 changed files with 116 additions and 52 deletions

View File

@ -4,6 +4,7 @@ KernelObjects
exec_test.c
false_main.c
fibo_main.c
fork_test.c
init.c
monitor_test.c
pipe_test.c

View File

@ -5,6 +5,7 @@
#include <OS.h>
#include <image.h>
#include <syscalls.h>
#include <termios.h>
@ -143,7 +144,7 @@ start_console(struct console *con)
static pid_t
start_process(const char *path, const char *name, char **argv, int argc, struct console *con)
start_process(int argc, const char **argv, struct console *con)
{
int saved_stdin, saved_stdout, saved_stderr;
pid_t pid;
@ -156,8 +157,9 @@ start_process(const char *path, const char *name, char **argv, int argc, struct
dup2(con->tty_slave_fd, 1);
dup2(con->tty_slave_fd, 2);
// XXX launch
pid = _kern_create_team(path, name, argv, argc, NULL, 0, 5/*, PROC_FLAG_NEW_PGROUP*/);
pid = load_image(argc, argv, (const char **)environ);
resume_thread(pid);
setpgid(pid, 0);
dup2(saved_stdin, 0);
dup2(saved_stdout, 1);
@ -192,15 +194,15 @@ main(void)
for (;;) {
pid_t shell_process;
status_t retcode;
char *argv[3];
const char *argv[3];
argv[0] = "/bin/shell";
// argv[1] = "-s";
// argv[2] = "/boot/loginscript";
shell_process = start_process("/bin/shell", "shell", argv, 1, &theconsole);
shell_process = start_process(1, argv, &theconsole);
wait_for_thread(shell_process, &retcode);
_kern_wait_for_team(shell_process, &retcode);
puts("Restart shell");
}

View File

@ -1,20 +1,22 @@
/*
** Copyright 2003-2004, The OpenBeOS Team. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
** Copyright 2003-2004, The Haiku Team. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <image.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syscalls.h>
int
main(int argc, char **argv)
{
int i, rc;
team_id pid;
int i;
thread_id pid;
status_t rc;
char temp[16];
char *var;
@ -42,7 +44,7 @@ main(int argc, char **argv)
if (argc > 1) {
char buffer[16];
char *_argv[] = { argv[0], buffer, NULL };
const char *_argv[] = { argv[0], buffer, NULL };
int val = atoi(argv[1]) - 1;
sprintf(temp, "VAR_%d", val);
@ -54,8 +56,8 @@ main(int argc, char **argv)
if (val > 0) {
printf("Spawning test (%d left)\n", val);
sprintf(buffer, "%d", val);
pid = _kern_create_team(_argv[0], _argv[0], _argv, 2, NULL, 0, 5);
_kern_wait_for_team(pid, &rc);
pid = load_image(2, _argv, (const char **)environ);
wait_for_thread(pid, &rc);
}
}
return 0;

View File

@ -12,20 +12,39 @@
#include <errno.h>
void
print_process_info(const char *text)
{
puts(text);
printf("\tsession_id = %ld\n", getsid(0));
printf("\tgroup_id = %ld\n", getpgid(0));
printf("\tprocess_id = %ld\n", getpid());
printf("\tparent_id = %ld\n", getppid());
}
int
main(int argc, char **argv)
{
printf("exec_test: this is thread %ld\n", find_thread(NULL));
if (argc < 2) {
print_process_info("before exec():");
puts("going to execl()...");
execl("/bin/exec_test", "/bin/exec_test", "argument 1", NULL);
printf("execl() returned: %s\n", strerror(errno));
} else {
int i;
print_process_info("after exec():");
puts("got arguments:");
for (i = 0; i < argc; i++)
printf("%d: \"%s\"\n", i, argv[i]);
setsid();
print_process_info("after setsid():");
}
return 0;

View File

@ -4,18 +4,18 @@
*/
#include <image.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syscalls.h>
static
void
static void
usage(char const *app)
{
printf("usage: %s [-s] ###\n", app);
_kern_exit(-1);
exit(-1);
}
@ -46,20 +46,20 @@ main(int argc, char *argv[])
if (num < 2) {
result = 1;
} else {
team_id pid;
int retcode;
thread_id pid;
status_t retcode;
char buffer[64];
char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL };
const char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL };
int aaargc = 3;
sprintf(buffer, "%d", num-1);
pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5);
_kern_wait_for_team(pid, &retcode);
pid = load_image(aaargc, aaargv, (const char **)environ);
wait_for_thread(pid, &retcode);
result = retcode;
sprintf(buffer, "%d", num-2);
pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5);
_kern_wait_for_team(pid, &retcode);
pid = load_image(aaargc, aaargv, (const char **)environ);
wait_for_thread(pid, &retcode);
result += retcode;
}

View File

@ -0,0 +1,31 @@
/*
** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <OS.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int
main(int argc, char **argv)
{
pid_t child = fork();
if (child == 0) {
write(1, "CHILD: is awake!\n", 17);
exit_thread(0);
//printf("CHILD: we're the child!\n");
} else if (child > 0) {
printf("PARENT: we're the parent, our child has pid %ld\n", child);
wait_for_thread(child, NULL);
} else
fprintf(stderr, "fork() failed: %s\n", strerror(errno));
return 0;
}

View File

@ -4,9 +4,11 @@
*/
#include <image.h>
#include <unistd.h>
#include <stdio.h>
#include <syscalls.h>
#include <stdlib.h>
#include <fcntl.h>
@ -26,32 +28,35 @@ setup_io(void)
int
main()
main(int argc, char **argv)
{
setup_io();
printf("Welcome to Haiku!\n");
{
team_id pid;
const char *args[] = {"fortune", NULL};
thread_id thread;
pid = _kern_create_team("/bin/fortune", "/bin/fortune", NULL, 0, NULL, 0, 5);
if (pid >= 0) {
int retcode;
_kern_wait_for_team(pid, &retcode);
thread = load_image(1, args, (const char **)environ);
if (thread >= B_OK) {
status_t returnCode;
wait_for_thread(thread, &returnCode);
} else
printf("Failed to create a team for fortune.\n");
}
while (1) {
team_id pid;
const char *args[] = {"shell", NULL};
thread_id thread;
pid = _kern_create_team("/bin/shell", "/bin/shell", NULL, 0, NULL, 0, 5);
if (pid >= 0) {
int retcode;
printf("init: spawned shell, pid 0x%lx\n", pid);
_kern_wait_for_team(pid, &retcode);
printf("init: shell exited with return code %d\n", retcode);
thread = load_image(1, args, (const char **)environ);
if (thread >= B_OK) {
status_t returnCode;
printf("init: spawned shell, pid 0x%lx\n", thread);
wait_for_thread(thread, &returnCode);
printf("init: shell exited with return code %ld\n", returnCode);
} else
printf("Failed to start a shell :(\n");
}

View File

@ -4,6 +4,8 @@
*/
#include <syscalls.h>
#include <image.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
@ -36,7 +38,7 @@ cmd_exec(int argc, char *argv[])
int
cmd_create_proc(int argc,char *argv[])
cmd_create_proc(int argc, char *argv[])
{
bool must_wait=true;
team_id pid;
@ -50,9 +52,9 @@ cmd_create_proc(int argc,char *argv[])
return 0;
}
tmp = argv[argc - 1];
tmp = argv[argc - 1];
if( !find_file_in_path(argv[0],filename,SCAN_SIZE)){
if (!find_file_in_path(argv[0], filename, SCAN_SIZE)) {
printf("can't find '%s' \n",argv[0]);
return 0;
}
@ -74,10 +76,10 @@ cmd_create_proc(int argc,char *argv[])
}
}
pid = _kern_create_team(filename,filename, argv, argc, NULL, 0, 5);
pid = load_image(argc, (const char **)argv, (const char **)environ);
if (pid >= 0) {
if (must_wait)
_kern_wait_for_team(pid, NULL);
wait_for_thread(pid, NULL);
} else {
printf("Error: cannot execute '%s'\n", filename);
return 0; // should be -1, but the shell would exit

View File

@ -1,6 +1,7 @@
#include <image.h>
#include <string.h>
#include <ctype.h>
#include <syscalls.h>
#include <stdio.h>
#include <stdlib.h>
#include <Errors.h>
@ -85,11 +86,11 @@ exec_file(int argc, char *argv[], status_t *retcode)
if (!find_file_in_path(argv[0], filename, SCAN_SIZE))
return SHE_FILE_NOT_FOUND;
pid = _kern_create_team(filename, filename, argv, argc, NULL, 0, 5);
pid = load_image(argc, (const char **)argv, (const char **)environ);
if (pid < 0)
return SHE_CANT_EXECUTE;
_kern_wait_for_team(pid, retcode);
wait_for_thread(pid, retcode);
return SHE_NO_ERROR;
}

View File

@ -345,19 +345,20 @@ int main(int argc, char **argv)
printf("spawning %d copies of true\n", 100);
for(i=0; i<100; i++) {
team_id id;
const char *args[] = {"true", NULL};
thread_id id;
bigtime_t t;
printf("%d...", i);
t = system_time();
id = _kern_create_team("/bin/true", "true", NULL, 0, NULL, 0, 20);
if(id <= 0x2) {
id = load_image(1, args, (const char **)environ);
if (id <= 0x2) {
printf("new team returned 0x%lx!\n", id);
return -1;
}
_kern_wait_for_team(id, NULL);
wait_for_thread(id, NULL);
printf("done (%Ld usecs)\n", system_time() - t);
}