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

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 (762.2 KB, 18 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ĐHHK2, 2022-2023</b>

<b>Lab 5</b>

</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(3);return 0;}

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

pthread_create(&tid[i], NULL, thr1, (void*) &tid[i]); }

for(i = 0; i < 3; i++)

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

{

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

printf("pthread_jold for thread %d fatlure\n", (int)tid[i]); return 0;

}

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

sleep(1); }

sleep(1); return 0;}

int count;

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

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

sleep(2);}

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

int i;

pthread_t tid[3];

int status, *pstatus = &status; for(i = 0; i < 3; i++) {

pthread_create(&tid[i], NULL, thr1, (void*) &tid[i]); }

for(i = 0; i < 3; i++) {

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

printf("pthread_joid 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); }

sleep(1); return 0;}

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

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

char *filename;};

static int sum = 0;

{

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

ap->n = 3; int i;

for (i = 0; i < ap->n; i++) {

ap->a[i] = i + 1; }

sum = s;}

fprintf(out, "\n");

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

} else

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

pthread_t tid[3];

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

struct file 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 10</span><div class="page_container" data-page="10">

B: K tế qu Demoả

B. PH N BÀI T PẦẬ

BÀI 1:

Viết chương trình tạo ba luồng thực hiện các tác vụ sau:

- Luồng đầu tiên lấy đối số từ mơi trường (argv [1]), kiểm tra xem số đó có bằng0 hay khơng, tính giai thừa của số này (có thể sử dụng biến struct hoặc biến tồncục).

- Luồng thứ 2 chờ luồng thứ nhất hoàn thành, thực hiện tính tốn số từ nhỏ hơnkết quả của hệ số giai thừa thứ nhất đã tính.

- Thread thứ 3 chờ thread thứ 2 hoàn thành, ghi kết quả vào file là đối số thứ 2 từbiến môi trường (argv [2]). Nội dung của bản ghi bao gồm: dòng đầu tiên lưu giátrị của argv [1]; Dòng thứ hai lưu kết quả của giai thừa; Dòng thứ ba lưu tổngcác số nhỏ hơn giai thừa.

Đầu ra của tệp res1 như sau:

<small>N = 4 4! = 24 Sum = 156 </small>

A: Code Chươ ng Trình:

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

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

struct ThreadArgs { int number; int factorial; int wordCount; char* outputFileName;};

void *calculateFactorial(void *args) {

struct ThreadArgs *data = (struct ThreadArgs *)args; int n = data->number;

void *calculateWordCount(void *args) {

struct ThreadArgs *data = (struct ThreadArgs *)args; while (data->factorial == 0) {

// Chờ cho đến khi luồng tính giai thừa hoàn thành }

token = strtok(NULL, " "); }

data->wordCount = count;

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

pthread_exit(NULL);}

void *writeToFile(void *args) {

struct ThreadArgs *data = (struct ThreadArgs *)args; while (data->wordCount == 0) {

// Chờ cho đến khi luồng tính số từ hồn thành }

FILE *file = fopen(data->outputFileName, "w"); if (file != NULL) {

fprintf(file, "N = %d\n", data->number);

fprintf(file, "%d! = %d\n", data->number, data->factorial); fprintf(file, "Sum = %d\n", data->wordCount);

fclose(file); } else {

printf("Lỗi khi mở tập tin.\n"); }

pthread_exit(NULL);}

int main(int argc, char *argv[]) { if (argc != 3) {

printf("Sử dụng: %s <số> <tập_tin_kết_quả>\n", argv[0]); return 1;

}

int number = atoi(argv[1]); struct ThreadArgs data; data.number = number; data.factorial = 0; data.wordCount = 0;

data.outputFileName = argv[2]; // Lưu tên tập tin kết quả pthread_t threads[3];

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

pthread_create(&threads[0], NULL, calculateFactorial, &data); pthread_create(&threads[1], NULL, calculateWordCount, &data); pthread_create(&threads[2], NULL, writeToFile, &data); pthread_join(threads[0], NULL);

pthread_join(threads[1], NULL); pthread_join(threads[2], NULL); return 0;

<small>Viết chương trình các luồng thực hiện các nhiệm vụ sau:- Lần đọc file đầu vào đầu tiên là đối số đầu tiên từ biến môi trường- Luồng thứ hai đếm tổng các số nguyên tố trong mảng</small>

<small>- Chuỗi thứ ba sắp xếp mảng tăng</small>

<small>- Luồng thứ tư thực hiện việc ghi file kết quả.Nội dung tập tin đầu vào và đầu ra như sau:Ví dụ: tệp đầu vào có định dạng sau10</small>

<small>457</small>

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

<small>Kết quả trong tệp kết quả trông như thế này: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>

int primeCount = 0;int primeSum = 0;// Đọc dữ liệu từ file input

void readInputFile(const char *fileName) { FILE *file = fopen(fileName, "r"); if (file == NULL) {

perror("Lỗi khi mở file"); exit(EXIT_FAILURE); }

// Đọc số lượng phần tử mảng từ file fscanf(file, "%d", &size);

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

// Đọc các phần tử mảng từ file for (int i = 0; i < size; i++) { fscanf(file, "%d", &array[i]); }

fclose(file);}

// Kiểm tra xem một số có phải số ngun tố khơngint isPrime(int num) {

if (num <= 1) return 0;

for (int i = 2; i * i <= num; i++) { if (num % i == 0) return 0; }

return 1;}

// Thread đếm số lượng số nguyên tố và tính tổng của chúngvoid *countPrimes(void *arg) {

for (int i = 0; i < size; i++) { if (isPrime(array[i])) {

primes[primeCount++] = array[i]; primeSum += array[i];

} }

return NULL;}

// Hàm so sánh dùng trong việc sắp xếp mảng số nguyên tốint compare(const void *a, const void *b) {

return (*(int *)a - *(int *)b);}

