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

báo cáo bài tập nhập môn hệ điều hành lab 6 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 (919.34 KB, 17 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</b>

<b>HK2, 2022-2023</b>

<b>Lab 6</b>

<b>Nhóm:</b> O1 <b>Tổ:</b> 02

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

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

char result[100]; int fp[2], pid; if (argc < 2) {

printf("Doi so thieu\n"); return -1;

}

if (pipe(fp) == 0) {

pid = fork(); if (pid < 0) {

printf("Fork failed\n"); return -1;

}

else if (pid == 0) {

printf("Data from child: %s\n", argv[1]); close(fp[0]);

write(fp[1], argv[1], strlen(argv[1])); }

else {

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

close(fp[1]);

read(fp[0], result, strlen(argv[1])); printf("Read from child: %s\n", result); }

} else {

printf("Pipe failed\n"); return -2;

}}

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

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

char s1[PIPE_BUF], s2[PIPE_BUF], buf[PIPE_BUF]; int childpid, readfd, writefd;

if ((mknod(FIFO1, S_IFIFO | PM, 0) < 0) && (errno != EEXIST)) {

printf("Fail to create FIFO 1. Aborted.\n"); return -1;

if ((readfd = open(FIFO1, 0)) < 0) {

perror("Child cannot open read FIFO.\n"); }

if ((writefd = open(FIFO2, 1)) < 0) {

perror("Child cannot open write FIFO.\n"); }

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

close(readfd); close(writefd); return 1; }

else if (childpid > 0) {

if ((writefd = open(FIFO1, 1)) < 0) {

perror("Parent cannot open write FIFO.\n"); }

if ((readfd = open(FIFO2, 0)) < 0) {

perror("Parent cannot open read FIFO.\n"); }

printf("Enter data to FIFO1 from Parent: "); fgets(s1, PIPE_BUF, stdin);

write(writefd, s1, strlen(s1)); read(readfd, s2, PIPE_BUF);

printf("Parent read from child: %s\n", s2); while (wait((int*)0) != childpid); close(readfd);

close(writefd); if (unlink(FIFO1) < 0) {

perror("Cannot remove FIFO1.\n"); }

if (unlink(FIFO2) < 0) {

perror("Cannot remove FIFO2.\n"); }

return 1; }

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

else {

printf("Fork failed.\n"); return -1;

}}

Vấn đề 2: Tiến trình cha đọc hai số nguyên và một phép toán +, -, *, / và chuyển tất cả cho tiến trình con. Tiến trình con tính tốn kết quả và trả về kết quả cho tiến trình cha. Tiến trình cha ghi kết quả vào một tập tin.

V nấ đ 1:ề

A: Code Chươ ng Trình:

//Unnamed_Pipe/bai1.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>

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

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

pid_t child_pid;

if ((child_pid = fork()) == -1) { perror("fork");

exit(1); }

if (child_pid == 0) { // Tiến trình con close(pipe_fd[1]); // Đóng đầu ghi int result = 1;

for (int i = 2; i <= num; i++) { result *= i;

wait(NULL); }

return 0;

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

if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }

pid_t child_pid = fork(); if (child_pid == -1) { perror("fork"); exit(EXIT_FAILURE); }

if (child_pid == 0) {

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

switch (op) { case '+':

result = num1 + num2; break;

perror("Chia cho số 0"); exit(EXIT_FAILURE); }

break; default:

perror("Tốn tử khơng hợp lệ"); exit(EXIT_FAILURE); }

close(pipefd[0]); // Đóng phần đọc của ống // In kết quả ra màn hình

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

printf("Kết quả: %d\n", result); exit(EXIT_SUCCESS); } else {

// Tiến trình cha

close(pipefd[0]); // Đóng phần đọc của ống int num1, num2;

int status; wait(&status); }

return 0;}

B: K t Qu Demo:ế ả

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

* Name Pipe:

Vấn đề 1: Quá trình cha truyền đối số đầu tiên từ argv [1] là một số nguyên lớn hơn 3 đối với quá trình con thơng qua Pipe. Tiến trình con nhận, tính giai thừa n! = 1*2*…* n và ghi nó vào đường ống. Quá trình cha mẹ nhận và xuất dữ liệu ra màn hình.

Vấn đề 2: Tiến trình cha đọc hai số nguyên và một phép toán +, -, *, / và chuyển tất cả cho tiến trình con. Tiến trình con tính tốn kết quả và trả về kết quả cho tiến trình cha. Tiến trình cha ghi kết quả vào một tập tin.

V nấ đ 1:ề

A: Code Chươ ng Trình:

//Named_Pipe/bai1.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>

