Giter Club home page Giter Club logo

cs744-pa1-pa2-shell-threads-synchronization's People

Contributors

fenilgmehta avatar

Stargazers

 avatar

Watchers

 avatar  avatar

cs744-pa1-pa2-shell-threads-synchronization's Issues

When a file is mapped to `stdin` in the shell, it goes into infinite loop

File to see

Steps to reproduce

  1. Create a file called a.txt with content as follows
    1
    3
    2
    
  2. Compile the shell
  3. Execute
    # Technique 1 to see the infinite loop
    ./shell_fm <<< "executeCommands a.txt"
    
    # Technique 2 to see the infinite loop
    ./shell_fm < a.txt

Description

Mostly, the problem is in the myShellInput(...) function.

  1. Use int instead of char to store the value returned by getchar(). So, convert static char ch; to static int ch;. Read the docs of getchar() to know why, and also refer https://stackoverflow.com/a/7119500
  2. After debugging I found that getchar() does not return EOF even after reading the file completely. Instead, it starts reading the file again from the start. I did not understand why this is happening, and according to my understanding of getchar() docs it should return EOF on reaching the end of file.
    • One solution to this is to use read(STDIN_FILENO, &ch, 1) instead of getchar()
      bool myShellInput(char *userInput) {
          static char ch;
          static bool eofFlag;
      
          eofFlag = 0;
          char *userInputOld = userInput;
          if (read(STDIN_FILENO, &ch, 1) <= 0) return false;
      
          while (ch != '\n') {
              if (eofFlag == 1) {
                  *userInput = '\0';
                  printflush(stderr, "DEBUG: finished myShellInput(...)\n");
                  return userInput != userInputOld;  // this returns false if not input read
              }
              *userInput = ch;
              ++userInput;
              if (read(STDIN_FILENO, &ch, 1) <= 0) {
                  eofFlag = 1;
              }
          }
      
          *userInput = '\0';
          return true;
      }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.