CS 160 Lab 1 lecture notes

1, What our lab will cover?

 

as you know, Dr Fleisch will talk about the concepts of concurrent system and thread. what he covers is a whole view of what is a concurrent system is, why we should have it, how it works, what is threads , why the threads should be synchronized, etc.

what you learned from his course is a conceptual view. He will not explain to you how to programming with threads. how to write beautiful and correct programs using threads is what i will discuss here in these lab sessions. We will discuss the details in thread programming, such as creating threads, detaching threads, using mutex, condition variables etc. And i can also explain much of the course materials if you have problem unerstanding it.

2, how these labs are conducted?

 

Usually, i will give out the outline of next lab session one or two days before lab time. On each lab, i will discuss the stuff i list in the outlines. then you can ask me questions about the course or assignments. I am not sure i will give out quiz or not. (I don't know how many of you would like to take quiz. :)?

3, First lab

 

summary: this lab session , i will go over the following items:

       C++ Basics

       void pointer vs null pointer

       Why Threads? 

(examples of concurrent systems, applications, programs,`)

 

       difference between sequential program and threaded programs

       some thread program examples

       examin the examples

 

Objective: can give out some answers to the following questions:

·          what is a thread?

·          Why we should have it?

·          What problems can threads have?

·          How to solve one of these problems?

·

      

4. C++ Basics

what you need to know is

       a. edit, compile, link and debug your source code. – gdb will be aware of your threads, however, it will serialize them ! as you can see it in the later cases.

       b. C++ classes. ( attributes and member functions.)

         class Obj {

              public:

                     int value;

              public:

void showvalue();

         }

         Obj a;

5. void pointer vs null pointer.

       void *p;

       //p can point to any data type, int or char or double etc.

       see examples

 

 

 

6. why we use thread?

1.     to achieve parallelism, To exploit the multi-processor architecture.

2.     to achieve concurrency – For single processor, you can do computation and while I/O is busy working at the same time. This would benefit you much,

3.     Even let you think more about your program? Is there any code or task I can have it run simultaneously? Once you decided you could have sth working simultaneously, thread is a ideal tool to realize your plan.

 

 

7, How about process? Is process enough?

              1), Yes, process can provide we some of concurrency! We can issue a command under shell, then shell will start a process to execute the command you give to it. And shell waits for your next command.

              2) Thread is more fine-grained. It is the active identity within a single process. Although the number of threads a process can hold is limited by system.

              3) They share program code segment, data segment , then can communicate with each other better than processes.

              4) easy to share data,

              5) lower cost for CPU to schedule them because they are very little 

 

 

8. Differences between sequential programs and concurrent programs

       1) thread support from OS -- Solaris, HP-UX, System V, Linux

the POSIX thread standard.

       we have to have the thread library.

       2) complicated thread sync requirements

              we have to consider carefully about the synchronization. Because you probably have hundreds of threads working on a same database. You have to be careful of their operations on data

       3) hard to debug

              you can use gdb as you deal with the multi-threaded programs as you deal with single-threaded programs. But GDB does not help much on multi-threads.

       4) ..

9 To thread or not to thread?

a.     Threads do not necessarily provide the best solution to every programming problem.

b.     They are not always easily to use.

c.     They don’t always result in better performance

   Threads is good for:

1)     Perform extensive computations that can be parallelized or decomposed into multiple threads,, and which is intended to run on multi-processors.

2)     Perform substantial I/O which can be overlapped to impove throughput --- many threads can wait for different I/O requests at the same time. E.g. “ Distributed server applications” respond to multiple client. ( a ftp client request a bunch of files that are located on different disks from a ftp server, many threads searching and transmitting different file at the same time.

 

Lets see how to create and sync threads with the following examples.

 

10. Example 1 , 2, 3

    you can download the examples and makefile to your home directory. Then make it and run them.

    Discuss the results

Example 1: a sentence of garbage.

Example 2: a number that might be 20,000 or 20,004 or 19,993 or sth else. Why?

Example 3: you will see two sentences

 

11. why ?

       Let’s examine the program together.

       You see, the threads interleave with each other.  If we have no control over their synchronization, the unexpected results will appear. If we use some means of control (such as in example 3, you will get two meaningful sentences)

 

       Explain the parameters of the pthread_create() and pthread_join() and pthread_mutex_lock & unlock.

       pthread_create(pthread_t *thread_id, const pthread_attr_t *attr, void *(*start_function)(void *), void *arg);

       pthread_join(pthread_t *thread_id);

 

I will explain more about Pthread functions in later sessions.

        

12, sth about make file , not a big problem, I will talk about it in later sessions.

 

13, Homework,

 

       write a program that can set multiple alarms by user, when a alarm’s time is up, it will beep and then print out a message.

e.g.

       ALARM> set 10 “wake up, you are bankrupted !”

       ALARM> set 20 “My car died.”

       ALARM>