#include <sys/wait.h> // Include sys/wait.h for the wait function#define FIFO_FILE "myfifo"

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

pid_t child_pid; if (argc != 2) {

printf("Usage: %s <number>\n", argv[0]); exit(1);

}

int num = atoi(argv[1]); mkfifo(FIFO_FILE, 0666);

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

if ((child_pid = fork()) == -1) { perror("fork");

exit(1); }

if (child_pid == 0) { // Tiến trình con int fd = open(FIFO_FILE, O_RDONLY); int received_num;

read(fd, &received_num, sizeof(received_num)); close(fd);

return 0;}

B: K t Qu Demo:ế ả

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

V nấ đ 2:ề

A: Code Chươ ng Trình:

//Named_Pipe/bai2.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>

#include <sys/wait.h> // Include sys/wait.h for the wait function#define FIFO_FILE "myfifo"

int main() {

mkfifo(FIFO_FILE, 0666); pid_t child_pid = fork(); if (child_pid == -1) { perror("fork"); exit(EXIT_FAILURE); }

if (child_pid == 0) { // Tiến trình con

int fd = open(FIFO_FILE, O_RDONLY); int num1, num2;

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

perror("Chia cho số 0"); exit(EXIT_FAILURE); }

break; default:

perror("Tốn tử khơng hợp lệ"); exit(EXIT_FAILURE); }

// In kết quả ra màn hình printf("Kết quả: %d\n", result); exit(EXIT_SUCCESS); } else {

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

write(fd, &num1, sizeof(int)); write(fd, &num2, sizeof(int)); write(fd, &op, sizeof(char)); close(fd);

// Chờ tiến trình con hồn thành int status;

wait(&status); }

return 0;}

B: K t Qu Demo:ế ả

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

C. K T LU NẾẬ

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

<small> </small>

<b><small>Unnamed Pipe (Pipe khơng tên):</small></b>

<small>Unnamed Pipe là một cách hiệu quả để truyền dữ liệu giữa các tiến trình cha và con hoặc giữa các tiến trình ngang hàng.</small>

<small>Chúng ta sử dụng hàm pipe để tạo pipe và fork để tạo tiến trình con.</small>

<small>Tiến trình cha sử dụng write để gửi dữ liệu và tiến trình con sử dụng read để đọc dữ liệu.</small>

<small>Pipe thường được sử dụng cho truyền dữ liệu nhanh chóng giữa các tiến trình có mối quan hệ gần.</small>

<b><small> Named Pipe (Pipe có tên):</small></b>

<small>Named Pipe (hoặc FIFO) là một tệp đặc biệt trên hệ thống tệp Linux được sử dụng để truyền dữ liệu giữa các tiến trình khơng liên quan.</small>

<small>Chúng ta sử dụng mkfifo để tạo Named Pipe.</small>

<small>Named Pipe cho phép truyền dữ liệu giữa các tiến trình khơng có mối quan hệ cha con hoặc khơng có chung bộ nhớ.</small>

<small>Named Pipe thường được sử dụng cho truyền dữ liệu giữa các tiến trình độc lập.</small>

<b><small> Phải quản lý đúng các thao tác ghi và đọc:</small></b>

<small>Trong cả Unnamed Pipe và Named Pipe, quản lý đúng thứ tự các thao tác ghi và đọc làrất quan trọng. Tiến trình ghi trước và tiến trình đọc sau đó.</small>

<small>Unnamed Pipe chỉ cho phép truyền dữ liệu một chiều, trong khi Named Pipe cho phép truyền hai chiều (hoặc cả hai đầu có thể sử dụng ghi và đọc).</small>

<b><small> Sử dụng Named Pipe với tệp đặc biệt:</small></b>

<small>Sử dụng Named Pipe đòi hỏi tạo và quản lý một tệp đặc biệt trên hệ thống tệp. Nó là một cách tiếp cận linh hoạt để truyền dữ liệu giữa các tiến trình.</small>

<b><small> Xóa Named Pipe sau khi sử dụng:</small></b>

<small>Khi bạn đã hoàn thành việc sử dụng Named Pipe, hãy chắc chắn xóa nó khỏi hệ thống bằng cách sử dụng lệnh unlink. Khơng xóa Named Pipe có thể gây ra sự lãng phí tài nguyên và lỗi trong tương lai.</small>

<b><small> Sử dụng Pipe cho truyền dữ liệu nhanh chóng:</small></b>

<small>Cả Unnamed Pipe và Named Pipe có thể được sử dụng để truyền dữ liệu giữa các tiến trình một cách nhanh chóng và hiệu quả, giúp tối ưu hóa việc trao đổi thông tin trong hệ thống.</small>

</div>

×