This commit is contained in:
p-w-rs
2022-09-22 14:34:03 -05:00
parent c506ca8b3b
commit ed1585d26b
32 changed files with 253 additions and 52 deletions

View File

@@ -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

BIN
Examples/pthreads/pthreads1 Executable file

Binary file not shown.

View File

@@ -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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads1</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads2 Executable file

Binary file not shown.

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads2</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads3 Executable file

Binary file not shown.

View File

@@ -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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads3</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads4 Executable file

Binary file not shown.

View File

@@ -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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads4</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads5 Executable file

Binary file not shown.

View File

@@ -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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads5</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads6 Executable file

Binary file not shown.

View File

@@ -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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads6</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

BIN
Examples/pthreads/pthreads_race Executable file

Binary file not shown.

View File

@@ -11,12 +11,14 @@
#include <stdlib.h>
#include <unistd.h>
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;

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads_race</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

Binary file not shown.

View File

@@ -6,16 +6,17 @@
*/
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdatomic.h>
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);
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.pthreads_race_atomic</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>