// Thread sắp xếp mảng số nguyên tố theo thứ tự tăng dầnvoid *sortPrimes(void *arg) {

qsort(primes, primeCount, sizeof(int), compare);

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

return NULL;}

// Ghi kết quả vào file outputvoid *writeResultFile(void *arg) { FILE *file = fopen("result.txt", "w"); if (file == NULL) {

perror("Error opening file"); exit(EXIT_FAILURE); }

// Ghi thông tin về mảng vào file fprintf(file, "Size of array: %d\n", size); fprintf(file, "Value of elements: "); for (int i = 0; i < size; i++) { fprintf(file, "%d ", array[i]); }

fprintf(file, "\nPrimes: ");

for (int i = 0; i < primeCount; i++) { fprintf(file, "%d ", primes[i]); }

fprintf(file, "\nSum of Primes: %d\n", primeSum); fprintf(file, "Primes sorted from smallest to highest: "); for (int i = 0; i < primeCount; i++) {

fprintf(file, "%d ", primes[i]); }

fclose(file); return NULL;}

int main(int argc, char *argv[]) { if (argc != 2) {

printf("Sử dụng: %s <file_input>\n", argv[0]); return EXIT_FAILURE;

}

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

// Đọc dữ liệu từ file input được truyền qua tham số readInputFile(argv[1]);

// Khởi tạo các thread pthread_t threads[3];

pthread_create(&threads[0], NULL, countPrimes, NULL); pthread_create(&threads[1], NULL, sortPrimes, NULL); // Chờ các thread kết thúc

pthread_join(threads[0], NULL); pthread_join(threads[1], NULL); // Ghi kết quả ra file

writeResultFile(NULL); return EXIT_SUCCESS;}

B: K t Qu Demo:ế ả

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

C. K T LU NẾẬ

<b>Sau khi học và hoàn thành phần lab 4 nhóm thu được kết sau:</b>

<b>1. Đồng thời và xử lý bất đồng bộ (Multithreading): Sử dụng các thread để thực hiện các nhiệm vụ cùng một lúc có thể cải thiện hiệu suất của chương trình, đặc biệt khi có các tác vụ độc lập có thể thực hiện song song.</b>

<b>2. Sử dụng thư viện POSIX Threads (pthread.h): Quản lý các thread để thực hiện các tác vụ cùng một lúc. Quen thuộc với việc tạo, chạy và kết thúc các thread.</b>

</div>

×