You are on page 1of 5

Operating system

Lab 3
Full name : Trịnh Mạnh Hùng
Student id: 1952740

4.1. Questions:

Problem 1.1. Serial version (3 Points):

Code :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <time.h>

int main(int argc, char* argv[]) {


clock_t start = clock();
long circle_points = 0, interval, i;
double rand_x, rand_y, z;
if(argc != 2) {
printf("Wrong input!!!\nUsage: ./pi_serial numberOfPoints\n");
return 1;
}
int numofpoint = atoi(argv[1]);
if(numofpoint < 0) {
printf("numberOfPoints: %d must >= 0\n", numofpoint);
return -1;
}
for(int i = 0; i < numofpoint; ++i) {
rand_x = (double)(rand())/RAND_MAX;
rand_y = (double)(rand())/RAND_MAX;
z = rand_x*rand_x + rand_y*rand_y;
if(z <= 1.0) circle_points++;
}
clock_t end = clock();
double timeuse = (double) (end - start)/CLOCKS_PER_SEC;
double pi = (double)4 * circle_points/numofpoint;

printf("The value of Pi is: %f\n", pi);


printf("Time spent: %f\n", timeuse);
}

Result:

Problem 1.2: Multi-threaded program version:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define NUM_OF_THREADS 3
void *cal1(void* tid) {
int *n = (int *)tid;
double * count = (double *)malloc(sizeof(double));
*count = 0;
//printf("so: %d\n", *n);
for(int i = 0; i < *n; ++i) {
double rand_x = (double) rand()/RAND_MAX;
double rand_y = (double) rand()/RAND_MAX;
double z = rand_x * rand_x + rand_y * rand_y;
if(z <= 1) *count += 1;
}
//printf("%d, %f\n", *n, *count);
pthread_exit((void *)count);
}

int main(int argc, char* argv[]) {


//srand(time(NULL));
if(argc != 2) {
printf("Wrong format!\nUsage: ./pi_multi-thread numberOfPoints\n");
return 1;
}

void * result;
int numOfPoints = atoi(argv[1]);
if(numOfPoints < 0) {
printf("numberOfPoints: %d must >= 0\n", numOfPoints);
return -1;
}
int phandu = numOfPoints % NUM_OF_THREADS,
thuong = (numOfPoints/NUM_OF_THREADS);
int rc;

// request time
clock_t begin = clock();
double total_in_circle = 0;
pthread_t thread[NUM_OF_THREADS+1];
for(int i= 0; i <= NUM_OF_THREADS; ++i) {
if(i == NUM_OF_THREADS) {
rc = pthread_create(&thread[i], NULL, cal1, &phandu);
if(rc) {
printf("ERROR! pthread_create() is %d\n", rc);
exit(-1);
}
}
else {
rc = pthread_create(&thread[i], NULL, cal1, &thuong);
if(rc) {
printf("ERROR! pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_join(thread[i], &result);
total_in_circle += *(double *)result;
}

clock_t end = clock();


double time_spent = (double)(end-begin)/ CLOCKS_PER_SEC;

//ket thuc chuong trinh


printf("Value for Pi: %f\n", 4.0*(total_in_circle/numOfPoints));
printf("Time spent is: %f\n", time_spent);
pthread_exit(0);
return 0;
}

Result:

Problem 2:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void * hello (void * tid){


int *id = (int *) tid;
printf("Hello from thread %d \n", *id);
pthread_exit(0);
}
int main(){
pthread_t tid[10];
for (int i = 0; i < 10; i++){
pthread_create(&tid[i], NULL, hello, &i);
pthread_join(tid[i], NULL);
}
pthread_exit(NULL);
return 0;
}

Result:

You might also like