1 operating systems chapter 2 process. 2 process concept process – a program in execution unix’s...

20
1 Operating Systems Chapter 2 Process

Upload: magnus-howard

Post on 01-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Operating SystemsChapter 2

Process

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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.

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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

9

Case Study: UNIX

exec - Replacing a Process imageint 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);}

Page 10: 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

10

fork - Duplicating a Process Image

pid_t fork(void)Initial process

fork()

Original process

continues

New process

Returns a new pid Returns zero

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

Page 11: 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

11

wait – Waiting for a Processpid_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);}

Page 12: 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

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

Page 13: 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

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 address2. SIG_DFL – for the default handler3. SIG_IGN – ignore this signal

Sending Signals

kill(pid_t pid,int sig)

Page 14: 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

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

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

18

Multiple Threads within a Task

Page 19: 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

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.

Page 20: 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

20

Solaris 2 Threads