CS 241 Section Week - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CS 241 Section Week

Description:

There is no bool in C... In HW#1, a number of people replaced a void function with bool. ... argv[1] may be NULL if (argc == 1) ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 30
Provided by: Alej198
Category:
Tags: argv | section | week

less

Transcript and Presenter's Notes

Title: CS 241 Section Week


1
CS 241 Section Week 4(09/18/08)
2
Topics This Section
  • C Review (regarding HW 1)
  • Disk I/O in C
  • Function pointers in C
  • MP 1
  • MP 2

3
C Review
4
There is no bool in C
  • In HW1, a number of people replaced a void
    function with bool.
  • Theres no bool in C.
  • Correct Response int
  • General Convention (but not always)
  • 0 false
  • (positive) true
  • (negative) error

5
strcat() v. strcpy()
  • In HW1, we asked
  • char str8strcat(str, abcdefgh)
  • There were two problems
  • Not enough memory to store an 8-length string.
  • strcat() concatenates strings together, but str
    may not be a 0-length string (it is initialized
    to undefined values).

6
Return Paths
  • In HW1, we asked
  • int main(a) if (a gt 5) return (1)
  • There was only one problem
  • Not all paths returned a value from main().
  • The declaration of main() wasnt actually wrong,
    its completely valid!

7
argc, argv
  • In HW1, we asked
  • int main(int argv, char argv)
    print(argv1)
  • There were two problems
  • printf() requires a format string.
  • Example printf(s, str)
  • argv1 may be NULL if (argc 1). Always need
    to change the correct number of command line
    arguments are passed!

8
Other minor problems
  • Other less common problems
  • Knowing (p) from (p).
  • Knowing define macros.
  • Freeing allocated memory.

9
I/O in C
10
I/O in C
  • MP2 requires you to read and write simple file in
    C.
  • File I/O was discussed briefly in Lecture 5.
  • Two primary means of doing I/O in C
  • Through lightly-wrapped system calls
  • open(), close(), read(), write(), etc
  • Through C-language standards
  • fopen(), fclose(), fread(), fwrite(), etc

11
I/O in C
  • Opening a file (Method 1)
  • fopen(const char filename, const char mode)
  • filename path to file to open
  • mode what do you wish to do with the file?
  • r read only
  • r read and write (file must already exist)
  • w write (or overwrite) a file
  • w write (or overwrite) a file and allow for
    reading
  • a append to the end of the file (works for new
    files, too)
  • a appends to end of file and allows for
    reading anywhere in the file however, writing
    will always occur as an append

12
I/O in C
  • Opening a file (Method 2)
  • open(const char filename, int flags, int mode)
  • filename path to file to open
  • flags what do you wish to do with the file?
  • One of the following is required
  • O_RDONLY, O_WRONLY, O_RDWR
  • And any number of these flags
  • O_APPEND Similar to a in fopen()
  • O_CREAT Allows creation of a file if it doesnt
    exist
  • O_SYNC Allows for synchronous I/O
    (thread-safeness)
  • To add these flags, simply binary-OR them
    together
  • (O_WRONLY O_APPEND O_CREAT)
  • mode what permissions should the new file have?
  • (S_IRUSR S_IWUSR) creates a user read-write
    file.

13
Opening Files in C
  • Return value of opening a file
  • Having called open() or fopen(), they will both
    create an entry in the OSs file descriptor
    table.
  • Specifics of a file descriptor table will be
    covered in-depth in the second-half of CS 241.
  • Both open() and fopen() returns information about
    its file descriptor
  • open() Returns an int.
  • fopen() Returns a (FILE ).

14
Reading Files in C
  • Two ways to read files in C
  • fread(void ptr, size_t size, size_t count, FILE
    s)
  • ptr Where should the data be read into?
    C-string?
  • size What is the size of each piece of data?
    sizeof(char)?
  • count How many pieces?
    strlen(str)?
  • s What (FILE ) do we read from?
    ptrFile?
  • read(int fd, void buf, size_t count)
  • fd What file do we read from?
  • buf Where should the data be read into?
  • count How many bytes should be read?

