sllul/thrdtest.c

113 lines
2.2 KiB
C
Raw Normal View History

// Test that thrd succeeds or fails gracefully.
// This file was modified from the xv6-based forktest.c
// Tiny executable so that the limit can be filling the proc table.
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
// N should be set HIGHER than the limit!
#define N 10
void
print(const char *s)
{
write(1, s, strlen(s));
}
void printdec(unsigned int i) {
if (i == 0) {
print("0");
return;
}
char foo[11];
int idx = 10;
while (i > 0) {
idx--;
foo[idx] = '0' + (i%10);
i /= 10;
}
foo[10] = 0;
print(foo+idx);
}
void thrdf(uint64 thrdnum) {
print("Hello from thread #"); printdec((int)thrdnum); print("\n");
int i, j, ch, pr;
pr = 7-(getpid() % 4);
ch = '0'+pr;
prio(getpid(), pr, 4);
//sleep(1);
for (i = 0; i < 100; i++) {
for (j = 0; j < 256; j++) {
ch++;
}
char str[2];
str[0] = (char)ch;
str[1] = 0;
print(str);
}
exit(0);
}
void
thrdtest(void)
{
int n, pid;
print("thrd test\n");
print("thrdf is at "); printdec((int)(uint64)&thrdf); print("\n");
//print("testing printdec(12345)="); printdec(12345); print("\n");
print("attempting to launch up to N="); printdec(N); print(" processes in the same address space...\n");
for(n=0; n<N; n++){
char* stack = malloc(1024);
print("stack at "); printdec((int)(uint64)stack); print("\n");
if (!stack) {
print("malloc failed\n");
break;
}
int i = 1024;
// Align stack, since this probably won't be guaranteed by a simple malloc
// And may differ by compiler/settings/etc.!!
while (((uint64)(stack+i)) % 16) {
i--;
}
pid = thrd(getpid(), &thrdf, stack+i, n);
if(pid < 0) {
print("thrd() syscall failed\n");
break;
}
}
print("spawned "); printdec(n); print(n == 1 ? " thread\n" : " threads\n");
if(n == N){
print("thrd() claimed to work N times! thrdtest can be updated so N is greater than the kernel's NPROC!\n");
//exit(1);
}
for(; n > 0; n--){
if(wait(0) < 0){
print("wait stopped early\n");
exit(1);
}
}
if(wait(0) != -1){
print("wait got too many\n");
exit(1);
}
print("\nthrd() test OK\n");
}
int
main(void)
{
thrdtest();
exit(0);
}