I'm trying to execute printf(">>");
only once in the parent process. Meaning that once I call fork() and a child process is born, the program (once it starts executing code from the top again), should skip printf(">>");
Note: I'm trying to implement something very similar to the Python Shell. Notice how when you enter print("example")
in the Python Shell, it print "example" and not ">>>example"
I apologize for the low readability, I really tried my best.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
char * selected = malloc(256);
char line[255];
pid_t pid = getpid();
while (1){
printf("(In parent) PID: %d, PPID: %d\n", getpid(), getppid());
// if (ps is parent && line is NOT executed later on by fork){
printf(">>");
//}
fgets(line, sizeof(line), stdin);
line[strlen(line) - 1] = '\0'; // removes \n
if (strcmp(line, "fork") == 0){
pid = fork();
if (pid == 0){
printf("(In child) PID: %d, PPID: %d\n", getpid(), getppid());
}
}
}
return 0;
}