From 11f2704bafef245d2140cd62257dec4237f93151 Mon Sep 17 00:00:00 2001 From: tobyatgithub Date: Mon, 21 Nov 2022 13:01:12 -0800 Subject: [PATCH] update readme, add chap27 code and code.md --- .idea/.gitignore | 8 + .idea/cs5600_computerSystem_fall2022.iml | 8 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .../Chap27-threads-api/Chap27-thread-api.md | 322 ++++++++++++++++++ .../Chap27-threads-api/threads-api/Makefile | 23 ++ .../Chap27-threads-api/threads-api/README.md | 32 ++ .../threads-api/common_threads.h | 37 ++ .../threads-api/main-deadlock | Bin 0 -> 16472 bytes .../threads-api/main-deadlock-global | Bin 0 -> 16560 bytes .../threads-api/main-deadlock-global.c | 31 ++ .../threads-api/main-deadlock.c | 28 ++ .../Chap27-threads-api/threads-api/main-race | Bin 0 -> 12344 bytes .../threads-api/main-race.c | 24 ++ .../threads-api/main-signal | Bin 0 -> 11608 bytes .../threads-api/main-signal-cv | Bin 0 -> 17632 bytes .../threads-api/main-signal-cv.c | 55 +++ .../threads-api/main-signal.c | 20 ++ README.md | 3 + 19 files changed, 605 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/cs5600_computerSystem_fall2022.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/Chap27-thread-api.md create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/Makefile create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/README.md create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/common_threads.h create mode 100755 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock create mode 100755 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global.c create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock.c create mode 100755 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-race create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-race.c create mode 100755 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal create mode 100755 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal-cv create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal-cv.c create mode 100644 Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal.c diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..1c2fda5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/cs5600_computerSystem_fall2022.iml b/.idea/cs5600_computerSystem_fall2022.iml new file mode 100644 index 0000000..633cf01 --- /dev/null +++ b/.idea/cs5600_computerSystem_fall2022.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b2e0018 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Homework9-threads-and-locks/Chap27-threads-api/Chap27-thread-api.md b/Homework9-threads-and-locks/Chap27-threads-api/Chap27-thread-api.md new file mode 100644 index 0000000..d7fe59c --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/Chap27-thread-api.md @@ -0,0 +1,322 @@ +# Homework Simulation Chapter 27 Thread API + +## Homework (Code) + +In this section, we’ll write some simple multi-threaded programs and use a specific tool, called `helgrind`, to find +problems in these programs. + +Read the README in the homework download for details on how to build the programs and run `helgrind`. + +## Question 1 + +> First build `main-race.c`. Examine the code so you can see the (hopefully obvious) data race in the code. Now +> run `helgrind` (by typing `valgrind --tool=helgrind main-race`) to see how it reports the race. Does it point to the +> right lines of code? What other information does it give to you? + +**Answer:** + +Yes, the `balance++` is not protected and being modified both in the main thread and in the newly created p_thread. + +``` +i$ valgrind --tool=helgrind ./main-race +==4916== Helgrind, a thread error detector +==4916== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. +==4916== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info +==4916== Command: ./main-race +==4916== +==4916== ---Thread-Announcement------------------------------------------ +==4916== +==4916== Thread #1 is the program's root thread +==4916== +==4916== ---Thread-Announcement------------------------------------------ +==4916== +==4916== Thread #2 was created +==4916== at 0x518460E: clone (clone.S:71) +==4916== by 0x4E4BEC4: create_thread (createthread.c:100) +==4916== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797) +==4916== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==4916== by 0x1087E2: main (main-race.c:14) +==4916== +==4916== ---------------------------------------------------------------- +==4916== +==4916== Possible data race during read of size 4 at 0x309014 by thread #1 +==4916== Locks held: none +==4916== at 0x108806: main (main-race.c:15) +==4916== +==4916== This conflicts with a previous write of size 4 by thread #2 +==4916== Locks held: none +==4916== at 0x10879B: worker (main-race.c:8) +==4916== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==4916== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==4916== by 0x518461E: clone (clone.S:95) +==4916== Address 0x309014 is 0 bytes inside data symbol "balance" +==4916== +==4916== ---------------------------------------------------------------- +==4916== +==4916== Possible data race during write of size 4 at 0x309014 by thread #1 +==4916== Locks held: none +==4916== at 0x10880F: main (main-race.c:15) +==4916== +==4916== This conflicts with a previous write of size 4 by thread #2 +==4916== Locks held: none +==4916== at 0x10879B: worker (main-race.c:8) +==4916== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==4916== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==4916== by 0x518461E: clone (clone.S:95) +==4916== Address 0x309014 is 0 bytes inside data symbol "balance" +==4916== +==4916== +==4916== For counts of detected and suppressed errors, rerun with: -v +==4916== Use --history-level=approx or =none to gain increased speed, at +==4916== the cost of reduced accuracy of conflicting-access information +==4916== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) +``` + +It correctly pointed out the two data-race places: main-race.c: line 8 and line 15. + +## Question 2 + +> What happens when you remove one of the offending lines of code? Now add a lock around one of the updates to the +> shared variable, and then around both. What does helgrind report in each of these cases? + +**Answer:** + +1. Remove the `balance++` in the worker method and helgrind gives no errors: + +``` +$ valgrind --tool=helgrind ./main-race +==5085== Helgrind, a thread error detector +==5085== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. +==5085== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info +==5085== Command: ./main-race +==5085== +==5085== +==5085== For counts of detected and suppressed errors, rerun with: -v +==5085== Use --history-level=approx or =none to gain increased speed, at +==5085== the cost of reduced accuracy of conflicting-access information +==5085== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +``` + +2. Adding one lock is not enough, will result in the same error message as shown in the last question. +3. Adding two locks will be sufficient and there will be no error nor race condition. + +## Question 3 + +> Now let’s look at `main-deadlock.c`. Examine the code. This code has a problem known as **deadlock** (which we discuss +> in much more depth in a forthcoming chapter). Can you see what problem it might have? + +**Answer:** + +Yes, both threads will try to acquire lock1 and lock2. Thus possibly locking out each other. + +## Question 4 + +> Now run `helgrind` on this code. What does `helgrind` report? + +**Answer:** + +``` +$ valgrind --tool=helgrind ./main-deadlock +==5874== Helgrind, a thread error detector +==5874== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. +==5874== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info +==5874== Command: ./main-deadlock +==5874== +==5874== ---Thread-Announcement------------------------------------------ +==5874== +==5874== Thread #3 was created +==5874== at 0x518460E: clone (clone.S:71) +==5874== by 0x4E4BEC4: create_thread (createthread.c:100) +==5874== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797) +==5874== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x1089E8: main (main-deadlock.c:24) +==5874== +==5874== ---------------------------------------------------------------- +==5874== +==5874== Thread #3: lock order "0x30A040 before 0x30A080" violated +==5874== +==5874== Observed (incorrect) order is: acquisition of lock at 0x30A080 +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x1088B6: worker (main-deadlock.c:13) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== +==5874== followed by a later acquisition of lock at 0x30A040 +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x1088E5: worker (main-deadlock.c:14) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== +==5874== Required order was established by acquisition of lock at 0x30A040 +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x108858: worker (main-deadlock.c:10) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== +==5874== followed by a later acquisition of lock at 0x30A080 +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x108887: worker (main-deadlock.c:11) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== +==5874== Lock at 0x30A040 was first observed +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x108858: worker (main-deadlock.c:10) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== Address 0x30a040 is 0 bytes inside data symbol "m1" +==5874== +==5874== Lock at 0x30A080 was first observed +==5874== at 0x4C3603C: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x108887: worker (main-deadlock.c:11) +==5874== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==5874== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==5874== by 0x518461E: clone (clone.S:95) +==5874== Address 0x30a080 is 0 bytes inside data symbol "m2" +==5874== +==5874== +==5874== +==5874== For counts of detected and suppressed errors, rerun with: -v +==5874== Use --history-level=approx or =none to gain increased speed, at +==5874== the cost of reduced accuracy of conflicting-access information +==5874== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 7 from 7) +``` + +"Thread #3: lock order "0x30A040 before 0x30A080" violated" + +## Question 5 + +> Now run `helgrind` on `main-deadlock-global.c`. Examine the code; does it have the same problem that `main-deadlock.c` +> has? Should `helgrind` be reporting the same error? What does this tell you about tools like `helgrind`? + +**Answer:** + +`helgrind` is reporting the same error. And I believe it should not report the same error as the `g` lock shall solve +the race condition issue. Thus we can see that the `helgrind` tool is not perfect (and detecting race condition could be +challenging indeed.) + +## Question 6 + +> Let’s next look at `main-signal.c`. This code uses a variable (`done`) to signal that the child is done and that the +> parent can now continue. Why is this code inefficient? (what does the parent end up spending its time doing, +> particularly if the child thread takes a long time to complete?) + + +**Answer:** + +This is the issue mentioned in the page 8 of the textbook chap 27. And the author warned us: "First, it performs poorly +in many cases (spinning for a long time just wastes CPU cycles). Second, it is error prone. As recent research +shows [X+10], it is surprisingly easy to make mistakes when using flags (as above) to synchronize between threads; in +that study, roughly half the uses of these ad hoc synchronizations were buggy! Don’t be lazy; use condition variables +even when you think you can get away without doing so." + +## Question 7 + +> Now run `helgrind` on this program. What does it report? Is the code correct? + +**Answer:** + +``` +$ valgrind --tool=helgrind ./main-signal +==6190== Helgrind, a thread error detector +==6190== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. +==6190== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info +==6190== Command: ./main-signal +==6190== +this should print first +==6190== ---Thread-Announcement------------------------------------------ +==6190== +==6190== Thread #1 is the program's root thread +==6190== +==6190== ---Thread-Announcement------------------------------------------ +==6190== +==6190== Thread #2 was created +==6190== at 0x518460E: clone (clone.S:71) +==6190== by 0x4E4BEC4: create_thread (createthread.c:100) +==6190== by 0x4E4BEC4: pthread_create@@GLIBC_2.2.5 (pthread_create.c:797) +==6190== by 0x4C38A27: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==6190== by 0x1087DD: main (main-signal.c:15) +==6190== +==6190== ---------------------------------------------------------------- +==6190== +==6190== Possible data race during read of size 4 at 0x309014 by thread #1 +==6190== Locks held: none +==6190== at 0x108802: main (main-signal.c:16) +==6190== +==6190== This conflicts with a previous write of size 4 by thread #2 +==6190== Locks held: none +==6190== at 0x108792: worker (main-signal.c:9) +==6190== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==6190== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==6190== by 0x518461E: clone (clone.S:95) +==6190== Address 0x309014 is 0 bytes inside data symbol "done" +==6190== +==6190== ---------------------------------------------------------------- +==6190== +==6190== Possible data race during write of size 1 at 0x5C551A5 by thread #1 +==6190== Locks held: none +==6190== at 0x4C3E546: mempcpy (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==6190== by 0x50EE933: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1258) +==6190== by 0x50E3A3E: puts (ioputs.c:40) +==6190== by 0x108817: main (main-signal.c:18) +==6190== Address 0x5c551a5 is 21 bytes inside a block of size 1,024 alloc'd +==6190== at 0x4C32F2F: malloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==6190== by 0x50E113B: _IO_file_doallocate (filedoalloc.c:101) +==6190== by 0x50F1328: _IO_doallocbuf (genops.c:365) +==6190== by 0x50F0447: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:759) +==6190== by 0x50EE98C: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1266) +==6190== by 0x50E3A3E: puts (ioputs.c:40) +==6190== by 0x108791: worker (main-signal.c:8) +==6190== by 0x4C38C26: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so) +==6190== by 0x4E4B6DA: start_thread (pthread_create.c:463) +==6190== by 0x518461E: clone (clone.S:95) +==6190== Block was alloc'd by thread #2 +==6190== +this should print last +==6190== +==6190== For counts of detected and suppressed errors, rerun with: -v +==6190== Use --history-level=approx or =none to gain increased speed, at +==6190== the cost of reduced accuracy of conflicting-access information +==6190== ERROR SUMMARY: 23 errors from 2 contexts (suppressed: 40 from 40) +``` + +Potential data race between line 9 `done = 1;` and line 16 `while (done == 0)` + +## Question 8 + +> Now look at a slightly modified version of the code, which is found in `main-signal-cv.c`. This version uses a +> condition variable to do the signaling (and associated lock). Why is this code preferred to the previous version? Is +> it +> correctness, or performance, or both? + +**Answer:** + +Both. + +## Question 9 + +> Once again run `helgrind` on `main-signal-cv`. Does it report any errors? + +**Answer:** +No errors! + +``` +$ valgrind --tool=helgrind ./main-signal-cv +==6342== Helgrind, a thread error detector +==6342== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. +==6342== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info +==6342== Command: ./main-signal-cv +==6342== +this should print first +this should print last +==6342== +==6342== For counts of detected and suppressed errors, rerun with: -v +==6342== Use --history-level=approx or =none to gain increased speed, at +==6342== the cost of reduced accuracy of conflicting-access information +==6342== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 7 from 7) +``` \ No newline at end of file diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/Makefile b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/Makefile new file mode 100644 index 0000000..029638f --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/Makefile @@ -0,0 +1,23 @@ + +FLAGS = -Wall -pthread -g + +all: main-race main-deadlock main-deadlock-global main-signal main-signal-cv + +clean: + rm -f main-race main-deadlock main-deadlock-global main-signal main-signal-cv + +main-race: main-race.c common_threads.h + gcc -o main-race main-race.c $(FLAGS) + +main-deadlock: main-deadlock.c common_threads.h + gcc -o main-deadlock main-deadlock.c $(FLAGS) + +main-deadlock-global: main-deadlock-global.c common_threads.h + gcc -o main-deadlock-global main-deadlock-global.c $(FLAGS) + +main-signal: main-signal.c common_threads.h + gcc -o main-signal main-signal.c $(FLAGS) + +main-signal-cv: main-signal-cv.c common_threads.h + gcc -o main-signal-cv main-signal-cv.c $(FLAGS) + diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/README.md b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/README.md new file mode 100644 index 0000000..0fb9d8e --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/README.md @@ -0,0 +1,32 @@ + +# Overview + +In this homework, you'll use a real tool on Linux to find problems in +multi-threaded code. The tool is called `helgrind` (available as part of the +valgrind suite of debugging tools). + +See `http://valgrind.org/docs/manual/hg-manual.htm` for details about +the tool, including how to download and install it (if it's not +already on your Linux system). + +You'll then look at a number of multi-threaded C programs to see how you can +use the tool to debug problematic threaded code. + +First things first: download and install `valgrind` and the related `helgrind` tool. + +Then, type `make` to build all the different programs. Examine the `Makefile` +for more details on how that works. + +Then, you have a few different C programs to look at: +- `main-race.c`: A simple race condition +- `main-deadlock.c`: A simple deadlock +- `main-deadlock-global.c`: A solution to the deadlock problem +- `main-signal.c`: A simple child/parent signaling example +- `main-signal-cv.c`: A more efficient signaling via condition variables +- `common_threads.h`: Header file with wrappers to make code check errors and be more readable + +With these programs, you can now answer the questions in the textbook. + + + + diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/common_threads.h b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/common_threads.h new file mode 100644 index 0000000..c4da8a6 --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/common_threads.h @@ -0,0 +1,37 @@ +#ifndef __common_threads_h__ +#define __common_threads_h__ + +#include +#include +#include + +#ifdef __linux__ +#include +#endif + +#define Pthread_create(thread, attr, start_routine, arg) assert(pthread_create(thread, attr, start_routine, arg) == 0); +#define Pthread_join(thread, value_ptr) assert(pthread_join(thread, value_ptr) == 0); + +#define Pthread_mutex_init(m, v) assert(pthread_mutex_init(m, v) == 0); +#define Pthread_mutex_lock(m) assert(pthread_mutex_lock(m) == 0); +#define Pthread_mutex_unlock(m) assert(pthread_mutex_unlock(m) == 0); + +#define Pthread_cond_init(cond, v) assert(pthread_cond_init(cond, v) == 0); +#define Pthread_cond_signal(cond) assert(pthread_cond_signal(cond) == 0); +#define Pthread_cond_wait(cond, mutex) assert(pthread_cond_wait(cond, mutex) == 0); + +#define Mutex_init(m) assert(pthread_mutex_init(m, NULL) == 0); +#define Mutex_lock(m) assert(pthread_mutex_lock(m) == 0); +#define Mutex_unlock(m) assert(pthread_mutex_unlock(m) == 0); + +#define Cond_init(cond) assert(pthread_cond_init(cond, NULL) == 0); +#define Cond_signal(cond) assert(pthread_cond_signal(cond) == 0); +#define Cond_wait(cond, mutex) assert(pthread_cond_wait(cond, mutex) == 0); + +#ifdef __linux__ +#define Sem_init(sem, value) assert(sem_init(sem, 0, value) == 0); +#define Sem_wait(sem) assert(sem_wait(sem) == 0); +#define Sem_post(sem) assert(sem_post(sem) == 0); +#endif // __linux__ + +#endif // __common_threads_h__ diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock new file mode 100755 index 0000000000000000000000000000000000000000..b4466cb71e5c6ae01a9edf0ed3deb26437d5713b GIT binary patch literal 16472 zcmeHOdvILUc|Uh|CGD=H_1f}7!lb??7-WK055HuHv1D2Him{C`GQk0SS?#W*#jD-z zK8$6c@nGUuaZKb6O(>lI?5s08nkGL_z;2vSuzuT3-cYPlPf^x9oY;G~yp98)eK(F&bbmISnvsW7F0 z^r)T)zv!DKMymBgess`W;-9#f(9m~#E7ZuIZf=}x*9X%v_emcr&Y z^D&B#)<|{|Wjs^XyBT^^&fhi!HE@g0Zz{j+(dB2#?a-V|^etc7oQyRk6RE?$hrZ2U<^>5uP!;^e=dIMn&h?fb0?@$}sftUG=k z>GB`Z-#UC2$XR93Ga)m}$o~kka`-2wke`@B{tL)W{GDMZ1m)}}rjR#5ZsP9@e-FVG zP9ga9_?NRU&_0>`U^X6!QKPg7tEYFP6^m!%1BqNdp6%V(l}xANy^+3TT&0yw(I%{+ zLO#CBN~WVb1dF1uosSpUyVHqOkx}qMN-CfvQ7Xf7VOhC+B)Y?j4(_n}BZ(vgkz6jG z%`+F>6|wpgsYo)h8>O;HlT-j0iopE(4LxhRthP{FXjw776u5z*bV}na_=EbH8@-VF znM-n>V&5|mI{-QL&lda(mnD33r~W|8W+xK0r2PhX3WoeCJn;(el6d0z`N8*ni;nEq z&k2^F)6Y|{X_A^?IrFC%C^_B3e4h~Rb(`G&+#9pW?f3mTn;aD@hVwSLzbF;rh?Z0F z44eF@OhkN;qkgS4*V2g!jJf4Iil3*kFqA=a1)I z<0r#=pYzgPr^5KMpVpgVe9;+PXDsMIjQBxHJp1}M!lEA$PgOa4Qu415PcA)sT=M@) zJXPuJQOSRYc&f_T!;*i7cyjgG(V~9;D0mrKf7vtg^7in^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and-locks/Chap27-threads-api/threads-api/main-deadlock-global b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global new file mode 100755 index 0000000000000000000000000000000000000000..3507182cf9384a1c3481b93d0a8f6a9b89bd0c0d GIT binary patch literal 16560 zcmeHOdvsh!d7rzxl6KejdTse3+X1hWIFUoNdiW6;FqUk~S*3miW2X*~+^n=KX=`a$ z?8C7F1|J+kgfVq`(&7`+Q&VUfT3Q0-v`KmnI5kZ}OR3=$5(+&$PQsCCA!-AuNr=_o zZ)U#L)zu2QoW@zgn$R_25HVX41P1L8=Ltt!bfQD-VKFuQOl-etMO~36&C(*4eaLCtjyn(1eZ%J!8u;#DeLN(Ell>-WjWR#_9=GR!^wZk9FhkpiTGFT}WdPN-V>z zFO)Nkw=L4_EGl@RqW4zlv7Endh+E(Ro8Mf1Ibq8$RNA3+IM=sjQ|oZ1WjL3w9BVn& zxus>xrevv*?9lb5ePVCkcSz?%w~DJ+=4JR%#`Kpzb7}uGcb`9g`wLCy?z;S^fxg3M zpP{b&5dF2o+ksqK1HA;YL=E}xLRJg^$Q<$ubIAV<@&tbVa1?@C_H%Q{TOd#1=MSHU z;A+1R{LT2)vairSqvfGuHl1Okv?;gu;9fVAEoKLErE<1-aPQ9HLOy#i-8Y;yX*E-< z33sGY&K`4z3;jowi27l>oUO9IRmkP5jDc73S^*{LXBoCA*DaOP{YTvXp(E}U**%fj^^OS=QPe0n%{4@%`ylPUm=q+$N%J-s`2x*f@m>Rf~)^&3{UESJN`3##vfXK?hN!IL-mfX0)T=ZB2@ zHXFI$J|{#zX`iRzM1pEc3pCRFOP&1paOzau-c<>e z`oQ^eVCrJ(9uicp%|3T{H^OLt8+;h5a zHFQ&_7kvUIQ>V8bLrzo6?}Lq>E?&D!i#`SBQ0jEu$3atDFQ>*|D6c{l-%b`)Ty)3O z_Xd)S*WRn`-*JJpn&x3LN8strecH^1E6`6pbtRY@pG-Y9u|4HHo%-69auW=$vj!K^ z;9T}jY}){ss(dIAmMya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W&y%Dw!F}!=9m7km{79m6|=)>D%jZQa9Jg_my(Dk2Mb{Fkq!QH z+yGS2ogtYWvLj_E10%v2RWoFahQyIWdL-A6GI4>Hg!@$z!<8#4$g+KvK_JulL5yw7 z=JEptiKqMexQMnX__ix6ad=6d8UG8)nG_zqm}8jGX(K}YGb&=e;TkZUONhSA_k||B zV8rFUA>wvKy!x^(5PGcuE%oc~g8XHi0ZIO6ZA7S)U*Fh~ zm6;;2{KQE4DPyf62xRn$ zsL8rYC}uP>0`u~QaWGzeS$_%Taf>&^EhF?n@Lv6dofr$1@`?@Nh5o)zU*<1DJA4I} ze8t`;eEM>3EY$9X3w+l2`IOa{_LKRUP=1=?4N`8A@z{DSFb5TyL?8RsFRIa5$k`tBD#lr21d{T5cQ1arvF)SRq!*!n15SUVj`ly!6zq{LFg4vTqoFnTN5VYw<~&rT?Bz& z#iRkKUw^&P|Bz3?$EuB1-(Sy1n~naDq+xvJ z7dxaf_AEq`vpmm77?S4^;yfB=TQkV>m(K@Z7CK#RGGx+cT=F%kT+7tfpWI>)Z}BPk GSoPmc%KuFO literal 0 HcmV?d00001 diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global.c b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global.c new file mode 100644 index 0000000..55202dc --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock-global.c @@ -0,0 +1,31 @@ +#include + +#include "common_threads.h" + +pthread_mutex_t g = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; + +void* worker(void* arg) { + Pthread_mutex_lock(&g); + if ((long long) arg == 0) { + Pthread_mutex_lock(&m1); + Pthread_mutex_lock(&m2); + } else { + Pthread_mutex_lock(&m2); + Pthread_mutex_lock(&m1); + } + Pthread_mutex_unlock(&m1); + Pthread_mutex_unlock(&m2); + Pthread_mutex_unlock(&g); + return NULL; +} + +int main(int argc, char *argv[]) { + pthread_t p1, p2; + Pthread_create(&p1, NULL, worker, (void *) (long long) 0); + Pthread_create(&p2, NULL, worker, (void *) (long long) 1); + Pthread_join(p1, NULL); + Pthread_join(p2, NULL); + return 0; +} diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock.c b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock.c new file mode 100644 index 0000000..ab29ac4 --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-deadlock.c @@ -0,0 +1,28 @@ +#include + +#include "common_threads.h" + +pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; + +void* worker(void* arg) { + if ((long long) arg == 0) { + Pthread_mutex_lock(&m1); + Pthread_mutex_lock(&m2); + } else { + Pthread_mutex_lock(&m2); + Pthread_mutex_lock(&m1); + } + Pthread_mutex_unlock(&m1); + Pthread_mutex_unlock(&m2); + return NULL; +} + +int main(int argc, char *argv[]) { + pthread_t p1, p2; + Pthread_create(&p1, NULL, worker, (void *) (long long) 0); + Pthread_create(&p2, NULL, worker, (void *) (long long) 1); + Pthread_join(p1, NULL); + Pthread_join(p2, NULL); + return 0; +} diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-race b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-race new file mode 100755 index 0000000000000000000000000000000000000000..0e46ba7eb6b59dcfd1fae51cb5fe0a494241ba34 GIT binary patch literal 12344 zcmds7eQ;dWb-!=Fw7Zg4Ys(*!os_k~SSDz-lD}k#L6R+d#n{F%GKKK*yxM(|7O!?S zyKjxG zIz-go-0bfyok+zXX%rgtD7Zc1(q`W6J%bzR`cbs&~_ksH4J^bg68A z+sqK3sFmU@%KA*%?hCM^IDgj?Rf;=Qdlws*{VIN@JP&QT?7*5;ZMjTqE}Jj!ZQa|s zrghD#c&QNYko_kAq`P_BPT3L-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27-threads-api/threads-api/main-race.c new file mode 100644 index 0000000..b4ae5c9 --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-race.c @@ -0,0 +1,24 @@ +#include + +#include "common_threads.h" + +int balance = 0; +pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER; +void* worker(void* arg) { +// pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER; + Pthread_mutex_lock(&lock1); + balance++; // unprotected access + Pthread_mutex_unlock(&lock1); + return NULL; +} + +int main(int argc, char *argv[]) { + + pthread_t p; + Pthread_create(&p, NULL, worker, NULL); + Pthread_mutex_lock(&lock1); + balance++; // unprotected access + Pthread_mutex_unlock(&lock1); + Pthread_join(p, NULL); + return 0; +} diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal new file mode 100755 index 0000000000000000000000000000000000000000..a7d9d9b236bd67a857da85bfc17bd11bf0fd05c4 GIT binary patch literal 11608 zcmeHNYj7Lab-s%S34$aDiF!~{8bQyHjtM?Q$&_7L1SwL>RIkdED>sr?Ah0Ac0s#!L zkVU0VRm*n7%1q=W^{B2JGtI=4{D|XBJgpkfr1Z#>#C^;-KbpwXw3R(_Bc)EG#<50? zZL#0Edk?T!f=N5mU!7bI_T2NG$9?afdoTZRaL+_r4B9ADAtDkW zJF4sXM#+LkWQOT!RgbfZvK_HxH>>Psl^s*T`igSX)0_jR^~33V4xHZ!Bzw()JKvl7nYRP_IsaRA5urae zXL;sd)6cvTxFqJceiqRDrqAQqw6+g9;wQ+}^(*s8oBo)1s?zn>B>xEU)HT=VB!7x{ zs@nAzBv1BZ=fTU=_0!?$??0$dzp0r>=cR|Nd>O1rFZ`*bW=W;bQ(y?|2<*qS&FI+3{DDJ4E$q(9iAo z7PxDTX!123kMH=u0bZl(oJO_8JD1=R?IHD}*T4)Fer;AsT`Z?dGR!H>e!CPH(`pn>sza~X}X8Bj)_S-T){oQN* z@P+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#mA5^VQgSqg z;{*CeABe$;;gqSmm@_WXRg$Br#FD%(!$tx>x`kcTN!!t zJ859QbG4T^)+K)fBsj)ZfO+!yZ#$?Jj_NM;#K}P$vR6=^51-Ym>mq2@d zNL6nCATTzuHY==O%{mq{xRS)O+-U@i+up2m4L#{rM&3;K4Sl%G-mHfWHSroZhBuTi zX58hQd6}V0$^tg!8h@WCvp4H&L;Xfk(=vBLt)GhhXXc}Zp4H>mxG`}X{&UDs#q_^f zuN%6{C~8_>{#VNE&AQ&uLw}+Cv#{q{mG=;nSudII<;&wWyrJKP4bKr6d$Uh4pqJK0 zkG`1S;d0AK9`f`gdFLJB8985H}n^fxa|w; z6$iiiV}D_%5Q0Dg}g%Wo?nDr4*ymEqK7GW4=5F9rIt z%2t_jzsAn!s&%;GiZo%X{Mxd8t8)990Ln31KCw{QKU5|xV+Ge)75@XV7ebZ* literal 0 HcmV?d00001 diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal-cv.c b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal-cv.c new file mode 100644 index 0000000..4ac4893 --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal-cv.c @@ -0,0 +1,55 @@ +#include + +#include "common_threads.h" + +// +// simple synchronizer: allows one thread to wait for another +// structure "synchronizer_t" has all the needed data +// methods are: +// init (called by one thread) +// wait (to wait for a thread) +// done (to indicate thread is done) +// +typedef struct __synchronizer_t { + pthread_mutex_t lock; + pthread_cond_t cond; + int done; +} synchronizer_t; + +synchronizer_t s; + +void signal_init(synchronizer_t *s) { + Pthread_mutex_init(&s->lock, NULL); + Pthread_cond_init(&s->cond, NULL); + s->done = 0; +} + +void signal_done(synchronizer_t *s) { + Pthread_mutex_lock(&s->lock); + s->done = 1; + Pthread_cond_signal(&s->cond); + Pthread_mutex_unlock(&s->lock); +} + +void signal_wait(synchronizer_t *s) { + Pthread_mutex_lock(&s->lock); + while (s->done == 0) + Pthread_cond_wait(&s->cond, &s->lock); + Pthread_mutex_unlock(&s->lock); +} + +void* worker(void* arg) { + printf("this should print first\n"); + signal_done(&s); + return NULL; +} + +int main(int argc, char *argv[]) { + pthread_t p; + signal_init(&s); + Pthread_create(&p, NULL, worker, NULL); + signal_wait(&s); + printf("this should print last\n"); + + return 0; +} diff --git a/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal.c b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal.c new file mode 100644 index 0000000..954f8de --- /dev/null +++ b/Homework9-threads-and-locks/Chap27-threads-api/threads-api/main-signal.c @@ -0,0 +1,20 @@ +#include + +#include "common_threads.h" + +int done = 0; + +void* worker(void* arg) { + printf("this should print first\n"); + done = 1; + return NULL; +} + +int main(int argc, char *argv[]) { + pthread_t p; + Pthread_create(&p, NULL, worker, NULL); + while (done == 0) + ; + printf("this should print last\n"); + return 0; +} diff --git a/README.md b/README.md index f5ef800..2ff65e4 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ Class repo for cs5600, where we dig deep into computer systems. ## Reading Notes +Textbook link: [Operating Systems: Three Easy Pieces](https://pages.cs.wisc.edu/~remzi/OSTEP/) + +Homework git resource: [github link](https://github.com/remzi-arpacidusseau/ostep-homework/) ### Chapter 2 intro