Finish assignment 2

This commit is contained in:
Isaac Shoebottom 2023-09-29 14:58:04 -03:00
parent 4392a02488
commit 50289cd303
2 changed files with 103 additions and 39 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.20)
project(Assignment2 C) project(Assignment2 C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)

View File

@ -1,21 +1,21 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <wait.h> #include <wait.h>
#include <limits.h>
void print_prompt() { #define MAX_ARGS 6 // 5 arguments and a null pointer
printf("[%d]> ", getpid());
void print_prompt(char *prompt) {
printf("(%d) %s", getpid(), prompt);
} }
void one_child() { void one_child() {
print_prompt(); print_prompt("Hello, I am a parent process\n");
printf("Hello, I am a parent process\n");
if (fork() == 0) { // Child process if (fork() == 0) { // Child process
print_prompt(); print_prompt("Hello, I am a child process\n");
printf("Hello, I am a child process\n");
exit(0); exit(0);
} else { // Parent process } else { // Parent process
wait(NULL); wait(NULL);
@ -37,8 +37,8 @@ void add_numbers() {
// Add the number to the subtotal // Add the number to the subtotal
subtotal += number; subtotal += number;
print_prompt(); print_prompt("The subtotal is ");
printf("The subtotal is %d\n", subtotal); printf("%d\n", subtotal);
// Write the sum to the receive_from_child pipe // Write the sum to the receive_from_child pipe
write(receive_from_child[1], &subtotal, sizeof(int)); write(receive_from_child[1], &subtotal, sizeof(int));
@ -47,16 +47,15 @@ void add_numbers() {
} else { // Parent process } else { // Parent process
int total = 0; int total = 0;
while (number != 0) { while (number != 0) {
print_prompt(); print_prompt("Please input a number, 0 to terminate:\n");
printf("Please input a number, 0 to terminate: ");
// Read the number from the user, and convert it to an int // Read the number from the user, and convert it to an int
char buffer[100]; char buffer[100];
fgets(buffer, 100, stdin); fgets(buffer, 100, stdin);
number = atoi(buffer); number = atoi(buffer);
// Display what was entered // Display what was entered
print_prompt(); print_prompt("You entered: ");
printf("You entered %d\n", number); printf("%d\n", number);
// Write to child process // Write to child process
write(send_to_child[1], &number, sizeof(int)); write(send_to_child[1], &number, sizeof(int));
@ -65,8 +64,8 @@ void add_numbers() {
read(receive_from_child[0], &total, sizeof(int)); read(receive_from_child[0], &total, sizeof(int));
} }
wait(NULL); wait(NULL);
print_prompt(); print_prompt("The total is ");
printf("The total is %d\n", total); printf("%d\n", total);
} }
close(send_to_child[0]); close(send_to_child[0]);
close(send_to_child[1]); close(send_to_child[1]);
@ -74,17 +73,94 @@ void add_numbers() {
close(receive_from_child[1]); close(receive_from_child[1]);
} }
void free_exec(char *buffer, char **subcommand) {
free(buffer);
for (int i = 0; i < MAX_ARGS; i++) {
free(subcommand[i]);
}
free(subcommand);
}
void exec() {
print_prompt("Please input exec subcommand:\n");
char *buffer = calloc(100, sizeof(char)); // input buffer size 100
char **subcommand = calloc(6, sizeof(char *)); //size 6 to allow for 5 arguments and a null pointer
for (int i = 0; i < MAX_ARGS; i++) {
subcommand[i] = NULL; //make sure each pointer is a null pointer
}
for (int i = 0; i < MAX_ARGS; i++) {
fgets(buffer, 100, stdin);
buffer[strcspn(buffer, "\n")] = 0; // Remove trailing newline
if (strcmp(buffer, ";") == 0) { // end of command, do not add to subcommand
break;
}
char *arg = calloc(strlen(buffer), sizeof(char) + 1); //allocate memory for argument of exact size
strncpy(arg, buffer, strlen(buffer)); //copy buffer into arg
subcommand[i] = arg; //add arg to subcommand
}
if (fork() == 0) { // Child process
execvp(subcommand[0], subcommand);
exit(0);
} else { // Parent process
wait(NULL);
free_exec(buffer, subcommand); //needs to be after wait to not use after free
}
}
int numPlaces(int n) {
if (n < 0) n = (n == INT_MIN) ? INT_MAX : -n;
if (n < 10) return 1;
if (n < 100) return 2;
if (n < 1000) return 3;
if (n < 10000) return 4;
if (n < 100000) return 5;
if (n < 1000000) return 6;
if (n < 10000000) return 7;
if (n < 100000000) return 8;
if (n < 1000000000) return 9;
return 10;
}
int pow2(int num) {
int ret = 2;
for (int i = 0; i < num - 1; i++) {
ret *= 2;
}
return ret;
}
void processtree() {
print_prompt("Please input a number:\n");
char buffer[100];
fgets(buffer, 100, stdin);
int number = atoi(buffer);
int power = pow2(number);
int string_length = numPlaces(power);
print_prompt("The value is 0\n");
for (int i = 1; i < power; i++) {
if (fork() == 0) { // Child process
char num[string_length];
sprintf(num, "The value is %d\n", i);
print_prompt(num);
exit(0);
} else {
wait(NULL);
}
}
}
int main() { int main() {
bool is_looping = true;
// Listen loop // Listen loop
while (is_looping) { while (true) {
print_prompt(); print_prompt("Please input a shell command:\n");
char input[100]; char input[100];
fgets(input, 100, stdin); // Input for commands fgets(input, 100, stdin); // Input for commands
if (strcmp(input, "stop\n") == 0) { // Stop command if (strcmp(input, "stop\n") == 0) { // Stop command
is_looping = false; break;
} }
if (strcmp(input, "onechild\n") == 0) { // One child command if (strcmp(input, "onechild\n") == 0) { // One child command
one_child(); one_child();
@ -92,23 +168,11 @@ int main() {
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
add_numbers(); add_numbers();
} }
if (strcmp(input, "exec\n") == 0) { if (strcmp(input, "exec\n") == 0) { // Exec command
char *args[2]; exec();
args[0] = "/bin/ls"; }
args[1] = NULL; if (strcmp(input, "processtree\n") == 0) { // Process tree command
execv(args[0], args); processtree();
} }
} }
}
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]);
} }