Week 1
Week 1
CS5008/9
Data Structures, Algorithms, and Their Applications Within Computer Systems
Week 1
Introduction and getting started
Slides reference
2
Plan for today
How to compile and run a C program
Very quick introduction to hardware
Very quick introduction to operating systems
C basics
3
A very simplified view of a computer system
Applications
Hardware Network
4
A very simplified view of a computer system
Applications
Mac OS or
Linux or Operating System + File System
Windows
Intel or
Hardware Network
Apple
5
A very simplified view of a computer system
Applications
Mac OS or
Linux or Operating System + File System
Windows
Intel or
Hardware Network
Apple
6
What computer to use?
You can use the Khoury Cloud servers which run Linux
• Much development in companies is done in the cloud on Linux
• You can use this class to learn now how to use cloud servers and github
repositories
• Employers like using cloud servers, because it allows them to provision and
control the development environment
7
If you use the Khoury Cloud servers
You will use your CIS/Khoury username and connect (via ssh) to
[email protected]
Once you have ssh’d in, you will be running on a Linux machine
8
Connecting to Khoury Cloud
Mac Users
9
Connecting to Khoury Cloud: Mac Users
Step 1: Open a terminal on your computer using
/Applications/Utilities/Terminal
Step 2: Type in the following line and then hit enter
ssh [yourusername]@login.khoury.northeastern.edu
10
Connecting to Khoury Cloud: Mac Users
• Step 3: Once you hit enter, you will be prompted to enter your
password. As you type your password, nothing will show up in the
terminal which is normal. Hit enter to fully login into the Linux
machine. Once you are fully logged in, you will see the following
screen.
12
Connecting to Khoury Cloud: Windows Users
Step 1: Download the PuTTY terminal emulator here:
https://www.putty.org
13
Connecting to Khoury Cloud: Windows Users
Step 2: Open PuTTY and navigate to configuration settings. Once you
get to the PuTTY configuration settings, enter the following:
• "login.khoury.northeastern.edu" into the Host Name text entry box
• 22 into the Port text entry box
• Check that the connection type is "SSH"
Your PuTTY configurations should look like the following:
14
Connecting to Khoury Cloud: Windows Users
Step 2: Open PuTTY and navigate to configuration settings. Once you
get to the PuTTY configuration settings, enter the following:
• "login.khoury.neu.edu" into the Host Name text entry box
• 22 into the Port text entry box
• Check that the connection type is "SSH"
Your PuTTY configurations should look like the following:
15
Connecting to Khoury Cloud: Windows Users
Step 3: Click open, and it should open a terminal like the following:
16
Connecting to Khoury Cloud: Windows Users
Step 4: Enter your CIS/Khoury username. Once you hit enter, you will
be prompted to enter your CIS/Khoury password. As you type your
password, nothing will show up in the terminal which is normal. Hit
enter to fully login into the Linux machine.
17
Connecting to Khoury Cloud: Windows Users
Once you are fully logged in, you will see a screen like this one.
All of the text editor examples I use are Emacs, but the CS5008 videos
are all done using VIM – I don’t care which text editor you use, but for
this course I strongly suggest you learn one of VIM or Emacs or Nano
Advantages of using the cloud server: gain experience with cloud, all
tool pre-configured for you
Disadvantages of using the cloud server: bandwidth, latency, resources
19
How to use editors
You can google how to use any of the text editors – there is a ton of tutorials
out there
Here is one starting point for each:
Emacs: https://www.linuxfordevices.com/tutorials/linux/emacs-editor-tutorial
Vim: https://www.tutorialspoint.com/vim/index.htm
Nano: https://www.tutorialspoint.com/how-to-use-nano-text-editor
20
What about Integrated Development
Environments (IDEs)?
If you are doing any serious software development, you will use an IDE
– it will help you eliminate silly bugs early and can also help you
integrate your code with code being developed by others.
But...for this class prefer you to work with the command line tools and
basic editors. None of the programs will be too complex and you
should have some grounding in low-level computer tools, in order to
really get an appreciation for what an IDE does for you.
Hopefully, you will be getting good IDE experience in your Java class
21
What about Integrated Development
Environments (IDEs)?
If you are doing any serious software development, you will use an IDE
– it will help you eliminate silly bugs early and can also help you
integrate your code with code being developed by others.
I will not enforce the ”no IDE”
But...for this class I prefer you to work with the command line tools and
recommendation
basic editors. – butwill
None of the programs trust me,complex
be too it will be
and you
should haveworth
some it grounding
for you in
tolow-level
use the computer tools,
basic stuff at in order to
really get an appreciation for what an IDE does for you.
least once in your new career.
Hopefully, you will be getting good IDE experience in your Java class
22
Github
23
Why Github?
Almost every CS-related job you will apply for will use Github or something like Github – learning enough to be
comfortable with Github may be one of the most important skills you will get out of this course – we will only use a
tiny bit of Github for delivering assignments and collecting your homework.
Used correctly, GitHub allows developers to collaborate and work on code for the same application while minimizing
issues with asynchronous work. More than that, open source projects are all managed on sites like Github – and many
employers check to see if you have contributed code to open source projects.
24
Getting started with Github
Go to github.com in your browser – if you already have an account then
login, if not sign up for a free account and then login
25
Getting started with Github
26
Getting started with Github
27
Getting started with Github – refresh browser
28
Getting started with Github
You will end up with a repository something like
https://github.com/Northeastern-CS5008-FA22-online/cs5008-fall-2022-bhailpern
(where the last word is YOUR user name and not “bhailpern”!)
Click on your repository URL and then edit the readme.md file in your
repository with your name and email and then commit the change by
hitting the “commit changes” button – this is part of your homework
29
Getting Started with Github
30
Getting Started with Github
31
Getting necessary tools
Khoury cloud, Mac laptop, Windows laptop
32
Getting necessary tools
Using the Khoury Cloud Servers
33
Khoury cloud servers
A big advantage of using the cloud servers is that they are already
provisioned with current versions of the C complier (gcc), git (the
GITHUB access software), and the text editors (emacs and vim)
34
Getting necessary tools
Using a Mac Laptop
35
If you are going to use a Mac laptop
There are two general approaches – both free:
36
Using a Mac - Xtools
37
Mac using Xcode
38
Mac using Xcode
39
Mac using Xcode
40
Using a Mac – Homebrew
Homebrew
41
Mac using Homebrew
Homebrew
Macs run a version of Unix (MacOS)
It does not include all the common Unix tools
Homebrew – is the “Missing Package Manager for macOS”
Instructions to install Homebrew are at https://brew.sh
42
Mac using Homebrew
43
Getting necessary tools
Using Windows 10
44
If you are going to use a Windows laptop
There are two general approaches – both free:
45
Using Windows
Native Tools
46
Windows
47
Windows
48
Windows
49
Windows
50
Windows
51
Windows
52
Windows
54
Windows 10 now allows Linux commands
WSL: https://docs.microsoft.com/en-us/windows/wsl/
Install WSL: https://docs.microsoft.com/en-us/windows/wsl/install
55
Getting started with WSL
To install WSL, you must be running Windows 10 or later. Upgrading
Windows is free, so if you are running Windows 9 or earlier please
upgrade first.
Go to https://docs.microsoft.com/en-us/windows/wsl/install
Follow the instructions to install WSL2.
56
Installing the tools
Tool package manager:
sudo apt update && sudo apt upgrade
Git
sudo apt install git
Install GCC
sudo apt install build-essential
57
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev
Install Emacs:
sudo apt-get install emacs
58
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev
Install Emacs:
sudo apt-get install emacs
Pick One
Vim – should be installed by default
59
Installing the tools
Install the MAN pages
sudo apt-get install manpages-dev
Install Emacs:
sudo apt-get install emacs
61
62
This is a text editor
(Emacs)
63
This is a text editor
(Emacs)
It knows enough
about C to help you
64
65
Library reference (input/output)
66
The main program –
the thing that actually “runs”.
67
Function to print out a string
68
Program is done
69
70
More details later in lecture
71
72
This is the
command shell
73
This is the
command shell
75
ls lists files
“hello*” is a pattern
76
“gcc” invokes a C
compiler
77
Compilers: gcc vs clang
The CS 5008 videos will show you can use either gcc or clang to “compile” your C
programs to run on the Khoury servers – and they suggest you choose one
In almost all cases the compiler commands we will use are identical for gcc and clang –
though in the videos you will see some minor differences between the two
78
Compilers: gcc vs clang
The CS 5008 videos will show you can use either gcc or clang to “compile” your C
programs to run on the Khoury servers – and they suggest you choose one
In almost all cases the compiler commands we will use are identical for gcc and clang –
though in the videos you will seeI will
some useminor differences
“gcc” instead between the two
of “clang”
79
“gcc” invokes the C if no “-o” then executable
compiler would be “a.out”
80
you now have 2 files –
the new one is
executable
81
Run the
program with
”./hello”
82
Run the
program with
”./hello” The output ♬ Ta Da ♬
83
Run the
program with
”./hello” The output ♬ Ta Da ♬
84
What does the C compiler do?
85
What does the C compiler do?
your
source
program
86
What does the C compiler do?
C program
with a lot
more
details
87
What does the C compiler do?
language
the
computer
knows
88
What does the C compiler do?
format the
computer
knows
(0,1)
89
What does the C compiler do?
program
with all
library
functions
90
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s
List Contents of Object File
cat helloworld.o (unreadable)
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
91
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s cat is the Unix command to print
(catalog) the contents of a file
List Contents of Object File
cat helloworld.o (unreadable) By default, Windows uses type
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
92
93
Assembly Code
94
Assembly Code
our string
95
Little exercise to see what compiler is doing
Generate assembly code
gcc -S helloworld.c
Investigate assembly
cat helloworld.s
Compile assembly to executable file
gcc helloworld.s -o hello
Generate Object file
gcc -c helloworld.s
List Contents of Object File
cat helloworld.o (unreadable)
Investigate Object File
objdump -d helloworld.o (disassembly)
objdump -t helloworld.o (symbol table)
96
Object File
byte
addresses
machine
instructions
equivalent
machine
assembler
instructions
code
equivalent
machine
assembler
instructions
code
The concept of libraries and how they get linked into your main
program
103
Quick introduction to hardware
Very, very, very quick!
104
105
Central Processing Unit (CPU) takes one instruction from
memory, decodes it, and executes it
PC = Program Counter
ALU = Arithmetic Logic Unit
106
Busses connect CPU to memory and other
peripheral devices
107
Data (and programs) are stored on Disk and
in Main Memory – and data is actually
moved in registers to be operated upon.
108
from: https://images.app.goo.gl/aguFzFGb2Tv8qdFE9
109
Quick introduction to operating
systems
110
What is an Operating System?
When you boot up a machine, you see your operating system booting up
Mac
Windows
Linux
111
What is an Operating System?
When you boot up a machine, you see your operating system booting up
Mac
Windows
112
Many Different Operating Systems (OSs)
Windows
Linux
BSD
113
What is an Operating System?
OS is software that sits between an application and
the hardware
OS is a resource manager and allocator
○ Decides between conflicting requests for
hardware
○ Attempts to be efficient and fair in sharing
OS is a control program
○ Controls execution of user programs
○ Prevents errors and improper use
114
What is an Operating System?
115
Most Common Operating System Families
POSIX
○ Anything Unix-ish
○ for example, Linux, BSDs, Mac OS, Android, iOS, QNX
Windows
○ Computers shipped by Microsoft
Many other operating systems may exist specific for a specific machine (like
the old mainframes) or for a domain (e.g., an operating system for a car or
handheld gaming device)
116
Some Unix Command Line Commands
ls
• lower case L/lower case S
• list files in the directory you are currently in
• what the Mac Finder/Windows File Explorer shows you automatically
cd
• connect to a directory
• what clicking on a folder in Finder/File Explorer does
pwd
• show me the current directory path (print working directory)
cat
• print out the contents of a file (concatenate and print files)
man
• show the online manual for a command: “man cd” shows the manual for the cd command
117
Some Command Line Commands (Unix)
cd ~
• navigate to home directory
cd .
• navigate to current directory
cd ..
• navigate to parent directory
118
C basics
For the history of C – see paper listed in your homework for this week
119
Returning to our helloworld program
120
Returning to our helloworld program
122
Variables
Variables represent a piece of memory - a “box” into which you can store and retrieve data
Every variable in C has a “type” – it denotes the kind of data stored and the size of that data
For example:
int x=5;
says that you are declaring a variable x to be an integer (one word of memory = 4 bytes)
and that it has been assigned an initial value of “5”
123
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage
124
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage
125
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage
126
Memory
In reality…
• Processor registers
• Processor caches
• Main memory When you declare a variable to
• Virtual memory be an “int” you are telling the
• “Hard disks” system to allocate 4 bytes of
• Network drives/Cloud storage space for it
Our programming model
• A linear array of memory locations containing bytes (8 bits)
• File systems
127
Memory
In reality…
• Processor registers
• Processor caches
• Main memory When you declare a variable to
• Virtual memory be an “int” you are telling the
• “Hard disks” system to allocate 4 bytes of
• Network drives/Cloud storage space for it
Our programming model
• A linear array of memory locations containing bytes (8 bits)
• File systems
128
A basic program with a variable
x is a variable
• x’s value is stuck into the %d placeholder
135
A basic program with a variable
136
A basic program with a variable
137
A basic program with a variable
138
What do we learn from that example?
How to declare a variable
Variables must be declared before being used
How to initialize a variable
How to assign a new value to a variable
How to print a string with a variable
Introducing a new scope {} lets you declare new variables, which will
hide variables of the same name from an outer scope – AVOID
DUPLICATING NAMES LIKE THIS – IT WILL CONFUSE HUMANS!
Variables “go away” when their scope closes
139
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program
For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
140
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program
For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
141
Functions
Functions represent sub-programs – convenient blocks of code that can be
easily described and/or reused in different places in your program
For example
int add (int x, int y) {return (x+y);}
declares a function add that takes two integer input arguments x, y and
returns an integer value representing the sum of the two inputs
142
A basic program with a function
145
comparisons:
== != < <= > >=
146
comparisons:
== != < <= > >=
147
for loop
Most programs do many repeated actions – and there are many ways
to tell C to repeat an action over and over again
The for loop is ideal for counting up (or counting down) a number of
steps
For example
for (x=0; x<10; x=x+1) {printf(“%d\n”,x);}
would iterate variable x from 0 to 9 with a step of 1 – each iteration
printing out the value of x on a new line
148
A basic program with if and for loop
For example
while (a < b) { a = a+1; b = b-1;}
checks the logical expression a<b – if it is true, it executes the body,
and retests the expression...but if false it just exits the loop
156
Memory
In reality…
• Processor registers
• Processor caches
• Main memory
• Virtual memory
• “Hard disks”
• Network drives/Cloud storage
157
Back to Variables
You can query the size of
a variable or a type with
sizeof()
159
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
160
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
161
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Address of c
and
address of i
162
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Tell C what
kind of
pointer &c
and &i is
163
C variables – Each variable has...
A name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
Tell C what
kind of
pointer &c
Telling/Forcing C to use a particular
and &i is
type for a variable is called a “cast”
165
C variables
Each variable has a name – for example, x
A type – like int or char
And a size – which can be found with sizeof(x)
And an address in memory – which is found with &x
166
C pointers
A “pointer” is another C data type
There are pointers for each type: pointer to int, pointer to char, etc.
• most importantly, pointers know about the size of the data item they are pointing to
167
Understanding pointers is one of KEY concepts to get out of this course
C pointers
A “pointer” is another C data type
There are pointers for each type: pointer to int, pointer to char, etc.
• most importantly, pointers know about the size of the data item they are pointing to
168
C pointers
169
C pointers
address of memory
value stored in
memory
170
171
“int*” tells C that &x is a
pointer to an integer
172
“void*” tells C that &y is a
pointer to something
173
To be safe, check if y is
NULL before accessing it
174
175
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the variable p points to
Example:
int x = 500;
int* y = &x;
printf (“%d %d\n”, x, *y); // prints 500 500
176
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the variable p points to
Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to
177
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value of the
*yvariable p points to y
Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to
178
Following a pointer (dereferencing)
Once you have a pointer, you will want the value that it points to
That is called “dereferencing” the pointer
• Remember ... another name for a pointer is a “reference”
Prefix pointer name with an asterisk to dereference it
If p is a pointer, then *p is the value
*yof the
x variable p points to &x y
Example:
int x = 500;
int* y = &x;
is the value
*yprints
printf (“%d %d\n”, x, *y); // 500 that
500 y points to
179
180
x: 5
181
x: 5
px:
182
x: 100
px:
183
184
Arrays
Our first C data structure
185
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0
186
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0
Arrays must be declared before being used, for example
int numbers[5];
represents 5 locations in memory, each of which holds an integer:
numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]
187
CS 5008 - Fall 2022 - Week 1 188
declare array of
5 integers
195
initialize the 10
elements of the
array
196
print array out
using a for loop
200
CS 5008 - Fall 2022 - Week 1 201
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0
202
C Arrays
Arrays are sequential locations in memory
Arrays are
Arrays aretyped and– homogenous
fixed size they cannot change during the program execution
Typing determines how much space is allocated for each element
So...we declare strings to hold the MAXIMUM size number of characters we
Arrays
mightare fixed in size when declared – no growing or shrinking
need
Access arrays by indexing – starting at 0
and C uses the ‘\0’ character to show where the real string actually ends
203
C Arrays
Arrays are sequential locations in memory
Arrays are typed and homogenous
Typing determines how much space is allocated for each element
Arrays are fixed in size when declared – no growing or shrinking
Access arrays by indexing – starting at 0
204
CS 5008 - Fall 2022 - Week 1 205
declare string
with max size
10 characters
211
Alternative syntax for initializing arrays
212
Another alternative syntax
213
Another alternative syntax
214
Another alternative syntax
215
Lets see address of characters in string
216
Lets see address of characters in string
217
Lets see address of characters in string
219
Lets see address of characters in string
sequential byte
addresses each
offset by 1
220
Summary
How to compile and run a C program
Quick introduction to hardware
Quick introduction to operating systems
C basics
221
Summary
How to compile and run a C program
Quick introduction to hardware
Quick introduction to operating systems
C basics
What to remember?
• The different parts of the C compiler/linker tool pipeline
• Hexadecimal notation/arithmetic
• Basic components of a computer (CPU, memory, registers, etc.)
• The C programming statements
• Pointers are addresses of variables in memory
222
OPTIONAL Homework videos
Mike Shah has a series of “master class” videos at
https://www.youtube.com/playlist?list=PLvv0ScY6vfd-GGT-aUH31X2yXgBSYXo6t
223