Đang tải... (xem toàn văn)
PHẦN THỰC HÀNHVí Dụ 1: Biên dịch chương trình đầu tiênA: Code Chương Trình#include#include#includevoid *thr1void* ar { printf"this is thread %d\n", *int*ar; sleep2; pthread_create&tid[i]
Trang 11: Trần Ngọc Hải My ( MSSV: 52300128)
Trang 2A PHẦN THỰC HÀNHVí Dụ 1: Biên dịch chương trình đầu tiên
A: Code Chương Trình
#include<pthread.h>#include<stdlib.h>#include<stdio.h>void *thr1(void* ar) {
printf("this is thread %d\n", *(int*)ar); sleep(2);
pthread_create(&tid[i], NULL, thr1, (void*) &tid[i]); }
sleep(3); return 0;}
B: Kết Quả Demo
Trang 3PAGE \* MERGEFORMAT 35
Ví Dụ 2: ……….A: Code chương trình
#include<stdio.h>#include<pthread.h>struct arr { int n; int a[10];};
void* thr1(void* ar) { int count;
struct arr *ap = (struct arr*) ar; for(count = 0; count<ap->n; count++) printf("%d\t", ap->a[count]); printf("\n");
int main(int argc, char* argv[]) { struct arr ar;
ar.n = 5; int i;
for(i = 0; i<ar.n; i++) ar.a[i] = i+1; pthread_t tid;
pthread_create(&tid, NULL, &thr1, &ar); sleep(2);
return 0;}
B: Kết quả Demo
Trang 4Ví Dụ 3:
A: Code Chương Trình
#include<pthread.h>#include<stdio.h>void* thr1(void* ar) { int count;
printf("this is thread %d\n", *((int*)ar)); sleep(2);
for(i = 0; i<3; i++) {
if(pthread_join(tid[i], (void**) pstatus)>0) {
printf("pthread_join for thread %d failure\n",(int)tid[i]); return 0;
}
printf("pthread waited of %d OK, return code %d\n", (int)tid[i], status);
sleep(1);
Trang 5PAGE \* MERGEFORMAT 35
} sleep(1); return 0;}
B: Kết Quả Demo
Ví Dụ 4:
A: Code Chương Trình
#include<pthread.h>#include<stdio.h>#include<stdlib.h>struct arr{ int n; int a[10];};
struct file { struct arr ar; char* filename;};
static int sum = 0;void* thr1(void* ar) {
struct arr *ap = (struct arr*) ar; ap->n = 3; int i = 0;
Trang 6for(i = 0; i<ap->n; i++) ap->a[i] = i + 1;}
void* thr2(void* ar) {
struct arr *ap = (struct arr*) ar; int i, s = 0;
for(i = 0; i<ap->n; i++) s = s + ap->a[i]; sum = s;
void* thr3(void* ar) {
struct file *fi = (struct file*) ar; FILE *out ; int count;
out = fopen(fi->filename,"wb");
fprintf(out, "number element of array: %d\n", fi->ar.n); for(count = 0; count<fi->ar.n; count++) {
fprintf(out, "%d\t", fi->ar.a[count]); }
int status, *pstatus = &status;
pthread_create(&tid[0], NULL, thr1, (void*) &ar); sleep(1);
if(pthread_join(tid[0], (void**) pstatus)==0) {
Trang 7PAGE \* MERGEFORMAT 35
pthread_create(&tid[1], NULL, thr2, (void*) &ar); if(pthread_create(&tid[1], NULL, thr2, (void*) &ar)==0) { struct file arf;
B: Kết Quả Demo
Trang 9B PHẦN BÀI TẬP
BÀI 1: Write a program that creates three threads that perform the following tasks: - The first thread takes arguments from the environment (argv [1]), checks if the number is 0, calculates the factorial of this number (can use struct or global variables) - The second thread waiting for the first thread to complete, perform the calculation of the number of words smaller than the result of the first factorial factor calculated - Third thread waiting for the second thread to complete, write the result into the file is the second argument from the environment variable (argv [2]) The contents of the record include: the first line stores the value of argv [1]; The second line stores the results of the factorial; The third line stores the sum of smaller numbers than the factorial
For example: run /bai1 4 res1 Output to res1 file is as follows: N = 4
4! = 24 Sum = 276
struct file { struct arr ar; char* filename;};
static int sum ;=
void*thr1(void* ar) {
struct arr ap (*=struct arr ) ar;*
ap factorial->= 1; for(int i ; i=<=ap->n; i++) ap factorial->*=i;}
void*thr2(void* ar) {
struct arr ap (*=struct arr ) ar;*
i, s ;int=
for(i = 1; i<ap factorial->; i++)
Trang 10s += i; sum s;=
void*thr3(void* ar) {
struct file fi (* =struct file ) ar;*
FILE out ; count;*int out =fopen(fi filename->,"wb"); fprintf(out, "N = %d\n", fi ar-> n); fprintf(out, "4! = %d\n", fi ar-> factorial); fprintf(out, "Sum = %d\n", sum); fclose(out);
intmain( argc, intchar* argv[]) { i;int
pthread_t tid[ ];3 struct arr ar; ar.n atoi(argv[ ]);1 status, pstatus int*=&status;
pthread_create(&tid[0], NULL, thr1, (void* &) ar); sleep(1);
if(pthread_join(tid[0], (void**) pstatus)==0) {
pthread_create(&tid[1], NULL, thr2, (void* &) ar); if(pthread_create(&tid[1], NULL, thr2, (void* &) ar)==0){
struct file arf; arf ar ar;=
arf filename.= argv[ ];2
pthread_create(&tid[2], NULL, thr3, (void*)
} } sleep(2); return 0;}
B: Kết Quả Demo:
Trang 11PAGE \* MERGEFORMAT 35
Trang 12Write a program of threads that perform the following tasks:
- The first read the input file is the first argument from the environment variable - The second thread counts the sum of primes in the array
- Third thread sorts array increase
- The fourth thread executes the writing of the result file The input and output file contents are as follows:
For example, the input file is of the following format ten
4 5 7 8 11 9 20 13 2 3
The result in the result file looks like this: Size of array: 10
Value of elements: 4 5 7 8 11 9 20 13 2 3 Primes: 5 7 11 13 2 3
Trang 13PAGE \* MERGEFORMAT 35
void*thr1(void* filenameIn) {
FILE in * =fopen((char*) filenameIn, "r"); (in if== NULL) {
printf("Error opening file.\n"); pthread_exit(NULL); }
fscanf(in, "%d", &n); for(int i ; i n; i=<++) { fscanf(in, "%d", &a[i]); }
fclose(in); pthread_exit(NULL);}
void*thr2(void* temp) { s int= 0;
for(int i ; i n; i=<++) { isPrime ;int=
( [i] ifa<= ) {1 isPrime = 0; } else if ( [i] ) {a!=2
for(int j ; j j =* <= [i]; ja++) { ( [i] j ifa% == 0) { isPrime = 0; break; } } }
(isPrime if== 1) { prime[k] =a[i]; primeSorted[k] = a[i]; s +=a[i]; k++; } } sum s;=
// Sort primeSorted array
for(int i ; i k ; i=< -1++) {
Trang 14for(int j i = + 1; j k; j<++) {
(ifprimeSorted[j] < primeSorted[i]) { temp int=primeSorted[i]; primeSorted[i] = primeSorted[j]; primeSorted[j] temp;=
} } }
pthread_exit(NULL);}
void*thr3(void* filenameOut) {
FILE out *=fopen((char*) filenameOut, "w"); (out if== NULL) {
printf("Error opening file for writing.\n"); pthread_exit(NULL);
}
fprintf(out, "Size of array: %d\n", n); fprintf(out, "Value of elements: "); for(int i ; i n; i=<++) { fprintf(out, "%d ", a[i]); }
fprintf(out, "\n"); fprintf(out, "Primes: "); for(int i ; i k; i=<++) { fprintf(out, "%d ", prime[i]); }
fprintf(out, "\n");
fprintf(out, "Sum of Primes: %d\n", sum); fprintf(out, "Primes sorted from smallest to highest: "); for(int i ; i k; i=<++) {
fprintf(out, "%d ", primeSorted[i]); }
fprintf(out, "\n"); fclose(out); pthread_exit(NULL);}
intmain( argc, intchar* argv[]) { (argc ) {if!=3
Trang 15PAGE \* MERGEFORMAT 35
printf("Usage: %s <input_filename> <output_filename>\n", argv[ ]);0
return 1; }
}
pthread_create(&tid[1], NULL, thr2, NULL); pthread_create(&tid[2], NULL, thr3, (void*) argv[ ]);2 pthread_join(tid[1], NULL);
pthread_join(tid[2], NULL); return 0;
B: Kết Quả Demo: