OS——process(Part One)

OS——Process(Part One)

A. What is process?

1.Process

– a program in execution; process execution must progress in sequential fashion.
Program is passive entity stored on disk (executable file), process is active.
– Program becomes process when executable file loaded into memory

  • Execution of program started via GUI mouse clicks, command line entry
    of its name, etc
  • One program can be several processes

– Consider multiple users executing the same program

2.A process includes:

– program code, known as text section
– data section, contains global variables.
– heap, containing memory dynamically allocated during run time.
– process stack, contains temporary data
• function parameters, return value
• return addresses
• local variables
– current activity, represented by the value of the program counter and the contents of the processor’s register.
在这里插入图片描述

3.Process State

As a process executes, it changes state
A process may be in one of the following states:
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 processor.
terminated: The process has finished execution.
Only one process can be running on any processor at any time, many processes may be ready and waiting
在这里插入图片描述

  • if there is progress which is ready, there must be some progress running. So,there can be at most (n-1) progresses on ready state when
    there are n progresses. The waiting ranges from 0 to n which can all
    be waiting in the IO queue.
  • one state must follow the flow
  • When to Switch a Process
    Clock interrupt
    – process has executed for the maximum allowable time slice
    I/O interrupt
    Memory fault
    – memory address is in virtual memory so it must be brought into main memory
    Trap
    – error occurred
    – may cause process to be moved to terminated/exit state
    System call
    – such as file open

4.PCB (Process Control Block, task control block)

  • Each process is represented in the OS by a PCB
  • PCB contains:
    Process state – ready, running, waiting, etc.
    Program counter – the address of the next instruction to be executed
    CPU registers – contents of all process-centric registers, e.g. index registers, stack pointers, general-purpose registers, condition-code, etc.
    CPU scheduling information- priorities, scheduling queue pointers
    Memory-management information – memory allocated to the process
    Accounting information – the amount of CPU time used, clock time elapsed since start, time limits, account numbers, etc.
    I/O status information – the list of I/O devices allocated to process, a list of open files, and so on.
    Process number – Identifiers, e.g. Identifier of this process , Identifier of its parent process, User identifier.

B.Progress Scheduling

1.queue

The objective of multiprogramming – to maximize CPU utilization.
The objective of time sharing – users can interact with each program while it is running.

– switch the CPU among processes frequently process scheduler, selects an available process for execution on the CPU.
On a single-processor system,
– Only one running process at most.
– the rest will have to wait until the CPU is free and can be rescheduled.

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.
Representation of Process Scheduling
Representation of Process Scheduling
Queueing diagram represents queues, resources, flows

2.scheduling

  • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue which controls the number of processes in memory, the degree of multiprogramming
  • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
    - Primary distinction: execution frequency
  • Medium term scheduling is responsible for swapping (交换/倒换)
    • swapping out: to reduce degree of multiprogramming, removing processes in ready or running states from main memory and into swapping section on disks the process that is swapped out is delayed to execute
    • swapping in: reintroducing the processes on the disk into memory into memory, restarting their executing

3.Context Switch

The context is represented in the PCB of the process.
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.
Known as a context switch.
– Save context of processor including program counter and other
registers
Update the PCB of the process that is currently running
– Move PCB to appropriate queue - ready, waiting
– Select another process for execution (–scheduling)
Update the PCB of the process selected
– Update memory-management data structures
– Restore context of the selected process
Context-switch time is overhead; the system does no useful work while switching.
Context-switch times dependent on hardware support.

C.Operations on Processes ——Process Creation & Process Termination

1.Process Creation

  • reason:
    • Submission of a batch job
    • User logs on
    • Created to provide a service such as printing
    • Process creates another process
  • step
    Assign a unique process identifier
    Allocate space for the process
    Initialize process control block (PCB)
    Set up appropriate linkages
    Create or expand other data structures
    Resource sharing
    – Parent and children share all resources.
    – Children share subset of parent’s resources.
    – Parent and child share no resources.
  • Execution
    – Parent and children execute concurrently.
    – Parent waits until some or all of its children terminate.
  • Address space
    – Child is a duplicate of the parent.
    – Child has a program loaded into it.
  • UNIX examples
    – each process is identified by its process identifier, an unique integer
    – system call fork creates new process
    – system call exec used after a fork to replace the child process’ memory space with a new program.

In this part ,I don’ t understand the linux example of progress creating , then I do it myself .
在这里插入图片描述
Most of us must think it will only output two lines. But, the result is
在这里插入图片描述
four ! but why?
After fork,OS will duplicate a new child progress which is the same as his father. But they only share the same code paragraph ,and their data space is independent. They run at the same time and keep the step.

for more information ,please click here. The blog is good

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值