200 likes | 340 Views
Operating Systems Chapter 2. Process. Process Concept. Process – a program in execution UNIX’s process definition: “an address space with one or more threads executing within that address space, and the required system resources for those threads.” A process includes: program counter
E N D
Operating SystemsChapter 2 Process
Process Concept • Process – a program in execution • UNIX’s process definition:“an address space with one or more threads executing within that address space, and the required system resources for those threads.” • A process includes: • program counter • stack • data section
Process State • As a process executes, it changes state • new: The process is being created. • running: Instructions are being executed. • waiting: The process is waiting for some event to occur. • ready: The process is waiting to be assigned to a process. • terminated: The process has finished execution.
Process Control Block (PCB) • Information associated with each process. • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information
Context Switch • When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. • Context-switch time is overhead; the system does no useful work while switching.
Some of the Process Scheduling Queues • Job queue – set of all processes in the system. • Ready queue – set of all processes residing in main memory,ready and waiting to execute. • Device queues – set of processes waiting for an I/O device. Process migration between the various queues.
Schedulers • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue. • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
Process Creation • Parent process creates children processes, which, in turn create other processes, forming a tree of processes. • Execution • Parent and children execute concurrently. • Parent waits until children terminate.
Case Study: UNIX • exec - Replacing a Process image int execve(const char* path,char* const argv[],char* const envp[]) path – pionter the the file name + path of the new process argv – pointers array (ends with NULL) to the arguments of the new process envp – pointers array (end with NULL) to the environment varibles used by the new process #include <unistd.h> #include <stdio.h> int main() { const char *argv[] ={“ps”,”-ax”,0}; printf("Running ps with execlp\n"); execve("/bin/ps", argv, 0); printf("Done.\n"); exit(0); }
fork - Duplicating a Process Image pid_t fork(void) Initial process #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main() { pid_t pid; char *message; int n; printf("fork program starting\n"); pid = fork(); switch(pid) { case -1: perror("fork failed"); exit(1); case 0: message = "This is the child"; n = 5; break; default: message = "This is the parent"; n = 3; break; } for(; n > 0; n--) { puts(message); sleep(1); } exit(0); } fork() Returns a new pid Returns zero Original process continues New process
wait – Waiting for a Process pid_t wait(int* stat_loc) ; pid_t waitpid(pid_t pid,int *stat_loc,int option) #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> int main() { pid_t pid; char *message; int n; int exit_code; printf("fork program starting\n"); pid = fork(); switch(pid) { case -1: exit(1); case 0: message = "This is the child"; n = 5; exit_code = 37; break; default: message = "This is the parent"; n = 3; exit_code = 0; break; } for(; n > 0; n--) { puts(message); sleep(1); } /* This section of the program waits for the child process to finish. */ if(pid) { int stat_val; pid_t child_pid; child_pid = wait(&stat_val); printf("Child has finished: PID = %d\n", child_pid); if(WIFEXITED(stat_val)) printf("Child exited with code %d\n", WEXITSTATUS(stat_val)); else printf("Child terminated abnormally\n"); } exit (exit_code); }
Signals • A signal is an event generated by the UNIX system in response to some condition: • Errors - memory violations, math processors errors • Interrupt • Calling to kill system call
Signals system call functions • Changing the Signal Handler signal(int sig,void (*func)(int)) • sig – code of the signal that is being change(except SIGKILL or SIGSTOP) • func – 1. the new handler function address 2. SIG_DFL – for the default handler 3. SIG_IGN – ignore this signal • Sending Signals kill(pid_t pid,int sig)
Signals example 1 #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int sig) { printf("OUCH! - I got signal %d\n", sig); signal(SIGINT, SIG_DFL); } int main() { (void) signal(SIGINT, ouch); while(1) { printf("Hello World!\n"); sleep(1); } }
Signals example 2 #include <signal.h> #include <stdio.h> #include <unistd.h> static int alarm_fired = 0; void ding(int sig) { alarm_fired = 1; } int main() { int pid; printf("alarm application starting\n"); if((pid = fork()) == 0) { sleep(5); kill(getppid(), SIGALRM); exit(0); } printf("waiting for alarm to go off\n"); signal(SIGALRM, ding); pause(); if (alarm_fired) printf("Ding!\n"); printf("done\n"); exit(0); }
Threads (briefly) • A thread (or lightweight process) is consists of: • program counter • register set • stack space • A thread shares with its peer threads its: • code section • data section • operating-system resources • A traditional or heavyweight process is equal to a task with one thread
Threads (Cont.) • In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. • Cooperation of multiple threads in same job confers higher throughput and improved performance. • Applications that require sharing a common buffer (i.e., producer-consumer) benefit from thread utilization. • Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. • Kernel-supported threads (Mach and OS/2). • User-level threads; supported above the kernel, via a set of library calls at the user level (MicroThread above DOS kernel). • Hybrid approach implements both user-level and kernel-supported threads (Solaris 2).
Threads Support in Solaris 2 • Solaris 2 is a version of UNIX with support for threads at the kernel and user levels, symmetric multiprocessing, and real-time scheduling. • LWP – intermediate level between user-level threads and kernel-level threads. • Resource needs of thread types: • Kernel thread: small data structure and a stack; thread switching does not require changing memory access information – relatively fast. • LWP: PCB with register data, accounting and memory information,; switching between LWPs is relatively slow. • User-level thread: only need stack and program counter; no kernel involvement means fast switching. Kernel only sees the LWPs that support user-level threads.