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

18 0 0
Tài liệu đã được kiểm tra trùng lặp
báo cáo bài tập nhập môn hệ điều hành lab 5

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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

Trang 1

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

BÁO CÁO BÀI TẬP NMHĐHHK2, 2022-2023

Lab 5

Trang 3

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

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

sleep(3);return 0;}

Trang 4

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

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

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

Trang 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;}

Trang 7

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

char *filename;};

static int sum = 0;

{

Trang 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

Trang 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;}

Trang 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 toàncục).

- Luồng thứ 2 chờ luồng thứ nhất hoàn thành, thực hiện tính toá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:

N = 4 4! = 24 Sum = 156

A: Code Chươ ng Trình:

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

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

Trang 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ừ hoà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];

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

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

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

- 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

457

Trang 14

Kết quả trong tệp kết quả trông như thế này:Size of array: 10

Value of elements: 4 5 7 8 11 9 20 13 2 3 Primes: 5 7 11 13 2 3

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

Trang 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ố nguyên 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);

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

}

Trang 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:ế ả

Trang 18

C K T LU NẾẬ

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

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.

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.

Ngày đăng: 08/05/2024, 16:24

Tài liệu cùng người dùng

Tài liệu liên quan