Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/tests/test6.c need to be fixed.

0001 /*                    The Quest Operating System
0002  *  Copyright (C) 2005-2010  Richard West, Boston University
0003  *
0004  *  This program is free software: you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation, either version 3 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "syscall.h"
0019 
0020 void
0021 putx (unsigned long l)
0022 {
0023 
0024   int i, li;
0025 
0026   for (i = 7; i >= 0; i--)
0027     if ((li = (l >> (i << 2)) & 0x0F) > 9)
0028       putchar ('A' + li - 0x0A);
0029     else
0030       putchar ('0' + li);
0031 }
0032 
0033 void
0034 print (char *s)
0035 {
0036   while (*s) {
0037     putchar (*s++);
0038   }
0039 }
0040 
0041 void
0042 _start ()
0043 {
0044   int pid;
0045   int info = meminfo ();
0046   unsigned shared_id;
0047   int *shared_mem;
0048   int i;
0049 
0050   print ("MEMINFO: ");
0051   putx (info);
0052   print ("\n");
0053 
0054   shared_id = shared_mem_alloc ();
0055   if (shared_id < 0) {
0056     _exit (1);
0057   }
0058   print ("shared_id = ");
0059   putx (shared_id);
0060   print ("\n");
0061 
0062   /* try to test a race condition */
0063 
0064 #define ITERATIONS 1000000
0065   if ((pid = fork ())) {
0066     /* PARENT */
0067 
0068     shared_mem = shared_mem_attach (shared_id);
0069     if ((unsigned) shared_mem == -1) {
0070       shared_mem_free (shared_id);
0071       _exit (1);
0072     }
0073     print ("parent shared_mem = ");
0074     putx ((unsigned) shared_mem);
0075     print ("\n");
0076 
0077     for (i = 0; i < ITERATIONS; i++) {
0078       int ebx, j;
0079 
0080     asm ("cpuid": "=b" (ebx):"a" (1));
0081 
0082       for (j = 0; j < (ebx >> 24); j++)
0083         asm volatile ("lock decl %0":"=m" (*shared_mem):);
0084     }
0085 
0086 
0087     if (waitpid (pid) < 0) {
0088       print ("waitpid returned -1\n");
0089     }
0090 
0091     print ("value = ");
0092     putx (*shared_mem);
0093     print ("\n");
0094 
0095     shared_mem_detach (shared_mem);
0096 
0097     shared_mem_free (shared_id);
0098     _exit (0);
0099 
0100   } else {
0101     /* CHILD */
0102     shared_mem = shared_mem_attach (shared_id);
0103     if ((unsigned) shared_mem == -1) {
0104       shared_mem_free (shared_id);
0105       _exit (1);
0106     }
0107     print ("child shared_mem = ");
0108     putx ((unsigned) shared_mem);
0109     print ("\n");
0110 
0111     for (i = 0; i < ITERATIONS; i++) {
0112       int ebx, j;
0113 
0114     asm ("cpuid": "=b" (ebx):"a" (1));
0115 
0116       for (j = 0; j < (ebx >> 24); j++)
0117         asm volatile ("lock incl %0":"=m" (*shared_mem):);
0118     }
0119 
0120     shared_mem_detach (shared_mem);
0121 
0122     _exit (0);
0123   }
0124 }
0125 
0126 /* 
0127  * Local Variables:
0128  * indent-tabs-mode: nil
0129  * mode: C
0130  * c-file-style: "gnu"
0131  * c-basic-offset: 2
0132  * End: 
0133  */
0134 
0135 /* vi: set et sw=2 sts=2: */