Finish assignment 2
This commit is contained in:
parent
4392a02488
commit
50289cd303
@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(Assignment2 C)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
|
@ -1,21 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <wait.h>
|
||||
#include <limits.h>
|
||||
|
||||
void print_prompt() {
|
||||
printf("[%d]> ", getpid());
|
||||
#define MAX_ARGS 6 // 5 arguments and a null pointer
|
||||
|
||||
void print_prompt(char *prompt) {
|
||||
printf("(%d) %s", getpid(), prompt);
|
||||
}
|
||||
|
||||
void one_child() {
|
||||
print_prompt();
|
||||
printf("Hello, I am a parent process\n");
|
||||
print_prompt("Hello, I am a parent process\n");
|
||||
if (fork() == 0) { // Child process
|
||||
print_prompt();
|
||||
printf("Hello, I am a child process\n");
|
||||
print_prompt("Hello, I am a child process\n");
|
||||
exit(0);
|
||||
} else { // Parent process
|
||||
wait(NULL);
|
||||
@ -37,8 +37,8 @@ void add_numbers() {
|
||||
// Add the number to the subtotal
|
||||
subtotal += number;
|
||||
|
||||
print_prompt();
|
||||
printf("The subtotal is %d\n", subtotal);
|
||||
print_prompt("The subtotal is ");
|
||||
printf("%d\n", subtotal);
|
||||
|
||||
// Write the sum to the receive_from_child pipe
|
||||
write(receive_from_child[1], &subtotal, sizeof(int));
|
||||
@ -47,16 +47,15 @@ void add_numbers() {
|
||||
} else { // Parent process
|
||||
int total = 0;
|
||||
while (number != 0) {
|
||||
print_prompt();
|
||||
printf("Please input a number, 0 to terminate: ");
|
||||
print_prompt("Please input a number, 0 to terminate:\n");
|
||||
// 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);
|
||||
print_prompt("You entered: ");
|
||||
printf("%d\n", number);
|
||||
|
||||
// Write to child process
|
||||
write(send_to_child[1], &number, sizeof(int));
|
||||
@ -65,8 +64,8 @@ void add_numbers() {
|
||||
read(receive_from_child[0], &total, sizeof(int));
|
||||
}
|
||||
wait(NULL);
|
||||
print_prompt();
|
||||
printf("The total is %d\n", total);
|
||||
print_prompt("The total is ");
|
||||
printf("%d\n", total);
|
||||
}
|
||||
close(send_to_child[0]);
|
||||
close(send_to_child[1]);
|
||||
@ -74,17 +73,94 @@ void add_numbers() {
|
||||
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() {
|
||||
bool is_looping = true;
|
||||
|
||||
// Listen loop
|
||||
while (is_looping) {
|
||||
print_prompt();
|
||||
|
||||
while (true) {
|
||||
print_prompt("Please input a shell command:\n");
|
||||
char input[100];
|
||||
fgets(input, 100, stdin); // Input for commands
|
||||
|
||||
if (strcmp(input, "stop\n") == 0) { // Stop command
|
||||
is_looping = false;
|
||||
break;
|
||||
}
|
||||
if (strcmp(input, "onechild\n") == 0) { // One child command
|
||||
one_child();
|
||||
@ -92,23 +168,11 @@ int main() {
|
||||
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
|
||||
add_numbers();
|
||||
}
|
||||
if (strcmp(input, "exec\n") == 0) {
|
||||
char *args[2];
|
||||
args[0] = "/bin/ls";
|
||||
args[1] = NULL;
|
||||
execv(args[0], args);
|
||||
if (strcmp(input, "exec\n") == 0) { // Exec command
|
||||
exec();
|
||||
}
|
||||
if (strcmp(input, "processtree\n") == 0) { // Process tree command
|
||||
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]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user