From 2a8e7aeeef99161b6f0378ac14e297c23ca6227b Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Wed, 4 Jun 2025 11:55:34 +0200 Subject: First 4 examples --- 03_threads/build.sh | 6 ++++++ 03_threads/index.html | 19 +++++++++++++++++ 03_threads/mime.txt | 1 + 03_threads/primes.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 03_threads/program.mjs | 6 ++++++ 03_threads/run-node.sh | 3 +++ 03_threads/run-server.sh | 6 ++++++ 03_threads/script.mjs | 16 +++++++++++++++ 8 files changed, 110 insertions(+) create mode 100755 03_threads/build.sh create mode 100644 03_threads/index.html create mode 100644 03_threads/mime.txt create mode 100644 03_threads/primes.c create mode 100644 03_threads/program.mjs create mode 100755 03_threads/run-node.sh create mode 100755 03_threads/run-server.sh create mode 100644 03_threads/script.mjs (limited to '03_threads') diff --git a/03_threads/build.sh b/03_threads/build.sh new file mode 100755 index 0000000..2f08909 --- /dev/null +++ b/03_threads/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +mkdir -p build +emcc -sEXPORTED_FUNCTIONS=_primes_in_range -sMODULARIZE -sEXPORT_NAME=Primes \ + -pthread -sPTHREAD_POOL_SIZE=16 \ + -o build/primes.mjs primes.c diff --git a/03_threads/index.html b/03_threads/index.html new file mode 100644 index 0000000..29cfaf8 --- /dev/null +++ b/03_threads/index.html @@ -0,0 +1,19 @@ + + + + + + Multiply two numbers + + + + +
+ x
+
+
+
+

+ + + diff --git a/03_threads/mime.txt b/03_threads/mime.txt new file mode 100644 index 0000000..6a9a425 --- /dev/null +++ b/03_threads/mime.txt @@ -0,0 +1 @@ +text/javascript mjs diff --git a/03_threads/primes.c b/03_threads/primes.c new file mode 100644 index 0000000..a5076c6 --- /dev/null +++ b/03_threads/primes.c @@ -0,0 +1,53 @@ +#include +#include + +#define NTHREADS 16 + +bool isprime(int); +void *pthread_routine(void *); + +struct interval { int low; int high; int count; }; + +int primes_in_range(int low, int high) { + pthread_t threads[NTHREADS]; + struct interval args[NTHREADS]; + + if (low < 0 || high < low) + return 0; + + int interval_size = (high-low)/NTHREADS + 1; + for (int i = 0; i < NTHREADS; i++) { + args[i].low = low + i*interval_size; + args[i].high = args[i].low + interval_size; + pthread_create(&threads[i], NULL, pthread_routine, &args[i]); + } + + int result = 0; + for (int i = 0; i < NTHREADS; i++) { + pthread_join(threads[i], NULL); + result += args[i].count; + } + + return result; +} + +bool isprime(int n) { + if (n < 2) + return false; + + for (int i = 2; i*i <= n; i++) + if (n % i == 0) + return false; + return true; +} + +void *pthread_routine(void *arg) { + struct interval *interval = arg; + + interval->count = 0; + for (int i = interval->low; i < interval->high; i++) + if (isprime(i)) + interval->count++; + + return NULL; +} diff --git a/03_threads/program.mjs b/03_threads/program.mjs new file mode 100644 index 0000000..a5a89ce --- /dev/null +++ b/03_threads/program.mjs @@ -0,0 +1,6 @@ +import Primes from "./build/primes.mjs" + +var primes = await Primes(); + +const count = primes._primes_in_range(1, 100); +console.log("There are " + count + " primes betwees 1 and 100"); diff --git a/03_threads/run-node.sh b/03_threads/run-node.sh new file mode 100755 index 0000000..a6c9aa8 --- /dev/null +++ b/03_threads/run-node.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +node program.mjs diff --git a/03_threads/run-server.sh b/03_threads/run-server.sh new file mode 100755 index 0000000..3a526f3 --- /dev/null +++ b/03_threads/run-server.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +darkhttpd . \ + --mimetypes mime.txt \ + --header 'Cross-Origin-Opener-Policy: same-origin' \ + --header 'Cross-Origin-Embedder-Policy: require-corp' diff --git a/03_threads/script.mjs b/03_threads/script.mjs new file mode 100644 index 0000000..4b52ae0 --- /dev/null +++ b/03_threads/script.mjs @@ -0,0 +1,16 @@ +import Primes from "./build/primes.mjs"; + +var primes = await Primes(); + +var aInput = document.getElementById("aInput"); +var bInput = document.getElementById("bInput"); +var button = document.getElementById("goButton"); +var resultText = document.getElementById("resultText"); + +button.addEventListener("click", () => { + var a = Number(aInput.value); + var b = Number(bInput.value); + const count = primes._primes_in_range(a, b); + resultText.innerText = "There are " + count + " primes between " + + a + " and " + b; +}); -- cgit v1.3