Class Note for CMPSCI 377 at UMass(9)
Class Note for CMPSCI 377 at UMass(9)
Popular in Course
Popular in Department
This 3 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Massachusetts taught by a professor in Fall. Since its upload, it has received 15 views.
Reviews for Class Note for CMPSCI 377 at UMass(9)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
CMPSCI 377 Operating Systems Spring 2009 Lecture 4 February 5 Lecturer Mark Carrier Scribes Bruno Silva Jim Partzm 41 C and Processes As with Lectures l and 2 most ofthe details of the C portion of this lecture is in the 377 Student Guide to C which has links on the course website Here are a few additional notes 42 Finishing up the C needed for C8377 421 C Standard Template Library STL The STL details are described in many places online see the CS377 webpage for some links and there s a very quick introduction in Section 14 of the 377 Student Guide t0 Ci Here are just a few additional notes Function calls in C Pass by value making a copy of the argument If you need to modify the argument or if making a copy of a large object would be consume too much time and memory then passing by reference a pointer is often preferable Similarly many of the STL methods eigi insert 0 etc are set up to pass potentially large objects by value rather than by reference For this reason one might want to create objects composed of pointers rather than objects For example if you wanted a queueof queues with larger datasets you d probably want to use a queueof pointerstoqueues instead such as queueltqueueltintgtgt myqueue rather than queueltqueueltintgt gt myqueue note the space in the second declaration because gtgt is a C operatori As a couple of very quick STL examples consider queueltint gt myqueue int ptr new int myqueue pushptr and mapltint chargt mymap mymap 10 a 4 1 42 Lecture 4 February 5 The second example is a map indexed by ints storing chars 422 CC mainint argc Char argv Commandline parameters are passed into programs using the arguments of main Here s a quick example for a program called by typing progname file txt 1 27 include ltiostreamgt include ltstdlibhgt using namespace std int mainint argc char argv char progname filename int valuel float value2 if argc 4 C cerr ltlt quotusage quot ltlt arngO ltlt quot filetxt int floatquot ltlt endl exit1 progname arngO argv0 is the executable s name itself filename arngl valuel atoiarngZJ convert Cstring to int with atoi value2 atof argv3 convert Cstring to float with atof cout ltlt progname ltlt quot quot ltlt filename ltlt quot quot ltlt valuel ltlt quot quot ltlt value2 ltltendl exit0 43 Processes and Threads Processes and threads each have their place in multi programming generally to hide latency and to maximize CPU utilization With the continuing spread of multi core processors in personal computers threads are becoming more and more important every day There are now threads in almost every type of application including client ap plications not just server applications Soon there are likely to be multi core processors even on cellphones and there will be threaded applications on cellphones These multiple cores make it possible to run several different lines of processing at the same time allowing the computer to run much faster than usual because of this however programmers must now explicitly make use of multithreaded programming Unfortunately parallel programming is very confusing and error prone In general parallel programming can be implemented either by using several concurrent processes or by using threads While processes have each their own address space separate program counters heaps stacks etc threads share their address spaces the programmer can use either of these to obtain concurrency Lecture 4 February 5 43 Besides maximizing CPU utilization the use of parallel programming also helps to hide latency eg waiting for the disk while using the CPU and to handle multiple asynchronous events As a rough analogy different processes are like different housing subdivisions quite separate and protected from each other and with communication possible but relatively difficult In this analogy different threads within the same process are like roommates communication and sharing is easy but there7s much less protection from each others mistakes 431 Unix process API The two most important function calls to use when programming with several processes are fork and exec o fork creates a copy of current process It gives a different return value to each process and works based on Copy On Write o exec replaces a process with an executable The Windows CreateProcess taking ten arguments is analogous Notice that fork implies that each process descends from another process In fact in Unix everything descends from a single process called init basically init forks a process and then replaces its code77 with say the code of bash using exec Example of how to use fork include ltunistdhgt include ltsyswaithgt include ltstdiohgt int parentid getpid char programname1024l getsprogramname reads the name of program we want to start int cid fork if cid0 i m the child execlpprogramname programname 0 loads the program and runs it printfquotif the above worked this line will never be reachednquot else i m the parent sleep 1 give my child time to start waitpidcid O 0 waits for my child to terminate printquotprogram Ks finishednquot programname
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'