Introduction to Linux System Programming

Introduction to Linux System Programming Electrical and Computer Engineering University of Waterloo Irene Huang http://ece.uwaterloo.ca/~yqhuang/labs/...
Author: Zoe Wood
14 downloads 1 Views 611KB Size
Introduction to Linux System Programming Electrical and Computer Engineering University of Waterloo Irene Huang http://ece.uwaterloo.ca/~yqhuang/labs/ece254/ http://ece.uwaterloo.ca/~yqhuang/labs/mte241/ 1

Lab Administration • Course Book is used for group signup and lab reservation https://ecewo32.uwaterloo.ca/cgi-bin/WebObjects/CourseBook

• Scheduled labs: – ECE254: Tue. Wed. Thu. in odd weeks – MTE241: Mon. Wed. Fri. afternoons in even weeks

• How to seek help – – – –

Piazza Q&A Platform (www.piazza.com). Find us in the lab Book an appointment for individual consultation Targeted response time: within one business day

• Read the Lab Policy on the Lab Home Page 2

Getting Started • Linux – A multiuser, multitasking and multiprocessing OS – ECE undergrad Linux hosts: • ecelinux.uwaterloo.ca (an alias of ecelinux1,2,3,4) • ecelinuxN.uwaterloo.ca (N=1,2,3,4, 5,6,7,8,9,10,11) • https://ieee.uwaterloo.ca/clients

• Your UWID and Nexus password • Logging on via a terminal – The SSH Secure Shell windows client (all nexus computers) • All Programs → Internet Tools → Secure Shell Client

– PuTTY (ECE Nexus computers only) • Start → All Programs → Portable PuTTY → PuTTY 3

Secure Shell Client

PuTTY

4

Shell • What is it? – A piece of software running on UNIX – Interprets commands sent from the terminal – Executes the commands

• Some commands – – – –

ls lists the files under current working directory pwd print name of current/working directory cd change the current directory man format and display the on-line manual pages

• Logging off – logout exit a login shell 5

Map Linux Account in Windows • Log onto your Nexus account • Map your ecelinux drive in Windows: – – – – –

Open My Computer Click on Tools → Map Network Drive Under Driver, choose “P:” Under Folder, type \\eceserv\homes Check “Reconnect at logon” and click Finish

6

Remote Unix Graphical Support • Xming – The X Server on ECE Nexus PCs – All Programs → Xming

• Enabling X11 – The SSH Secure Shell windows client setting • Edit → Settings → Profile settings → connection → Tunneling • Tick the box next to “Tunnel X11 connections”

– PuTTY setting • Click on SSH → X11 • Tick the box to enable X11 forwarding.

7

Secure Shell Client

PuTTY

8

Editors • vi, vim, gvim – Editing mode, use “i” or “a” to enter – Command mode, use “Esc” to enter

• emacs – Can split the window to use the debugger in the second window

• pico, nano – More like notepad, easy to use – But not designed for programmers

9

C Development on Linux • Edit the source code – Use your favorite editor to write the .c file

• Compile the source code – gcc – Frequently used options of the gcc compiler • -c: generate object code • -g: generate symbol table information for debugger • -o: give the output file (i.e. the executable) a name

• Run the executable – ./ (i.e. ./a.out)

10

Automate Build: Make • Makefile Basic target: prereq1 prereq2 commands

This is a TAB, not white spaces. You have been warned!

• Example: main.out: main.o gcc -o main.out main.o main.o: main.c gcc –c main.c

• To build: type “make main.out” 11

A Sample Makefile # Makefile CC=gcc CFLAGS=-Wall LD=gcc LDFLAGS= OBJS=main.o all: main.out main.out: $(OBJS) $(LD) $(CFLAGS) $(LDFLAGS) -o main.out $(OBJS) main.o: main.c $(CC) $(CFLAGS) -c main.c

.PHONY: clean clean: rm -f *.o *.out 12

More Development Tools • Debugger – Command line: gdb – XWin GUI: ddd

• Eclipse with C/C++ Plugin – Available on ecelinux hosts (requires X-Window support) – Path: /opt/eclipse64/eclipse – Free downloadable from www.eclipse.org

• Version control Software – SVN

13

Example C System Code #include #include #include int main( void ){ struct rlimit proc_limit; printf("Resource\tlimit\t\tmax\n"); getrlimit(RLIMIT_CPU, &proc_limit); printf("CPU\t\t%lld\t\t%lld\n", (long long)proc_limit.rlim_cur, (long long)proc_limit.rlim_max); getrlimit(RLIMIT_NPROC, &proc_limit); printf("NPROC\t\t%lld\t\t%lld\n", (long long)proc_limit.rlim_cur, (long long)proc_limit.rlim_max); return 0; }

14

Linux Programmer’s Resources • Manuals are divided by sections – – – –

Section 1: user commands Section 2: system calls Section 3: library calls Other sections

• Examples – man 3 printf – man 2 stat

• Web Resources – linux.die.net – www.advancedlinuxprogramming.com – www.tldp.org 15