cs 590 programming environments with unix. computer lab account course homepage kkeen/cs590

24
CS 590 Programming Environments with UNIX

Upload: stewart-austin

Post on 02-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

CS 590Programming Environments with

UNIX

Page 2: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Computer Lab Accountwww.cs.uah.edu/account

Course Homepagewww.cs.uah.edu/~kkeen/CS590

LASER Lab N328

Page 3: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Some Background

UNIX UNIX System V BSDPOSIX

LINUX

Page 4: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

LASER LAB

System Names:

catalina conquest crusader

dakota duchess havoc

hawker invader lightning

marauder

shrike whirlwind

Page 5: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Remote Access

PuTTY is a free telnet/SSH clientYou can download PuTTY here

Upload/Download requires SFTPYou can use PSFTP to upload/download.

Page 6: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Remote Access from UNIX/LINUX

ssh username@host

Ex: ssh [email protected]

Page 7: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

What you are expected to already know

bash and simple shell operations Files and directories

File structure / . .. Absolute vs. relative paths File and directory permissions

File and directory commands cp, mv, scp (secure copy), rm, mkdir, rmdir, tar

Set up environment variables PATH, LD_LIBRARY_PATH

At least one text editor vi, emacs, pico

Strong C programming skills

Page 8: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Getting Help The man pages

section 1: User commandssection 2: system callssection 3: library callssection 4: special or device filessection 5: file formats and conventionssection 6: games for linuxsection 7: macro packages and conventionssection 8: system management commands

Page 9: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

UNIX/LINUX System Overview

Apps

Shell

Kernel

H.W.

System call

interface

Page 10: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Multi UserMulti Process system

Every user has a UID Every user belongs to at least one

UNIX group GID Root always has UID of 0

Page 11: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Programs and Processes

A program is an executable file A process is an executing instance

of a program

Page 12: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Kernel Mode vs. User Mode

User mode Most apps run in this mode

Kernel mode “trusted” code

Page 13: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

System Calls

AppC Library

func

func

func

func

func

func

Kernel

Page 14: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

System Call Error Handling

For most system calls, return values are: 0 – success Negative number – failure

errno perror strerror

Page 15: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

perror

void perror (const char *msg);

Prints the text in msg followed by a colon and text description of the error number.

Page 16: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

perror example

FILE *myFile;myFile = fopen(“file.txt”, “r”);if(myFile == NULL) { perror(“unable to open file”);}

Page 17: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

strerror

char *strerror(int errnum);

Prints out text for given error number. Can be used as part of a larger error message.

Page 18: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

strerror example

FILE *myFile;myFile = fopen(“file.txt”, “r”);if(myFile == NULL) { fprintf(stderr, “unable to open file:

%s”, strerror(errno));}

Page 19: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Basic IPC - Signals

Inter Process Communication (IPC). Most basic form is via signals. Used to notify a process of some condition.

We can catch signals and Ignore them Let default action take place Call a custom signal handler

Page 20: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Example Program

Files and Directories http://www.cs.uah.edu/~kkeen/CS590/examples/intro/pseudols

More about files and directories in chapter 4

Page 21: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Time in UNIX

Historically Calendar Time Process Time

Clock Time User CPU Time System CPU Time

Page 22: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

Command Line Arguments

int main(int argc, char** argv)int main(int argc, char *argv[]) argc – argument count argv – array of character pointers Dealing with numbers

atoi atof

Page 23: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

getoptint getopt(int argc, char* const argv[], const char *optstring);

Function to allow easy parsing of command line options

Looks for options we specify Sets optarg and optind Can specify required argument by

using ‘:’ after the option in optstring

Page 24: CS 590 Programming Environments with UNIX. Computer Lab Account   Course Homepage kkeen/CS590

getopt_long

Uses two dashes instead of one Allows longer, more descriptive

options.