Operating Systems Chapter 2

Download Report

Transcript Operating Systems Chapter 2

Operating Systems
Chapter 2
Process
1
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
2
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.
3
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
4
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.
5
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.

6
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.
7
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.
8
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);
}
9
fork - Duplicating a Process Image
pid_t fork(void)
#include <sys/types.h>
for(; n > 0; n--) {
#include <unistd.h>
puts(message);
#include <stdio.h>
sleep(1);
int main()
}
{
exit(0);
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;
}
Initial process
fork()
Returns a new pid
Original
process
continues
Returns zero
New
process
10
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>
for(; n > 0; n--) {
#include <sys/wait.h>
puts(message);
#include <unistd.h>
sleep(1);
#include <stdio.h>
}
int main()
{
/* This section of the program waits for the child process to finish. */
pid_t pid;
char *message;
if(pid) {
int n;
int stat_val;
int exit_code;
pid_t child_pid;
printf("fork program starting\n");
pid = fork();
child_pid = wait(&stat_val);
switch(pid)
{
printf("Child has finished: PID = %d\n", child_pid);
case -1:
if(WIFEXITED(stat_val))
exit(1);
printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
case 0:
else
message = "This is the child";
printf("Child terminated abnormally\n");
n = 5;
}
exit_code = 37;
exit (exit_code);
break;
}
default:
message = "This is the parent";
n = 3;
exit_code = 0;
break;
}
11
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
12
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)

13
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);
}
}
14
Signals example 2
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
printf("waiting for alarm to go off\n");
signal(SIGALRM, ding);
pause();
if (alarm_fired)
printf("Ding!\n");
static int alarm_fired = 0;
void ding(int sig)
{
alarm_fired = 1;
}
printf("done\n");
exit(0);
}
int main()
{
int pid;
printf("alarm application starting\n");
if((pid = fork()) == 0) {
sleep(5);
kill(getppid(), SIGALRM);
exit(0);
}
15
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
16
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., producerconsumer) 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 kernelsupported threads (Solaris 2).
17
Multiple Threads within a Task
18
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 kernellevel 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.
19
Solaris 2 Threads
20