Top Banner
System Programming with C and Unix CSCI 1730 April 1 st , 2014
30

CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Dec 14, 2015

Download

Documents

Raven Longton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

System Programmingwith C and Unix

CSCI 1730April 1st, 2014

Page 2: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

MaterialsClass notes

slides & some “plain old” html & source code examples linked from course calendar

board notes & diagramsTextbook (Hoover)

http://www.amazon.com/System-Programming-Unix-Adam-Hoover/dp/0136067123

Buy new / buy used / rent / eTextbook

Page 3: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

TextSystem Programming with C and Unix by

Hoover Ch 1: Introduction Ch 2: Bits, Bytes and Data Types Ch 3: Arrays and Strings Ch 4: Pointers and Structures

Ch 5: Input/OutputCh 6: Program Management

Ch 7: System CallsCh 8: Libraries Ch 9: Scripting Languages

Page 4: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

The UNIX operating systemkernel + shell + system callskernel:

provides access to H/W resources of computer manages memory and CPU

shell:provides command-line interface to services

system calls:provide method-call interface to services

Page 5: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Overview of UNIX portion of course We'll (try to) cover: basic concepts and terminology files and directories processes interprocess communication

signals and signal handling pipes shared memory, messages, semaphores X terminal i/o sockets

standard i/o library

Page 6: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Basics: Files and Processes info stored in files while files have logical structure to programs

that create and use them, at the UNIX level they are just a sequence of bytes no record terminators, no file terminators, no

special file typesnames < 256 characters

< 14 to be backward compatible

Page 7: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Directories, pathnamesdirectories have hierarchical, tree-like

structure each directory can contain files and

subdirectories full names of UNIX files are pathnames,

including directories

/users/faculty/eileen/junk.txt

/users

Faculty

eileen

junk.txt

Page 8: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Example /users/faculty/eileen/junk.txt (absolute

pathname)

if _current working directory_ is /users/faculty/eileen can use junk.txt (relative pathname)

if _current working directory_ is /users/faculty can use eileen/junk.txt (also a relative

pathname)

Page 9: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Ownership, permissions each file has an owner

that owner is a member of a _group_ each file has 3 sets of permissions:

read/write/execute one set for owner, one set for group, one set

for public

-rwxr-xr-x. 1 eileen users 50 Mar 31 20:29 play.exe-rw-r--r--. 1 eileen users 50 Mar 31 20:28 junk3.txt-rw-r-----. 1 eileen users 50 Mar 31 20:28 junk2.txt

Page 10: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

DevicesUNIX treats devices like files. filenames exist that represent devices like a

keyboard or printer.

To write to the printer, you can just write to the file that represents it... for examplecat fileX > /dev/rmt0

causes the contents of fileX to be written to the tape drive associated with the rmt0 file in the /dev directory

Page 11: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Processes process = instance of an executing program When you type: > ls at the command line, the shell process

creates a process to run the ls program. UNIX is multitasking

more than one process can run at the same time

i.e., multiple processes share the CPU

Page 12: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

IPC: Interprocess CommunicationConsists of mechanisms that allow processes

to send info to one anotherMethods differ in :

type/amount of infonumber of processeswhether processes need to exist at the same

timewhether processes need to be on the same

machine

Page 13: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

IPC: InterProcess Communicationpipes

output of one program is input of another signals

processes that exist at same time on same machine can send integer signals

shared memoryprocesses that exist at same time can share variablessemaphores control access to shared memory

socketsprocesses that exist at the same time on same or

different machines can communicate arbitrary info

Page 14: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

The shell supports pipes:>ls | more output of the ls program is input to the more program

>ls | grep notes | more output of the ls program is input to the grep program notes is a command line parameter to output of grep is input to more

Page 15: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

System calls and library functionskernel

memory resident program, deals with process scheduling and i/o control

system calls the interface to the kernel and the resources it

controls.invoked like library subroutines (but typically

more efficient, lower level, run in 'kernel mode' rather than 'user mode').

Library routines are a layer between user code and system calls.

Page 16: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Common shell commandscdpwdset which

Page 17: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Common system commandsgreplsmanmoretimesort

Page 18: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

A “review” of C

Page 19: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

C review – 4 data types

/* A review of the basic data types in C. */

#include <stdio.h>

int main(){int x,y;char a;float f,e;double d;

x=4;y=7;a='H';f=-3.4;d=54.123456789;e=54.123456789;

printf("%d %c %f %lf\n",x,a,e,d);printf("%d %c %.9f %.9lf\n",x,a,e,d);}

Page 20: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

C review – arithmetic

/* A review of the basic arithmetic operators in C. */

#include <stdio.h>

int main(){int x,y;int r1,r2,r3,r4,r5;

x=4;y=7;r1=x+y;r2=x-y;r3=x/y;r4=x*y;printf("%d %d %d %d\n",r1,r2,r3,r4);

r3++;r4--;r5=r4%r1;printf("%d %d %d\n",r3,r4,r5);}

Page 21: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

C review – loops#include <stdio.h>

/* A review of the loop types in C. */int main(){int i,x;

x=0;for (i=0; i<4; i++) { x=x+i; printf("%d\n",x); }while (i<7) { x=x+i; i++; printf("%d\n",x); }do { x=x+i; i++; printf("%d\n",x); }while (i<9);}

Page 22: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

C review – blocks

/* A review of conditionals and blocks in C. */

#include <stdio.h>

int main(){int i,x;

x=0;for (i=0; i<5; i++) { if (i%2 == 0 || i == 1) x=x+i; else x=x-i; printf("%d\n",x); }}

Page 23: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

C review – flow control

/* A review of flow control statements in C. */

#include <stdio.h>

int main(){int i,x;

x=0;for (i=0; i<5; i++) { if (i%2 == 0) continue; x=x-i; if (i%4 == 0) break; printf("%d\n",x); }}

Page 24: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

System ProgrammingChapter 2

Page 25: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

ASCII

/* This program shows the dual interpretations of char and** unsigned char data types. */

#include <stdio.h>

main(){char a;unsigned char b;

a='A';b='B';printf("%c %c %d %d\n",a,b,a,b);a=183;b=255;printf("%d %d\n",a,b);}

Page 26: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

sizeof() operator

/* This program demonstrates the sizeof() operator. */

#include <stdio.h>

main(){int i;char c;double d;

printf("%d %d %d %d\n",sizeof(i),sizeof(c),sizeof(d),sizeof(float));}

Page 27: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Bitwise NOT

/* This program demonstrates the bitwise not operator. */

#include <stdio.h>

main(){unsigned char a;

a=17;a=~a;printf("%d\n",a);}

Page 28: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Bitwise AND

/* This program demonstrates the bitwise and operator. */

#include <stdio.h>

main(){unsigned char a,b;

a=17;b=22;a=a & b;printf("%d\n",a);}

Page 29: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Bitwise OR

/* This program demonstrates the bitwise or operator. */

#include <stdio.h>

main(){unsigned char a,b;

a=17;b=22;a=a | b;printf("%d\n",a);}

Page 30: CSCI 1730 April 1 st, 2014. Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.

Bit operators (variables and constants)

/* This program demonstrates using the bitwise operators** with variables and constants. */

#include <stdio.h>

main(){char x,y;

x=7;y=6;x=x&y;y=x|16;printf("%d %d\n",x,y);}