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

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

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 (3 MB, 20 trang )

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

<b>TRƯỜNG ĐẠI HỌC TÔN ĐỨC THẮNG KHOA CÔNG NGHỆ THÔNG TIN </b>

<b>BÁO CÁO BÀI TẬP NMHĐH HK2, 2021-2022</b>

<b>Lab 4</b>

<b>Thành Viên: Nguyễn Ngô Đăng Khoa (521H0084)</b>

2. Viết chương trình đa luồng để xuất ra số nguyên tố. Người dùng chạy chương trình và nhập vào một số ngun thơng qua đối số tại dịng lệnh. Chương trình sau đó sẽ tạo ra một tiến trình riêng biệt xuất ra tất cả các số nguyên tố nhỏ hơn hoặc bằng số được nhập bởi người dùng...12

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

3. Chỉnh sửa lại bài 1 sao cho thay vì xuất kết quả ra màn hình thì kết quả sẽ được ghi vào tập tin result.txt ở cùng thư mục với chương trình chạy...154. Viết chương trình để sao chép dữ liệu từ file nguồn vào file đích. Với tên file nguồn và file đích là đối số đầu vào. Đếm số kí tự đã sao chép. Giả sử chương trình chỉ chạy trên tập tin text...17KẾT LUẬN...20

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

printf("This is thread %d\n", * int*(( ) ar)); sleep( );2

pthread_create(&tid[i], NULL thr1, , (void* &tid[i]);) sleep( );1 N u b dịng này ch ng trình s không hi n khi

run 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">

Ví Dụ 2:

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

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>struct arr

{ n;int a[int 10];};

count;int

structarr * =ap (struct arr*) ;ar

for(count=0; count ap n; count< -> ++) printf("%d\t",ap a[count]);-> printf("\n");

}

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

intmain( intargc, char*argv[]){

structarr ar; ar.n ;=5

i;int

for =(i 0;i ar.n;i++) ar.a[i]=i+1; pthread_t tid;

pthread_create( tid,& NULL thr1,& , ar);& sleep( );2

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

if(pthread_join(tid[i],(void**) pstatus) )>0

printf("pthread_join for thread %d failure\n", (int)tid[i]);

printf("pthread_waited of %d OK, return code: %d\n", (int)tid[i], status);

sleep( );1

} sleep( );1

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

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>struct arr

{ n;int a[int 10];};struct file{

structarr ar; char* filename;};

static int sum =0;void*thr1(void*ar){

structarr *ap (= structarr*) ;ar

ap->n=3; i ;int =0

for =(i 0;i ap n;i-> ++) ap a[i] i ;-> = +1

structarr *ap (= structarr*) ;ar

i, s ;int =0

for =(i 0;i ap n;i-> ++) s s ap= + ->a[i]; sum=s;}

void*thr3 (void*ar){

structfile * = structfi ( file*) ;ar

FILE *out; count;int out= fopen(fi filename,-> "wb");

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

fprintf(out,"number element or array: %d\n", fi->ar.n); for(count=0; count< ->fi ar.n; count++)

fprintf(out,"%d\t",fi ar.a[count]);-> fprintf(out," "\n);

fprintf(out,"sum = %d\n",sum); printf("tong: %d\n",sum); fclose(out);

status, int *pstatus= &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) {

structfile arf; arf.ar=ar;

arf.filename=argv[ ];1

pthread_create( tid[ ],& 2 NULL thr3, ,(void* &arf);) }

} sleep( );2

return ;0

}

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

<b>A Code chương trình:</b>

#include <pthread.h>#include <stdlib.h>#include <stdio.h>typedef struct data{ int* arr; n_arr;int} data;

void*average(void*p){ data ptr (data ) p;* = *

// Declare sum dynamically to return to join: float* avr (= float*) calloc(1, sizeof float( )); avr[ ] ;0 =0

size ptr n_arr ;int = -> for(int = i 0; i size; i< ++){ avr[ ] avr[ ] ptr0 = 0 + ->arr[i];

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

}

avr[ ] avr[0 = 0]*1.0/(size); pthread_exit(avr);}

void*max(void*p){ data ptr (data ) p;* = *

// Declare sum dynamically to return to join: int* max (= int*) calloc(1, sizeof int( )); max[ ] ;0 =0

size ptr n_arr ;int = -> for(int = i 0; i size; i< ++){ if(max[0] ptr< ->arr[i]) max[ ] ptr0 = ->arr[i]; };

pthread_exit(max);}

void*min(void*p){ data ptr (data ) p;* = *

pthread_exit(min);}

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

