You are on page 1of 6

University of Technology, Jamaica Operating Systems

School of Computing & Information Technology CIT3002

Linux Terminal: Compilers


1 Command Line Interface
A command line interface (CLI) is a text-based user interface (UI) used to view and manage computer
files. Prior to the mouse users utilized the keyboard to type commands in the CLI to run tasks on a
computer. The output from the system is usually a message, table, list, or some other confirmation
from the system/application used.

A shell (a.k.a. command language interpreter) is the software that handles the CLI. Two commonly
used shells are

 Powershell for Windows


 Bash (Bourne Again Shell) for Linux and macOS

Shells are the outermost layer of the operating system (OS) and are often separated from the
underlying OS kernel. A shell operates like an application and can be replaced. Because the shell is
only one layer above the OS, users can perform operations that are not available in other interface
types. However, shell users are required to know the syntax of a scripting language. Most command
line shells save sequences of commands for reuse in a script, which is the foundation of basic
systems management automation.

Pg. 1 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002

2 Bash Commands: which, type, command -v


If you want to determine whether a command exists you can utilize , , or .
However, there is a difference.

Used to identify whether a command exist based on the location of executable files within
the PATH environment.
PATH is an environmental variable that tells the shell and other programs where to search
for executable file. To view content of PATH variable use:

Used to display information about a command. It will also specify whether


a command is either as an alias, keyword, functions, built-in, file, or an
empty string for unknown types.
Note: the alias command allows you to set a pseudonym for a command.
Example: make temp the pseudonym of test

3 Compilers in Linux
One of the advantages of using Linux is that you can compile and execute your own program from
the terminal. We will now examine how Linux works with program from C, C++, Java, and python.

3.1 GNU Compiler Collection


In Linux, the GCC stands for GNU Compiler Collection. It is a compiler system for the various
programming languages. It is mainly used to compile the C and C++ programs. It takes the name of
the source program as a necessary argument; rest arguments are optional (see table 3-1) such as
debugging, warning, object file, and linking libraries.

Before compiling you need to check to see if either of these compilers are present, using commands
from section 2 above. Bellow are some other examples of verifying if installed.

Pg. 2 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002

Table 3-1: GCC command line options

Compilers are not usually installed, therefore follow these steps in Ubuntu to utilize gcc or g++

STEP 1: Install GNU c/c++ compiler

Enter the following two commands


$ sudo apt-get update
$ sudo apt-get install build-essential

STEP 2: Verify Installation

I will you which, but you can use any other command you are comfortable with.
Example of results:

3.1.1 C programs in Linux


#include <stdio.h>
int main (void)
{
printf("Hello World\n");
}
← hello.c

You can use any of the following syntax below to compile c program, both will create a
default executable a.out

To run a.out, enter this at prompt:

Pg. 3 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
Preceding can be accomplished another way by compiling (pre-processing, compiling, and
assembling) and linking processes in a single step by using the -g to produce debugging
information and -o option to create an output file with a different name.

To run the output file hi, enter this at prompt:

You can also use the make command instead of cc or gcc to compile a C program

Above creates an output file called hello, but a file called hello.c must exist

3.1.2 C++ programs in Linux


#include <iostream>
using namespace std;

int main (void)


{
cout << "Hello World\n";
return 0;
}
← hello.cpp

NOTE: g++ can also be used to compile C program

3.2 Java programs in Linux


public class hello
{
public static void main(String[] args)
{ System.out. println( "Hello World"); }
}
← hello.java

The primary Java compiler from Oracle Corporation is called javac. To check if already
installed use:

You may even notice that it is not installed, if you get the following error:

Install using one of sudo above; once installed you can compile.
javac will generate a hello.class file and that is the file you run using

Pg. 4 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002

3.3 Python programs in Linux


To check if already installed use:
If not installed, you get the following error:

Install Python using one of the sudo above.

There is no compilation required for python. If you have a file called hello.py with the contents
below, just use to execute.

print( "Hello World")


← hello.py

4 Activity
1. Modify fifo.c below to accept arrival time for process

#include<stdio.h>

int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)

Pg. 5 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002

{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}

2. Utilize the gcc to run other CPU scheduling algorithms.

5 References
Gillis, A. S. (2021, August 18). command line interface (CLI). Retrieved from TechTarget:
https://searchwindowsserver.techtarget.com/definition/command-line-interface-CLI

Musso, C. (2004). Retrieved from Development with GNU/Linux: http://labor-liber.org/en/gnu-


linux/development/

Developed by: Khalilah Burrell-Battick (2021)

Pg. 6 of 6

You might also like