Change to functions and kill children
This commit is contained in:
parent
ca9bab7416
commit
4392a02488
@ -10,35 +10,26 @@ void print_prompt() {
|
|||||||
printf("[%d]> ", getpid());
|
printf("[%d]> ", getpid());
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
void one_child() {
|
||||||
bool is_looping = true;
|
|
||||||
// Listen loop
|
|
||||||
while (is_looping) {
|
|
||||||
char input[100];
|
|
||||||
print_prompt();
|
print_prompt();
|
||||||
fgets(input, 100, stdin); // Input for commands
|
printf("Hello, I am a parent process\n");
|
||||||
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
|
if (fork() == 0) { // Child process
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("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
|
||||||
print_prompt();
|
|
||||||
printf("Hello, I am a parent process\n");
|
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
|
|
||||||
|
void add_numbers() {
|
||||||
int number = 1;
|
int number = 1;
|
||||||
int send_to_child[2];
|
int send_to_child[2];
|
||||||
int receive_from_child[2];
|
int receive_from_child[2];
|
||||||
pipe(send_to_child);
|
pipe(send_to_child);
|
||||||
pipe(receive_from_child);
|
pipe(receive_from_child);
|
||||||
|
|
||||||
if (fork() == 0) {
|
if (fork() == 0) { // Child process
|
||||||
int subtotal = 0;
|
int subtotal = 0;
|
||||||
while (number != 0) {
|
while (number != 0) {
|
||||||
// Read the numbers from the send_to_child pipe
|
// Read the numbers from the send_to_child pipe
|
||||||
@ -53,7 +44,7 @@ int main() {
|
|||||||
write(receive_from_child[1], &subtotal, sizeof(int));
|
write(receive_from_child[1], &subtotal, sizeof(int));
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
} else {
|
} else { // Parent process
|
||||||
int total = 0;
|
int total = 0;
|
||||||
while (number != 0) {
|
while (number != 0) {
|
||||||
print_prompt();
|
print_prompt();
|
||||||
@ -73,11 +64,35 @@ int main() {
|
|||||||
// Read the total from the child process
|
// Read the total from the child process
|
||||||
read(receive_from_child[0], &total, sizeof(int));
|
read(receive_from_child[0], &total, sizeof(int));
|
||||||
}
|
}
|
||||||
|
wait(NULL);
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("The total is %d\n", total);
|
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]);
|
||||||
}
|
}
|
||||||
if (strcmp(input, "exec\n")) {
|
|
||||||
|
int main() {
|
||||||
|
bool is_looping = true;
|
||||||
|
|
||||||
|
// Listen loop
|
||||||
|
while (is_looping) {
|
||||||
|
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
|
||||||
|
one_child();
|
||||||
|
}
|
||||||
|
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
|
||||||
|
add_numbers();
|
||||||
|
}
|
||||||
|
if (strcmp(input, "exec\n") == 0) {
|
||||||
char *args[2];
|
char *args[2];
|
||||||
args[0] = "/bin/ls";
|
args[0] = "/bin/ls";
|
||||||
args[1] = NULL;
|
args[1] = NULL;
|
||||||
@ -96,4 +111,4 @@ void temp() {
|
|||||||
}
|
}
|
||||||
//split buffer on spaces into input array
|
//split buffer on spaces into input array
|
||||||
sscanf(buffer, "%s %s %s %s %s", input[0], input[1], input[2], input[3], input[4]);
|
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