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.

Slides:



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

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Threads Programming Thread creation Synchronization.
1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
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.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
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.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
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.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Pthreads: A shared memory programming model
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
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 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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Posix Threads Topics PthreadsReadings January 12, 2012 CSCE 713 Advanced Computer Architecture.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
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
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
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.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
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.
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
Threads in C Caryl Rahn.
Threads Threads.
Netprog: Threads Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread Programming.
Chapter 4: Threads.
Linux Processes & Threads
Concurrent Programming November 13, 2008
Concurrent Servers Topics Limitations of iterative servers
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
Operating System Concepts
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Concurrent Programming November 13, 2008
Presentation transcript:

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 a child, and child handles the client While this paradigm has served well for many years, there are problems with fork:

Problem with fork Fork is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Interprocess communication(IPC) is required to pass information between the parent and child after the fork. Information from the parent to the child before the fork is easy, since the child starts with a copy of the parent’s data space and with a copy of all the parent’s descriptors. But returning information from the child to the parent takes more work

threads Threads are sometime called lightweight process Thread creation can be times faster than process creation. All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of synchronization.

All threads within a process share Process instructions Most data Open files( e.g. descriptor) Signal handlers and signal dispositions Current working directory, and User and group IDs

Each thread has its own Thread ID Set of registers, including program counter and stack pointer Stack( for local variables and return address) Errno Signal mask and priority

Posix thread Posix thread also called Pthreads. Standarized in 1995 and most version of Unix will support them in the future. All thread function begin with pthread_ We will cover five basic thread functions and use these to code TCP client-server using thread instead of fork

pthread_create When a program is started by exec, a single thread is created, called the initial thread or main thread. Additional threads are created by pthread_create #include int pthread_create( int *tid, const pthread_attr_t *attr, void*(*func)(void *), void *arg); Return 0 if OK, positive Exxx value on error

int pthread_create( int *tid, const pthread_attr_t *attr, void*(*func)(void *), void *arg); Each thread is identified by a thread ID (tid) Each thread has attributes: –Priority, initial stack size and so on. When create a thread, they can be specified or default if pass a null pointer Specify a function to execute. The thread starts by calling function, terminates either explicitly( calling pthread_exit) or implicitly( letting function return). The address of the function Function takes one argument, a generic pointer(void *) and return a generic pointer (void *). This lets us pass one poniter(to anything we want) to the thread, and lets the thread return one pointer( to anything we want)

pthread_join function We can wait for a given thread to terminate by calling pthread_join. #include int pthread_join( int tid, void **status); return: 0 if OK, positive Exxx value on error -1 means wait for the first thread to terminate If the status pointer is nonnull, the return value from the thread( a pointer to some object) is stored in the location pointed to by status

pthread_self function Each thread has an ID that identifies it within a given process. A thread fetch this value for iteself using pthread_self #include int pthread_self(void); return thread ID of calling thead

pthread_detach A thread is either joinable( the default) or detached. When a joinable thread terminates, its thread ID and exit status retained until another thread call pthread_join. When a detached thread terminates, all its resources are released and we cannot wait for it to terminate. If one thread need to know when another thread terminates, it is best to leave the thread as joinable int pthread_detach(int tid); Return 0 if OK, positive Exxx value on error

pthread_exit function One way for a thread to terminate is to call pthread_exit. void pthread_exit(void *status); It the thread is not detached, its tid and exit status are retained for a later pthread_join by someother thread in the calling process. The pointer status must not point to an object that is local to the calling thread, since that object disappears when the thread terminates

Two other ways for a thread to terminate The function that started the thread( the third argument to pthread_create) can return. Since this function must be declared as returning a void pointer, that return value is the exit status of the thread If the main function of the process returns or if any thread calls exit, the process terminates, including any thread.

TCP echo server using thread static void *doit(void *); int main( int argc, char **argv) { int s, conn, tid; for(;;) { len = addrlen; conn = Accept(s, client, &len); pthread_create(&tid, NULL, &doit, (void *)conn); }

Doit() static void * doit( void *arg) // arg is conn { pthread_detach(pthread_self()); str_echo((int) arg); close((int)arg); return NULL; }

For(;;) { len = addrlen; iptr = malloc(sizeof(int)); *iptr = Accept( s, clinet, &len); pthread_create(&tid, NULL, &doit, iptr); }

Doit static void * doit( void *arg) { int conn; conn = *((int *)arg); free(arg); pthread_detach(pthread_self()); string_echo(conn); close(conn); retun (NULL); }