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 1TRƯỜ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: Trn Thanh Liêm ( MSSV: 52300041)
Trang 2B 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 3if (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 4if (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 5printf("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 7perror("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 8return 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 9case '*':
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 10return 1;
}
return 0;
}
B KVt quX demo:
Trang 11KẾ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