示例#1
0
int main(int argc, char* argv[]){
	
	// test 1 : pipe, lock and cvar
	int lock_id, cvar_id, pipe_id;
	int pid,status;
	int condition=0;
	char *test = "Yalnix Works";
	char *buffer = (char*)malloc(1024);

	TracePrintf(1, "init main: test pipe, lock, cvar.\n");
	LockInit(&lock_id);
	CvarInit(&cvar_id);
	PipeInit(&pipe_id);
	TracePrintf(1, "init main: Lockid %d.\n", lock_id);
	TracePrintf(1, "init main: Cvarid %d.\n", cvar_id);
	TracePrintf(1, "init main: Pipeid %d.\n", pipe_id);

	
	pid = Fork();
	if (pid == 0) {	
			TracePrintf(1,"init main: child \n");
			Acquire(lock_id);
			TracePrintf(1,"init main: child acquire the lock\n");
			condition=1;
			TracePrintf(1,"init main: child condition %d\n",condition);
			PipeWrite(pipe_id, &condition,sizeof(int));
			TracePrintf(1,"init main: child change the condition and write it to pipe\n");
			TracePrintf(1,"init main: child cvar signal\n");
			CvarSignal(cvar_id);
			Release(lock_id);
			TracePrintf(1,"init main: child write to pipe: %s\n",test);
			PipeWrite(pipe_id,test,20);
			TracePrintf(1,"init main: child release the lock\n");
			Exit(0);

	}
	else{
		TracePrintf(1,"init main: parent\n");
		Acquire(lock_id);
		TracePrintf(1,"init main: parent acquire the lock\n");
		while(!condition){
			TracePrintf(1,"init main: parent cvar wait, condition %d\n",condition);
			CvarWait(cvar_id,lock_id);
			PipeRead(pipe_id,&condition,sizeof(int));
			TracePrintf(1,"init main: parent read the condition from pipe, condition %d\n",condition);
		}
		TracePrintf(1,"init main: parent wake up\n");
		Release(lock_id);
		TracePrintf(1,"init main: parent release the lock\n");		
		PipeRead(pipe_id,buffer,20);
		TracePrintf(1,"init main: parent read from pipe: %s\n",buffer);
		
	}
	
	Reclaim(lock_id);
	Reclaim(cvar_id);
	Reclaim(pipe_id);
	Exit(0);
	
 
}
示例#2
0
int main(int argc, char *argv[]) {
    // Create a single lock that the initial process obtains.
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "Creating the lock.\n");
    int lock_id;
    int parent_cvar_id;
    LockInit(&lock_id);


    int rc;
    // Init with invalid id pointer
    rc = CvarInit((void *) 10);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ invalid pointer: rc = %d\n", rc);

    // Signal with bad id
    rc = CvarSignal(2388);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarSignal w/ invalid id: rc = %d\n", rc);

    // Broadcast with bad id
    rc = CvarBroadcast(2388);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarBroadcast w/ invalid id: rc = %d\n", rc);

    // Wait w/ bad cvar id
    rc = CvarWait(lock_id, 1239988);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ invalid cvar id: rc = %d\n", rc);

    // Wait w/ lock I don't own
    rc = CvarWait(lock_id, parent_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ lock I don't own: rc = %d\n", rc);

    // Wait with bad lock id
    rc = CvarWait(1283, parent_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarWait w/ invalid cvar id: rc = %d\n", rc);

    int child_cvar_id;
    rc = CvarInit(&child_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ %x: rc = %d\n", &child_cvar_id, rc);

    // Signal with no one waiting
    rc = CvarSignal(child_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarSignal w/ no waiting proc rc = %d\n", rc);

    // Broadcast with no one waiting
    rc = CvarBroadcast(child_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarBroadcast w/ no waiting proc rc = %d\n", rc);

    rc = CvarInit(&parent_cvar_id);
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "CvarInit w/ %x: rc = %d\n", &parent_cvar_id, rc);

    // The initial process then attempts to acquire the lock it holds. This should return immediately.
    // Spawn 3 processes, each of which waits on the lock.
    int i;
    int n = 3;
    bool is_parent = true;
    for (i = 0; i < n; i++) {
        rc = Fork();

        if (0 == rc) { // Child process
            TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm a child with PID %d and I WANT the lock!\n",
                 GetPid());

            is_parent = false;

            Acquire(lock_id);

            break;
        }
    }

    // When a spawned process acquires the locks, it delays for a while, then releases it.
    TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm process %d and I HAVE the lock!\n", GetPid());

    if (!is_parent) {
        TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm a child w/ PID %d and I'm waiting on my cvar!\n",
            GetPid());
        CvarWait(child_cvar_id, lock_id);
        TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'ma a child w/ PID %d and I've finished waiting!\n",
            GetPid());

        CvarSignal(parent_cvar_id); // signal with proc waiting
    } else {
        Delay(5); // let children start waiting
        Acquire(lock_id);
        TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I'm broadcasting for child  cvar!\n",
            GetPid());

        CvarBroadcast(child_cvar_id); // Broadchat with procs waiting
        TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I'm waiting on my cvar!\n",
            GetPid());
        CvarWait(parent_cvar_id, lock_id);
        TracePrintf(TRACE_LEVEL_TESTING_OUTPUT, "I'm parent w/ PID %d and I've finished waiting!!\n",
            GetPid());
    }
    Release(lock_id);
    
    int status;
    if (is_parent) {
        for (i = 0; i < n; i++) {
            Wait(&status);
        }
        Reclaim(lock_id);
        Reclaim(child_cvar_id);
        Reclaim(parent_cvar_id);
    }

    return 0;
}
示例#3
0
文件: init.c 项目: jiayisuse/kernel
int main(int argc, char **argv)
{
	int pid, i, j, ret;
	int exit_status;
	char *exec_argv[] = { "haha", NULL };
	int *a = (int *)calloc(3, sizeof(int));
	int num_a = 100;
	char *str;
	unsigned int delay_ticks = 2;
	char buf[2 * TERMINAL_MAX_LINE + 2];
	char pipe_buf[1025];
	int pipe_fd;
	int lock_fd;
	int cvar_fd;

#ifdef SWAP_TEST
	free(a);
	a = (void *)calloc(num_a, PAGESIZE);
	if (a == NULL)
		goto loop;

	if (Fork() == 0) {
		while (1) {
			Delay(2);
			a[0] = 1000;
			TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]);
		}
	}
	for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int)))
		a[i] = 100;

	if (Fork() == 0) {
		while (1) {
			Delay(2);
			a[0] = 2000;
			TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]);
		}
	}
	for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int)))
		a[i] = 100;

	if (Fork() == 0) {
		while (1) {
			Delay(2);
			a[0] = 3000;
			TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]);
		}
	}
	for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int)))
		a[i] = 100;

	if (Fork() == 0) {
		while (1) {
			Delay(2);
			a[0] = 4000;
			TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]);
		}
	}
	for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int)))
		a[i] = 100;

	if (Fork() == 0) {
		while (1) {
			Delay(2);
			a[0] = 5000;
			TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]);
		}
	}
	for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int)))
		a[i] = 100;
