Add addnumbers command, needs work
This commit is contained in:
parent
d7cc2be178
commit
9f70a9a66c
@ -3,23 +3,77 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <wait.h>
|
||||||
|
|
||||||
|
void print_prompt() {
|
||||||
|
printf("[%d]> ", getpid());
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
bool is_looping = true;
|
bool is_looping = true;
|
||||||
// Listen loop
|
// Listen loop
|
||||||
while (is_looping) {
|
while (is_looping) {
|
||||||
|
char input[100];
|
||||||
|
print_prompt();
|
||||||
|
fgets(input, 100, stdin);
|
||||||
|
if (strcmp(input, "stop\n") == 0) {
|
||||||
|
is_looping = false;
|
||||||
|
}
|
||||||
|
if (strcmp(input, "onechild\n") == 0) {
|
||||||
|
if(fork() == 0) {
|
||||||
|
print_prompt();
|
||||||
|
printf("Hello, I am a child process\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print_prompt();
|
||||||
|
printf("Hello, I am a parent process\n");
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (strcmp(input, "addnumbers\n") == 0) {
|
||||||
|
int number = 1;
|
||||||
|
int send_to_child[2];
|
||||||
|
int receive_from_child[2];
|
||||||
|
pipe(send_to_child);
|
||||||
|
pipe(receive_from_child);
|
||||||
|
|
||||||
|
if (fork() == 0) {
|
||||||
|
while (number != 0) {
|
||||||
|
// Read the numbers from the send_to_child pipe
|
||||||
|
read(send_to_child[0], &number, sizeof(int));
|
||||||
|
print_prompt();
|
||||||
|
printf("The sum of the numbers is %d\n", number);
|
||||||
|
// Write the sum to the receive_from_child pipe
|
||||||
|
write(receive_from_child[1], &number, sizeof(int));
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
while (number != 0) {
|
||||||
|
|
||||||
|
print_prompt();
|
||||||
|
printf("Please input a number, 0 to terminate: ");
|
||||||
|
char buffer[100];
|
||||||
|
fgets(buffer, 100, stdin);
|
||||||
|
number = atoi(buffer);
|
||||||
|
write(send_to_child[1], &number, sizeof(int));
|
||||||
|
read(receive_from_child[0], &number, sizeof(int));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void temp() {
|
||||||
char input[5][100];
|
char input[5][100];
|
||||||
char buffer[500];
|
char buffer[500];
|
||||||
//Print prompt
|
//Print prompt
|
||||||
printf("[%d]> ", getpid());
|
print_prompt();
|
||||||
if (fgets(buffer, 500, stdin) == NULL) {
|
if (fgets(buffer, 500, stdin) == NULL) {
|
||||||
break;
|
exit(0);
|
||||||
}
|
}
|
||||||
//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]);
|
||||||
|
};
|
||||||
if (strcmp(input[0], "stop") == 0) {
|
|
||||||
is_looping = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user