DOC PREVIEW
U of I CS 241 - Threads

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ThreadsCS241 Discussion Section Week 3September 17, 2009Outline MP1 Issues MP2 Overview pthreads Thread SafetyMP1 Issues• How to store history, when you are not supposed to assume the size of history?MP1 Issues• How to store history, when you are not supposed to assume the size of history?• Two solutions -• linked list • Array + realloc()• Remember to free memory when you exitMP Issues (Contd..)• How to determine whether the command run is a valid one or not? (i.e. ls –a vs ls –abcde)MP Issues (Contd..)• How to determine whether the command run is a valid one or not? (i.e. ls –a vs ls –abcde)• Check the return value of the exec system callMP Issues (Contd..)• Why does my PID change or get set to zero after executing the code?MP Issues (Contd..)• Why does my PID change or get set to zero after executing the code?• When you enter a command, the shell checks to see if it is a built-in command, and if so it executes it. If it is not a built-in the shell forks a new process in which to execute the command.• Therefore, if you run a built-in command in a forked process you will see the pid 0MP2 OverviewMP2 is an introduction to threadsGoal: sort an enormous data set in parallel using threadsFile I/OI/O in CMP2 requires you to read and write simple file in C.Two primary means of doing I/O in C:Through lightly-wrapped system calls•open(), close(), read(), write(), etcThrough C-language standards•fopen(), fclose(), fread(), fwrite(), etcI/O in COpening a file (Method #1):fopen(const char *filename, const char *mode);filename: path to file to openmode: 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 appendI/O in COpening a file (Method #2):open(const char *filename, int flags, int mode);filename: path to file to openflags: 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 doesn’t 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.Opening Files in CReturn value of opening a file:Having called open() or fopen(), they will both create an entry in the OS’s 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 *).Reading Files in CTwo 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?Reading Files in CReading more advancely…fscanf(FILE *stream, const char *format, …);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().Writing Files in CWriting 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 writing to files (works like printf()).Closing Files in CAlways 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.Threads vs. ProcessesWhat are the main differences?Memory / address spaceScheduling (concurrent execution)OverheadWhen should we use a process?A thread?envstack1freeheapstaticcodestack2 stack3© Angrave 2008PropertyProcesses created with forkThreads of a process Ordinary function callsvariablesget copies of all variablesshare global variablesshare global variablesIDs get new process IDsshare the same process ID but have unique thread IDshare the same process ID (and thread ID)CommunicationMust explicitly communicate, e.g.pipesor use small integer return valueMay communicate with return valueor shared variablesif done carefullyMay communicate with return valueor shared variables(don't have to be careful)Parallelism (one CPU)Concurrent ConcurrentSequentialParallelism (multiple CPUs)May be executed simultaneouslyKernel threads may be executed simultaneouslySequentialThreads versus ProcessesCopyright ©: Nahrstedt, Angrave, Abdelzaher21Threads vs. ProcessesEach thread execute separatelyThreads in the same process share resourcesNo protection among threads!!POSIX Threads (Pthreads)Standardized, portable thread APITo use POSIX thread functions#include <pthread.h>gcc –o main main.c -lpthreadCopyright ©: Nahrstedt, Angrave, Abdelzaher23Creating a ThreadWhen a new thread is created it runs concurrently with the creating process.When creating a thread you indicate which function the thread should execute. Copyright ©: Nahrstedt, Angrave, Abdelzaher24Creating a thread with pthreadA thread is created withint pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); The creating process (or thread) must provide a location for storage of the thread id. The third parameter is just the name of the function for the thread to run.The last parameter is a pointer to the arguments.Problem 1Hello World! (thread edition)We’ll create two threads and one will print out“Hello”, and


View Full Document

U of I CS 241 - Threads

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?