CS3413/Assignment2/main.c

114 lines
3.0 KiB
C
Raw Normal View History

2023-09-26 11:27:36 -03:00
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdbool.h>
2023-09-26 14:23:21 -03:00
#include <stdlib.h>
#include <wait.h>
void print_prompt() {
printf("[%d]> ", getpid());
}
2023-09-26 11:27:36 -03:00
2023-09-26 16:16:09 -03:00
void one_child() {
print_prompt();
printf("Hello, I am a parent process\n");
if (fork() == 0) { // Child process
print_prompt();
printf("Hello, I am a child process\n");
exit(0);
} else { // Parent process
wait(NULL);
}
}
void add_numbers() {
int number = 1;
int send_to_child[2];
int receive_from_child[2];
pipe(send_to_child);
pipe(receive_from_child);
if (fork() == 0) { // Child process
int subtotal = 0;
while (number != 0) {
// Read the numbers from the send_to_child pipe
read(send_to_child[0], &number, sizeof(int));
// Add the number to the subtotal
subtotal += number;
print_prompt();
printf("The subtotal is %d\n", subtotal);
// Write the sum to the receive_from_child pipe
write(receive_from_child[1], &subtotal, sizeof(int));
}
exit(0);
} else { // Parent process
int total = 0;
while (number != 0) {
print_prompt();
printf("Please input a number, 0 to terminate: ");
// Read the number from the user, and convert it to an int
char buffer[100];
fgets(buffer, 100, stdin);
number = atoi(buffer);
// Display what was entered
print_prompt();
printf("You entered %d\n", number);
// Write to child process
write(send_to_child[1], &number, sizeof(int));
// Read the total from the child process
read(receive_from_child[0], &total, sizeof(int));
}
wait(NULL);
print_prompt();
printf("The total is %d\n", total);
}
close(send_to_child[0]);
close(send_to_child[1]);
close(receive_from_child[0]);
close(receive_from_child[1]);
}
2023-09-26 11:27:36 -03:00
int main() {
bool is_looping = true;
2023-09-26 16:16:09 -03:00
2023-09-26 11:27:36 -03:00
// Listen loop
while (is_looping) {
2023-09-26 14:23:21 -03:00
print_prompt();
2023-09-26 16:16:09 -03:00
char input[100];
2023-09-26 14:47:40 -03:00
fgets(input, 100, stdin); // Input for commands
if (strcmp(input, "stop\n") == 0) { // Stop command
2023-09-26 14:23:21 -03:00
is_looping = false;
}
2023-09-26 14:47:40 -03:00
if (strcmp(input, "onechild\n") == 0) { // One child command
2023-09-26 16:16:09 -03:00
one_child();
2023-09-26 11:27:36 -03:00
}
2023-09-26 14:47:40 -03:00
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
2023-09-26 16:16:09 -03:00
add_numbers();
2023-09-26 11:27:36 -03:00
}
2023-09-26 16:16:09 -03:00
if (strcmp(input, "exec\n") == 0) {
2023-09-26 15:45:41 -03:00
char *args[2];
args[0] = "/bin/ls";
args[1] = NULL;
execv(args[0], args);
}
2023-09-26 11:27:36 -03:00
}
}
2023-09-26 14:23:21 -03:00
void temp() {
char input[5][100];
char buffer[500];
//Print prompt
print_prompt();
if (fgets(buffer, 500, stdin) == NULL) {
exit(0);
}
//split buffer on spaces into input array
sscanf(buffer, "%s %s %s %s %s", input[0], input[1], input[2], input[3], input[4]);
2023-09-26 16:16:09 -03:00
}