mirror of
https://github.com/tiennm99/HDH.git
synced 2026-06-08 20:13:15 +00:00
Update
This commit is contained in:
Binary file not shown.
+18
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
void *hello(void *tid)
|
||||
{
|
||||
printf("Hello from thread %ld\n", (long)tid);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pthread_t tid[10];
|
||||
long i;
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
pthread_create(&tid[i], NULL, hello, (void *)i);
|
||||
pthread_join(tid[i], NULL);
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
all: pi_serial.o pi_multi-thread.o code.o
|
||||
gcc -o pi_serial pi_serial.o
|
||||
gcc -o pi_multi-thread pi_multi-thread.o -lpthread
|
||||
gcc -o code code.o -lpthread
|
||||
pi: pi_serial.o pi_multi-thread.o
|
||||
gcc -o pi_serial pi_serial.o
|
||||
gcc -o pi_multi-thread pi_multi-thread.o -lpthread
|
||||
pi_serial: pi_serial.o
|
||||
gcc -o pi_serial pi_serial.o
|
||||
pi_multi-thread: pi_multi-thread.o
|
||||
gcc -o pi_multi-thread pi_multi-thread.o -lpthread
|
||||
code: code.o
|
||||
gcc -o code code.o -lpthread
|
||||
pi_serial.o: pi_serial.c
|
||||
gcc -c pi_serial.c
|
||||
pi_multi-thread.o: pi_multi-thread.c
|
||||
gcc -c pi_multi-thread.c
|
||||
code.o: code.c
|
||||
gcc -c code.c
|
||||
clean:
|
||||
rm -rf *.o pi_serial pi_multi-thread code
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
clock_t t1, t2;
|
||||
srand(time(NULL));
|
||||
double x, y;
|
||||
int count, nPoints, i;
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "usage: pi_serial <integer value>\n");
|
||||
return -1;
|
||||
}
|
||||
nPoints = atoi(argv[1]);
|
||||
if (nPoints < 0)
|
||||
{
|
||||
fprintf(stderr, "%d must be >= 0\n", nPoints);
|
||||
return -1;
|
||||
}
|
||||
time(&t1);
|
||||
count = 0;
|
||||
unsigned int state = rand();
|
||||
for (i = 0; i < nPoints; ++i)
|
||||
{
|
||||
x = (double)rand_r(&state) / RAND_MAX;
|
||||
y = (double)rand_r(&state) / RAND_MAX;
|
||||
if (x * x + y * y - 1 <= 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
time(&t2);
|
||||
printf("Time to Execute: %lds\n", (t2 - t1));
|
||||
printf("PI = %lf\n", 4 * (double)count / nPoints);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user