Comment some code
This commit is contained in:
parent
11d9b2e41e
commit
d119dc459f
@ -16,23 +16,22 @@ int main() {
|
|||||||
while (is_looping) {
|
while (is_looping) {
|
||||||
char input[100];
|
char input[100];
|
||||||
print_prompt();
|
print_prompt();
|
||||||
fgets(input, 100, stdin);
|
fgets(input, 100, stdin); // Input for commands
|
||||||
if (strcmp(input, "stop\n") == 0) {
|
if (strcmp(input, "stop\n") == 0) { // Stop command
|
||||||
is_looping = false;
|
is_looping = false;
|
||||||
}
|
}
|
||||||
if (strcmp(input, "onechild\n") == 0) {
|
if (strcmp(input, "onechild\n") == 0) { // One child command
|
||||||
if(fork() == 0) {
|
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 {
|
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("Hello, I am a parent process\n");
|
printf("Hello, I am a parent process\n");
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (strcmp(input, "addnumbers\n") == 0) {
|
if (strcmp(input, "addnumbers\n") == 0) { // Add numbers command
|
||||||
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];
|
||||||
@ -44,26 +43,34 @@ int main() {
|
|||||||
while (number != 0) {
|
while (number != 0) {
|
||||||
// Read the numbers from the send_to_child pipe
|
// Read the numbers from the send_to_child pipe
|
||||||
read(send_to_child[0], &number, sizeof(int));
|
read(send_to_child[0], &number, sizeof(int));
|
||||||
|
// Add the number to the subtotal
|
||||||
subtotal += number;
|
subtotal += number;
|
||||||
|
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("The subtotal is %d\n", subtotal);
|
printf("The subtotal is %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));
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
while (number != 0) {
|
while (number != 0) {
|
||||||
|
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("Please input a number, 0 to terminate: ");
|
printf("Please input a number, 0 to terminate: ");
|
||||||
|
// 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
|
||||||
print_prompt();
|
print_prompt();
|
||||||
printf("You entered %d\n", number);
|
printf("You entered %d\n", number);
|
||||||
|
|
||||||
|
// Write to child process
|
||||||
write(send_to_child[1], &number, sizeof(int));
|
write(send_to_child[1], &number, sizeof(int));
|
||||||
|
|
||||||
|
// Read the total from the child process
|
||||||
read(receive_from_child[0], &total, sizeof(int));
|
read(receive_from_child[0], &total, sizeof(int));
|
||||||
}
|
}
|
||||||
print_prompt();
|
print_prompt();
|
||||||
|
Loading…
Reference in New Issue
Block a user