15
Reading Files in C
  • Two basic ways to read files in C
  • fread(void ptr, size_t size, size_t count, FILE
    s)
  • ptr Where should the data be read into?
    C-string?
  • size What is the size of each piece of data?
    sizeof(char)?
  • count How many pieces?
    strlen(str)?
  • s What (FILE ) do we read from?
    ptrFile?
  • read(int fd, void buf, size_t count)
  • fd What file do we read from?
  • buf Where should the data be read into?
  • count How many bytes should be read?

16
Reading Files in C
  • Reading more advancely
  • fscanf(FILE stream, const char format, )
  • Saw this on HW 1.
  • Allows for reading at a semantic-level (eg ints,
    doubles, etc) rather than a byte-level.
  • The format string (format) is of the same format
    as printf().
  • No equivalent command for open().

17
Writing Files in C
  • Writing is a lot like reading
  • fwrite(void ptr, size_t size, size_t count, FILE
    s)
  • Writing of bytes with (FILE ).
  • write(int fd, void buf, size_t count)
  • Writing of bytes with a file descriptor (int).
  • fprintf(FILE stream, const char format, )
  • Formatted writiing to files (works like
    printf()).

18
Closing Files in C
  • Always close your files!
  • fclose(FILE stream)
  • close(int fd)
  • Failure to close a file is much like a memory
    leak, but may corrupt files rather than memory.
  • If a file is written to, the program ends, and
    the file is never closed the write never may
    never be written!
  • Reason write(), and especially
    fwrite()/fprintf(), may be buffered before being
    written out to disk.
  • On close()/fclose(), all buffers are cleared and
    the file is properly closed.

19
Function Pointers in C
20
Passing Functions in C
  • In lecture, you saw pthreads
  • When calling pthread_create(), you must pass a
    function
  • pthread_create(, , void (start_routine)(void)
    , )
  • What is void (start_routine)(void)?
  • A parameter that requires a void ___(void ___)
    function!
  • Ex void myfunc(void vptr)
    printf("I'm a thread!")

21
Passing Functions in C
  • In this MP, you must use qsort()
  • The qsort() function definition looks like
  • void qsort (void base, size_t num, size_t size,
    int (comparator)(const void , const
    void ))
  • What is int (comparator)(const void , const
    void )?
  • A parameter that requires a function of the
    following format int ___(const void ___,
    const void ___)
  • That function should return (negative) if
    the (first param) lt (second param) 0
    if the (first param) (second param)
    (positive) if the (first param) gt (second param)

22
Passing Functions in C
  • Comparing Strings
  • A function that could be accepted by qsort() to
    compare strings is int mystrcmp(const void
    s1, const void s2) return strcmp((char
    )s1, (char )s2)
  • In MP 2, you will write a function for integers.

23
Multithreaded I/O
24
Reading/Writing across Threads
  • Thread safe vs. expected output
  • The C documentation states that fopen(), fread(),
    fwrite(), and fclose() are thread-safe.
  • What does thread-safe mean?
  • Short Answer Concurrent calls wont cause a
    race-condition.
  • Results
  • If (t1) calls fread() before (t2), (t1) will
    receive data before (t2).
  • However, from execution to execution, the value
    returned to (t1) and (t2) will be different
    because the calls are made in a different order.

25
MP2 Overview
26
MP2 Overview
  • Assignment
  • Create a C program to read in N files
  • Launch N threads to process each file
    concurrently
  • Have each thread sort the specific file assigned
    to it
  • Write the sorted data to N different files
  • and count the total lines written as youre
    writing them.
  • and print that output to the console.

27
MP2 Overview
  • Grading
  • 80 60 Execution 20 Question
  • 15 Memory cleaned up? (Includes files!)
  • 5 Comments/readability
  • This MP is harder than MP1!
  • Dont wait until Monday to start on it!
  • Requires a good understanding of pointers,
    threads, and files all in one MP.

28
MP1 Initial Results
29
MP1 Initial Results
  • Autograder
  • Maximum Possible 80 of the 100 total points
  • Max 80/80
  • Min 12/80
  • Average 67.696/80 (aka 84.62)
  • Std. Dev. 18.34
  • Expect full MP1 grades next week.
Write a Comment
User Comments (0)
About PowerShow.com