Concurrent Processing in Client-Server Software Prof. Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN MCSE Lab, NTUT, TAIWAN 1
Introduction Concurrency in Networks Concurrency in Servers Terminology and Concepts Example for Concurrent Process Creation Executing New Code Context Switching and Protocol Software Design Concurrency and Asynchronous I/O NTUT, TAIWAN 2
Concurrency in Networks Concurrency provides much of the power behind clientserver interaction makes the software difficult to design and build NTUT, TAIWAN 3
Concurrency in Network Concurrency = simultaneous computing virtually or practically Time sharing scheme Multiprocessing Concurrent processing is fundamental to distributed computing and occurs in Network A computer system NTUT, TAIWAN 4
Single Network A C D B NTUT, TAIWAN 5
Computer System A time sharing system ftp rlogin http User 1 User 2 User 3 NTUT, TAIWAN 6
Concurrency in Networks Besides, the set of all clients on a set of machines can execute concurrently c3 Internet c2 c4 c5 c1 NTUT, TAIWAN 7
Concurrency in Networks Client software does not require any special attention or effort on the part of the programmer to make it usable concurrently i.e. each individual client operates much like any conventional program NTUT, TAIWAN 8
Concurrency in Servers In contrast to a client, concurrency within a server requires considerable effort c3 Internet server c4 c5 c1 NTUT, TAIWAN 9
Terminology and Concepts Process (task, or job) Threads Local and global variables Procedure calls NTUT, TAIWAN 10
Process Fundamental unit of computation An address space + at least one thread of execution Information associated with a process instruction pointer (associated with a thread) identity of the user compiled program memory locations for the program text and data NTUT, TAIWAN 11
Process The process concept includes only the active execution of a computation, not the static version of the program In a uniprocessor architecture, the single CPU can only execute one thread at any time instance To achieve concurrency by switching the CPU among all threads rapidly Concurrent execution NTUT, TAIWAN 12
Process On a multiprocessor machine, all CPUs can execute simultaneously Keep in mind, the key issue is transparency NTUT, TAIWAN 13
Threads Threads of execution (lightweight processes) Using instruction pointer Having a copy of local variables Independent from the other threads Associated with a single process Sharing all the global variables Sharing all the resources allocated to the process, including file descriptors NTUT, TAIWAN 14
Threads A concurrent program can be written either to create separate processes or to create multiple threads within a single process NTUT, TAIWAN 15
Programs vs. Threads When multiple threads execute a piece of code concurrently, each thread has its own independent copy of the local variables associated with the code Each process receives a separate copy of global variables; if multiple threads execute within a single process, they each have a copy of local variables, but all share the process copy of the global variables NTUT, TAIWAN 16
Procedure calls In a procedure-oriented language, like C, executed code can contain calls to subprograms (procedures or functions) Using a stack to handle procedure calls When multiple threads execute a piece of code concurrently, each has its own run-time stack of procedure activation records NTUT, TAIWAN 17
Example on Concurrency Process Run sum.c on-line System function fork Divides the running program into two identical processes Starts a thread executing the new process in the same place of the code Inserting fork into sum.c and run sum_fork.c NTUT, TAIWAN 18
Example NTUT, TAIWAN 19
Example on Concurrency Process /* sum.c - A conventional C programe that sums integers from 1 to 5 */ #include <stdlib.h> #include <stdio.h> int sum; /* sum is a global variable */ main(){ int i; /* i is a local variable */ sum=0; for (i=1;i<=5; i++){ /* iterate i from 1 to 5 */ printf("the value of i is %d\n", i); fflush(stdout); /* flush the buffer */ sum += i; } printf("the sum is %d\n", sum); exit(0); /* terminate the program */ } NTUT, TAIWAN 20
Example on Concurrency Process /* sum.c - A conventional C programe that sums integers from 1 to 5 */ #include <stdlib.h> #include <stdio.h> int sum; /* sum is a global variable */ main(){ int i; /* i is a local variable */ sum=0; fork(); /* create a new process */ for (i=1;i<=5; i++){ /* iterate i from 1 to 5 */ printf("the value of i is %d\n", i); fflush(stdout); /* flush the buffer */ sum += i; } printf("the sum is %d\n", sum); exit(0); /* terminate the program */ } NTUT, TAIWAN 21
Concurrency Process In a uniprocess computer, OS allocates the available CPU power for a short time to each threads in round-robin fashion Timesliceing mechanism Allocate the available processing equally among all available threads NTUT, TAIWAN 22
Single-Threaded Process Assumption fork() not duplicate all of the threads create a copy of a running process and the new process contains exactly one thread The newly created process by fork() is said to be a singly-threaded process Although a process can contain multiple threads, the newly created process that results from a call to fork is singly-threaded. NTUT, TAIWAN 23
Processes Diverge New process generated by fork Not absolutely identical to the original process fork() returns a value to the original process and the new process 0 to the new process A positive integer to the original process to identify the newly created process The value returned to the original is called a process identifier or process id (pid) NTUT, TAIWAN 24
Processes Diverge #include <stdlib.h> #include <stdio.h> main(){ int pid; /* child process id */ pid = fork(); if (pid!=0){ /* parent process */ printf("the parent process prints this.\n"); } else { printf("the child process prints this.\n"); } exit(0); /* terminate the program */ } NTUT, TAIWAN 25
NTUT, TAIWAN 26
Executing New Code UNIX provide s a mechanism that allows any process to execute an independent, separatecompiled program The mechanism consists of a system call, execve, that replace the code that the currently executing process runs with the code from the new program Way to new process that executes the object code from a file, a process call fork and execve execve is very important for server to handle diverse services NTUT, TAIWAN 27
Context Switching when the OS switch one thread to another, a context switch occurs Context switching between threads in a process requires less overhead than switching between a thread in one process and a thread in another NTUT, TAIWAN 28
Context Switching Context switching needs CPU; therefore, is counted as an overhead to support concurrent processing To avoid unnecessary overhead, protocol software should be designed to minimize the context switching NTUT, TAIWAN 29
Concurrency and Asynchronous I/O OS allows a single application to initiate and control concurrent I/O System call select, is such an operation which is used to find out which I/O device becomes ready first. NTUT, TAIWAN 30