Threads Lab 04 1. 2 اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Threads Programming Thread creation Synchronization.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Thread Synchronization with Semaphores
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
CS431-cotter1 Linux Programming, Processes, and Threads Man pages (fork, clone, execve, pthread_create) The Linux Programming Interface – Kerrisk, No Starch,
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Fork VS. Threads Lab 05.
Threads in C Caryl Rahn.
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Thread Programming.
Chapter 4: Threads.
Linux Processes & Threads
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
Operating System Concepts
Chien-Chung Shen CIS/UD
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Presentation transcript:

Threads Lab 04 1

2 اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً

Lab Objective To practice using threads. 3

THREADS VS. PROCESSES 4

5 Global Variables Code Stack Single Process 123

6 Global Variables Code Stack Multiple Processes using fork() 3 Global Variables Code Stack 2 Global Variables Code Stack 1

Samar Alsaleh OS - CS242 - Spring Global Variables Code Stack Single Process with Multiple Threads 1 2 3

Thread Creation When a program is started, a single thread is created, called the initial thread or main thread. Additional threads are created by: Returns 0 if OK, positive Exxx value on error tid  The newly-created thread ID attr  the new thread attributes, use NULL to get system default func  Pointer to a function to execute when the thread starts arg  Pointer to func argument (multiple arguments can be passed by creating a structure and passing the address of the structure) 8 int pthread_create (pthread_t * tid, const pthread_attr_t *attr, void *(*func) (void*), void *arg); int pthread_create (pthread_t * tid, const pthread_attr_t *attr, void *(*func) (void*), void *arg);

Thread Management Each thread has a unique ID, a thread can find out its ID by calling: A tread can be terminated by calling: The main thread can wait for a thread to terminate by calling: – Note: with pthread_join we must specify the tid of the thread. 9 pthread_t pthread_self(); void pthread_exit(); int pthread_join(pthread_t tid, void **status);

Summary for Thread Functions Samar Alsaleh OS - CS242 - Spring int pthread_create(pthread_t * tid,const pthread_attr_t *attr, void *(*func) (void*),void *arg); pthread_t pthread_self(); void pthread_exit(); int pthread_join(pthread_t tid, void **status);

Very Important Note Use the option -pthread or -lpthread with the compilation command to enable the support of multithreading with the pthread library. Your command line should look something like this: 11 $ g++ lab4.C –o lab4 -pthread

Practice In the following C++ program, the main process creates two threads of the function doit The function has a loop to increment the global variable counter by 1 for 10 times. Within every iteration of the loop, the function prints out the ID of the thread that is running and the current value of counter Write, compile and run the program in Linux then answer the questions in the check-off section. 12

Samar Alsaleh OS - CS242 - Spring #include #include "pthread.h“ using namespace std; //Output a new line #define NLOOP 10 //Constant value int counter = 0; void * doit(void *); int main() { pthread_t tidA, tidB; pthread_create(&tidA, NULL, doit, NULL); pthread_create(&tidB, NULL, doit, NULL); pthread_join(tidA, NULL); pthread_join(tidB, NULL); exit(0); }//end main void * doit(void *vprt) { int i, val; for( i = 0; i<NLOOP; i++) { val = counter; cout<<"Thread = "<<pthread_self(); cout<<" Counter = "<<counter<<endl; sleep(2); counter = val+1; } return (NULL); } //end doit function Each thread increments the global variable counter by 1 for 10 times Create two threads to run the function doit Wait for both threads to terminate Global variable incremented by the threads

Check Off 1)Why the final value of counter is 10 and not 20? 2)Run the program again. while it’ running, use the command ps – all in a separate window. Write down the PID of the process(es) related to the program. Explain the difference between this program and the program you had in the previous lab in terms of number of PIDs. 3)modify the loop in the doit function to be as follows: Recompile the program and run it. what is the maximum value of counter ? 4)Briefly explain the behavior of the program based on the results you obtain from the previous questions. 14 for( i = 0; i<NLOOP; i++) { cout<<"Thread = "<<pthread_self(); cout<<" Counter = "<<dec<<counter<<endl; counter++;

Output Samar Alsaleh OS - CS242 - Spring

Q3 – Output(With sleep(2)) Samar Alsaleh OS - CS242 - Spring

Q3 – Output(Without sleep(2)) Samar Alsaleh OS - CS242 - Spring

??? ANY QUESTIONS ??? 18