process outline
TRANSCRIPT
Process outlineWhat is process?
Process in memory, process state, process control block, threads.
Process scheduling: scheduling queue, schedulers.
Operations on processes: process creation, process termination.
Interprocess communication: shared memory, message passing.
Client-server systems: sockets, remote procedure calls, pipes.
Homework: Server/Client programming
ProcessA process (job) is an executable program which is submitted to OS.OS manages the process from its birth to death (error handling).
heap
stack
data
code
Function calls
Dynamic allocation
Global variables
Code Note:An executable file can producemultiple processes.Process in memory
Process state
ready running
new terminated
waiting
admittedinterrupt
exit
scheduler dispatchI/O or event waitI/O or event wait
completion
Process control block
process state
process number
register
memory limits
list of open files
âĶ
PCB
In brief, the PCB simply serves as the repository for any information that may vary from process to process (PCB is a data structure).
âĒ Process stateâĒ Program counterâĒ CPU registersâĒ CPU-scheduling informationâĒ Memory-management informationâĒ Accounting informationâĒ I/O status information
See also Figure 3.4 (p. 105)
Scheduling queuesJob queue: all processes in the system.Ready queue: processes that are residing in main memory.Device queue: processes that are waiting for I/O devices.
Once the process is allocated the CPU and is executing, one of several events could occur:
1. issue an I/O request and then be placed in the device queue2. create a new subprocess and wait for subprocessâs termination3. removed forcibly from the CPU, as a result of an interrupt,
and then put back in the ready queue.
See Figure 3.6 â 3.7 (p. 107 â 108)
Ready = in memory
SchedulersLong-term scheduler (Job scheduler), loading a process into memory.
execute less oftencontrol the degree of multi-programming (#processes in memory)select a good process mix of I/O-bound and CPU-bound processes
Short-term scheduler (CPU scheduler), allocate CPU to one of them.execute very often (every 100 milliseconds)if the scheduler takes 10 milliseconds to decide to execute a process
for 100 milliseconds, then 10/(100 + 10) = 9% overheads
Medium-term scheduler (swapping)remove processes from memory (if advantageous)when memory requirement overcommitted available memorysee Figure 3.8
Context switch save unloaded process and restore loaded process
P1 CPUscheduling
P2Save P1āļĨāļ mem
Context switchRestore P2
āđāļŠāđ cpu
Process CreationParent / child / pid (See Figure 3.9 Solaris system, âpsâ and âkillâ command)
Concurrent vs parallel execution
fork / exec (see Figure 3.10, 3.11, p. 113)
wait() â system call
WaitForSignalObject() â Win32 API
// compile: gcc fork.c -o fork// run: ./fork āļŦāļĢāļ·āļ ./a.out
#include <sys/types.h>#include <stdio.h>#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;
} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);
} else {wait(NULL);printf("Child Complete");
}return 0;
}
Parent
Child
Error! no child
āļāļīāļāļāļĢāļĢāļĄ #1 (āļ āļģāđāļ Linux āđāļāđāļģāļāļąāļāđ)āđāļāđāđāļāļĢāđāļāļĢāļĄāļāļĩāđāđāļŦāļŠāđāļĢāļģāđāļāļĨāļāļđ 10 āļāļ§āļą āļĨāļāļđāđāļāđāļĨāļ°āļāļ§āļąāļāļĄāļīāļāļāđāļāđāļāļ§āļģāļĄāļāļāļąāļāļĩāđ(āļāļŦāļđāļāļģāđāļāļāļąāđāļ)
Optional āđāļŦāļĨāđāļāļđāđāļāđāļĨāļ°āļāļ§āļąāļāđāļ§āļĒāļāļāļąāļāļģāļ§āļāđāđāļŦāļĨāļāđāļāļĨāļŦāđāļĨāļģāļĒ āđ āđāļāļĨāđ āļŦāļĢāļāļ·āđāļāļĨāđāđāļāļĩāļĒāļ§āđāļāđāļāđāļ§āļĒāļāļāļąāđāļŦāļĨāļāļāļāļĨāļ°āļŠāđāļ§āļ āļāļāļąāļāļāļķāđāļāđāļĨāļ°āļŠāđāļ§āļāļĨāļāđāļāđāļāđāļāļĨ āđāđāļĨāļ§āđāđāļŦāļāđāļāđāļĢāļ§āļĄāđāļāļĨāļāđāļąāļāđāļŦāļĄāļāđāļŦāđāđāļāđāļāđāļāļĨāđāđāļāļĩāļĒāļ§
āļāļ Linux āđāļāļāđ āļģāļŠāļąāđāļ man āđāļāđāļ man fork āļŦāļĢāļāļ· man waitāđāļāđāļ·āļāđāļĢāļĒāļĩāļāļ āļđdocument āļāļāļāļāļąāļāļāļāđāļāļąāđāļāļ āļģāļĐāļģ C
if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;
} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);
} else {wait(NULL);printf("Child Complete");
}
if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;
} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);
} else {wait(NULL);printf("Child Complete");
}
Parent process (pid = child)
Child process (pid = 0)
Exec āļāļāļ·āđāļŦāļĨāļ program āđāļŦāļĄāđāļĄāļēāļāļąāļ
āļāļĩāļāđ āļēāđāļāļāļāļĩ āđāđāļāļĢāļēāļ°āđāļāļĒāļĩāļāđāļāļĢāđāļāļĢāļĄ OS āļāđāļēāļĒfork āļāđāļāļāļ·āļāļēāļĢ duplicate āļŦāļāđāļ§āļĒāļāļ§āļēāļĄāļ āļē
Unix / Linux
āļāļ§āļąāļāļĒāđāļēāļ output āļāđāļĩāļāđāļāļāļāļēāļĢI'm the child number 0 (pid 25580)
I'm the child number 1 (pid 25581)
I'm the child number 2 (pid 25582)
I'm the child number 3 (pid 25583)
I'm the child number 4 (pid 25584)
I'm the child number 5 (pid 25585)
I'm the child number 6 (pid 25586)
I'm the child number 7 (pid 25587)
I'm the child number 8 (pid 25588)
I'm the child number 9 (pid 25589)
Parent terminates (pid 25579)
āđāļāļāļąāđāļāļāļāđāļāļą getpid()
āļŦāļĒāļāļļāļĢāļāļāļāļāļĢāļ°āļāļąāđāļāļĨāļāļđāļāļ§āļąāđāļāļāļ§āļąāļŦāļāļķāđāļāļ āļģāļāļģāļāđāļŠāļĢāļāđāđāļĄāđāđāļāđāļĢāļāļāļāļĨāļāļđāļāļāļļāļāļ§āļąāļ āļģāļāļģāļāđāļŠāļĢāļāđ āļāđāļģāļāđāļāļāļŠāļģāļĢāļāļĩ āđ
// compile: MS Visual C++ 2008 Express
#include "stdafx.h"#include "windows.hâint _tmain(int argc, _TCHAR *argv[]) {
STARTUPINFO si;PROCESS_INFORMATION pi;ZeroMemory(&si, sizeof(si));si.cb = sizeof(si);ZeroMemory(&pi, sizeof(pi));LPTSTR szCmdline = _tcsdup(TEXT("c:\\windows\\system32\\mspaint.exe"));if (!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
fprintf(stderr, "Create Process Failed");}WaitForSingleObject(pi.hProcess, INFINITE);printf("Child Complete");CloseHandle(pi.hProcess);CloseHandle(pi.hThread);return 0;
}
Windows
āļ āļģāļĐāļģ C āļāļāļāđāļāđāļĨāļ° OS āļāļĨāļģāđāļĒāļāļāļąāđāļāđāđāļĄāđāđāļāđāđāļŦāļĄāļ·āļāļāļāļāļąāļāļąāļāđāļŦāļĄāļ āđāļāļĒāđāļāļāļģāļ° C Libraryāđāļāđāļ Linux āļĄāļĩ fork() āđāļāđ Windows āđāļĄāđāļĄāļĩWindows āļĄāļĩ CreateProcess() āđāļāđ Linux āđāļĄāđāļĄāļĩ
Process Terminationexit() â system call
All the resources of the process â including physical and virtual memory, open files, and I/O buffers â are deallocated by the operating system.
TerminateProcess() â Win32 API
A parent may terminate the execution of one of its children for a variety of reasons, such as these:
1) the child has exceeded its usage of some of the resources that it has been allocated.
2) the task assigned to the child is no longer required.3) the parent is exiting, and the OS does not allow a child to continue
if its parent terminates (cascading termination).
Interprocess communicationThere are several reasons for providing an environment that allows process cooperation:
Information sharing shared file (lock/unlock)
Computation speed parallel sortingModularity web browser ( 1 web = 1 process)Convenience copy & paste, drag & drop
Cooperating processes require an âinterprocess communication (IPC).â
Shared Memory vs. Message Passing (Figure 3.13, p. 117)
Shared-memory systemsProducer-consumer problem (āļŠ āļēāļŦāļĢāļąāļ users āļāļąāđāļ§āđāļ āļŠāļāļāđāļāđ pipe āļāļ shell āļāđāļāļ)āđāļāđāļ cat /usr/share/dict/american-english | grep board | wc -lāđāļĢāļēāļŠāļēāļĄāļēāļĢāļāđāļāļĒāļĩāļāđāļāļĢāđāļāļĢāļĄ āđāļāđāļ āļ āļēāļĐāļē C āđāļŦāđāļŠāļĢāđāļēāļ pipe āđāļāļ·āļāđāđāļāļ·āļāđāļĄāļĢāļ°āļŦāļ§āđāļēāļ 2 processes āđāļāđ āļ āļēāļĐāļēāļāļ·āļāđāļāđāļāļāļāļđāļ§āđāļēāļĄāļĩ APIs āđāļŦāđāļŦāļĢāļ·āļāđāļĄāđ?
#define BUFFER_SIZE 10
typedef struct {âĶ âĶ âĶ
} item// circular queueitem buffer[BUFFER_SIZE];int in = 0; // tailint out = 0; // head
Shared memory (queue)
item nextProduced;
while (true) {while (((in + 1) % BUFFER_SIZE) == out) { }buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;
}
Producer process
item nextConsumed;
while (true) {while (in == out) { }nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;
}
Consumer process
Pipe and Redirect (one-way communication)
âĒ In C programming, there are 3 stardard I/O channels.Standard input (stdin) = keyboardStandard output (stdout) = monitorStandard error (stderr) = monitor
Pipe is used to connect stdout (one process) to stdin (another)For example, âls | moreâ or âtar -c myfolder | gzip -c > myfolder.tar.gzâ
âĒ Redirect is used to redirect input or outputFor example, âa.out > file.txtâ or âa.out >> file.txtâ (append).āļāļģāđāđāļŠāļāđāļ§āļąāđāļĨāļ āđāļāđāļ 1> āļŦāļĢāļāļ· 2> āļŦāļĄāļģāļĒāļāļķāļ āļāļĢāļāļāđāļāļģāđāļāļāļģāļ° stdout āđāļĨāļ° stderr
2>&1 āļŦāļĄāļģāļĒāļāļķāļ āđāļŦāđāđāļāļģ stderr āđāļāļāļāļāļāļĩāđ stdout āđāļāļ
âĒ < gets input from fileFor example, âtar -xvz < filename.tar.gzâ
âĒ << gets input from the shell file that is currently executing.āđāļĄāđāļāļāđāļĒāđāļāđāđāļāđ
fprintf(stdout, "%s", "Hello world\n");fprintf(stderr, "%s", âError message\n");
// compile: gcc shared_mem.c
#include <sys/types.h>#include <sys/shm.h>#include <sys/stat.h>#include <stdio.h>#include <unistd.h>
int main() {
int segment_id;char *shared_memory;
segment_id = shmget(IPC_PRIVATE, 4096, S_IRUSR | S_IWUSR);shared_memory = (char *)shmat(segment_id, NULL, 0);
sprintf(shared_memory, "Hi there!");
// continue on the next page
Adopted from Figure 3.16 (p. 125)
pid_t pid;
pid = fork();
if (pid == 0) { // child
printf("*%s\n", shared_memory);
} else { // parent
wait(NULL);shmdt(shared_memory); // detachshmctl(segment_id, IPC_RMID, NULL); // remove
}
return 0;}
Client-Server SystemsSockets, ports
See Figure 3.19 â 3.20 (p. 130 â 131)
sock.accept() is a blocking method (usually spawn a process or a thread).
127.0.0.1 is loop back.
Sockets is low-level method of communication (cannot impose a structure on the data). Think about passing a tree from one computer to another!
VMware can create a virtual network that connects VMs together (Team),suitable for developing and testing network applications (e.g. NFS).
āļāļīāļāļāļĢāļĢāļĄ #2 (āļ āļģāđāļ Linux āđāļāđāļģāļāļąāļāđ)āļāļāđāļ new process āļŦāļĢāļāļ· spawn threadāđāļāđāļ·āļāđāļŦāļāđāļĢāļāļīāļģāļĢ client āđāļāļŦāđāļĨāļģāļĒ āđ āļāļ§āļąāļāļĢāļāđāļĄāļāļāļą
āļ§āļ loop āļŠāđāļāļāļāđāļĄāļĨāļđāļ§āļāļąāđāļ§āļĨāļģāļāļąāļāļāļāļļāļāļąāđāļāđāļĢāļ·āđāļāļĒ āđ
āđāļāđāļ āļēāļĐāļēāļāļ·āļāđ āđ āļāđāđāļ āđāļ āļēāļāļ Linux āđāļāļēāđāļāđāļąāļ
Server
Client
Client
āļ§āļāļąāđāļ§āļĨāļģāļāļąāļāļāļāļļāļāļą
āļ§āļāļąāđāļ§āļĨāļģāļāļąāļāļāļāļļāļāļą
Print āļ§āļāļąāđāļ§āļĨāļģāļāļāļāļĄāļģāđāļĢāļ·āđāļāļĒ āđ
Print āļ§āļāļąāđāļ§āļĨāļģāļāļāļāļĄāļģāđāļĢāļ·āđāļāļĒ āđ
āļāļģāļāļāļ°āļŠāđāļāļāļāđāļĄāļĨāļđāļ§āļāļąāđāļ§āļĨāļģāļāļāļļ āđ 1 āļ§āļīāļāļģāļāļĩāđāļāđāļ·āļāđāļĄāđāđāļŦāđāđāļĢāļ§āđāđāļāļīāļāđāļ
āļāļāđāļāđāļāļĩāļĒāļāđāļāļĢāđāļāļĢāļĄāđāļŦ āđfork (āļŠāļĢāļģāđāļ process āđāļŦāļĄ)āđ āđāļāđāļ·āļāļāļĢāļāļīāļģāļĢ client āđāļāđāļĨāļ°āļāļ§āļąāļāļķāļāļāļ°āđāļŦāđāļāļ§āđāļģ client āļāļāļļāļāļ§āļąāđāļāļĢāđāļāļąāļāļĢāļāļīāļģāļĢāđāļāļāļĢāļāđāļĄ āđ āļāļāļą
Message PassingMP is useful in distributed environment, where the communicating processes may reside on different computers connected by a network (e.g. Cluster).
OS āļŠāļĢāļģāđāļ message queue āļ āļģāļŦāļāļģāđāļāđāļĩāļāļĨāļģāđāļĒ āđ āļāļāļą mail boxāđāļāļ 1. āļŦāļĨāļģāļĒ āđ process āļāļāđāļāļĢāļ·āđāļāļāđāļāļĩāļĒāļ§āļāļāļą āļĄāļĩ mail box āļāļāļļāđāļāļĩāļĒāļ§āđāļāļ 2. āļĄāļĩāļŦāļĨāļģāļĒ āđ āđāļāļĢāļ·āđāļāļ āđāļāđāļĨāļ°āđāļāļĢāļ·āđāļāļāļāļāđāļāļĄāļĩ mail box āļāļāļāļāļ§āļąāđāļāļ
Remote Procedure Calls
Programmerâsview
What actuallyhappen
āļāļĨāļģāđāļĒāđ virtualization āļŦāļĨāļāļāļ§āđāļģāļāļąāļāļāļāđāļāļąāļāļąāļāđ execute āļāļĒāļđāđāļāļāđāļāļĢāļ·āđāļāļāļāļĢāļāļīāđ
Why RPC
âĒ If no RPC, you have to use socket programming and you have to impose a structure on data! (Java uses serialization)
âĒ Only one copy of a function, updating a service at one place,i.e. banking application.
âĒ Distributing jobs to many servers. Each server is good at a particular job. Parallel programming!
âĒ Easy interprocess communication (no socket programming, no imposing data), i.e. e-exam (multiple choices).
Server
Client Client Client Client
string LoadQuestion(int i)string LoadChoice(int i, int j)bool Save(string id, int i, int j)
Faster coding compared tosocket programming orweb programming.
Todayâs RPCâĒ .NET RemotingâĒ Java RMI (remote method invocation)
āļāđāļāđāļŠāļĩāļĒāļāļāļ·āļāļąāļāđ server āđāļĨāļ° client āļāđāļāļāđāļāđāļ platform āđāļāļĒāļĩāļ§āļāļąāļ
Web ServicesâĒ āđāļāļ āđāļģāļĐāļģāļāđāļ·āļ āđ āļāļāļāļāļģāļ C# āļāđāđāļāļĩāļĒāļ web service āđāļāđâĒ āđāļĢāļĒāļĩāļāđāļ āđweb service āļāļģāļāļ āļģāļĐāļģāđāļāļāđāđāļāđâĒ āđāļāļāļąāđāļāļŦāļģāļāļāļāļāđāđāļ§āļĢāđāđāļāđāļ·āļāļ āļāļīāļāļāđāļģāļāļĢāļāļīāļģāļĢāļāļģāļāļ āļģāļāļ§āļāļāļĢāļąāļāđāļāļĩāđāđāļāļāđāļģāļ āļŦāļĢāļāļ·āļĢāļ°āļĒāļ°āđāļ§āļĨāļģāļāđāļĩāđāļāļāđāļģāļāđāļāđ
<%@WebService Language="C#" Class="MyService"%>
using System.Web.Services;
public class MyService : WebService {
[WebMethod]public int Add(int a, int b) {
return a + b;}
}
MyService.asmx(in a virtual directory)
Client(MyService.dll)
using System;using MyNameSpace;
class MyProg {
public static void Main(string [] Args) {
MyService S = new MyService();Console.WriteLine(S.Add(1,2));
}}
Process CommunicationāļāļąāļāļāļāļļāļāļąāđāļĢāļģāļĄāļāļąāļāļ°āļāļāđāļāđāļāļĩāļĒāļāđāļāļĢāđāļāļĢāļĄāļāļāļāđāļĢāļģāđāļŦ āđcommunicate āļāļāļąāđāļāļĢāđāļāļĢāļĄāļŠ āļģāđāļĢāļāđāļĢāļđāļāļāļ·āđāļāđ (āļāļķāļāđāļāļģāļāļāļ°āļāļĒāļđāļāđāļāđāļāļĢāļ·āđāļāļāđāļāļĩāļĒāļ§āļāļāļąāļŦāļĢāļāļ·āļāļāļĨāļ°āđāļāļĢāļ·āđāļāļāļāđāđāļ)āđ
.NET (C#) simplified code
ExcelApp = new Excel.ApplicationClass();ExcelBook = ExcelApp.Workbooks.Open(Filename, âĶ, âĶ, âĶ);ExcelSheet = ExcelBook.Worksheets.get_Item(1);
// writeExcelSheet.Cells[row,col] = âChatchawitâ;
// readname = ExcelSheet.Cells[row,col];
ExcelBook.Close(true, Missing.Value, Missing.Value);ExcelApp.Quit(); āļāļąāļāļāļļāļāļāļąāļŠāļēāļĄāļēāļĢāļāļāđāļēāļāđāļāļĩāļĒāļāđāļāļĨ āđExcel āđāļāđāļāļĢāļ āđ āđāļĨāļĒ
āļŠāļĢāļģāđāļ process āļĢāļāļą Excel
Homework
POSIX Message Passing (p. 148)
- write âcentral.câ and âexternal.câ- 1 copy of âcentralâ and 4 copies of âexternalâ can be started in any order.- begin from 2 processes and one-way communication.- then, two-way communication (2 mailboxes) and 5 processes (5 mailboxes).- use 5 terminals for the ease of debugging.- always check and clear message queues by âipcsâ and âiprm -q qid.â
central.c external.c
āđāļāļāļĒāđ
Central
size āļāļāļ message āļāđāļĩāļāļ°āļŠāđāļāđāļāļāļąāļāļāļāđāļāļą msgrcv āļāļāļąāđāļāđāļāļĩ āđ
āļāļāļąāļāļāļąāļ§āđāļģāļāļ§āļąāđāļĢāļāđāļ struct āļāļāđāļāđāļāđāļ message type(āļŦāļĢāļāļ·āļāļ°āļĄāļāļāļ§āđāļģāđāļāđāļ priority āļāđāđāļ)āđ
āđāļāļĨāļ string āđāļāđāļ int
āļāļāļļāļŦāļ āļĄāļđāļīāđāļĢāļīāļĄāđāļāļāđ
msqid āļāļ·āļ message queue id, 70 āļāļ·āļ queue name (users 2 āļāļāļāļģāļāļāļ°āđāļ āđqueue name āļ āļģāđāļāļāļąāđāļ)āđ0600 āļāļ·āļāļĒāļāļĄāđāļŦ āđprocess āļāļāļāļāļđāļŠāđāļĢāļģāđāļāļāđāļģāļāđāļāļĩāļĒāļ mailbox āđāļāđIPC_CREAT āļāļ·āļ āļāļģāđāļĒāļāļąāđāļĄāđāļĄāļĩāđāļŦāļŠāđāļĢāļģāđāļāđāļŦāļĄāđ
āļāļ message queue id āļāļāļ external 4 āļāļ§āļąwhile loop āđāļāđāļ·āļāļĢāļāđāļŦ āđexternal āļŠāļĢāļģāđāļ
blocking
0 āļāđāļģāļ first message āļāļ queue>0 āđāļāđāļ 2 āđāļāļģ first message āļāđāļĩ priority = 2<0 āđāļāđāļ -2 āđāļāļģ first message āļāđāļĩ priority <= 2