Pthreads void *sleeping(void *); /* thread routine */

Slides:



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

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
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.
Pthread II. Outline Join Mutex Variables Condition Variables.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
Programming with POSIX* Threads Intel Software College.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Pthreads.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
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.
Threads Dr. Yingwu Zhu. Threaded Applications Web browsers: display and data retrieval Web servers Many others.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
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.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Tutorial 4. In this tutorial session we’ll see Threads.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.
Fork VS. Threads Lab 05.
סמפורים.
Threads in C Caryl Rahn.
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Linux Processes & Threads
Concurrency I: Threads April 10, 2001
Realizing Concurrency using Posix Threads (pthreads)
Concurrent Programming
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
BOOM! count is [ ], should be
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
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.
Lecture 19: Threads CS April 3, 2019.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Pthreads void *sleeping(void *); /* thread routine */ #include <pthread.h> pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 4; pthread_create(&tid, NULL, sleeping, &time) ; } void *sleeping((void *) sleep_time) { int *ptr ; ptr = (int *) sleep_time ; printf(“Getting ready to sleepfor %d secs\n”, *ptr) ; sleep (*ptr) ; printf(“Hello I’m BACK!!\n”) ;

Pthreads void *sleeping(void *); /* thread routine */ #include <pthread.h> pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 4; pthread_create(&tid, NULL, sleeping, &time) ; } void *sleeping((void *) sleep_time) { int *ptr ; ptr = (int *) sleep_time ; //type cast printf(“Getting ready to sleepfor %d secs\n”, *ptr) ; //dereferencing sleep (*ptr) ; printf(“Hello I’m BACK!!\n”) ;

Problem: When main exits, all threads are terminated. Solution: pthread_join(tid, NULL) //this waits for a //particular thread with id tid. pthread_join(NULL) //waits for any thread.

#include <pthread.h> pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 4; pthread_create(&tid, NULL, sleeping, &time) ; pthread_join(tid, NULL) ; } void *sleeping((void *) sleep_time) { int *ptr ; ptr = (int *) sleep_time ; //type cast printf(“Getting ready to sleepfor %d secs\n”, *ptr) ; //dereferencing sleep (*ptr) ; printf(“Hello I’m BACK!!\n”) ;

pthread_t tids[10] ; main() { for (j = 0 ; j < 10 ; j++) pthread_create(&tids[i], NULL, sleeping, &time) ; for(j = 0 ; j < 10 ; j++) pthread_join(tid[i], NULL) ; }

All theads share: global variables file descriptors static variables within creating function. Local variables are private to each thread. void *sleeping(int * sleep_time) { static int tootoo = 10 ; //shared int june ; //private printf(“thread sleeping for %d secs\n”, *sleep_time) ; tootoo++ ; //will have final value of 20. }