Change to functions and kill children
This commit is contained in:
parent
ca9bab7416
commit
4392a02488
@ -10,74 +10,89 @@ void print_prompt() {
|
||||
printf("[%d]> ", getpid());
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
bool is_looping = true;
|
||||
|
||||
// Listen loop
|
||||
while (is_looping) {
|
||||
char input[100];
|
||||
print_prompt();
|
||||
|
||||
char input[100];
|
||||
fgets(input, 100, stdin); // Input for commands
|
||||
if (strcmp(input, "stop\n") == 0) { // Stop command
|
||||
is_looping = false;
|
||||
}
|
||||
if (strcmp(input, "onechild\n") == 0) { // One child command
|
||||
if (fork() == 0) { // Child process
|
||||
print_prompt();
|
||||
printf("Hello, I am a child process\n");
|
||||
exit(0);
|
||||
} else { // Parent process
|
||||
print_prompt();
|
||||
printf("Hello, I am a parent process\n");
|
||||
wait(NULL);
|
||||
}
|
||||
one_child();
|
||||
}
|
||||
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
|
||||
int number = 1;
|
||||
int send_to_child[2];
|
||||
int receive_from_child[2];
|
||||
pipe(send_to_child);
|
||||
pipe(receive_from_child);
|
||||
|
||||
if (fork() == 0) {
|
||||
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 {
|
||||
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));
|
||||
}
|
||||
print_prompt();
|
||||
printf("The total is %d\n", total);
|
||||
}
|
||||
add_numbers();
|
||||
}
|
||||
if (strcmp(input, "exec\n")) {
|
||||
if (strcmp(input, "exec\n") == 0) {
|
||||
char *args[2];
|
||||
args[0] = "/bin/ls";
|
||||
args[1] = NULL;
|
||||
@ -96,4 +111,4 @@ void temp() {
|
||||
}
|
||||
//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