diff --git a/02-TeenyTinyShell/ttsh.c b/02-TeenyTinyShell/ttsh.c index e93c0f5..02b9c87 100644 --- a/02-TeenyTinyShell/ttsh.c +++ b/02-TeenyTinyShell/ttsh.c @@ -95,7 +95,6 @@ int main() // Chop the commands into arguments and execute one at a time for (int i = 0; i < cmd_count; i++) { - // TODO: // 1) Chop the command into command line arguments // need to handle up to 10 arguments diff --git a/Examples/pthreads/pthreads1 b/Examples/pthreads/pthreads1 new file mode 100755 index 0000000..1628871 Binary files /dev/null and b/Examples/pthreads/pthreads1 differ diff --git a/Examples/pthreads/pthreads1.c b/Examples/pthreads/pthreads1.c index 292e0e9..ae6eb2a 100644 --- a/Examples/pthreads/pthreads1.c +++ b/Examples/pthreads/pthreads1.c @@ -15,9 +15,10 @@ int global_value = 10; // global data is shared between threads // // NOTE: that parameters and return value are pointers -void* thread_routine() +void *thread_routine() { - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) + { printf("THREAD READS GLOBAL VALUE: %d\n", global_value); global_value++; sleep(1); @@ -29,13 +30,15 @@ int main() { // Create a thread pthread_t thr_id; - if(pthread_create(&thr_id, NULL, thread_routine, NULL) == -1) { + if (pthread_create(&thr_id, NULL, thread_routine, NULL) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } // Write to global data - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) + { printf("PARENT READS GLOBAL: %d\n", global_value); sleep(1); } diff --git a/Examples/pthreads/pthreads1.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..79aa0ca --- /dev/null +++ b/Examples/pthreads/pthreads1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads1.dSYM/Contents/Resources/DWARF/pthreads1 b/Examples/pthreads/pthreads1.dSYM/Contents/Resources/DWARF/pthreads1 new file mode 100644 index 0000000..2916c2d Binary files /dev/null and b/Examples/pthreads/pthreads1.dSYM/Contents/Resources/DWARF/pthreads1 differ diff --git a/Examples/pthreads/pthreads2 b/Examples/pthreads/pthreads2 new file mode 100755 index 0000000..b99dc95 Binary files /dev/null and b/Examples/pthreads/pthreads2 differ diff --git a/Examples/pthreads/pthreads2.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..331c66f --- /dev/null +++ b/Examples/pthreads/pthreads2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads2.dSYM/Contents/Resources/DWARF/pthreads2 b/Examples/pthreads/pthreads2.dSYM/Contents/Resources/DWARF/pthreads2 new file mode 100644 index 0000000..eb42dbd Binary files /dev/null and b/Examples/pthreads/pthreads2.dSYM/Contents/Resources/DWARF/pthreads2 differ diff --git a/Examples/pthreads/pthreads3 b/Examples/pthreads/pthreads3 new file mode 100755 index 0000000..ea7c1e0 Binary files /dev/null and b/Examples/pthreads/pthreads3 differ diff --git a/Examples/pthreads/pthreads3.c b/Examples/pthreads/pthreads3.c index ad6fcd0..6aa39e6 100644 --- a/Examples/pthreads/pthreads3.c +++ b/Examples/pthreads/pthreads3.c @@ -11,8 +11,10 @@ // Returns 1 if prime, 0 if not prime int is_prime(int v) { - for(int i = 2; i < v; i++) { - if(v % i == 0) { + for (int i = 2; i < v; i++) + { + if (v % i == 0) + { return 0; } } @@ -23,12 +25,15 @@ int is_prime(int v) // calls is_prime to check if the input argument is prime // // NOTE: that parameters and return value are pointers -void* thread_routine(void* args) +void *thread_routine(void *args) { - int* val = (int*)args; - if(is_prime(*val)) { + int *val = args; + if (is_prime(*val)) + { printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val); - } else { + } + else + { printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val); } return NULL; @@ -42,11 +47,13 @@ int main() // Create 2 threads, passing a different value to each pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, &val1) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } - if(pthread_create(&thr2, NULL, thread_routine, (void*)&val2) == -1) { + if (pthread_create(&thr2, NULL, thread_routine, &val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } diff --git a/Examples/pthreads/pthreads3.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads3.dSYM/Contents/Info.plist new file mode 100644 index 0000000..16b917a --- /dev/null +++ b/Examples/pthreads/pthreads3.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads3 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads3.dSYM/Contents/Resources/DWARF/pthreads3 b/Examples/pthreads/pthreads3.dSYM/Contents/Resources/DWARF/pthreads3 new file mode 100644 index 0000000..c64ca22 Binary files /dev/null and b/Examples/pthreads/pthreads3.dSYM/Contents/Resources/DWARF/pthreads3 differ diff --git a/Examples/pthreads/pthreads4 b/Examples/pthreads/pthreads4 new file mode 100755 index 0000000..88288f4 Binary files /dev/null and b/Examples/pthreads/pthreads4 differ diff --git a/Examples/pthreads/pthreads4.c b/Examples/pthreads/pthreads4.c index 60e2867..2245d52 100644 --- a/Examples/pthreads/pthreads4.c +++ b/Examples/pthreads/pthreads4.c @@ -14,8 +14,10 @@ // Returns 1 if prime, 0 if not prime int is_prime(int v) { - for(int i = 2; i < v; i++) { - if(v % i == 0) { + for (int i = 2; i < v; i++) + { + if (v % i == 0) + { return 0; } } @@ -27,13 +29,16 @@ int is_prime(int v) // Modifies the input parameter by dereferencing the pointer // // NOTE: that parameters and return value are pointers -void* thread_routine(void* args) +void *thread_routine(void *args) { - int* val = (int*)args; - if(is_prime(*val)) { + int *val = (int *)args; + if (is_prime(*val)) + { printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val); *val = 1; - } else { + } + else + { printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val); *val = 0; } @@ -51,11 +56,13 @@ int main() // Create 2 threads pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&val1) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } - if(pthread_create(&thr2, NULL, thread_routine, (void*)&val2) == -1) { + if (pthread_create(&thr2, NULL, thread_routine, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } diff --git a/Examples/pthreads/pthreads4.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads4.dSYM/Contents/Info.plist new file mode 100644 index 0000000..0666b71 --- /dev/null +++ b/Examples/pthreads/pthreads4.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads4 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads4.dSYM/Contents/Resources/DWARF/pthreads4 b/Examples/pthreads/pthreads4.dSYM/Contents/Resources/DWARF/pthreads4 new file mode 100644 index 0000000..7dec464 Binary files /dev/null and b/Examples/pthreads/pthreads4.dSYM/Contents/Resources/DWARF/pthreads4 differ diff --git a/Examples/pthreads/pthreads5 b/Examples/pthreads/pthreads5 new file mode 100755 index 0000000..f888d68 Binary files /dev/null and b/Examples/pthreads/pthreads5 differ diff --git a/Examples/pthreads/pthreads5.c b/Examples/pthreads/pthreads5.c index 854b2cc..0463864 100644 --- a/Examples/pthreads/pthreads5.c +++ b/Examples/pthreads/pthreads5.c @@ -17,8 +17,10 @@ // Returns 1 if prime, 0 if not prime int is_prime(int v) { - for(int i = 2; i < v; i++) { - if(v % i == 0) { + for (int i = 2; i < v; i++) + { + if (v % i == 0) + { return 0; } } @@ -29,12 +31,15 @@ int is_prime(int v) // calls is_prime to check if the input argument is prime // // NOTE: that parameters and return value are pointers -void* thread_routine(void* args) +void *thread_routine(void *args) { - int* val = (int*)args; - if(is_prime(*val)) { + int *val = (int *)args; + if (is_prime(*val)) + { printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val); - } else { + } + else + { printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val); } return NULL; @@ -49,7 +54,8 @@ pthread_t start_thread(int val) { pthread_t thr; printf("Before thread creation val=%d\n", val); - if(pthread_create(&thr, NULL, thread_routine, (void*)&val) == -1) { + if (pthread_create(&thr, NULL, thread_routine, (void *)&val) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } diff --git a/Examples/pthreads/pthreads5.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads5.dSYM/Contents/Info.plist new file mode 100644 index 0000000..2c5eb57 --- /dev/null +++ b/Examples/pthreads/pthreads5.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads5 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads5.dSYM/Contents/Resources/DWARF/pthreads5 b/Examples/pthreads/pthreads5.dSYM/Contents/Resources/DWARF/pthreads5 new file mode 100644 index 0000000..a73a7c9 Binary files /dev/null and b/Examples/pthreads/pthreads5.dSYM/Contents/Resources/DWARF/pthreads5 differ diff --git a/Examples/pthreads/pthreads6 b/Examples/pthreads/pthreads6 new file mode 100755 index 0000000..7a85c74 Binary files /dev/null and b/Examples/pthreads/pthreads6 differ diff --git a/Examples/pthreads/pthreads6.c b/Examples/pthreads/pthreads6.c index c119517..c406a56 100644 --- a/Examples/pthreads/pthreads6.c +++ b/Examples/pthreads/pthreads6.c @@ -16,8 +16,10 @@ // Returns 1 if prime, 0 if not prime int is_prime(int v) { - for(int i = 2; i < v; i++) { - if(v % i == 0) { + for (int i = 2; i < v; i++) + { + if (v % i == 0) + { return 0; } } @@ -29,13 +31,16 @@ int is_prime(int v) // Modifies the input parameter by dereferencing the pointer // // NOTE: that parameters and return value are pointers -void* thread_routine(void* args) +void *thread_routine(void *args) { - int* val = (int*)args; - if(is_prime(*val)) { + int *val = (int *)args; + if (is_prime(*val)) + { printf("THREAD %lu FOUND that %d is prime %p\n", pthread_self(), *val, val); *val = 1; - } else { + } + else + { printf("THREAD %lu FOUND that %d is not prime %p\n", pthread_self(), *val, val); *val = 0; } @@ -48,18 +53,21 @@ int main() // Create 10 threads pthread_t thr[10]; - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) + { // NOTE: there is a single memory location for val which is passed to all threads val = i + 2; - if(pthread_create(&thr[i], NULL, thread_routine, (void*)&val) == -1) { + if (pthread_create(&thr[i], NULL, thread_routine, (void *)&val) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } } // Wait for threads to finish - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) + { pthread_join(thr[i], NULL); } diff --git a/Examples/pthreads/pthreads6.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads6.dSYM/Contents/Info.plist new file mode 100644 index 0000000..35695e0 --- /dev/null +++ b/Examples/pthreads/pthreads6.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads6 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads6.dSYM/Contents/Resources/DWARF/pthreads6 b/Examples/pthreads/pthreads6.dSYM/Contents/Resources/DWARF/pthreads6 new file mode 100644 index 0000000..c43aeb7 Binary files /dev/null and b/Examples/pthreads/pthreads6.dSYM/Contents/Resources/DWARF/pthreads6 differ diff --git a/Examples/pthreads/pthreads_race b/Examples/pthreads/pthreads_race new file mode 100755 index 0000000..fc5c6e1 Binary files /dev/null and b/Examples/pthreads/pthreads_race differ diff --git a/Examples/pthreads/pthreads_race.c b/Examples/pthreads/pthreads_race.c index 5234ee8..31ee6e2 100644 --- a/Examples/pthreads/pthreads_race.c +++ b/Examples/pthreads/pthreads_race.c @@ -11,12 +11,14 @@ #include #include -int global_value = 0; +int global_value[5] = {0}; -void* thread_routine() +void *thread_routine(void *arg) { - for(int i = 0; i < 100000000; i++) { - global_value++; + int *idx = (int *)(arg); + for (int i = 0; i < 1000000; i++) + { + global_value[*idx]++; } return NULL; } @@ -25,21 +27,26 @@ int main() { // Create 5 threads pthread_t thr_id[5]; - for(int i = 0; i < 5; i++) { - if(pthread_create(&thr_id[i], NULL, thread_routine, NULL) == -1) { + int args[5] = {0, 1, 2, 3, 4}; + for (int i = 0; i < 5; i++) + { + if (pthread_create(&thr_id[i], NULL, thread_routine, (void *)&args[i]) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } } // Wait for all threads to finish - for(int i = 0; i < 5; i++) { + for (int i = 0; i < 5; i++) + { pthread_join(thr_id[i], NULL); } // Print out final value of global value // 5 threads * 100000000 iterations - expected value is 500000000 - printf("PARENT READS GLOBAL: %d\n", global_value); + for (int i = 0; i < 5; i++) + printf("PARENT READS GLOBAL: %d\n", global_value[i]); // Return success return 0; diff --git a/Examples/pthreads/pthreads_race.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads_race.dSYM/Contents/Info.plist new file mode 100644 index 0000000..4dfdaab --- /dev/null +++ b/Examples/pthreads/pthreads_race.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads_race + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads_race.dSYM/Contents/Resources/DWARF/pthreads_race b/Examples/pthreads/pthreads_race.dSYM/Contents/Resources/DWARF/pthreads_race new file mode 100644 index 0000000..3a45fdf Binary files /dev/null and b/Examples/pthreads/pthreads_race.dSYM/Contents/Resources/DWARF/pthreads_race differ diff --git a/Examples/pthreads/pthreads_race_atomic b/Examples/pthreads/pthreads_race_atomic new file mode 100755 index 0000000..0fe95ca Binary files /dev/null and b/Examples/pthreads/pthreads_race_atomic differ diff --git a/Examples/pthreads/pthreads_race_atomic.c b/Examples/pthreads/pthreads_race_atomic.c index e3b1391..a70a1d7 100644 --- a/Examples/pthreads/pthreads_race_atomic.c +++ b/Examples/pthreads/pthreads_race_atomic.c @@ -1,21 +1,22 @@ /* * pthreads_race_atomic.c - Uses an atomic add instruction to prevent * the race condition that can happen when - * multiple threads modify a global value at the + * multiple threads modify a global value at the * same time. */ #include +#include #include #include #include -#include atomic_int global_value = 0; -void* thread_routine() +void *thread_routine() { - for(int i = 0; i < 100000000; i++) { + for (int i = 0; i < 1000000; i++) + { atomic_fetch_add(&global_value, 1); } return NULL; @@ -25,15 +26,18 @@ int main() { // Create 5 threads pthread_t thr_id[5]; - for(int i = 0; i < 5; i++) { - if(pthread_create(&thr_id[i], NULL, thread_routine, NULL) == -1) { + for (int i = 0; i < 5; i++) + { + if (pthread_create(&thr_id[i], NULL, thread_routine, NULL) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } } // Wait for all threads to finish - for(int i = 0; i < 5; i++) { + for (int i = 0; i < 5; i++) + { pthread_join(thr_id[i], NULL); } diff --git a/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Info.plist b/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Info.plist new file mode 100644 index 0000000..5453dd8 --- /dev/null +++ b/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.pthreads_race_atomic + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Resources/DWARF/pthreads_race_atomic b/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Resources/DWARF/pthreads_race_atomic new file mode 100644 index 0000000..0707da5 Binary files /dev/null and b/Examples/pthreads/pthreads_race_atomic.dSYM/Contents/Resources/DWARF/pthreads_race_atomic differ