intmain( intargc, char*argv[]){

// Start both threads:

pthread_create( tid[& 0 NULL], , average, &thread_data[ ]);0

pthread_create( tid[& 1 NULL], , max, &thread_data[ ]);1

pthread_create( tid[& 2 NULL], , min, &thread_data[ ]);1

// Declare space for sum: float* avr;

int* max;

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

int* min;

// Retrieve sum of threads: pthread_join(tid[0], (void**)&avr); pthread_join(tid[1], (void**)&max); pthread_join(tid[2], (void**)&min); printf("Gia tri trung binh: %.2f\n", *avr); printf("Gia tri lon nhat: %i\n", *max); printf("Gia tri nho nhat: %i\n", *min);

<b>A Code chương trình:</b>

#include <pthread.h>#include <stdlib.h>#include <stdio.h>typedef structdata{ int* arr;

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

number;int} data;intisPrime( intn){

// Corner case ( if n<= )1

return ;0

// Check from 2 to n-1 for int = ( i 2; i ; i<n ++) ( i if n% == 0) return ;0

int* avr (= int*) calloc(1, sizeof int( )); count ;int =0

size ptr number ;int = -> for(int = i 0; i <= size; i++){ if(isPrime(i) == )1

printf("%d ",i); }

pthread_exit(0);}

intmain( intargc, char*argv[]){

// T o m ng m i và đ i đ i s đã nh p v ki u d li u s

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

number int =atoi(argv[ ]);1

count ;int =0

for(int = i 0; i number; i< ++){ if(isPrime(i) == )1

count++; }

int* int_arr (= int*) calloc(count, sizeof(int));

// Declare arguments for both threads: data thread_data[1];

thread_data[ ].arr int_arr;0 = thread_data[ ].number number;0 = // Declare thread IDs:

pthread_t tid[1];

// Start both threads:

pthread_create( tid[& 0], NULL printPrime, , &thread_data[ ]);0

// Declare space for sum: float* avr;

int* max; int* min;

// Retrieve sum of threads: pthread_join(tid[0], NULL); printf("\n");

return ;0

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

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

3. Chỉnh sửa lại bài 1 sao cho thay vì xuất kết quả ra màn hình thì kết quả sẽ được ghi vào tập tin result.txt ở cùng thư mục với chương trình chạy

<b>A Code chương trình:</b>

#include <pthread.h>#include <stdlib.h>#include <stdio.h>typedef structdata{ int* arr; number;int} data;intisPrime( intn){

// Corner case ( if n<= )1

return ;0

// Check from 2 to n-1 for int = ( i 2; i ; i<n ++) ( i if n% == 0) return ;0

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

printf("%d ",i); }

pthread_exit(0);}

intmain( intargc, char*argv[]){

count++; }

int* int_arr (= int*) calloc(count, sizeof(int));

// Declare arguments for both threads: data thread_data[1];

thread_data[ ].arr int_arr;0 = thread_data[ ].number number;0 = // Declare thread IDs:

pthread_t tid[1];

// Start both threads:

pthread_create( tid[& 0], NULL printPrime, , &thread_data[ ]);0

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

// Declare space for sum: float* avr;

int* max; int* min;

// Retrieve sum of threads: pthread_join(tid[0], NULL); printf("\n");

<b>A Code chương trình:</b>

#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct data{ char* filename1 ; char* filename2 ;

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

void*copyFile(void*p){ data ptr (data ) p;* = *

FILE fp1 * = fopen(ptr->filename1, " "r); FILE fp2 * = fopen(ptr->filename2, " "w);

printf("%s ",ptr filename1);-> char = c fgetc(fp1); while (c EOF)!= {

fputc(c, fp2); c = fgetc(fp1); }

fclose(fp1); fclose(fp2); pthread_exit(0);}

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

intmain( intargc, char*argv[]){

// Declare arguments for both threads: data thread_data[1];

thread_data[ ].filename1 argv[0 = 1]; thread_data[ ].filename2 argv[0 = 2]; // Declare thread IDs:

pthread_t tid[2];

// Start both threads:

pthread_create( tid[& 0 NULL], , countCharacters, &thread_data[ ]);0

pthread_create( tid[& 1 NULL], , copyFile, &thread_data[ ]);0

// Declare space for sum: int* count ;=0

// Retrieve sum of threads:

pthread_join(tid[0], (void**)&count); pthread_join(tid[1 NULL], );

printf("Da sao chep thanh cong %i ki tu \n",*count); return ;0

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

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

KẾT LUẬN

<b>Sau khi học và hoàn thành phần lab 2 em thu được kết sau:- Hoàn thành tất cả phần thực hành</b>

<b>- Hoàn thành 4/6 bài tập</b>

</div>

×