Create 4
This commit is contained in:
1 parent
8b3baccedc
commit
42a79e4154
4 files changed
+180
No files matched your search
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FIFO_PATH "/tmp/collector_fifo"
|
||||
|
||||
int main() {
|
||||
int fd;
|
||||
int num;
|
||||
int sum = 0;
|
||||
|
||||
printf("Calculator started. Waiting for collector...\n");
|
||||
|
||||
// Open FIFO for reading
|
||||
fd = open(FIFO_PATH, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("Failed to open FIFO");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Connected! Ready to receive integers.\n");
|
||||
|
||||
// Read integers from the FIFO and calculate the sum
|
||||
while (read(fd, &num, sizeof(int)) > 0) {
|
||||
sum += num;
|
||||
printf("Received: %d, Current Sum: %d\n", num, sum);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
printf("Calculator exiting.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
|
||||
int main() {
|
||||
int pipe_fd[2];
|
||||
pid_t pid;
|
||||
int num, sum = 0;
|
||||
|
||||
// Create pipe
|
||||
if (pipe(pipe_fd) == -1) {
|
||||
perror("Pipe creation failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Create child process
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
// Fork failed
|
||||
perror("Fork failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else if (pid == 0) {
|
||||
// Child process - calculator
|
||||
close(pipe_fd[1]); // Close write end of pipe
|
||||
|
||||
printf("[Calculator] Started. Waiting for integers...\n");
|
||||
|
||||
while (read(pipe_fd[0], &num, sizeof(int)) > 0) {
|
||||
sum += num;
|
||||
printf("[Calculator] Received: %d, Current Sum: %d\n", num, sum);
|
||||
}
|
||||
|
||||
close(pipe_fd[0]);
|
||||
printf("[Calculator] Exiting.\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else {
|
||||
// Parent process - collector
|
||||
close(pipe_fd[0]); // Close read end of pipe
|
||||
|
||||
printf("[Collector] Started. Enter integers (Ctrl+D to exit):\n");
|
||||
|
||||
while (scanf("%d", &num) == 1) {
|
||||
if (write(pipe_fd[1], &num, sizeof(int)) == -1) {
|
||||
perror("Write error");
|
||||
break;
|
||||
}
|
||||
printf("[Collector] Sent: %d\n", num);
|
||||
}
|
||||
|
||||
close(pipe_fd[1]); // Close write end to signal EOF to child
|
||||
printf("[Collector] Waiting for calculator to finish...\n");
|
||||
|
||||
// Wait for child to finish
|
||||
wait(NULL);
|
||||
|
||||
printf("[Collector] Exiting.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define FIFO_PATH "/tmp/collector_fifo"
|
||||
|
||||
int main() {
|
||||
int fd;
|
||||
int num;
|
||||
|
||||
// Create the FIFO if it doesn't exist
|
||||
if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
|
||||
perror("mkfifo error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Collector started. Connecting to calculator...\n");
|
||||
|
||||
// Open FIFO for writing
|
||||
fd = open(FIFO_PATH, O_WRONLY);
|
||||
if (fd == -1) {
|
||||
perror("Failed to open FIFO");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Connected! Enter integers (Ctrl+D to exit):\n");
|
||||
|
||||
while (scanf("%d", &num) == 1) {
|
||||
if (write(fd, &num, sizeof(int)) == -1) {
|
||||
perror("Write error");
|
||||
break;
|
||||
}
|
||||
printf("Sent: %d\n", num);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
printf("Collector exiting.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
volatile int number = 0; // Current even number
|
||||
volatile int direction = 1; // 1 for ascending, -1 for descending
|
||||
|
||||
void handle_sigint(int sig) {
|
||||
// Change direction when CTRL+C is pressed
|
||||
direction = -direction;
|
||||
printf("\nChanging direction to %s\n",
|
||||
(direction > 0) ? "ascending" : "descending");
|
||||
// Re-register the signal handler
|
||||
signal(SIGINT, handle_sigint);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Register SIGINT handler
|
||||
signal(SIGINT, handle_sigint);
|
||||
|
||||
printf("Starting program: outputting even numbers\n");
|
||||
printf("Press Ctrl+C to change direction\n");
|
||||
|
||||
while (1) {
|
||||
printf("%d\n", number);
|
||||
number += 2 * direction; // Add or subtract 2 based on direction
|
||||
fflush(stdout); // Force output to be displayed immediately
|
||||
sleep(1); // Wait for 1 second
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user