tsk1.c

来自「经典的psos编程实例源码」· C语言 代码 · 共 69 行

C
69
字号
/* tsk1.c - Task #1 */#include <psos.h>#include <prepc.h>#include <stdio.h>#include "misc.h"#include "appl.h"void	await_events(ULONG my_name);void	broadcast(int cond, ULONG qid, int numtasks, ULONG my_name);void tsk1(ULONG me, ULONG l2, ULONG l3, ULONG qid){	ULONG	rc;	int	index;	bprintf("%s Task begin\n", (char *) me);	srand(123456781L);	/* Wait for all tasks to be ready */	await_events(me);	for (index = 0; index < LOOP_MAX; index++) {		bprintf("%s: Sending broadcast (FALSE)\n", me);		broadcast(FALSE, qid, 3, me);		await_events(me);	}	bprintf("%s: Sending broadcast (TRUE)\n", me);	broadcast(TRUE, qid, 3, me);	/* Wait for all tasks to say they saw the last message */	await_events(me);	if (0 != (rc = q_delete(qid)))		fail(rc, "%s q_delete(%s) failed", me, Q_NAM);	bprintf("%s Task end\n", (char *) me);	exit(0);}voidbroadcast(int cond, ULONG qid, int numtasks, ULONG my_name){	ULONG	rc, actual;	struct	message	out;	int	index;	static	char	raw[] = "abcdefghijklmnopqrstuvwxyz";	static	int	raw_size = sizeof(raw);	out.flag = cond;	for (index = 0; index < sizeof(out.data); index++)		out.data[index] = raw[rand() % (raw_size-1)];	if (0 != (rc = q_broadcast(qid, (ULONG *) &out, &actual)))		fail(rc, "q_broadcast() failed", my_name);	if (numtasks != actual)		fail(rc, "%s: numtasks(%d) vs. actual(%d) error",			my_name, numtasks, actual);}voidawait_events(ULONG my_name){	ULONG	rc, ignored;	if (0 != (rc = ev_receive(T2_EV | T3_EV | T4_EV,				EV_WAIT | EV_ALL, 0, & ignored)))		fail(rc, "%s ev_receive() failed", my_name);	/* Else, we got all three events.	   Return to caller */}

⌨️ 快捷键说明

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