#endif

#ifdef PIPE_TEST
	ret = PipeInit(&pipe_fd);
	pid = Fork();
	bzero(pipe_buf, sizeof(pipe_buf));
	if (pid != ERROR && !pid) {
		TtyPrintf(CONSOLE, "pipe_fd = %u\n", pipe_fd);
		j = 0;
		Delay(1);
		while (1) {
			for (i = 0; i < sizeof(pipe_buf); i++)
				pipe_buf[i] = (j % 26) + 'a';
			TtyPrintf(CONSOLE, ">>>>>>>>>>> write pipe\n");
			ret = PipeWrite(pipe_fd, pipe_buf, sizeof(pipe_buf));
			TtyPrintf(CONSOLE, "write pipe ret = %d, pid = %u\n", ret, GetPid());
			j++;
		}
		Exit(0);
	}
	while (1) {
		bzero(pipe_buf, sizeof(pipe_buf));
		TtyPrintf(CONSOLE, ">>>>>>>>>>> read pipe\n");
		ret = PipeRead(pipe_fd, pipe_buf, sizeof(pipe_buf) - 7);
		TtyPrintf(CONSOLE, "<<<<<<<<<<< read pipe ret = %d, pid = %u, %s\n", ret, GetPid(), pipe_buf);
	}
	Reclaim(pipe_fd);
#endif

#ifdef CVAR_TEST
	ret = LockInit(&lock_fd);
	ret = CvarInit(&cvar_fd);
	pid = Custom0(0, 0, 0, 0);
	if (pid != ERROR && !pid) {
		Acquire(lock_fd);
		while (!condition)
			CvarWait(cvar_fd, lock_fd);
		Delay(2);
		Release(lock_fd);
		Exit(7);
	}
	Acquire(lock_fd);
	condition = 1;
	CvarSignal(cvar_fd);
	Release(lock_fd);
	ret = Reclaim(lock_fd);
	if (ret)
		Exit(-1);
#endif

#ifdef TTY_TEST
	for (i = 0; i < sizeof(buf) - 1; i++)
		buf[i] = '9';
	buf[i] = '\0';
	TtyPrintf(CONSOLE, buf);
	TtyPrintf(CONSOLE, "\n");

	a[0] = 10;
	a[2] = 100;

	TtyPrintf(CONSOLE, "Enter somthing:\n");
	bzero(buf, sizeof(buf));
	ret = TtyRead(CONSOLE, buf, sizeof(buf));
	TtyPrintf(CONSOLE, "You just entered: %s (len = %d)\n", buf, ret);
#endif

#ifdef COW_TEST
	if (argc == 2)
		delay_ticks = atoi(argv[1]);

	pid = Fork();
	if (pid == ERROR) {
		Delay(2);
		return ERROR;
	} else if (!pid) {
		GetPid();
		delay_ticks = 5;

		TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid());
		pid = Fork();
		if (pid != ERROR && !pid) {
			GetPid();
			delay_ticks = 8;
			TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid());
			Delay(delay_ticks);
			Exec("exec_test", exec_argv);
		}
		pid = Wait(&exit_status);
		TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid());
		TtyPrintf(CONSOLE, " wait child = %u, status = %d\n", pid, exit_status);
		Delay(delay_ticks);
		Exit(10);
	}

	pid = Fork();
	if (pid != ERROR && !pid) {
		incursive_func(0);
		GetPid();
		delay_ticks = 9;
		TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid());
		Delay(delay_ticks);
		Exit(100);
	}

	TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid());

	Delay(delay_ticks);
	GetPid();
	Wait(&exit_status);
	Wait(&exit_status);
	GetPid();
#endif

loop:
	while (1)
		Delay(delay_ticks);

	return 0;
}