YOU ARE VIEWING DOCUMENTATION FOR ioL v.1. Click here for documentation for older v.0.9x versions of ioL.
ioL

Programming manual (doc.iol.science)



Running your programs

Before we bring ioL into the picture, let's start out with a simple C program written in the conventional way.

#include <stdio.h>

int main(int argc, char* argv[]) {
    printf("Hello world!\n");
    return 0;
}

Let's save our C code file as hello-plain.c . Next we need to compile it into an executable program. We'll use the gcc compiler which is available in many Linux environments, and compile it from the command line.

$ gcc hello-plain.c -o hello-plain
$ 

This gives us an executable file named hello-plain which we can now run.

It is the nature of C programs (unlike certain other languages) that programs have to be compiled into native machine code executable files before you can actually run them.

Now let's run our program in our TTY terminal window and look at the output: (If you're usng a different language other than C, the command to run your program will look different.)

$ ./hello-plain
Hello world!
$ 

A fairly straightforward example, but what is actually going on here?

Standard io

We've taken advantage of a very fundamental feature of the operating system, called standard io  ('standard input and output'), to transmit output from our program to our TTY terminal window. When the program starts, the operating system opens up a stream for input and another stream for output. It connects these to our TTY terminal window, which waits for characters to arrive on the stream, and then displays those characters on the screen exactly as the program printed them. If the user types characters into the TTY window, the characters typed are treated as input to be sent back to the program. The program can read these characters as 'standard input'.

Standard
output from the program goes directly to the TTY termial, and standard input
flows the opposite way.
Standard io is the simplest and oldest way for computer programs to communicate. It has been around since the dawn of computing, and actually evolved out of early telegraph systems.
During the early 20th century, post offices around the world used TTY machines (big ugly electric typewriters that would print characters onto rolls of paper) to transmit text messages around the world via a network of telegraph lines. Early computers were plugged in to TTY machines to allow humans to interact with them, and that's where the idea of a terminal came from.

The TTY terminal is old-fashined in many ways. It can only display characters in the order they are printed from the program, and can't interpret program output further than that. You can't display images, for example.

Fortunately, standard io is also very flexible. Just because most operating systems are designed by default to hook up your program's input and output with an old-school TTY interface, doesn't mean your program can only interact with users in an old-fashioned way.

Capturing standard io

With ioL, we can capture your program's standard io to allow you to be a bit more creative with what you can do with your program's output.

Let's write a more interesting version of the program above. We'll name this one hello-markup.c .

#include <stdio.h>
#include <unistd.h> //this lets us use the sleep() function.

int main(int argc, char* argv[]) {
    printf("<span size=48,{pt} backgroundColor={blue} italic=true {Hello world!}>\n");
    sleep(5);  //keep the program running for 5 sec so it doesn't terminate immediately.
    return 0;
}

Let's compile and then run our program, as before. We're printing some unusual stuff to our standard output. What do you think will happen this time?

$ gcc hello-markup.c -o hello-markup
$ ./hello-markup
<span size=50,{pt} backgroundColor={blue} italic=true {Hello world!}> 
Wow, that was so boring! We just got exactly what we printed. What a waste of time!

Just a moment. Clearly this output wasn't intented to be shown directly to the user. Maybe we're not running our program in the right way?

For analogy, think of your web browser. All websites are written in HTML code, but your web browser doesn't show you the HTML code on the screen as it arrives from the web server — instead you see a nice web page on your screen, because your web browser takes the HTML code and interprets it first.

In order to run this program in the correct environment, we'll need to get ioL to capture the program's standard io streams.

By capturing
the io streams, they will no longer flow to the TTY termial, but flow to the
input output layer instead.

Let's run it in a different way. (We don't need to repeat the gcc command again since our program has already been compiled.)

$ iol -- ./hello-markup
ioL: input/output Layer
Copyright © General Development Systems
_______________________________________________

This time, a second window also appears on the screen. This is our ioL console:

Hello world!

By putting iol --  in front of our command, we specify that the program's input and output should be handled through an ioL environment instead of the usual plain-text TTY environment. ioL tells the operating system that it will play the role of the terminal for the task of running this program.

In practice the -- argument isn't always needed. You only need it in cases where your program takes in extra arguments of its own. In such cases, -- tells ioL that everything that follows is related to running your program, not ioL itself.

Review

  1. Why did we need to put the sleep(5); statement into our program? What happens to the program window after 5 seconds when the program ends? Try running the code above through the ioL console by following the example above, then try deleting the sleep statement from the code. Recompile and re-run the program through the iol environment. What's different this time? Did you expect this? Why?
  2. In the above example, we printed 'Hello world!' in large italic text. Can you figure out how to make the text bold as well?
  3. Try messing up the program's output by adding in some extra { and } characters in the quoted markup string inside your printf("..."); statement. What happens?
  4. This program is meant to be run through ioL, since the output isn't intended to be read by the user directly. Can you think of a situation where you might want to run an ioL program without ioL?
  5. Something extra...

  6. Try rewriting the same program in a different programming language instead of C. For example, if you are familiar with Python, write a Python program to print the same output string as the example above, and then sleep for 5 seconds as previously. Run the program in the terminal using the appropriate command (for a python program named hello-markup.py, you would run it by typing python3 hello-markup.py, depending on your installation), and then figure out how to run the program within the ioL environment like we did with our C program above.
    • Note: depending on how your Python or other language environment is set up, you might need to "flush" your program's output before putting your program to sleep to ensure the output is printed to ioL before your program sleeps and not after.

    • Is there any difference in what you see on the screen?
    • Does ioL know or care whether your program is implemented in C, Python, Java, or another language? What does ioL care about?