1/363
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
gdb - steps through program one step at a time
n (next)
A product that can be used to determine and find memory leaks
Valgrind
Create and maintain static libraries
ar (archive)
A single copy of a library loaded into memory
Shared libraries
First call to open returns
3
File descriptor
fd
Function that returns a different value every second
time()
Libraries in Linux are known as archives. What is the typical file extension?
.a
Create directory named 'cs3336' (located back one directory in myClass)
mkdir ../myClasses/cs3336
Print all files named fooxx (f case insensitive, xx - two base 10 digits) to printer ecs312
lpr -P ecs312 [Ff]oo[0-9][0-9]
Find the max value using a bash shell script
#!/bin/bash
maxValue() {
max=$1
shift
for val in "$@"
do
if [[ $val -gt $max ]]; then
max=$val
fi
done
echo $max
}
result=$(maxValue 15 18 32 4 98 1)
echo "Max is: $result"
Write a makefile for project (myprog)
- Must have ability to clean, use at least 1 macro, force gnu89 standard on all compiles
CC = gcc -std=gnu89
OBJ = main.o a.o b.o
myprog: $(OBJ)
$(CC) -o myprog $(OBJ)
main.o: main.c a.h b.h
$(CC) -c main.c
a.o: a.c a.h b.h
$(CC) -c a.c
b.o: b.c b.h
$(CC) -c b.c
clean:
rm -f myprog $(OBJ)
What is a system call?
Fundamental interface between an application and the Linux kernel.
How is a system call similar/different from other functions?
Other functions (wrapper functions) invoke system calls. System calls perform the same functions as library functions, but at a different layer of abstraction.
When would you choose to use a system call versus another function?
When working with file descriptors and pipes, system calls must be used to read and write.
Using only system calls for I/O, write readADigit()
- reads individual digit, give int back to calling function if it exists
#include
int readADigit(int fd) {
char ch;
int result = -1;
if (read(fd, &ch, 1) == 1) {
if (ch >= '0' && ch <= '9') {
result = ch - '0';
}
}
return result;
}
Code fragment that uses getopt() to count number of v options given on command land and prints count
-v option: get increasingly verbose diagnostic output
#include
#include
int main(int argc, char *argv[]) {
int opt;
int vcount = 0;
while ((opt = getopt(argc, argv, "v")) != -1) {
if (opt == 'v') {
vcount++;
}
}
printf("Number of -v options: %d\n", vcount);
return 0;
}
Calls the command pwd and redirect its stderr to a file named errLog
pwd 2> errLog
Variable expansion but not file name expansion
" "
The (1) symbol will be expanded as the path to your current directory.
.
Old internet standard protocol for a remote terminal connection
Telnet (23)
Secure alternate protocol
ssh (22), encrypts communication between server and client
Print the number of lines that the string "Bear" is found in the file pressRelease
grep -c "Bear" pressRelease
Replace all occurrences of "Dwayne Johnson" in a file named pressRelease with "The Rock"
sed 's/Dwayne Johnson/The Rock/g' pressRelease
Vim: moves cursor to beginning of file
gg
Vim: get editor to save copy of current file
:w
Vim: get editor to move cursor to end of current line
$
Regex that matches: catinthehat.txt
grep -E "catinthehat.txt" file_name
Regex that matches bears (first letter case insensitive)
grep -E "[Bb]ears" file_name
Shell Expansions: "file" followed by any single char followed by ".txt"
file?.txt
Shell Expansions: all files located in user SMITHB's home directory ending in ".sh"
~SMITHB/*.sh
Shell Expansions: files named "foo" with the first character case insensitive
[Ff]oo
Write to a stream (overwrite)
>
Write to a stream (append)
>>
Developers of C
Denis Ritchie, Brian Kernighan
KISS Principle
Keep it Small and Simple
Good for quick and simple development
shell
C shell
csh
Bash spawns new shell, executes given command, then returns result. (T/F)
True
Bash built-ins run on the original bash, and as a result, is faster and more efficient. (T/F)
True
Do not suppress variable expansion. Suppress shell expansion.
" "
Suppress everything
' '
``Command substitution
- Identical to $(_)
``
Standard in
0
Standard out
1
Standard error
2
Universal trash can in Linux
/dev/null
kills precess specified by pid
kill
grabs all processes related to a specified user
ps -ef | grep user_name
returns first 10 lines
head
returns first num lines
head -num
returns last 10 lines
tail
returns last num lines
tail -num
takes stdout for the first process and uses it as stdin for another process
pipe
allows every instance of the shell to have access to the variable
export
In Bash, 0 is considered true (T/F)
True
global regular expression print
grep
special command (substitution in ed)
- manipulates strings, but by default, does not modify files
sed
Remove all instances of 'TX', replace with Texas
sed 's/TX/Texas/g' file_name
change in place
sed: i
character class
[...] (file name expansion)
range of a number of occurrences (at least n, at most m)
{n, m}
exactly n occurrences
{n}
at least n occurences
{n, }
at most m occurrences
{ ,m}
or
|
escape
\
Shebang line
#!/usr/bin/bash
- indicates what interprets the file
Read, write, executable permissions
- 3 sets of octal numbers
- chmod
-rwxrwxrwx
You must give file executable permissions OR (1)
1. explicitly give it bash when executing
ex. $bash ./hw.sh
Substrings in bash
echo ${foo: starting point: num of char}
Use $(( )) for simple arithmetic and let for unary operations. (T/F)
True
Test
[ ]
[[ ]]
String comparisons
= equivalence
!= not equal
-n not null
-z is null
Number comparison
-eq
-ne
-lt
-gt
-le
-ge
File tests
-d: is a directory
-e: does it exist
-f: regular file
if
if [[ test ]]
then
# stuff
fi
if-elif-else
if [[ test ]]
then
# stuff
elif [[ test ]]
then
# stuff
else
#stuff
fi
case
case val in
pattern1)
# stuff
;;
pattern2)
# stuff
;;
pattern3)
# stuff
;;
esac
for
for var in $set
do
# stuff
done
while
while condition
do
body
done
shifts all items in command line/positional parameters
- allows for reuse of $1
shift
until
until condition
do
body
done
shell suppression
../
Functions in bash
function()
{
body
}
Returning from a function (bash)
1. use of return command (returns an integer, stored in $?)
2. returning as we know it (using echo)
C is a (1) of C++
1. subset
Compile in gnu89 standard
gcc -std=gnu89 file_name -o executable_name
Analogous to new and delete
malloc and free
variables which hold addresses
pointers
printf
%i - integer
%d - decimal
%c - character
%s - string
%f - float
%g - double
%lf - long float
%5d - set width of 5, double, right justified
%-5d - set width of 5, double, left justified
%05d - zeroes leading number
%8.2f - 8 columns, 2 decimals
Interfacing directly with OS via System Call
1. Process control
2. File management
3. Device management
4. Information management
5. Communication
Electric fence
If memory is used incorrectly, segmentation fault
User Space
User Applications, GNU C libraries, Shell
Kernel Space
System calls interface -> device drivers
Hardware Space
Hardware
System-wide error variable
Errno
read()
read(int, void *buffer, size)
write()
write(int, void* buffer, size)
open()
open(char *name, int flags)