Tải bản đầy đủ (.pdf) (18 trang)

báo cáo bài tập nhập môn hệ điều hành lab 5 1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.15 MB, 18 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

1: Trần Ngọc Hải My ( MSSV: 52300128)

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

<b>A. PHẦN THỰC HÀNH</b>

<b>Ví Dụ 1: Biên dịch chương trình đầu tiên</b>

<b>A: Code Chương Trình</b>

#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>B: Kết Quả Demo</b>

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<small> PAGE \* MERGEFORMAT 35</small>

<b>Ví Dụ 2: ……….A: Code chương trình</b>

#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>B: Kết quả Demo</b>

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

<b>Ví Dụ 3: </b>

<b>A: Code Chương Trình</b>

#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);

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

<small> PAGE \* MERGEFORMAT 35</small>

} sleep(1); return 0;}

<b>B: Kết Quả Demo</b>

<b>Ví Dụ 4: </b>

<b>A: Code Chương Trình</b>

#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;

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

for(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) {

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

<small> PAGE \* MERGEFORMAT 35</small>

pthread_create(&tid[1], NULL, thr2, (void*) &ar); if(pthread_create(&tid[1], NULL, thr2, (void*) &ar)==0) { struct file arf;

<b>B: Kết Quả Demo</b>

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

<b>B. PHẦN BÀI TẬP</b>

<b>BÀI 1: </b><small> 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. </small>

<small>For example: run ./bai1 4 res1 Output to res1 file is as follows: N = 4 </small>

<small>4! = 24 Sum = 276 </small>

<small>struct file { struct arr ar; char* filename;};</small>

<small>static int sum ;=</small>

<small>void*thr1(void* ar) {</small>

<small> struct arr ap (*=struct arr ) ar;*</small>

<small> ap factorial->= 1; for(int i ; i=<=ap->n; i++) ap factorial->*=i;}</small>

<small>void*thr2(void* ar) {</small>

<small> struct arr ap (*=struct arr ) ar;*</small>

<small> i, s ;int=</small>

<small> for(i = 1; i<ap factorial->; i++)</small>

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

<small> s += i; sum s;=</small>

<small>void*thr3(void* ar) {</small>

<small> struct file fi (* =struct file ) ar;*</small>

<small> 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);</small>

<small>intmain( argc, intchar* argv[]) { i;int</small>

<small> pthread_t tid[ ];3 struct arr ar; ar.n atoi(argv[ ]);1 status, pstatus int*=&status;</small>

<small> pthread_create(&tid[0], NULL, thr1, (void* &) ar); sleep(1);</small>

<small> if(pthread_join(tid[0], (void**) pstatus)==0) {</small>

<small> pthread_create(&tid[1], NULL, thr2, (void* &) ar); if(pthread_create(&tid[1], NULL, thr2, (void* &) ar)==0){</small>

<small> struct file arf; arf ar. ar;=</small>

<small> arf filename.= argv[ ];2</small>

<small> pthread_create(&tid[2], NULL, thr3, (void*) </small>

<small> } } sleep(2); return 0;}</small>

<b>B: Kết Quả Demo:</b>

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

<small> PAGE \* MERGEFORMAT 35</small>

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

<small>Write a program of threads that perform the following tasks: </small>

<small>- 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</small>

<small>- Third thread sorts array increase </small>

<small>- The fourth thread executes the writing of the result file. The input and output file contents are as follows: </small>

<small>For example, the input file is of the following format ten </small>

<small>4 5 7 8 11 9 20 13 2 3 </small>

<small>The result in the result file looks like this: Size of array: 10 </small>

<small>Value of elements: 4 5 7 8 11 9 20 13 2 3 Primes: 5 7 11 13 2 3 </small>

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

<small> PAGE \* MERGEFORMAT 35</small>

<small>void*thr1(void* filenameIn) {</small>

<small> FILE in * =fopen((char*) filenameIn, "r"); (in if== NULL) {</small>

<small> printf("Error opening file.\n"); pthread_exit(NULL); }</small>

<small> fscanf(in, "%d", &n); for(int i ; i n; i=<++) { fscanf(in, "%d", &a[i]); }</small>

<small> fclose(in); pthread_exit(NULL);}</small>

<small>void*thr2(void* temp) { s int= 0;</small>

<small> for(int i ; i n; i=<++) { isPrime ;int=</small>

<small> ( [i] ifa<= ) {1 isPrime = 0; } else if ( [i] ) {a!=2</small>

<small> for(int j ; j j =* <= [i]; ja++) { ( [i] j ifa% == 0) { isPrime = 0; break; } } }</small>

<small> (isPrime if== 1) { prime[k] =a[i]; primeSorted[k] = a[i]; s +=a[i]; k++; } } sum s;=</small>

<small> // Sort primeSorted array</small>

<small> for(int i ; i k ; i=< -1++) {</small>

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

<small> for(int j i = + 1; j k; j<++) {</small>

<small> (ifprimeSorted[j] < primeSorted[i]) { temp int=primeSorted[i]; primeSorted[i] = primeSorted[j]; primeSorted[j] temp;=</small>

<small> } } }</small>

<small> pthread_exit(NULL);}</small>

<small>void*thr3(void* filenameOut) {</small>

<small> FILE out *=fopen((char*) filenameOut, "w"); (out if== NULL) {</small>

<small> printf("Error opening file for writing.\n"); pthread_exit(NULL);</small>

<small> }</small>

<small> fprintf(out, "Size of array: %d\n", n); fprintf(out, "Value of elements: "); for(int i ; i n; i=<++) { fprintf(out, "%d ", a[i]); }</small>

<small> fprintf(out, "\n"); fprintf(out, "Primes: "); for(int i ; i k; i=<++) { fprintf(out, "%d ", prime[i]); }</small>

<small> fprintf(out, "\n");</small>

<small> fprintf(out, "Sum of Primes: %d\n", sum); fprintf(out, "Primes sorted from smallest to highest: "); for(int i ; i k; i=<++) {</small>

<small> fprintf(out, "%d ", primeSorted[i]); }</small>

<small> fprintf(out, "\n"); fclose(out); pthread_exit(NULL);}</small>

<small>intmain( argc, intchar* argv[]) { (argc ) {if!=3</small>

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

<small> PAGE \* MERGEFORMAT 35</small>

<small> printf("Usage: %s <input_filename> <output_filename>\n", argv[ ]);0</small>

<small> return 1; }</small>

<small> }</small>

<small> pthread_create(&tid[1], NULL, thr2, NULL); pthread_create(&tid[2], NULL, thr3, (void*) argv[ ]);2 pthread_join(tid[1], NULL);</small>

<small> pthread_join(tid[2], NULL); return 0;</small>

<b>B: Kết Quả Demo:</b>

</div>

×