Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Chapter 2

Similar presentations


Presentation on theme: "Operating Systems Chapter 2"— Presentation transcript:

1 Operating Systems Chapter 2
Process

2 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

3 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.

4 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

5 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.

6 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.

7 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.

8 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.

9 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); }

10 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; } for(; n > 0; n--) { puts(message); sleep(1); } exit(0); fork() Returns a new pid Returns zero Original process continues New process

11 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; } 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);

12 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

13 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)

14 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);

15 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); }

16 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

17 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).

18 Multiple Threads within a Task

19 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.

20 Solaris 2 Threads


Download ppt "Operating Systems Chapter 2"

Similar presentations


Ads by Google