diff --git a/package/Config.in b/package/Config.in index 682faf3697b5..d677e90de9c9 100644 --- a/package/Config.in +++ b/package/Config.in @@ -2189,6 +2189,11 @@ menu "Miscellaneous" source "package/xutil_util-macros/Config.in" endmenu +menu "MULTIPROCESS Packages" + source "package/pth_hello/Config.in" + source "package/thread_sum/Config.in" +endmenu + menu "Networking applications" source "package/aircrack-ng/Config.in" source "package/alfred/Config.in" diff --git a/package/pth_hello/Config.in b/package/pth_hello/Config.in new file mode 100644 index 000000000000..ef98529d6cab --- /dev/null +++ b/package/pth_hello/Config.in @@ -0,0 +1,5 @@ +config BR2_PACKAGE_PTH_HELLO + bool "pth_hello" + help + pth_hello package. + diff --git a/package/pth_hello/pth_hello.mk b/package/pth_hello/pth_hello.mk new file mode 100644 index 000000000000..8e26c066fab6 --- /dev/null +++ b/package/pth_hello/pth_hello.mk @@ -0,0 +1,19 @@ +################################################################################ +# +# pth_hello package +# +################################################################################ + +PTH_HELLO_VERSION = 1.0 +PTH_HELLO_SITE = package/pth_hello/src +PTH_HELLO_SITE_METHOD = local# Other methods like git,wget,scp,file etc. are also available. + +define PTH_HELLO_BUILD_CMDS + $(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D) +endef + +define PTH_HELLO_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 0755 $(@D)/pth_hello $(TARGET_DIR)/usr/bin +endef + +$(eval $(generic-package)) diff --git a/package/pth_hello/src/Makefile b/package/pth_hello/src/Makefile new file mode 100644 index 000000000000..058e786a0e27 --- /dev/null +++ b/package/pth_hello/src/Makefile @@ -0,0 +1,8 @@ +.PHONY: clean +.PHONY: pth_hello + +pth_hello: pth_hello.c + $(CC) -o '$@' '$<' -lpthread + +clean: + -rm pth_hello diff --git a/package/pth_hello/src/pth_hello.c b/package/pth_hello/src/pth_hello.c new file mode 100644 index 000000000000..ae8c255e68f4 --- /dev/null +++ b/package/pth_hello/src/pth_hello.c @@ -0,0 +1,39 @@ +#include +#include +#include + +/* Global variable: accessible to all threads */ +int thread_count; + +void* Hello(void* rank); // Thread function + +int main(int argc, char* argv[]){ + long thread; + pthread_t* thread_handles; + + /* Get number of threads from command line */ + thread_count = strtol(argv[1], NULL, 10); + + thread_handles = malloc(thread_count*sizeof(pthread_t)); + + for (thread = 0; thread < thread_count; thread++) + pthread_create(&thread_handles[thread], NULL, + Hello, (void*) thread); + + printf("Hello from the main thread\n"); + + for (thread = 0; thread < thread_count; thread++) + pthread_join(thread_handles[thread], NULL); + + free(thread_handles); + + return 0; +} /* main */ + +void* Hello(void* rank){ + long my_rank = (long) rank; + + printf("Hello from thread %ld of %d\n", my_rank, thread_count); + + return NULL; +} /* Hello */ diff --git a/package/thread_sum/Config.in b/package/thread_sum/Config.in new file mode 100644 index 000000000000..1062244b1768 --- /dev/null +++ b/package/thread_sum/Config.in @@ -0,0 +1,5 @@ +config BR2_PACKAGE_THREAD_SUM + bool "thread_sum" + help + thread_sum package. + diff --git a/package/thread_sum/src/Makefile b/package/thread_sum/src/Makefile new file mode 100644 index 000000000000..9ffabe5a64e7 --- /dev/null +++ b/package/thread_sum/src/Makefile @@ -0,0 +1,8 @@ +.PHONY: clean +.PHONY: thread_sum + +thread_sum: thread_sum.c + $(CC) -o '$@' '$<' -lpthread + +clean: + -rm thread_sum diff --git a/package/thread_sum/src/thread_sum.c b/package/thread_sum/src/thread_sum.c new file mode 100644 index 000000000000..53986e07daac --- /dev/null +++ b/package/thread_sum/src/thread_sum.c @@ -0,0 +1,65 @@ +#include +#include +#include + +/* Global variable: accessible to all threads */ +int thread_count; +long long n; +double sum; +pthread_mutex_t mutex; + +void* Thread_sum(void* rank); // Thread function + +int main(int argc, char* argv[]){ + long thread; + pthread_t* thread_handles; + + pthread_mutex_init( &mutex, NULL ); + + /* Get number of threads from command line */ + thread_count = strtol(argv[1], NULL, 10); + + thread_handles = malloc(thread_count*sizeof(pthread_t)); + + printf("Thread_sum from the main thread\n"); + for ( n = 1e5; n <= 1e10; n *= 10, sum = 0 ) { + for (thread = 0; thread < thread_count; thread++) + pthread_create(&thread_handles[thread], NULL, + Thread_sum, (void*) thread); + + for (thread = 0; thread < thread_count; thread++) + pthread_join(thread_handles[thread], NULL); + printf("pi = %.11f @ n = %lld\n", 4.0*sum, n); + } + + pthread_mutex_destroy( &mutex ); + + free(thread_handles); + + return 0; +} /* main */ + +void* Thread_sum(void* rank){ + long my_rank = (long) rank; + + double factor; + long long i; + long long my_n = n/thread_count; + long long my_first_i = my_n * my_rank; + long long my_last_i = my_first_i + my_n; + double my_sum = 0.0; + + if (my_first_i % 2 == 0) + factor = 1.0; + else + factor = -1.0; + + for (i = my_first_i; i < my_last_i; i++, factor = -factor) + my_sum += factor/(2*i+1); + + pthread_mutex_lock(&mutex); + sum += my_sum; + pthread_mutex_unlock(&mutex); + + return NULL; +} /* Thread_sum */ diff --git a/package/thread_sum/thread_sum.mk b/package/thread_sum/thread_sum.mk new file mode 100644 index 000000000000..b26c25e8c81f --- /dev/null +++ b/package/thread_sum/thread_sum.mk @@ -0,0 +1,19 @@ +################################################################################ +# +# thread_sum package +# +################################################################################ + +THREAD_SUM_VERSION = 1.0 +THREAD_SUM_SITE = package/thread_sum/src +THREAD_SUM_SITE_METHOD = local# Other methods like git,wget,scp,file etc. are also available. + +define THREAD_SUM_BUILD_CMDS + $(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D) +endef + +define THREAD_SUM_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 0755 $(@D)/thread_sum $(TARGET_DIR)/usr/bin +endef + +$(eval $(generic-package))