C++ and Processes - Lecture Notes | CMPSCI 377, Study notes of Operating Systems

Material Type: Notes; Professor: Corner; Class: Operating Systems; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-ju6
koofers-user-ju6 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPSCI 377 Operating Systems Spring 2009
Lecture 4: February 5
Lecturer: Mark Corner Scribes: Bruno Silva,Jim Partan
4.1 C++ and Processes
As with Lectures 1 and 2, most of the 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.
4.2 Finishing up the C++ needed for CS377
4.2.1 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 to C++. 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 (e.g. insert(), 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 queue-of-queues, with larger datasets, you’d probably want to
use a queue-of-pointers-to-queues instead, such as
queue<queue<int>*> my_queue;
rather than
queue<queue<int> > my_queue;
(note the space in the second declaration, because >> is a C++ operator).
As a couple of very quick STL examples, consider
queue<int *> myqueue;
int *ptr = new int;
myqueue.push(ptr);
and
map<int,char> mymap;
mymap[10]=’a’;
4-1
pf3

Partial preview of the text

Download C++ and Processes - Lecture Notes | CMPSCI 377 and more Study notes Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Spring 2009

Lecture 4: February 5

Lecturer: Mark Corner Scribes: Bruno Silva,Jim Partan

4.1 C++ and Processes

As with Lectures 1 and 2, most of the 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.

4.2 Finishing up the C++ needed for CS

4.2.1 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 to C++. 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 (e.g. insert(), 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 queue-of-queues, with larger datasets, you’d probably want to use a queue-of-pointers-to-queues instead, such as

queue<queue*> my_queue;

rather than

queue<queue > my_queue;

(note the space in the second declaration, because >> is a C++ operator).

As a couple of very quick STL examples, consider

queue<int *> myqueue; int *ptr = new int; myqueue.push(ptr);

and

map<int,char> mymap; mymap[10]=’a’;

4-2 Lecture 4: February 5

The second example is a map indexed by ints, storing chars.

4.2.2 C/C++ main(int argc, char *argv[])

Command-line 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 2.7”:

#include #include <stdlib.h> using namespace std;

int main(int argc, char *argv[]) { char *progname, *filename; int value1; float value2;

if (argc != 4) { cerr << "usage: " << argv[0] << " file.txt int float" << endl; exit(1); }

progname = argv[0]; // argv[0] is the executable’s name itself filename = argv[1]; value1 = atoi(argv[2]); // convert C-string to int with atoi() value2 = atof(argv[3]); // convert C-string to float with atof()

cout << progname << " " << filename << " " << value1 << " " << value2 <<endl; exit(0); }

4.3 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 multi-threaded 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.