6,
Process concept A program in execution Program code PCB (process control block) Program counter, registers, etc. Stack Heap Data section => global variable
Process in memory
Process state New Running Waiting Ready Terminated Zombie ( defunct) :, wait(),
Process state transition
PCB Process table, task control block Process state - running, waiting, etc. Program counter CPU registers - Scheduling - Memory - Accouting information - CPU, I/O status - ( open ),
CPU switch from process to process
Threads Single-threaded vs. multi-threaded
Process scheduling Long-term scheduling & short-term scheduling (preemption) Job queue - Ready queue (run queue) - Wait queue (device queue) - I/O
Ready queue and various I/O device queues
Representation of process scheduling ready queue Wait queue
Scheduler Long-term scheduler (or job scheduler) Ready queue Short-term scheduler (or CPU scheduler) CPU ready queue, STS I/O bound process, CPU-bound process LTS ready queue (CPU I/O)
Context switch CPU PCB Context switch overhead switch,,
Process creation => fork ) init => => fork pid X (concurrently) (wait)
Process tree in Linux init pid = 1 login pid = 8415 kthreadd pid = 2 sshd pid = 3028 bash pid = 8416 khelper pid = 6 pdflush pid = 200 sshd pid = 3610 ps pid = 9298 emacs pid = 9204 tcsch pid = 4005
Process creation fork () UNIX exec( ) fork() => exec( )
Process creation fork() #include <unistd.h> #include <stdio.h> int main() { pid_t pid; pid = fork(); if (pid > 0) printf ( I am the parent of pid=%d!\n, pid); else if (!pid) printf( I am the child!\n ); else if (pid == -1) printf( Error\n ); }
Process creation fork() => exec family #include <unistd.h> #include <stdio.h> int main() { pid_t pid; pid = fork(); if (pid == -1) printf( Error\n ); if (!pid) { const char *args[] = { vi, NULL}; int ret; ret = execvp ( vi, args); if (ret == -1) { printf( Error\n ); exit (EXIT_FAILURE); } } }
Process termination exit () wait() wait() zombile state OS abort()
Wait () #include <unistd.h> #include <string.h> #include <stdio.h> int main() { int pid; pid = fork(); if (pid == 0) { sleep(5) exit(1); } else if (pid > 0) { getchar(); } } Zombie process ps -ef grep defunct grep -v grep ps -ef grep defunct awk {print $3} xargs kill -9
Wait () #include <unistd.h> #include <string.h> #include <stdio.h> int main() { int pid; int status; pid = fork(); if (pid == 0) { sleep(5) exit(1); } else if (pid > 0) { getchar(); pid = wait(&status); } } Zombie process
Interprocess communication (IPC) Cooperating processes Information sharing, computation speed up, and synchronization Shared memory & message passing process A process B process A shared memory process B message queue m 0 m 1 m 2... m 3 kernel (a) m n kernel (b)
IPC Shared memory Producer-consumer problem Message passing IPC Pipe, Unix Domain Socket, Message Queue, Mail Box, Mail Slot
IPC - pipe #include <unistd.h> int pipe (int filedes[2]); One pipe for reading and the other for writing => half-duplex * Named pipe (stream pipe) => full-duplex parent fork child user kernel fd[0] fd[1] fd[0] fd[1] pipe
IPC - pipe #include <unistd.h> #include <stdlib.h> #include <stdio.h> int main() { int n, fd[2]; pid_t pid; char line[100]; if (pipe(fd) < 0) { perror( pipe error : ); exit(0); } if ((pid = fork()) < 0) { perror( fork error: ); exit(0); } else if (pid > 0) { close(fd[0]); write(fd[1], hello world\n, 12); } else { close(fd[1]); n = read(fd[0], line, 100); printf( Received from parent %s\n, line); } exit(0); }
Synchronization Synchronization for IPC Blocking IPC (synchronous) receive send block receive receive Non-blocking IPC (asynchronous) send receive ( ) null
Remote procedure call Linux rpcgen JAVA RMI (remote method invocation) SOAP (simple object access protocol) Python Pyro Microsoft DCOM
HW #1 fork() => exec() : 1, wait(),, 0 100 0+1+2+3+ +100 : Hello, system call : Due date: 18 April