How to debug an infinite loop in a C++ program using fork gymnastics?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
carl123
Messages
55
Reaction score
0
7.JPG

note.JPG


This is what I did but I'm getting an infinite loop of the reversed string whenever I run the program. I don't know why

C:
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* for fork() and getpid() */
#include <sys/types.h>
#include <iostream>
using namespace std;
string result;
void print(string s, int index){
   pid_t pid;
   pid = fork();
   if(pid == 0){
       //child
       count<<s[index];
       index--;
       print(s,index);
       exit(0);
   }
   else if (pid < 0){
       count<<s[index];
       index--;
       print(s,index);
       exit(0);
   }
   else{
       count<<s[index];
       index--;
       exit(0);
   }
}
int main(){
   string s;
   int length;
   count<<"Enter string to invert\n";
   cin>>s;
   count<<"Reverse String: \n";
   print(s,s.size()-1);
}
 
Last edited by a moderator:
on Phys.org
Dr Transport said:
looks like index decrements without bound...
Can you explain what you mean sorry?