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

Click For Summary
SUMMARY

The discussion focuses on debugging an infinite loop in a C++ program that utilizes fork() for string reversal. The primary issue arises from the lack of a base case in the recursive function print(), leading to an unbounded decrement of the index variable. Participants highlighted that the index variable does not have a defined stopping condition, causing the program to continuously fork new processes without terminating. The absence of proper error handling for the fork() function and the misuse of the exit() function further complicate the debugging process.

PREREQUISITES
  • Understanding of C++ programming language syntax and semantics
  • Familiarity with process management concepts, specifically fork() and process IDs
  • Knowledge of recursion and its base case requirements
  • Experience with debugging techniques in C++
NEXT STEPS
  • Learn about implementing base cases in recursive functions in C++
  • Research error handling for fork() in Unix-based systems
  • Explore debugging tools available in Visual Studio 2015 for C++ applications
  • Study process management and inter-process communication in C++
USEFUL FOR

C++ developers, software engineers, and anyone involved in debugging recursive functions and process management in C++ applications.

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:
Physics news on Phys.org
looks like index decrements without bound...
 
  • Like
Likes   Reactions: FactChecker
Dr Transport said:
looks like index decrements without bound...
Can you explain what you mean sorry?
 
index-- has no lower bound, in other words how does it know when to stop when it gets to 0? I can't help you trace it because I do not have <unistd.h>...i.e. it isn't a part of Visual Studio 2015...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K