0

Introduction: I've been following K&R as my guide suggested. I came across the following code and I've been trying to figure out what's actually going on in the background.

Code:

// Program to count lines, words and characters in input

#include <stdio.h>

#define IN 1        // inside a word
#define OUT 0       // outside a word

int main()
{
    int c, nl, nw, nc, state;

    state = 0;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF)
           {
               ++nc;            // increment newchar count
               if (c == '\n')
                   ++nl;        // increment newline count
               if (c == ' ' || c == '\n' || c == '\t')
                   state = OUT;
               else if (state == OUT)
               {
                   state = IN;
                   ++nw;        // increment newword count
               }
           }

    printf("%d  %d  %d\n", nl, nw, nc);
}

Question: I understand that after typing in the input, Ctrl+D(Linux/Mac Based OS) is needed to insert the EOF character which finally breaks the code out of the while loop to print the output, but what I'm unable to figure out is that what's happening in the background while I'm typing the input. Is the input being processed simultaneously and just waiting for Ctrl+D (EOF character) to print the output, or is all the input processed after Ctrl+D is processed.

In the former way, the processing power should the balanced but the latter way puts the entire load on the processor at once, and that's the reason I want to know how it's working.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Harshit Jindal
  • 621
  • 8
  • 26
  • 1
    basically your question is how operation system works – Iłya Bursov Aug 02 '17 at 00:07
  • @IlyaBursov my question is how this particular code will be handled by the compiler, not how the operating system works – Harshit Jindal Aug 02 '17 at 00:08
  • compiler will generate calls to functions, provided by operating system, which can either send you symbol by symbol or have internall buffer and flush it to you when decided to do so, so yes, your question is how operating system works – Iłya Bursov Aug 02 '17 at 00:09
  • @IlyaBursov That did not answer my question. Also why downvote? What's wrong being curious about how OS works? Isn't it the right platform to ask about that topic? I'm new here, so I'd be glad to have help and suggestions – Harshit Jindal Aug 02 '17 at 00:12
  • https://stackoverflow.com/help/how-to-ask – Iłya Bursov Aug 02 '17 at 00:13
  • 1
    actually https://stackoverflow.com/help/dont-ask `Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much.` – Iłya Bursov Aug 02 '17 at 00:16
  • @IlyaBursov Okay I understand. That's fair – Harshit Jindal Aug 02 '17 at 00:17
  • 1
    @HarshitJindal: The short answer is, if you're typing at the terminal, then by default one chunk of input will be sent to your program every time you hit Enter. The "balance" isn't particularly relevant, since your loop will probably finish processing that input before your finger is off the Enter key again. You can confirm this by adding strategically placed output functions in your loop, so you can see when it's working. – Crowman Aug 02 '17 at 00:20
  • @PaulGriffiths That seems to answer my question. I'll try adding output functions to see how it works and get back here if I have any further query. Thanks – Harshit Jindal Aug 02 '17 at 00:23
  • 1
    You might find some useful information in [Canonical vs non-canonical terminal input](https://stackoverflow.com/questions/358342/canonical-vs-non-canonical-terminal-input). – Jonathan Leffler Aug 02 '17 at 04:53

1 Answers1

2

As the commenters pointed out this question is essentially asking how the operating system implements input and output because your getchar() call will likely end up as a system call (a function in kernel space).

A bird’s eye view of how an operating system might implement such a call, without buffering, is like this: the OS will put your thread in a waiting state and run other other things on the CPU. When a key is pressed, the driver will let the OS know which will decide where to route it to, in this case your program. It will wake the program up from its waiting state and return the key code from the syscall.

The magic here is the ability of processors to put an entire program on hold (while it waits), do something else, and restore it again later.

Real operating systems will implement forms of buffering, perhaps in the kernel but also in the standard library where getchar() is implemented on top of the syscall. There will be layers of abstraction and redirection. For example, in C standard input and output streams are represented as files.

(I’m no operating system expert so please correct me if I made any grave mistakes.)

Sijmen Mulder
  • 5,767
  • 3
  • 22
  • 33