1. Trang chủ
  2. » Luận Văn - Báo Cáo

báo cáo bài tập nmhđh

11 0 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Báo Cáo Bài Tập NMHĐH
Tác giả Trần Thanh Liêm
Trường học Trường Đại Học Tôn Đức Thắng
Chuyên ngành Công Nghệ Thông Tin
Thể loại bài tập
Năm xuất bản 2023-2024
Định dạng
Số trang 11
Dung lượng 530,91 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

PHẦN BÀI TẬP1.. The child process passes the first argument n from argv [1], which is an integer greater than 3, to its parent process through the Message Queue.. The parent process rece

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ĐH

HK2, 2023-2024

Lab 7 Nhóm: 10 Tổ: 02

1: Trn Thanh Liêm ( MSSV: 52300041)

Trang 2

B PHẦN BÀI TẬP

1 The child process passes the first argument n from argv [1], which is an integer greater than

3, to its parent process through the Message Queue The parent process receive, calculates the sum of factorial of numbers from 1 to n and writes it to the Message Queue Child processes receive and output data to the screen

A Code chương tr6nh:

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#define MAX_QUEUE_SIZE 1024

typedef struct {

long mtype;

int data;

} message_t;

int factorial(int n) {

if (n == 0 || n == 1)

return 1;

else

return n * factorial(n - 1);

}

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

key_t key;

int msgid, n, sum = 0;

message_t msg;

Trang 3

if (argc != 2) {

fprintf(stderr, "Usage: %s <n>\n", argv[0]);

return 1;

}

n = atoi(argv[1]);

if (n <= 3) {

fprintf(stderr, "n must be greater than 3\n");

return 1;

}

key = ftok("/tmp", 'a');

if (key == -1) {

perror("ftok");

return 1;

}

msgid = msgget(key, IPC_CREAT | 0666);

if (msgid == -1) {

perror("msgget");

return 1;

}

pid_t pid = fork();

if (pid == -1) {

perror("fork");

return 1;

} else if (pid == 0) {

msg.mtype = 1;

msg.data = n;

Trang 4

if (msgsnd(msgid, &msg, sizeof(msg.data), 0) == -1) {

perror("msgsnd");

return 1;

}

printf("Child process sent n = %d to the parent process\n", n);

if (msgrcv(msgid, &msg, sizeof(msg.data), 0, 0) == -1) {

perror("msgrcv");

return 1;

}

printf("Child process received the sum: %d\n", msg.data);

printf("Child process output: %d\n", msg.data);

} else {

if (msgrcv(msgid, &msg, sizeof(msg.data), 0, 0) == -1) {

perror("msgrcv");

return 1;

}

printf("Parent process received n = %d from the child process\n", msg.data); for (int i = 1; i <= msg.data; i++) {

sum += factorial(i);

}

msg.mtype = 1;

msg.data = sum;

if (msgsnd(msgid, &msg, sizeof(msg.data), 0) == -1) {

perror("msgsnd");

return 1;

}

Trang 5

printf("Parent process calculated the sum: %d\n", sum);

}

if (msgctl(msgid, IPC_RMID, NULL) == -1) {

perror("msgctl");

return 1;

}

return 0;

}

B K7t qu: demo:

2 The child process reads data from a file consisting of two integers and an operation +, -, *, / and passes all to the parent process (by a Message Queue) The parent process calculates the result and returns it to the child process The child process writes the result to the file

A Code chương trRnh:

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

Trang 6

#include <sys/msg.h>

#define MAX_QUEUE_SIZE 1024

typedef struct {

long mtype;

int num1;

int num2;

char op;

int result;

} message_t;

int main(void) {

key_t key;

int msgid;

message_t msg;

FILE *file;

key = ftok("/tmp", 'a');

if (key == -1) {

perror("ftok");

return 1;

}

msgid = msgget(key, IPC_CREAT | 0666);

if (msgid == -1) {

Trang 7

perror("msgget");

return 1;

}

pid_t pid = fork();

if (pid == -1) {

perror("fork");

return 1;

} else if (pid == 0) {

file = fopen("data.txt", "r");

if (file == NULL) {

perror("fopen");

return 1;

}

fscanf(file, "%d %d %c", &msg.num1, &msg.num2, &msg.op); fclose(file);

msg.mtype = 1;

if (msgsnd(msgid, &msg, sizeof(msg) - sizeof(msg.mtype), 0) == -1) { perror("msgsnd");

return 1;

}

printf("Child process sent data to the parent process\n");

if (msgrcv(msgid, &msg, sizeof(msg) - sizeof(msg.mtype), 0, 0) == -1) { perror("msgrcv");

Trang 8

return 1;

}

printf("Child process received the result: %d\n", msg.result);

file = fopen("data.txt", "w");

if (file == NULL) {

perror("fopen");

return 1;

}

fprintf(file, "%d %c %d = %d", msg.num1, msg.op, msg.num2, msg.result); fclose(file);

printf("Child process wrote the result to the file\n");

} else {

if (msgrcv(msgid, &msg, sizeof(msg) - sizeof(msg.mtype), 0, 0) == -1) { perror("msgrcv");

return 1;

}

printf("Parent process received data from the child process\n");

switch (msg.op) {

case '+':

msg.result = msg.num1 + msg.num2;

break;

case '-':

msg.result = msg.num1 - msg.num2;

break;

Trang 9

case '*':

msg.result = msg.num1 * msg.num2;

break;

case '/':

if (msg.num2 == 0) {

fprintf(stderr, "Error: Division by zero\n");

return 1;

}

msg.result = msg.num1 / msg.num2;

break;

default:

fprintf(stderr, "Error: Invalid operator\n");

return 1;

}

printf("Parent process calculated the result: %d\n", msg.result);

msg.mtype = 1;

if (msgsnd(msgid, &msg, sizeof(msg) - sizeof(msg.mtype), 0) == -1) { perror("msgsnd");

return 1;

}

printf("Parent process sent the result to the child process\n"); }

if (msgctl(msgid, IPC_RMID, NULL) == -1) {

perror("msgctl");

Trang 10

return 1;

}

return 0;

}

B KVt quX demo:

Trang 11

KẾT LUẬN

Sau khi học và hoàn thành phần lab 7 nhóm thu được k7t sau:

- Bi7t cách tạo Messenger Queue

Ngày đăng: 07/05/2024, 21:51

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w