astest.c

来自「pebble」· C语言 代码 · 共 89 行

C
89
字号
/* 
 * Copyright 1999, 2000, 2001, 2002 Lucent Technologies Inc.
 * All Rights Reserved.
 * Information Sciences Research Center, Bell Labs.
 *
 * LUCENT TECHNOLOGIES DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE 
 * OR THE SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The
 * software is provided "as is" without expressed or implied warranty 
 * of any kind.
 *
 * These notices must be retained in any copies of any part of this
 * software.
 *
 */

#include "pebble.h"
#include "unistd.h"
#include "types.h"
#include "time.h"
#include "synch.h"

#define	NDOMAIN	8

static Barrier *b;
static int my_asid;
static int worker_asid[NDOMAIN];

void
worker(int i, int n)
{
	my_asid = get_asid();
	fdprintf(2, "worker %d of %d initializing. asid=%d\n", i, n, my_asid);

	if (barrier_wait(b, i, n) < 0)
		panic("barrier_wait:");

	fdprintf(2, "worker %d of %d passed 1st barrier. asid=%d\n", i, n, my_asid);

	if (barrier_wait(b, i, n) < 0)
		panic("barrier_wait:");

	fdprintf(2, "worker %d of %d passed 2nd barrier. asid=%d\n", i, n, my_asid);

	/* workers wait here to allow parent to terminate first */
	if (barrier_wait(b, i, n) < 0)
		panic("barrier_wait:");
}

int
main(void)
{
	int i;

	fdprintf(2, "astest starting\n");

	b = (Barrier *)shm_create(sizeof(Barrier));
	if (b == (Barrier *)-1)
		panic("shm_create");

	if (barrier_init(b) < 0)
		panic("barrier_init:");

	for (i = 1; i < NDOMAIN; i++) {
		if ((worker_asid[i] = domain_fork(NULL)) < 0)
			panic("domain_fork:");
		if (worker_asid[i] == 0) {
			worker(i, NDOMAIN);
			task_exit(0);
		}
	}

	worker_asid[0] = my_asid = get_asid();
	fdprintf(2, "parent initializing. asid=%d\n", my_asid);

	if (barrier_wait(b, 0, NDOMAIN) < 0)
		panic("barrier_wait:");

	fdprintf(2, "parent passed 1st barrier. asid=%d\n", my_asid);

	if (barrier_wait(b, 0, NDOMAIN) < 0)
		panic("barrier_wait:");

	fdprintf(2, "parent passed 2nd barrier. asid=%d\n", my_asid);

	fdprintf(2, "ASTEST ENDED\n");
	task_exit(0);
	return(1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?