You are on page 1of 8
/* ex7a.c - xmain, prod2, cons2 */ #tinclude #include int n=0; /* external variables are shared by all processes */ int produced, consumed; [tecenee: - * main producer and consumer processes synchronized with messages fee wees ” ymain() t int prod2(), cons2()7 consumed = create (cons2, INITSTK, INITPRIO, "cons", 0); produced = create (prod2, INITSTK, INITPRIO, "prod", 0); int a, ke for (isl; i<=2000; i++) { ntt7 send(consumed, n)? k = receive(); , waiting for it to be produced * cons2 -- print n 2000 time: int i, msg: for (int; 4<=2000; i++) ¢ msg = receive(); print£("n is %d\n", msg); send (produced, 1); C1 \XINU4WIN\NEWSRC\EXAMPLES>tcc -Ic:\xinudwin\newsre ex7a.c c:\xinudwin\ne warc\xinu.1ib Turbo C++ Version 3.00 Copyright (c) 1992 Borland International rex7a.c Warning ex7a.c 24: Function should return a value in function »main Warning ex7a.c 39: Function should return a value in function prod2 Warning ex7a.c 39: ‘k’ is assigned a value that is never used in function prod2 Warning ex7a.c 54: Function should return a value in function cons2 Turbo Link Version 5.0 Copyright (c) 1992 Borland International Available memory 4117208 C2 \XINU4WIN\NEWSRC\EXAMPLES>ex7a.exe Initializing... Disk read error 80H reading drive 0 PC-Xinu Version 6pc (1-Dec-87) 63892 real mem 21380 ba Hit any key to continue. . . is 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2UBHH BEBE DRED ++ system halt -- PC-Xinu terminated with 3 processes active Returning . . . (C1 \XINU4W98\NEWSRC\EXAMPLES> /* ex8.c - xmain, Inci, Inc2, Inc3, Pr */ #include #include [te * xmain bea int count1 = 0, count2 = 0, count3 = 07 xmain() t int Inc(), Pr()z resume( create(Inc, INITSTK, INITPRIO, “proc 1", 1, gcounti) ); create(Inc, INITSTK, INITPRIO, "proc 2", 1, scount2) ); ereate(Inc, INITSTK, INITPRIO - 1, “proc 3", 1, &count3) ); create(Pr, INITSTK, INITPRIO + 1, “proc 4", 0) ); Increment counter via pointer ” Inc(int ptr) t int *ptri; perl = (int *)ptr; while (1) (*ptrl) +4; + /* Inc */ Pr() { while(1) 4 sleep(3); print£("countl = %d, count2 = %d, count3 = %d\n", count1, count2, count3); ) /* while */ ./* Pr */ C:\XINU4WIN\NEWSRC\EXAMPLES>ex8 Initializing. . . PC-Xinu Version 6pc (1-Dec-87) 63892 real mem 21428 base addr 42464 avail mem + + + Hit any key to continue ° counti = 7538, count2 = -1985, count3 = 0 counti = 22240, count2 = -4213, count3 = 0 count1 = 27389, count2 = -21535, count3 = counti = 365, count2 = 1281, count3 = 0 countl = 13911, count2 = -30480, count3 = 0 counti = 10266, count2 = 2410, count3 = 0 countl = 969, count2 = 21056, count3 = 0 count1 = -29742, count2 = 9732, count3 = 0 count1 = -5733, count2 = -8151, count3 = 0 *@ -- system halt -- PC-Xinu terminated with 8 processes active Returning . . . C:\XINU4WIN\NEWSRC\EXAMPLES> /* ex9.c - xmain, Incl, Inc2, Inc3, Pr */ flinclude #include * xmain -- example of creating processes in PC-Xinu unsigned long int counti = 0, count2 = 0, count3 = 0; ymain() { dnt Inc(), Pr()) resume( create(Inc, INITSTK, INITPRIO, "proc 1! 1, &count1) ); resume( create(Inc, INITSTK, INITPRIO, "proc 2", 1, &count2) ); resume( create(Inc, INITSTK, INITPRIO - 1, "proc 3", 1, &count3) ); resume( create(Pr, INITSTK, INITPRIO + 1, "proc 4", 0) )7 Increment counter via pointer Inc(int ptr) re unsigned long int *ptri; ptrl = (unsigned long int *)pt: while (1) (*ptrl1) +4; } /* Inc */ Pr() 4 char str[80}; while(1) { sleep (3); sprintf£(str, "countl = %lu, count2 = %lu, count3 = %lu\n", counti, count2, count3); printf (str); } /* while */ dss Pr ey C:\XINU4WIN\NEWSRC\EXAMPLES>ex9 Initializing... PC-Xinu Version 6pc (1-Dec-87) 63884 real mem 21600 base addr 42284 avail mem Hit any key to continue . count1 = 40839366, count2 = 34947102, count3 = count1 = 80054725, count2 = 72117117, count3 = count1 = 122269891, count2 = 105023888, count3 count1 = 161485095, count2 = 142149479, count3 count1 = 203695766, count2 = 176996089, count3 count1 = 242906994, count2 = 212557897, count3 count1 = 285120341, count2 = 246458574, count3 count1 = 324295710, count2 = 282874952, count3 count1 = 366426584, count2 = 317024186, count3 count1 = 405637331, count2 = 354345046, count3 count1 = 447836276, count2 = 388738821, count3 count1 = 487048573, count2 = 425370357, count3 @ -- system halt -- PC-Xinu terminated with 8 processes active Returning... C:\XINU4WIN\NEWSRC\EXAMPLES> wnounnnoe ec0e0cccc00e 7* exll.c = Acc 8 how many increments can be done until preemption */ #include #include * amain -- example of creating processes in PC-xinu unsigned long int who is_current = 0, count = 0; ymain () € Amt me(), POs INITSTK, INITPRIO, "proc 1", 0) ); resume( create(Pr, INITSTK, INITPRIO, "proc 2", 0) )7 while (1) 7 /* Do nothing but serve as alternative to Inc */ 7* Pr may not be ready (printing) */ » pe * Ine ” - Increment count tne() « int mypidy mypid = getpid(); /* Store my pid */ while (1) € count = 0; who_is_current = mypid; /* Signal I am current */ 7* Xs long as I am current, increment count */ while(who_is_current == mypid) count++; /* XT have been replaced and become current again */ PE() char str[60]; int count_value; int mypiay mypid = getpid(); while(1) € count_value = count; /* Store count value aside, locally */ who_is_current = mypid; /* Signal I am current */ sprintf (str, "count_value = %lu\n", ‘count_value); printf (str); while (who_is_current == mypid) /* Do not continue until */ 2 /* 1 am replace and current again */ } /* while */ die PE ey C:\XINU4WIN\NEWSRC\EXAMPLES>ex11. Initializing . . PC-xinu Version 6pc (1-Dec-87) 63876 real mem 21560 base addr 42316 avail mem Hit any key to continue... count_value = 32605 count_value = 33938 count_value = 33214 count_value = 51413 count_value = 14015 count_value = 17067 count_value = 28126 count_value = 29102 count_value = 29110 *@count_value = 27751 count_value = 29147 count_value = 29506 count_value = 28982 count_value = 28305 count_value = 28095 count_value = 46711 -- system halt -- PC-Xinu terminated with 7 processes active Returning . . . C:\XKINU4WIN\NEWSRC \EXAMPLES>

You might also like