1 / 30

System Programming with C and Unix

System Programming with C and Unix. CSCI 1730 April 1 st , 2014. Materials. Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams Textbook (Hoover ) http://www.amazon.com/System-Programming-Unix-Adam-Hoover/dp/0136067123

effie
Download Presentation

System Programming with C and Unix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programmingwith C and Unix CSCI 1730 April 1st, 2014

  2. Materials • Class notes • slides & some “plain old” html & source code examples • linked from course calendar • board notes & diagrams • Textbook (Hoover) • http://www.amazon.com/System-Programming-Unix-Adam-Hoover/dp/0136067123 • Buy new / buy used / rent / eTextbook

  3. Text • System 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/Output • Ch 6: Program Management • Ch 7: System Calls • Ch 8: Libraries • Ch 9: Scripting Languages

  4. The UNIX operating system • kernel + shell + system calls • kernel: • 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

  5. 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

  6. 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 types • names < 256 characters • < 14 to be backward compatible

  7. Directories, pathnames • directories 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 …

  8. 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)

  9. 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

  10. Devices • UNIX 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 example • cat fileX > /dev/rmt0 causes the contents of fileX to be written to the tape drive associated with the rmt0 file in the /devdirectory

  11. 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

  12. IPC: Interprocess Communication • Consists of mechanisms that allow processes to send info to one another • Methods differ in : • type/amount of info • number of processes • whether processes need to exist at the same time • whether processes need to be on the same machine

  13. IPC: InterProcess Communication • pipes • output of one program is input of another • signals • processes that exist at same time on same machine can send integer signals • shared memory • processes that exist at same time can share variables • semaphores control access to shared memory • sockets • processes that exist at the same time on same or different machines can communicate arbitrary info

  14. 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

  15. System calls and library functions • kernel • 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.

  16. Common shell commands • cd • pwd • set • which

  17. Common system commands • grep • ls • man • more • time • sort

  18. A “review” of C

  19. C review – 4 data types /* A review of the basic data types in C. */ #include <stdio.h> int main() { intx,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); }

  20. C review – arithmetic /* A review of the basic arithmetic operators in C. */ #include <stdio.h> int main() { intx,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); }

  21. C review – loops #include <stdio.h> /* A review of the loop types in C. */ int main() { inti,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); }

  22. C review – blocks /* A review of conditionals and blocks in C. */ #include <stdio.h> int main() { inti,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); } }

  23. C review – flow control /* A review of flow control statements in C. */ #include <stdio.h> int main() { inti,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); } }

  24. System Programming Chapter 2

  25. 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); }

  26. 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)); }

  27. Bitwise NOT /* This program demonstrates the bitwise not operator. */ #include <stdio.h> main() { unsigned char a; a=17; a=~a; printf("%d\n",a); }

  28. 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); }

  29. 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); }

  30. 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); }

More Related