├── .gitignore ├── LICENSE ├── MIT6_828F12_assignment1.pdf ├── MIT6_828F12_assignment10.pdf ├── MIT6_828F12_assignment11.pdf ├── MIT6_828F12_assignment13.pdf ├── MIT6_828F12_assignment18.pdf ├── MIT6_828F12_assignment2.pdf ├── MIT6_828F12_assignment21.pdf ├── MIT6_828F12_assignment22.pdf ├── MIT6_828F12_assignment3.pdf ├── MIT6_828F12_assignment4.pdf ├── MIT6_828F12_assignment5.pdf ├── MIT6_828F12_assignment6.pdf ├── MIT6_828F12_assignment7.pdf ├── MIT6_828F12_assignment8.pdf ├── MIT6_828F12_assignment9.pdf ├── MIT6_828F12_inode.pdf ├── README.md ├── barrier.c ├── big.c ├── ph.c ├── sh.c ├── uthread.c └── uthread_switch.S /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /MIT6_828F12_assignment1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment1.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment10.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment11.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment13.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment18.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment18.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment2.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment21.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment22.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment22.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment3.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment4.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment5.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment6.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment7.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment8.pdf -------------------------------------------------------------------------------- /MIT6_828F12_assignment9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_assignment9.pdf -------------------------------------------------------------------------------- /MIT6_828F12_inode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonleaster/MIT_6_828_assignments_2012/7bbb8e114d3b146e310e35bf1f5502a2ab817ffc/MIT6_828F12_inode.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MIT_6_828_assignments_2012 2 | Here is my solutions for 6.828's assignments 3 | -------------------------------------------------------------------------------- /barrier.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // #define SOL 8 | 9 | static int nthread = 1; 10 | static int round = 0; 11 | 12 | struct barrier { 13 | pthread_mutex_t barrier_mutex; 14 | pthread_cond_t barrier_cond; 15 | int nthread; // Number of threads that have reached this round of the barrier 16 | int round; // Barrier round 17 | } bstate; 18 | 19 | static void 20 | barrier_init(void) 21 | { 22 | assert(pthread_mutex_init(&bstate.barrier_mutex, NULL) == 0); 23 | assert(pthread_cond_init(&bstate.barrier_cond, NULL) == 0); 24 | bstate.nthread = 0; 25 | } 26 | 27 | static void 28 | barrier() 29 | { 30 | bstate.round++; 31 | } 32 | 33 | static void * 34 | thread(void *xa) 35 | { 36 | long n = (long) xa; 37 | long delay; 38 | int i; 39 | 40 | for (i = 0; i < 20000; i++) { 41 | int t = bstate.round; 42 | assert (i == t); 43 | barrier(); 44 | usleep(random() % 100); 45 | } 46 | } 47 | 48 | int 49 | main(int argc, char *argv[]) 50 | { 51 | pthread_t *tha; 52 | void *value; 53 | long i; 54 | double t1, t0; 55 | 56 | if (argc < 2) { 57 | fprintf(stderr, "%s: %s nthread\n", argv[0], argv[0]); 58 | exit(-1); 59 | } 60 | nthread = atoi(argv[1]); 61 | tha = malloc(sizeof(pthread_t) * nthread); 62 | srandom(0); 63 | 64 | barrier_init(); 65 | 66 | for(i = 0; i < nthread; i++) { 67 | assert(pthread_create(&tha[i], NULL, thread, (void *) i) == 0); 68 | } 69 | for(i = 0; i < nthread; i++) { 70 | assert(pthread_join(tha[i], &value) == 0); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /big.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | #include "fcntl.h" 5 | 6 | int 7 | main() 8 | { 9 | char buf[512]; 10 | int fd, i, sectors; 11 | 12 | fd = open("big.file", O_CREATE | O_WRONLY); 13 | if(fd < 0){ 14 | printf(2, "big: cannot open big.file for writing\n"); 15 | exit(); 16 | } 17 | 18 | sectors = 0; 19 | while(1){ 20 | *(int*)buf = sectors; 21 | int cc = write(fd, buf, sizeof(buf)); 22 | if(cc <= 0) 23 | break; 24 | sectors++; 25 | if (sectors % 100 == 0) 26 | printf(2, "."); 27 | } 28 | 29 | printf(1, "\nwrote %d sectors\n", sectors); 30 | 31 | close(fd); 32 | fd = open("big.file", O_RDONLY); 33 | if(fd < 0){ 34 | printf(2, "big: cannot re-open big.file for reading\n"); 35 | exit(); 36 | } 37 | for(i = 0; i < sectors; i++){ 38 | int cc = read(fd, buf, sizeof(buf)); 39 | if(cc <= 0){ 40 | printf(2, "big: read error at sector %d\n", i); 41 | exit(); 42 | } 43 | if(*(int*)buf != i){ 44 | printf(2, "big: read the wrong data (%d) for sector %d\n", 45 | *(int*)buf, i); 46 | exit(); 47 | } 48 | } 49 | 50 | exit(); 51 | } 52 | -------------------------------------------------------------------------------- /ph.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SOL 8 | #define NBUCKET 5 9 | #define NKEYS 100000 10 | 11 | struct entry { 12 | int key; 13 | int value; 14 | struct entry *next; 15 | }; 16 | struct entry *table[NBUCKET]; 17 | int keys[NKEYS]; 18 | int nthread = 1; 19 | volatile int done; 20 | 21 | 22 | double 23 | now() 24 | { 25 | struct timeval tv; 26 | gettimeofday(&tv, 0); 27 | return tv.tv_sec + tv.tv_usec / 1000000.0; 28 | } 29 | 30 | static void 31 | print(void) 32 | { 33 | int i; 34 | struct entry *e; 35 | for (i = 0; i < NBUCKET; i++) { 36 | printf("%d: ", i); 37 | for (e = table[i]; e != 0; e = e->next) { 38 | printf("%d ", e->key); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | 44 | static void 45 | insert(int key, int value, struct entry **p, struct entry *n) 46 | { 47 | struct entry *e = malloc(sizeof(struct entry)); 48 | e->key = key; 49 | e->value = value; 50 | e->next = n; 51 | *p = e; 52 | } 53 | 54 | static 55 | void put(int key, int value) 56 | { 57 | struct entry *n, **p; 58 | for (p = &table[key%NBUCKET], n = table[key % NBUCKET]; n != 0; p = &n->next, n = n->next) { 59 | if (n->key > key) { 60 | insert(key, value, p, n); 61 | goto done; 62 | } 63 | } 64 | insert(key, value, p, n); 65 | done: 66 | return; 67 | } 68 | 69 | static struct entry* 70 | get(int key) 71 | { 72 | struct entry *e = 0; 73 | for (e = table[key % NBUCKET]; e != 0; e = e->next) { 74 | if (e->key == key) break; 75 | } 76 | return e; 77 | } 78 | 79 | static void * 80 | thread(void *xa) 81 | { 82 | long n = (long) xa; 83 | int i; 84 | int b = NKEYS/nthread; 85 | int k = 0; 86 | double t1, t0; 87 | 88 | // printf("b = %d\n", b); 89 | t0 = now(); 90 | for (i = 0; i < b; i++) { 91 | // printf("%d: put %d\n", n, b*n+i); 92 | put(keys[b*n + i], n); 93 | } 94 | t1 = now(); 95 | printf("%ld: put time = %f\n", n, t1-t0); 96 | 97 | // Should use pthread_barrier, but MacOS doesn't support it ... 98 | __sync_fetch_and_add(&done, 1); 99 | while (done < nthread) ; 100 | 101 | t0 = now(); 102 | for (i = 0; i < NKEYS; i++) { 103 | struct entry *e = get(keys[i]); 104 | if (e == 0) k++; 105 | } 106 | t1 = now(); 107 | printf("%ld: lookup time = %f\n", n, t1-t0); 108 | printf("%ld: %d keys missing\n", n, k); 109 | } 110 | 111 | int 112 | main(int argc, char *argv[]) 113 | { 114 | pthread_t *tha; 115 | void *value; 116 | long i; 117 | double t1, t0; 118 | 119 | if (argc < 2) { 120 | fprintf(stderr, "%s: %s nthread\n", argv[0], argv[0]); 121 | exit(-1); 122 | } 123 | nthread = atoi(argv[1]); 124 | tha = malloc(sizeof(pthread_t) * nthread); 125 | srandom(0); 126 | assert(NKEYS % nthread == 0); 127 | for (i = 0; i < NKEYS; i++) { 128 | keys[i] = random(); 129 | } 130 | t0 = now(); 131 | for(i = 0; i < nthread; i++) { 132 | assert(pthread_create(&tha[i], NULL, thread, (void *) i) == 0); 133 | } 134 | for(i = 0; i < nthread; i++) { 135 | assert(pthread_join(tha[i], &value) == 0); 136 | } 137 | t1 = now(); 138 | printf("completion time = %f\n", t1-t0); 139 | } 140 | -------------------------------------------------------------------------------- /sh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // Simplifed xv6 shell. 11 | 12 | #define MAXARGS 10 13 | 14 | // All commands have at least a type. Have looked at the type, the code 15 | // typically casts the *cmd to some specific cmd type. 16 | struct cmd { 17 | int type; // ' ' (exec), | (pipe), '<' or '>' for redirection 18 | }; 19 | 20 | struct execcmd { 21 | int type; // ' ' 22 | char *argv[MAXARGS]; // arguments to the command to be exec-ed 23 | }; 24 | 25 | struct redircmd { 26 | int type; // < or > 27 | struct cmd *cmd; // the command to be run (e.g., an execcmd) 28 | char *file; // the input/output file 29 | int mode; // the mode to open the file with 30 | int fd; // the file descriptor number to use for the file 31 | }; 32 | 33 | struct pipecmd { 34 | int type; // | 35 | struct cmd *left; // left side of pipe 36 | struct cmd *right; // right side of pipe 37 | }; 38 | 39 | int fork1(void); // Fork but exits on failure. 40 | struct cmd *parsecmd(char*); 41 | 42 | // Execute cmd. Never returns. 43 | void 44 | runcmd(struct cmd *cmd) 45 | { 46 | int p[2], r; 47 | struct execcmd *ecmd; 48 | struct pipecmd *pcmd; 49 | struct redircmd *rcmd; 50 | 51 | if(cmd == 0) 52 | exit(0); 53 | 54 | switch(cmd->type){ 55 | default: 56 | fprintf(stderr, "unknown runcmd\n"); 57 | exit(-1); 58 | 59 | case ' ': 60 | ecmd = (struct execcmd*)cmd; 61 | if(ecmd->argv[0] == 0) 62 | exit(0); 63 | fprintf(stderr, "exec not implemented\n"); 64 | // Your code here ... 65 | break; 66 | 67 | case '>': 68 | case '<': 69 | rcmd = (struct redircmd*)cmd; 70 | fprintf(stderr, "redir not implemented\n"); 71 | // Your code here ... 72 | runcmd(rcmd->cmd); 73 | break; 74 | 75 | case '|': 76 | pcmd = (struct pipecmd*)cmd; 77 | fprintf(stderr, "pipe not implemented\n"); 78 | // Your code here ... 79 | break; 80 | } 81 | exit(0); 82 | } 83 | 84 | int 85 | getcmd(char *buf, int nbuf) 86 | { 87 | 88 | if (isatty(fileno(stdin))) 89 | fprintf(stdout, "$ "); 90 | memset(buf, 0, nbuf); 91 | fgets(buf, nbuf, stdin); 92 | if(buf[0] == 0) // EOF 93 | return -1; 94 | return 0; 95 | } 96 | 97 | int 98 | main(void) 99 | { 100 | static char buf[100]; 101 | int fd, r; 102 | 103 | // Read and run input commands. 104 | while(getcmd(buf, sizeof(buf)) >= 0){ 105 | if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){ 106 | // Clumsy but will have to do for now. 107 | // Chdir has no effect on the parent if run in the child. 108 | buf[strlen(buf)-1] = 0; // chop \n 109 | if(chdir(buf+3) < 0) 110 | fprintf(stderr, "cannot cd %s\n", buf+3); 111 | continue; 112 | } 113 | if(fork1() == 0) 114 | runcmd(parsecmd(buf)); 115 | wait(&r); 116 | } 117 | exit(0); 118 | } 119 | 120 | int 121 | fork1(void) 122 | { 123 | int pid; 124 | 125 | pid = fork(); 126 | if(pid == -1) 127 | perror("fork"); 128 | return pid; 129 | } 130 | 131 | struct cmd* 132 | execcmd(void) 133 | { 134 | struct execcmd *cmd; 135 | 136 | cmd = malloc(sizeof(*cmd)); 137 | memset(cmd, 0, sizeof(*cmd)); 138 | cmd->type = ' '; 139 | return (struct cmd*)cmd; 140 | } 141 | 142 | struct cmd* 143 | redircmd(struct cmd *subcmd, char *file, int type) 144 | { 145 | struct redircmd *cmd; 146 | 147 | cmd = malloc(sizeof(*cmd)); 148 | memset(cmd, 0, sizeof(*cmd)); 149 | cmd->type = type; 150 | cmd->cmd = subcmd; 151 | cmd->file = file; 152 | cmd->mode = (type == '<') ? O_RDONLY : O_WRONLY|O_CREAT|O_TRUNC; 153 | cmd->fd = (type == '<') ? 0 : 1; 154 | return (struct cmd*)cmd; 155 | } 156 | 157 | struct cmd* 158 | pipecmd(struct cmd *left, struct cmd *right) 159 | { 160 | struct pipecmd *cmd; 161 | 162 | cmd = malloc(sizeof(*cmd)); 163 | memset(cmd, 0, sizeof(*cmd)); 164 | cmd->type = '|'; 165 | cmd->left = left; 166 | cmd->right = right; 167 | return (struct cmd*)cmd; 168 | } 169 | 170 | // Parsing 171 | 172 | char whitespace[] = " \t\r\n\v"; 173 | char symbols[] = "<|>"; 174 | 175 | int 176 | gettoken(char **ps, char *es, char **q, char **eq) 177 | { 178 | char *s; 179 | int ret; 180 | 181 | s = *ps; 182 | while(s < es && strchr(whitespace, *s)) 183 | s++; 184 | if(q) 185 | *q = s; 186 | ret = *s; 187 | switch(*s){ 188 | case 0: 189 | break; 190 | case '|': 191 | case '<': 192 | s++; 193 | break; 194 | case '>': 195 | s++; 196 | break; 197 | default: 198 | ret = 'a'; 199 | while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s)) 200 | s++; 201 | break; 202 | } 203 | if(eq) 204 | *eq = s; 205 | 206 | while(s < es && strchr(whitespace, *s)) 207 | s++; 208 | *ps = s; 209 | return ret; 210 | } 211 | 212 | int 213 | peek(char **ps, char *es, char *toks) 214 | { 215 | char *s; 216 | 217 | s = *ps; 218 | while(s < es && strchr(whitespace, *s)) 219 | s++; 220 | *ps = s; 221 | return *s && strchr(toks, *s); 222 | } 223 | 224 | struct cmd *parseline(char**, char*); 225 | struct cmd *parsepipe(char**, char*); 226 | struct cmd *parseexec(char**, char*); 227 | 228 | // make a copy of the characters in the input buffer, starting from s through es. 229 | // null-terminate the copy to make it a string. 230 | char 231 | *mkcopy(char *s, char *es) 232 | { 233 | int n = es - s; 234 | char *c = malloc(n+1); 235 | assert(c); 236 | strncpy(c, s, n); 237 | c[n] = 0; 238 | return c; 239 | } 240 | 241 | struct cmd* 242 | parsecmd(char *s) 243 | { 244 | char *es; 245 | struct cmd *cmd; 246 | 247 | es = s + strlen(s); 248 | cmd = parseline(&s, es); 249 | peek(&s, es, ""); 250 | if(s != es){ 251 | fprintf(stderr, "leftovers: %s\n", s); 252 | exit(-1); 253 | } 254 | return cmd; 255 | } 256 | 257 | struct cmd* 258 | parseline(char **ps, char *es) 259 | { 260 | struct cmd *cmd; 261 | cmd = parsepipe(ps, es); 262 | return cmd; 263 | } 264 | 265 | struct cmd* 266 | parsepipe(char **ps, char *es) 267 | { 268 | struct cmd *cmd; 269 | 270 | cmd = parseexec(ps, es); 271 | if(peek(ps, es, "|")){ 272 | gettoken(ps, es, 0, 0); 273 | cmd = pipecmd(cmd, parsepipe(ps, es)); 274 | } 275 | return cmd; 276 | } 277 | 278 | struct cmd* 279 | parseredirs(struct cmd *cmd, char **ps, char *es) 280 | { 281 | int tok; 282 | char *q, *eq; 283 | 284 | while(peek(ps, es, "<>")){ 285 | tok = gettoken(ps, es, 0, 0); 286 | if(gettoken(ps, es, &q, &eq) != 'a') { 287 | fprintf(stderr, "missing file for redirection\n"); 288 | exit(-1); 289 | } 290 | switch(tok){ 291 | case '<': 292 | cmd = redircmd(cmd, mkcopy(q, eq), '<'); 293 | break; 294 | case '>': 295 | cmd = redircmd(cmd, mkcopy(q, eq), '>'); 296 | break; 297 | } 298 | } 299 | return cmd; 300 | } 301 | 302 | struct cmd* 303 | parseexec(char **ps, char *es) 304 | { 305 | char *q, *eq; 306 | int tok, argc; 307 | struct execcmd *cmd; 308 | struct cmd *ret; 309 | 310 | ret = execcmd(); 311 | cmd = (struct execcmd*)ret; 312 | 313 | argc = 0; 314 | ret = parseredirs(ret, ps, es); 315 | while(!peek(ps, es, "|")){ 316 | if((tok=gettoken(ps, es, &q, &eq)) == 0) 317 | break; 318 | if(tok != 'a') { 319 | fprintf(stderr, "syntax error\n"); 320 | exit(-1); 321 | } 322 | cmd->argv[argc] = mkcopy(q, eq); 323 | argc++; 324 | if(argc >= MAXARGS) { 325 | fprintf(stderr, "too many args\n"); 326 | exit(-1); 327 | } 328 | ret = parseredirs(ret, ps, es); 329 | } 330 | cmd->argv[argc] = 0; 331 | return ret; 332 | } 333 | -------------------------------------------------------------------------------- /uthread.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "stat.h" 3 | #include "user.h" 4 | 5 | /* Possible states of a thread; */ 6 | #define FREE 0x0 7 | #define RUNNING 0x1 8 | #define RUNNABLE 0x2 9 | 10 | #define STACK_SIZE 8192 11 | #define MAX_THREAD 4 12 | 13 | typedef struct thread thread_t, *thread_p; 14 | typedef struct mutex mutex_t, *mutex_p; 15 | 16 | struct thread { 17 | int sp; /* curent stack pointer */ 18 | char stack[STACK_SIZE]; /* the thread's stack */ 19 | int state; /* running, runnable, waiting */ 20 | }; 21 | static thread_t all_thread[MAX_THREAD]; 22 | thread_p current_thread; 23 | thread_p next_thread; 24 | extern void thread_switch(void); 25 | 26 | void 27 | thread_init(void) 28 | { 29 | current_thread = &all_thread[0]; 30 | current_thread->state = RUNNING; 31 | } 32 | 33 | static void 34 | thread_schedule(void) 35 | { 36 | thread_p t; 37 | 38 | /* Find another runnable thread. */ 39 | for (t = all_thread; t < all_thread + MAX_THREAD; t++) { 40 | if (t->state == RUNNABLE && t != current_thread) { 41 | next_thread = t; 42 | break; 43 | } 44 | } 45 | 46 | if (t >= all_thread + MAX_THREAD && current_thread->state == RUNNABLE) { 47 | /* The current thread is the only runnable thread; run it. */ 48 | next_thread = current_thread; 49 | } 50 | 51 | if (next_thread == 0) { 52 | printf(2, "thread_schedule: no runnable threads; deadlock\n"); 53 | exit(); 54 | } 55 | 56 | if (current_thread != next_thread) { /* switch threads? */ 57 | next_thread->state = RUNNING; 58 | thread_switch(); 59 | } else 60 | next_thread = 0; 61 | } 62 | 63 | void 64 | thread_create(void (*func)()) 65 | { 66 | thread_p t; 67 | 68 | for (t = all_thread; t < all_thread + MAX_THREAD; t++) { 69 | if (t->state == FREE) break; 70 | } 71 | t->sp = (int) (t->stack + STACK_SIZE); // set sp to the top of the stack 72 | t->sp -= 4; // space for return address 73 | * (int *) (t->sp) = (int)func; // push return address on stack 74 | t->sp -= 32; // space for registers that thread_switch will push 75 | t->state = RUNNABLE; 76 | } 77 | 78 | void 79 | thread_yield(void) 80 | { 81 | current_thread->state = RUNNABLE; 82 | thread_schedule(); 83 | } 84 | 85 | static void 86 | mythread(void) 87 | { 88 | int i; 89 | printf(1, "my thread running\n"); 90 | for (i = 0; i < 100; i++) { 91 | printf(1, "my thread 0x%x\n", (int) current_thread); 92 | thread_yield(); 93 | } 94 | printf(1, "my thread: exit\n"); 95 | current_thread->state = FREE; 96 | thread_schedule(); 97 | } 98 | 99 | 100 | int 101 | main(int argc, char *argv[]) 102 | { 103 | thread_init(); 104 | thread_create(mythread); 105 | thread_create(mythread); 106 | thread_schedule(); 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /uthread_switch.S: -------------------------------------------------------------------------------- 1 | .text 2 | 3 | /* Switch from current_thread to next_thread. Make next_thread 4 | * the current_thread, and set next_thread to 0. 5 | * Use eax as a temporary register, which should be caller saved. 6 | */ 7 | .globl thread_switch 8 | thread_switch: 9 | /* YOUR CODE HERE */ 10 | ret /* pop return address from stack */ 11 | --------------------------------------------------------------------------------