You are on page 1of 24

CSCI 530 Operating Systems

Function of OS Kernel: Table 3.7

Process Management
Memory Management
I/O Management ( Include File Management)
Support Functions ( Accounting, Monitoring)

11

CSCI 530 Operating Systems


Process Creation

Assign a unique process identifier


Allocate space for the process
Initialize process control block
Set up appropriate linkages
Ex: add new process to linked list used for
scheduling queue
Create other expanded data structures
Ex: maintain an accounting file for resource
usage

22

CSCI 530 Operating Systems


When to Switch a Process
Clock interrupt (timer)
process has executed for the maximum allowable
time slice
I/O interrupt
Memory fault ( page fault)
memory address is in virtual memory so it must be
brought into main memory

33

CSCI 530 Operating Systems


When to Switch a Process
Trap
error occurred
may cause process to be moved to Exit
state
Supervisor call ( System Call)
such as file open and close
Some other OS book define trap as system
call

44

CSCI 530 Operating Systems


Change of Process State
Save context of processor including program counter
and other registers
Update the process control block of the process that is
currently running
Move process control block to appropriate queue ready, blocked, suspended ( logical queue)
Select another process for execution

55

CSCI 530 Operating Systems


Change of Process State
Update the process control block of the process newly
selected
Update memory-management data structure to reflect
memory mapping change ( suspending, swap, etc.)
Restore the context of the selected process ( PC
register will point to new process, etc.)

66

CSCI 530 Operating Systems


System Call
Differ it from user function call or API Library call
Average computing time:
2000 nano-seconds system call
90 nano-seconds user function call
. Related with trap and Processor Protection mode

77

CSCI 530 Operating Systems


Swap
Process Swap and Suspend State
swap is a mechanism to coordinate processes to
share main memory efficiently, and make the
processor as busy as possible (for higher
performance).
Process is swapped out from main memory to disk, or
from disk to main memory.

88

CSCI 530 Operating Systems


What if no swap?
Limit maximum size of a process and limit the number
of processes that can run in the system concurrently.
Related with virtual memory.
Review:
Suspend State (read suspended or block-suspended),
store full or partial of a process in disk
New process created in Ready-suspend state or ready
state. Both waits for memory available.

99

CSCI 530 Operating Systems


Mode of Processor Execution
Instruction in user program has no permission to access
critical kernel data directly. It is running in user
mode. User program triggers system instruction that
is running in kernel mode (system mode) and OS
kernel will handle such privileged instruction invoked
by the user program (such as file write/close/delete).
Change the data in process control block can only run
in system mode. This way to protect OS related
control tables such as file, I/O/memory table.

10
10

CSCI 530 Operating Systems


OS kernel module (code) itself is always
running in system mode where all
privileged instruction can be executed.
Usually the user instruction of a process is
limited to its own process private address
space when executed until it make a system
call.

11
11

CSCI 530 Operating Systems


Highlighted Concepts in Chapter 3:

Process /context switching


Process swapping
Example to create a process
Example to terminate a process
Example of process interruption
Physical and logical Parallelism
Process state and the event that causes the state change
Processor execution mode and trap
System call

12
12

CSCI 530 Operating Systems


Unix Process Commands
User Commands: ps report process status
DESCRIPTION
The ps command prints information about active
processes. Without options, ps prints information
about processes associated with the controlling
terminal. The output contains only the process ID,
terminal identifier, cumulative execution time, and the
command name. Otherwise, the information that is
displayed is controlled by the options.

13
13

CSCI 530 Operating Systems


System Calls

fork()

fork(): create a new process


The fork() functions create a new process. The
new process (child process) is an exact copy of
the calling process (parent process).
The child process inherits the following major
attributes from the parent process:

14
14

CSCI 530 Operating Systems


System Call: fork ()

real user ID, real group ID, effective user ID,


effective group ID
environment
open file descriptors
all attached shared memory segments
process group ID -- memory mappings
current working directory
root directory
controlling terminal
saved user ID and group ID

15
15

CSCI 530 Operating Systems


The child process differs from the parent process in the
following ways:
The child process has a unique process ID which
does not match any other active process group ID.
The child process has a different parent process ID
(that is, the process ID of the parent process).
The child process has its own copy of the parent's
file descriptors and directory streams.

16
16

CSCI 530 Operating Systems


Each of the child's file descriptors shares a
common file pointer with the corresponding
file descriptor of the parent.
Process locks, text locks, data locks, and
other memory locks are not inherited by
the child

17
17

CSCI 530 Operating Systems


RETURN VALUES
Upon successful completion, fork() return
0 to the child process and return the
process ID of the child process to the parent
process.
Otherwise, -1 is returned to the parent
process, no child process is created, and
errno is set to indicate the error.

18
18

CSCI 530 Operating Systems


User Commands: Kill
kill - terminate or signal processes
SYNOPSIS
/usr/bin/kill -s signal pid...
/usr/bin/kill [ -signal ] pid...
DESCRIPTION

The kill utility sends a signal to the process or


processes specified by each pid operand.

19
19

CSCI 530 Operating Systems


Kill
The signaled process must belong to the
current user unless the user is the superuser.

20
20

CSCI 530 Operating Systems


Assignment: Fork Project and Multi-tasking
Multi-tasking through system call fork(), learn system
call, and observe interleaving when multiple
processes are executed concurrently.
Please reference the class support web site for
basics of Unix C programming, such as: editing tool
vi, compiler gcc;
Learn other Unix user commands for files operations.

21
21

CSCI 530 Operating Systems


Sample code:
#include <sys/types.h>
#include <sys/wait.h>
main() {
/*PC register will point to here
when a process is scheduled to
execute */
int orig=500;
int pid;

22
22

CSCI 530 Operating Systems


if ( (pid=fork() = -1 ) {
printf("sorry, fork
failed");
exit (-1);}
}
else if ((pid != 0 ) {
/*this is parent process */
orig=orig+100;
printf(inside Parent: orig=%d\n,orig);
exit (0);
}

23
23

CSCI 530 Operating Systems


else

/* this is a child process */


orig=orig+100;
printf(inside Child: orig=%d\n,
orig);
exit (0);
}
} // end of main

24
24