Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 537 Section 1 Programming in Unix and C

Similar presentations


Presentation on theme: "CS 537 Section 1 Programming in Unix and C"— Presentation transcript:

1 CS 537 Section 1 Programming in Unix and C
Michael Swift © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

2 Sections Ask questions about lecture material Project discussion
C programming © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

3 Project 1 Part 1: Shuffle a file Part 2: add a system call to xv6
You will write a program that reads a text file Files can be of any length, with lines up to 512 characters Output: shuffle lines from beginning and end Program input: read a file specified on the command line: shuffle –i input.txt –o output.txt Part 2: add a system call to xv6 getppid() – get PID of parent of a process © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

4 xv6 Unix version 6: classic version of Unix from 1974
Fully documented, commented by Lyons Ported to x86 by MIT Allows class projects to use a real operating system From 1974 Avoids complexity of Linux, Windows, & MacOS 16 million lines of code for Linux 7,200 lines of code for xv6 © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

5 Downloading + compiling xv6
Copy: cp ~cs537-1/ta/xv6/xv6.tar.gz . Unpack: tar -xzf xv6.tar.gz Compile: cd xv6 make Run: make qemu Add a program in xv6: cd user Add file to user director Add file to user/makefile.mk Recompile xv6 © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

6 Facilities Department Linux machines (penguins): 1355: emperor-01 - 07
1358: rockhopper 1366: royal 1368: snares01 – 10 © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

7 Issues with C Little hand-holding for programmer
Manual memory management Small standard library No native support for threads and concurrency Weak type checking © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

8 Using C and Unix Make Makefile program.c Compiler program.h stdio.h
gcc -c program.c program.c Compiler program.h program.o stdio.h Linker libc.a program © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

9 C language #include <stdio.h> int main(int argc, char * argv[])
Preprocessor include directive for header files #include <stdio.h> int main(int argc, char * argv[]) { printf(“Hello, world: %s\n”,argv[1]); return(0); } Declaration of main function and arguments Print first command-line parameter © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

10 Issues with C Memory allocation Pointer arithmetic and arrays
malloc(), free() Pointer arithmetic and arrays Preprocessor © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

11 Example #include <stdio.h> int main(int argc, char * argv[]) {
int i; for (i = 0; i < argc; i++) { printf("argv[%d] = %s\n",i, argv[i]); } return(0); © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

12 Strings Strings in C are arrays of bytes:
char str[100]; They are null terminated – so you need to make space for it str[0] = ‘\0’; strlen(str) = 0; There are a bunch of functions for working with them: strlen, strcpy, strcat © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

13 String Example #include <stdio.h> #include <string.h>
int main(int argc, char *argv[]) { char s[100]; strcpy(s,"hello"); strcat(s,", world"); printf("S = %s\n",s); } © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

14 Memory You have to mange memory yourself.
Fixed-size variables can be allocated on a stack The contents of these variables go way when the function returns. char str[100] = “hello, world\n”; Variable-size variables are allocated using malloc, like new() in Java. Memory from malloc only becomes invalid when you free it: char *str; str = malloc(n); strpy(str,”hello, world\n”); free(str); © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

15 Memory Example 1 #include <stdio.h> #include <stdlib.h>
int main(int argc, char * argv[]) { int * array = NULL; int i; array = (int *) malloc(100 * sizeof(int)); if (array == NULL) { printf("Malloc failed\n"); return(0); } for (i = 0; i < 100; i++) array[i] = 100-i; printf("array[%d] = %d\n",i,array[i]); free(array); © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

16 Memory Example 2 #include <stdio.h> #include <string.h>
#include <stdlib.h> int main(int argc, char *argv[]) { char * s; s = malloc(100); if (s == NULL) { printf("Malloc failed\n"); return(0); } strcpy(s,"hello"); strcat(s,", world"); printf("S = %s\n",s); free(s); © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

17 File I/O f* functions for accessing files:
struct FILE *: represents an open file f = fopen(“foo”,”r”) – open file foo for reading fclose(f) - says you are done with f bytes = fread(buffer,size,count,f) = reads size x count bytes from f into buffer fwrite(buffer, size, count ,f) = writes size x count bytes to f from buffer © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

18 File Example #include <stdio.h>
int main(int argc, char * argv[]) { FILE * f; char s[101]; f = fopen("test.txt","r"); if (f == NULL) { printf("Error opening file\n"); return(0); } while (fgets(s, 100, f) != NULL) printf("%s",s); fclose(f); © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

19 Data Structures in C Global arrays Dynamic arrays are pointers
int global_data[500]; Dynamic arrays are pointers int * dyn_array; dyn_array = malloc(sizeof(int) * 100); Structures typedef struct __rec_t { int key; int size; int record[NUMRECS]; } rec_t; rec_t r; r.key = 10; r.size = 10; r.record[0] = 0; … © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

20 More advanced topics Compiler errors and warnings Multiple files
gcc –o foo -Wall foo.c Multiple files gcc –o foo foo.c bar.c baz.c © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

21 Documentation Unix/Linux man pages example: “man close”
CLOSE(3) BSD Library Functions Manual FCLOSE(3) NAME fclose -- close a stream LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <stdio.h> int fclose(FILE *stream); DESCRIPTION The fclose() function dissociates the named stream from its underlying RETURN VALUES Upon successful completion 0 is returned. Otherwise, EOF is returned and © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

22 Man pages Documentation is divided into sections
Programs, commands System calls Subroutine libraries Hardware Config files Games Miscellaneous System administration man returns the result from the lowest-numbered section apropos searches for commands with a word © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift

23 Debugging Compile with debugging using “-g” Run your program with gdb
gcc -g -o foo.o foo.c Run your program with gdb gdb foobar GNU gdb 6.3 <copyright omitted> (gdb) break main breakpoint 1 at 0x80483b0: in file foo.c, line 5 (gdb) run Starting program: /afs/cs.wisc.edu/…/foobar Breakpoint 1, main (argc=1, argv=0xbfe27804) at foo.c:5 if (argc > 1) { (gdb) print argc $1 = 1 (gdb) © Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift


Download ppt "CS 537 Section 1 Programming in Unix and C"

Similar presentations


Ads by Google