System Programming with C and Unix CSCI 1730 April 1 st , 2014
Dec 14, 2015
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
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
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
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
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
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
…
…
…
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)
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
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
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
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
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
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
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.
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);}
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);}
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);}
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); }}
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); }}
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);}
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));}
Bitwise NOT
/* This program demonstrates the bitwise not operator. */
#include <stdio.h>
main(){unsigned char a;
a=17;a=~a;printf("%d\n",a);}
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);}
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);}