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 (374.65 KB, 16 trang )
<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">
#include <stdio.h>#include <stdlib.h>#include <unistd.h>
int factorial(int n) { int result = 1;
for (int i = 2; i <= n; i++) { result *= i;
} return result;}
int main(int argc, char *argv[]) { if (argc != 2) {
fprintf(stderr, "Usage: %s <number>\n", argv[0]); return EXIT_FAILURE;
}
int num = atoi(argv[1]);
</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">pid_t pid = fork(); if (pid < 0) { perror("fork"); return EXIT_FAILURE; }
if (pid == 0) { // Tiến trình con close(pipefd[0]);
int result = factorial(num);
if (write(pipefd[1], &result, sizeof(result)) == -1) { perror("write");
return EXIT_FAILURE; }
</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">close(pipefd[1]); exit(EXIT_SUCCESS); } else { // Tiến trình cha close(pipefd[1]);
int result;
if (read(pipefd[0], &result, sizeof(result)) == -1) { perror("read");
return EXIT_FAILURE; }
close(pipefd[0]);
printf("Factorial of %d is: %d\n", num, result); }
return EXIT_SUCCESS;}
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/wait.h>int main(int argc, char *argv[]) { if (argc != 5) {
fprintf(stderr, "Usage: %s <num1> <num2> <operation> <output_filename>\nNote that multiplication will be written as '*' to avoid the machine misinterpreting it as another function\n", argv[0]);
return EXIT_FAILURE; }
int num1 = atoi(argv[1]); int num2 = atoi(argv[2]); char op = argv[3][0];
char* output_filename = argv[4];
int pipefd[2]; pid_t pid;
if (pipe(pipefd) == -1) { perror("pipe"); return EXIT_FAILURE; }
pid = fork(); if (pid < 0) { perror("fork"); return EXIT_FAILURE; }
</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">if (pid == 0) { // Tiến trình con close(pipefd[0]); int result; switch (op) { case '+':
result = num1 + num2; break;
fprintf(stderr, "Error: Division by zero\n"); close(pipefd[1]);
exit(EXIT_FAILURE); }
break; default:
fprintf(stderr, "Error: Invalid operation\n"); close(pipefd[1]);
exit(EXIT_FAILURE); }
FILE *file = fopen(output_filename, "w"); if (file == NULL) {
perror("fopen"); close(pipefd[1]); exit(EXIT_FAILURE); }
</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">fprintf(file, "%d %c %d = %d\n", num1, op, num2, result); fclose(file);
close(pipefd[1]); exit(EXIT_SUCCESS); } else { // Tiến trình cha close(pipefd[1]);
wait(NULL); // Đợi tiến trình con hồn thành
printf("Result has been written to '%s'\n", output_filename); }
return EXIT_SUCCESS;}
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/wait.h>
</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">int factorial(int n) {if (n == 0) return 1;
return n * factorial(n - 1);}
int main(int argc, char *argv[]) {if (argc != 2) {
fprintf(stderr, "Usage: %s <number>\n", argv[0]); return 1;
if (pid == 0) { // Child process int fd = open(fifo_name, O_WRONLY); if (fd == -1) {
perror("open"); return 1; }
int result = factorial(num); write(fd, &result, sizeof(result)); close(fd);
exit(0);
} else { // Parent process
int fd = open(fifo_name, O_RDONLY); if (fd == -1) {
perror("open"); return 1;
</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">} int result;
read(fd, &result, sizeof(result));
printf("Factorial of %d is: %d\n", num, result); close(fd);
wait(NULL);}
unlink(fifo_name); // Remove the FIFOreturn 0;
#define FIFO_FILE "/tmp/calculator_fifo"
int main() {
// Create named pipemkfifo(FIFO_FILE, 0666);
</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">pid_t pid = fork();if (pid == -1) { perror("fork"); exit(EXIT_FAILURE);
// Receive data from parent process int num1, num2;
char operation;
read(fd, &num1, sizeof(num1)); read(fd, &num2, sizeof(num2)); read(fd, &operation, sizeof(operation));
// Calculate result int result; switch (operation) { case '+':
</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">result = num1 + num2; break;
fprintf(stderr, "Error: Division by zero\n"); exit(EXIT_FAILURE);
} break; default:
fprintf(stderr, "Error: Invalid operation\n"); exit(EXIT_FAILURE);
}
// Close pipe close(fd);
// Open pipe for writing
fd = open(FIFO_FILE, O_WRONLY);
</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">if (fd == -1) { perror("open"); exit(EXIT_FAILURE); }
// Send result back to parent process write(fd, &result, sizeof(result));
// Close pipe close(fd);
exit(EXIT_SUCCESS);} else { // Parent process // Open pipe for writing
int fd = open(FIFO_FILE, O_WRONLY); if (fd == -1) {
perror("open"); exit(EXIT_FAILURE); }
// Get input from user int num1, num2; char operation;
printf("Enter two integers and an operation (+, -, *, /): "); scanf("%d %d %c", &num1, &num2, &operation);
// Send data to child process
</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">write(fd, &num1, sizeof(num1)); write(fd, &num2, sizeof(num2)); write(fd, &operation, sizeof(operation));
// Close pipe close(fd);
// Open pipe for reading
fd = open(FIFO_FILE, O_RDONLY); if (fd == -1) {
perror("open"); exit(EXIT_FAILURE); }
// Receive result from child process int result;
read(fd, &result, sizeof(result));
// Close pipe close(fd);
// Write result to file
FILE *file = fopen("result.txt", "w"); if (file == NULL) {
perror("fopen"); exit(EXIT_FAILURE);
</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">