- Page 1 and 2: CSC322 C Programming and UNIX Steph
- Page 3 and 4: The UNIX Operating System UNIX and
- Page 5 and 6: The UNIX Operating System UNIX and
- Page 7 and 8: UNIX is a big-iron operating system
- Page 9 and 10: UNIX First portable operating syste
- Page 11 and 12: UNIX history tree (simplified) For
- Page 13 and 14: 1978 3BSD, adding virtual memory 19
- Page 15: A Short History of UNIX and C 1991
- Page 19 and 20: Upshot You don’t have to grow a b
- Page 21 and 22: Shell UNIX Architecture Libraries U
- Page 23 and 24: Some Concepts UNIX is a multi-user
- Page 25 and 26: UNIX: Provide Tools, Not Policy The
- Page 27 and 28: Default KDE Desktop (SuSE Linux) St
- Page 29 and 30: KDE Desktop with Terminal Applicati
- Page 31 and 32: Text Interface Example (contd.) Mos
- Page 33 and 34: Typical File System Layout / (Root
- Page 35 and 36: More about files We can use the -l
- Page 37 and 38: GNU info files UNIX Online Document
- Page 39 and 40: CSC322 C Programming and UNIX UNIX
- Page 41 and 42: Command Types There are different t
- Page 43 and 44: UNIX User Commands: rm, mkdir, rmdi
- Page 45 and 46: Effective Shell Use: Editing/Comple
- Page 47 and 48: Example: File Handling and Globbing
- Page 49 and 50: UNIX User Commands: cat/more/less c
- Page 51 and 52: Getting into it: vi vi flyby Modal
- Page 53 and 54: Emacs for Everyone Getting into it:
- Page 55 and 56: Emacs for Everyone III Emacs is non
- Page 57 and 58: ed is the standard text editor When
- Page 59 and 60: ? ? quit ? exit ? bye ? hello? ? ea
- Page 61 and 62: SO-CALLED "VISUAL" EDITORS HAVE BEE
- Page 63 and 64: Usage: grep . . . UNIX User Comma
- Page 65 and 66: Simple Output Redirection You can r
- Page 67 and 68:
Input Redirection You can also redi
- Page 69 and 70:
Basic Process Control You can start
- Page 71 and 72:
Job Control Example $ emacs & (Star
- Page 73 and 74:
CSC322 C Programming and UNIX Progr
- Page 75 and 76:
Consider the following C program #i
- Page 77 and 78:
#include #include int main(void)
- Page 79 and 80:
A Closer Look (4) gcc is the GNU C
- Page 81 and 82:
The Fahrenheit-Celsius Example Comp
- Page 83 and 84:
Variables “int fahrenheit, celsiu
- Page 85 and 86:
A while-loop has the form while()
- Page 87 and 88:
UNIX User Commands: cp and mv cp wi
- Page 89 and 90:
CSC322 C Programming and UNIX Progr
- Page 91 and 92:
Flow-Control: if The primary means
- Page 93 and 94:
Flow-Control: for The for-loop in C
- Page 95 and 96:
for vs. while Note that for is more
- Page 97 and 98:
Types: Integers and Characters C ha
- Page 99 and 100:
Simple Character I/O The C library
- Page 101 and 102:
#include #include int main(void)
- Page 103 and 104:
Exercises Write a programm that con
- Page 105 and 106:
Nomination: Most Useless Use of cat
- Page 107 and 108:
Example: Word Counting /* Count wor
- Page 109 and 110:
Another View at Expressions Express
- Page 111 and 112:
Expressions: Associativity of Binar
- Page 113 and 114:
Expressions: Logical Operators Logi
- Page 115 and 116:
Floating Point Numbers C supports t
- Page 117 and 118:
Administrative Notes Please ssh to
- Page 119 and 120:
CSC322 C Programming and UNIX Progr
- Page 121 and 122:
#include #include #include int m
- Page 123 and 124:
Initializing Arrays In the example,
- Page 125 and 126:
No Safety Belts and No Air Bag! C d
- Page 127 and 128:
Functions Functions are the primary
- Page 129 and 130:
Function Calling A function is call
- Page 131 and 132:
Example: Printing Character Frequen
- Page 133 and 134:
Assignment A prime number is a (pos
- Page 135 and 136:
A function is a named subroutine Re
- Page 137 and 138:
Example: int read int10(void) /* We
- Page 139 and 140:
Do we have a Valid Digit? /* Is a c
- Page 141 and 142:
Build General Functions! Good progr
- Page 143 and 144:
Example: Printing Integers We now w
- Page 145 and 146:
Writing a Number in any Base (
- Page 147 and 148:
Putting Things Together: A Base Con
- Page 149 and 150:
Usage Example $ ./base converter In
- Page 151 and 152:
CSC322 C Programming and UNIX Progr
- Page 153 and 154:
Includes Translates into Is combibe
- Page 155 and 156:
The C Preprocessor The C preprocess
- Page 157 and 158:
myfile.c: A Poem #include "mary" $
- Page 159 and 160:
Simple Macro Definitions: #define T
- Page 161 and 162:
Macros with Arguments Macro definit
- Page 163 and 164:
#define Caveats Since macros work b
- Page 165 and 166:
cond preproc.c: #define hallo #defi
- Page 167 and 168:
CSC322 C Programming and UNIX Progr
- Page 169 and 170:
cond preproc.c: #define hallo #defi
- Page 171 and 172:
Combinations of #ifdef and #include
- Page 173 and 174:
Definitions and Declarations Defini
- Page 175 and 176:
Scoping Rules There are two kinds o
- Page 177 and 178:
Limiting Potential Scope By default
- Page 179 and 180:
Persistent Local Variables: static
- Page 181 and 182:
Example: Static and Automatic Varia
- Page 183 and 184:
Implement the libray in its own sou
- Page 185 and 186:
Project: An RPN Calculator Aim: A c
- Page 187 and 188:
$ ./rpn calc Usage Example 10 8 + S
- Page 189 and 190:
Subtasks From the above, we can ide
- Page 191 and 192:
The Character I/O Library: Ideas Ma
- Page 193 and 194:
The Character I/O Library: Global V
- Page 195 and 196:
int LookChar(void) { int c = GetCha
- Page 197 and 198:
#include "chario.h" The Integer I/O
- Page 199 and 200:
The Integer I/O Library: Helper fun
- Page 201 and 202:
The Integer I/O Library: Helper fun
- Page 203 and 204:
The Integer I/O Library: Checking f
- Page 205 and 206:
Exercises Download the program from
- Page 207 and 208:
Recapitulation: Some of our Library
- Page 209 and 210:
Case Distinctions Note: The operato
- Page 211 and 212:
The Stack Abstract Datatype A stack
- Page 213 and 214:
Pushing things onto the stack: PUSH
- Page 215 and 216:
The Main Program: Prelimaries and D
- Page 217 and 218:
The Main Loop: Arithmetic operators
- Page 219 and 220:
The Main Loop: I/O operators switch
- Page 221 and 222:
case ’S’: for(i=0; i
- Page 223 and 224:
UNIX User Commands: dc dc is an arb
- Page 225 and 226:
CSC322 C Programming and UNIX Progr
- Page 227 and 228:
#include #include int main(void)
- Page 229 and 230:
Bitwise Operators Bitwise operators
- Page 231 and 232:
Example These macros can be used to
- Page 233 and 234:
Conditional Expressions Similarly t
- Page 235 and 236:
Type Conversion (Casting) As alread
- Page 237 and 238:
CSC322 C Programming and UNIX Progr
- Page 239 and 240:
Order Of Execution In general, the
- Page 241 and 242:
Types in C C offers a set of basic
- Page 243 and 244:
Defining New Types with typedef The
- Page 245 and 246:
Enumerations in C Enumeration data
- Page 247 and 248:
enum and typedef Typically, enumera
- Page 249 and 250:
Aggregating Data Types Let’s agai
- Page 251 and 252:
Consider the following definition:
- Page 253 and 254:
Exercises Create a function that ha
- Page 255 and 256:
Assume the following problem: Repre
- Page 257 and 258:
typedef enum { circle, square, rect
- Page 259 and 260:
typedef union { CircleCoord circle_
- Page 261 and 262:
Pointers Pointers are derived types
- Page 263 and 264:
#include #include void swap(int *
- Page 265 and 266:
Why Pointers? The are two main reas
- Page 267 and 268:
Exercises Write a program that prin
- Page 269 and 270:
Refresher: Pointers A pointer type
- Page 271 and 272:
The malloc() function We request a
- Page 273 and 274:
More on Dynamic Memory Allocation G
- Page 275 and 276:
Example: SecureMalloc() Note: In my
- Page 277 and 278:
Example: Linear Lists (of Integers)
- Page 279 and 280:
#ifndef INT_LISTS #define INT_LISTS
- Page 281 and 282:
Example - Inserting At the End /* I
- Page 283 and 284:
Example - Freeing Lists /* Free the
- Page 285 and 286:
Example - Main Function int main(vo
- Page 287 and 288:
Use this datatype and the functions
- Page 289 and 290:
Monday, Oct. 14th, 11:00 - 11:50 To
- Page 291 and 292:
Refresher: Dynamic Memory Handling
- Page 293 and 294:
int array[10]; int *a, *b; a = arra
- Page 295 and 296:
Compiling: gcc -o csc322 csc322.c R
- Page 297 and 298:
#include #include #include void
- Page 299 and 300:
CSC322 C Programming and UNIX Midte
- Page 301 and 302:
The File System / (Root directory)
- Page 303 and 304:
Some Important UNIX Commands (1) Or
- Page 305 and 306:
Input and Output Redirection, Pipin
- Page 307 and 308:
C Compiling with gcc Programs consi
- Page 309 and 310:
if...else - Conditional execution s
- Page 311 and 312:
C Preprocessor The #include directi
- Page 313 and 314:
CSC322 C Programming and UNIX Progr
- Page 315 and 316:
#include #include #define BUF_SIZ
- Page 317 and 318:
Example: Growing the Buffer as Need
- Page 319 and 320:
char *cp, *cq; int *ip, *iq; Pointe
- Page 321 and 322:
Reading the Command Line: argc and
- Page 323 and 324:
#include #include Example: Echoin
- Page 325 and 326:
CSC322 C Programming and UNIX Makin
- Page 327 and 328:
chario.h The rpn calc Example (Simp
- Page 329 and 330:
UNIX User Utilities: make make is a
- Page 331 and 332:
Built-In Rules and Makefile Variabl
- Page 333 and 334:
Phony Targets Not all targets need
- Page 335 and 336:
CSC322 C Programming and UNIX Odds
- Page 337 and 338:
Assertions Internal consistency che
- Page 339 and 340:
#include #include #include int g
- Page 341 and 342:
Search in Loops A frequent use of l
- Page 343 and 344:
Selective Operations and Special Ca
- Page 345 and 346:
do/while Loops Both while and for l
- Page 347 and 348:
E theorem prover Some Loop Statisti
- Page 349 and 350:
CSC322 C Programming and UNIX Funct
- Page 351 and 352:
Function Pointers We can use the ad
- Page 353 and 354:
Example #include #include int add
- Page 355 and 356:
C Library Functions: qsort() qsort(
- Page 357 and 358:
#include #include Example typedef
- Page 359 and 360:
Unsorted: 103 70 105 115 81 127 74
- Page 361 and 362:
C Standard Library Organisation - a
- Page 363 and 364:
#include #include #include #incl
- Page 365 and 366:
CSC322 C Programming and UNIX C Sta
- Page 367 and 368:
Some C Character Classes All charac
- Page 369 and 370:
#include #include #include int m
- Page 371 and 372:
Strings Strings are not part of the
- Page 373 and 374:
Example: Duplicating Strings Severa
- Page 375 and 376:
String Functions from (3) int strc
- Page 377 and 378:
Generic Memory Access Functions The
- Page 379 and 380:
Memory Functions from (2) void *me
- Page 381 and 382:
CSC322 C Programming and UNIX C Sta
- Page 383 and 384:
Memory Functions from (1) void *me
- Page 385 and 386:
Example #include #include #includ
- Page 387 and 388:
Input and Output in the Standard Li
- Page 389 and 390:
Opening File Streams In addition to
- Page 391 and 392:
Simple Stream Based I/O Functions (
- Page 393 and 394:
#include #include #include #incl
- Page 395 and 396:
} for(i=1; i
- Page 397 and 398:
Exercises Write a version of memmov
- Page 399 and 400:
Remark about fgets() char *fgets(ch
- Page 401 and 402:
int main(int argc, char *argv[]) {
- Page 403 and 404:
$ man man | ./mycat1 Example Output
- Page 405 and 406:
Buffering and Flushing Both input a
- Page 407 and 408:
#include #include int main(int ar
- Page 409 and 410:
More Operations on Files int remove
- Page 411 and 412:
Error Functions Each FILE data stru
- Page 413 and 414:
CSC322 C Programming and UNIX C Sta
- Page 415 and 416:
A first Example printf("%d divided
- Page 417 and 418:
Format Specifiers Format specifiers
- Page 419 and 420:
Some Conversion Letters (2) f: Prin
- Page 421 and 422:
#include #include int main(void)
- Page 423 and 424:
Size Modifiers Size modifiers are u
- Page 425 and 426:
The Flags -: Left-justify output (o
- Page 427 and 428:
#include #include Example int mai
- Page 429 and 430:
$ arch322 Makefile sort_csc322.c ut
- Page 431 and 432:
Processes A UNIX process is an inst
- Page 433 and 434:
$ ps PID TTY TIME CMD 1125 pts/3 00
- Page 435 and 436:
$ ps aux Interesting ps Example USE
- Page 437 and 438:
Signals Signals are a way to signal
- Page 439 and 440:
Some UNIX Signals UNIX defines abou
- Page 441 and 442:
top is an interactive version of ps
- Page 443 and 444:
Catching Signals User programs can
- Page 445 and 446:
ANSI C Signal Handling with signal.
- Page 447 and 448:
ANSI C Signal Handers A signal hand
- Page 449 and 450:
#include #include #include #incl
- Page 451 and 452:
int main(int argc, char* argv[]) {
- Page 453 and 454:
Exercises Start a long running proc
- Page 455 and 456:
UNIX philosophy: Everything is a fi
- Page 457 and 458:
FIFOs (named pipes) UNIX File Types
- Page 459 and 460:
The stat() Functions (2) All three
- Page 461 and 462:
#include #include #include #incl
- Page 463 and 464:
} Example Continued else if(S_ISLNK
- Page 465 and 466:
Example Output (of device directory
- Page 467 and 468:
Links (2) Soft links create indirec
- Page 469 and 470:
Exercises Read man stat and extend
- Page 471 and 472:
All files have an owner (a user) Fi
- Page 473 and 474:
User Groups Groups are used in UNIX
- Page 475 and 476:
UNIX User Utilities: chown and chgr
- Page 477 and 478:
Symbolic Encoding ls -l prints a st
- Page 479 and 480:
Examples rw-r--r-- is the most comm
- Page 481 and 482:
chmod Examples chmod ugo+rwx myfile
- Page 483 and 484:
Exercises Read the man and info pag
- Page 485 and 486:
File Decriptors Files are identifie
- Page 487 and 488:
Option flags for open() Note: All o
- Page 489 and 490:
Notes on open() and close() The mod
- Page 491 and 492:
#include #include #include #incl
- Page 493 and 494:
} Example Continued while((count =
- Page 495 and 496:
Cheating with fdopen() Formatted, b
- Page 497 and 498:
CSC322 C Programming and UNIX More
- Page 499 and 500:
FILE* myfile Standard IO Library Th
- Page 501 and 502:
Manipulating File Descriptors: fcnt
- Page 503 and 504:
#include #include #include #incl
- Page 505 and 506:
if(flags & O_APPEND) { printf("Appe
- Page 507 and 508:
Multiplexing I/O Often, a program h
- Page 509 and 510:
select() Arguments fd set is define
- Page 511 and 512:
#include #include #include #incl
- Page 513 and 514:
$ ./select_example 2 file descripto
- Page 515 and 516:
CSC322 C Programming and UNIX Basic
- Page 517 and 518:
Networking Stephan Schulz 517
- Page 519 and 520:
Networking Stephan Schulz 519
- Page 521 and 522:
Networking Stephan Schulz 521
- Page 523 and 524:
Networking Concepts Is communicatio
- Page 525 and 526:
Level 2 protocol (Hardware-Agnostic
- Page 527 and 528:
The Transmission Control Protocol (
- Page 529 and 530:
A server offers a certain service C
- Page 531 and 532:
The Client Side for TCP Connections
- Page 533 and 534:
Creating Sockets with socket() (2)
- Page 535 and 536:
Socket Adresses for IPv4 For IPv4 a
- Page 537 and 538:
Connecting to a Remote Port: connec
- Page 539 and 540:
Example (2) sock = socket(PF_INET,
- Page 541 and 542:
The Server Side A server has a more
- Page 543 and 544:
Naming a Socket (Binding a Socket t
- Page 545 and 546:
Accepting Connections To finally es
- Page 547 and 548:
Example (2) if(argc!=2) { fprintf(s
- Page 549 and 550:
man pages: - man socket - man 2 bin
- Page 551 and 552:
CSC322 C Programming and UNIX Proce
- Page 553 and 554:
Process Properties For each process
- Page 555 and 556:
Exiting There are three normal ways
- Page 557 and 558:
Cleaning up: atexit() ANSI C allows
- Page 559 and 560:
Example (2) int main(void) { if(ate
- Page 561 and 562:
Running other Programs: system() Th
- Page 563 and 564:
Termination Status Interpretation T
- Page 565 and 566:
Example: Executing Commands int mai
- Page 567 and 568:
Exercises Write a program that prin
- Page 569 and 570:
Creating new Processes: fork() The
- Page 571 and 572:
} Example if(child_pid == 0) { pid
- Page 573 and 574:
Comments on fork() Order of executi
- Page 575 and 576:
#include int main(int argc, char*
- Page 577 and 578:
FILE* myfile Standard IO Library I/
- Page 579 and 580:
Example: Bufferd I/O and Forking /*
- Page 581 and 582:
Waiting for Children to Die As stat
- Page 583 and 584:
Example (2) for(i=0; i
- Page 585 and 586:
Output: Example Output Parent. My P
- Page 587 and 588:
CSC322 C Programming and UNIX Proce
- Page 589 and 590:
Getting and Changing Process Groups
- Page 591 and 592:
Example (2) res = setpgid(0,0); if(
- Page 593 and 594:
Example Output $ ./pg example Paren
- Page 595 and 596:
Is this good for Something? There a
- Page 597 and 598:
Example (2) pid = getpid(); pgid =
- Page 599 and 600:
Example Output with kill Queen bee:
- Page 601 and 602:
Waiting for Termination: waitpid()
- Page 603 and 604:
Exercises Write a program that keep
- Page 605 and 606:
Process Environment Each UNIX proce
- Page 607 and 608:
$ printenv Example: Part of my Envi
- Page 609 and 610:
Accessing the Environment from a Pr
- Page 611 and 612:
The POSIX Interface to the Environm
- Page 613 and 614:
The 6 exec Functions #include int
- Page 615 and 616:
#include #include #include #incl
- Page 617 and 618:
int count_words(char* line) { int w
- Page 619 and 620:
} Example (5) for(i=0; i
- Page 621 and 622:
int main(void) { pid_t child_pid; c
- Page 623 and 624:
} { /* Parent */ setpgid(child_pid,
- Page 625 and 626:
# ls * Command: ls Arguments: ls *
- Page 627 and 628:
CSC322 C Programming and UNIX Final
- Page 629 and 630:
Pointers and Dynamic Arrays Arrays
- Page 631 and 632:
New Flow Control Constructs break i
- Page 633 and 634:
Standard Library: Characters and St
- Page 635 and 636:
Standard Library: Buffered I/O Stan
- Page 637 and 638:
Processes and Signals Processes are
- Page 639 and 640:
UNIX: Everything is a file File typ
- Page 641 and 642:
File Descriptors and select() File
- Page 643 and 644:
TCP/IP (v4) Connections Reliable, s
- Page 645 and 646:
Process Environment and Program Exe