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

Click For Summary

Discussion Overview

The discussion focuses on debugging an infinite loop in a C++ program that attempts to reverse a string using process forking. Participants explore the behavior of the program and the implications of the index variable used in the recursive function.

Discussion Character

  • Technical explanation, Debugging, Debate/contested

Main Points Raised

  • One participant describes the issue of an infinite loop occurring when the program runs, specifically mentioning that the reversed string is printed repeatedly.
  • Another participant suggests that the index variable decrements without bound, implying that there is no condition to stop the recursion once the index reaches zero.
  • A further response seeks clarification on the previous comment about the index, indicating a lack of understanding of the issue being raised.
  • Another participant reiterates the concern about the index decrementing without a lower bound and notes their inability to help trace the issue due to missing the necessary header file for process forking in their development environment.

Areas of Agreement / Disagreement

Participants generally agree that the index variable's behavior is problematic, but the discussion remains unresolved regarding the specific implications and potential solutions to the infinite loop.

Contextual Notes

There are limitations in the discussion regarding the assumptions about the index variable's bounds and the specific environment constraints faced by some participants, which may affect their ability to test the code.

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
3K
  • · 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