Esempio n. 1
0
File: ctest.c Progetto: forwhat/OS
void test_main()
{
	int s, pid;
	s = SemaGet(1);
	if (s == -1) {
		print("Error in getting semaphore!");
		getline();
		exit(-1);
	}
	pid = fork();
	if (pid == -1) {
		print("Error in fork!");
		getline();
		exit(-1);
	}
	else if (pid == 0) {
		while (1) {
			P(s);
			putwords("one word after another word");
			V(s);
		}
		exit(0);
	}
	else {
		while (1) {
			P(s);
			int i;
			for (i = 0; i < size; ++i) {
				print(words[i]);
				print(" ");
			}
			print("\n");
			V(s);
		}
		exit(0);
	}
}
Esempio n. 2
0
File: e.c Progetto: caiyanzhi/os
void cmain()
{
	int s;
	int pid;
	int tmp;
	s = SemaGet(1);
	if(s < 0)
	{
		myprintf("get Semaphore failed\r\n");
		exit(-1);
	}
	myInt2Chars(s,tmpMess);
	myprintf("get Semaphore = ");
	myprintf(tmpMess);
	myprintf("\r\n");
	clearWords();
	pid = fork();
	if(pid > 0)
	{
		pid = fork();
	}
	if(pid == 0)
	{
		while(1)
		{
			p(s);
			clearWords(); /*清空CS缓冲区*/
			strcopy(words,zhufu,pid);/*写CS缓冲区*/
			tmp = max_read;/*max_read用于判断是否结束*/
			v(s);
			if(max_read == 0)
			{
				myprintf("son stop sending zhufu\r\n");
				break;
			}
		}
	}
	else if(pid == -1)
	{
		myprintf("create sub prog failed!\r\n");
		exit(-1);
	}
	else
	{
		while(1)
		{
			p(s);
			if(words[0]) /*若缓冲区有内容*/
			{
				max_read--;/*max_read用于判断是否结束*/
				myprintf("receive zhufu: ");
				myprintf(words);/*读CS缓冲区*/

				myprintf("\r\n");
				isZhuFu();/*判断CS缓冲区区是否是祝福*/
			}
			else
				;
			v(s);

			if(max_read == 0)
			{
				SemaFree(s);
				myprintf("father quit\r\nrecveive zhufu count = ");
				myInt2Chars(zhufu_crt,tmpMess);
				myprintf(tmpMess);
				myprintf("\r\n");
				break;
			}
		}
	}

	exit(0);
}