Understanding OS Processes: User-level View, Implementation, and System Calls, Slides of Introduction to Computers

An in-depth look into processes in operating systems, covering the user-level view, implementation, and various system calls such as fork(), exec(), wait(), and exit(). It includes examples and explanations of concepts like process hierarchy, unix startup, and context switching.

Typology: Slides

2010/2011

Uploaded on 10/07/2011

rolla45
rolla45 🇺🇸

4

(6)

133 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program Translation and Execution II:
Processes
Oct 1, 1998
Topics
User-level view of processes
Implementation of processes
setjmp/longjmp
class12.ppt
15-213
Introduction to Computer Systems
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Understanding OS Processes: User-level View, Implementation, and System Calls and more Slides Introduction to Computers in PDF only on Docsity!

Program Translation and Execution II:

Oct 1, 1998Processes

Topics

User-level view of processes

Implementation of processes

setjmp/longjmp

class12.ppt

Introduction to Computer Systems

  • 2 –

class12.ppt

Processes

A process is an instance of a running program

runs concurrently with other processes (

multitasking)

managed by a shared piece of OS code called the

kernel

process^ – kernel is no t a separate pro cess, but rath er runs as part o f some user

each process has its own data space and process id (pid)

data for each protected protected from other processes Process A

Process B

user codekernel codeuser codekernel codeuser code

Time

of instructions!Just a stream

  • 4 –

class12.ppt

Exit

void exit(int status)

exits a process

void cleanup(void)atexit() function registers functions to be executed on exit

printf(“cleaning

(^) up\n”);

main() {} if (fork() == 0)atexit(cleanup);

printf(“hello

(^) from

(^) child\n”);

else {} printf(“hello

(^) from

(^) parent\n”);

exit();}

  • 5 –

class12.ppt

Wait

int wait(int child_status)

main() {waits for a child to terminate and returns status and pid if (fork() == 0)int child_status;

printf(“hello

(^) from

(^) child\n”);

else {} printf(“hello

(^) from

(^) parent\n”);

printf(“childwait(&child_status);

(^) has (^) terminated\n”);

exit();}

CS 213 F’

  • 7 –

class12.ppt

void main() Example: Concurrent network server

{

master_sockfd

(^) = sl_passivesock(port); /* create master socket

(^) */

while (1) { slave_sockfd

(^) = sl_acceptsock(master_sockfd); /* await request */

switch (fork()) { case 0: /* child closes its

(^) master and manipulates slave */

/* code to read and writeclose(master_sockfd);

(^) to/from slave socket goes here */

exit(0);

default: /* parent closes its copy of slave and repeats */ close(slave_sockfd);

case -1: /* error */ exit(0);fprintf("fork error\n");

}

}

}

  • 8 –

class12.ppt

Process hierarchy

shell

child

child

child

grandchild

grandchild

init (1) (0)

e.g. snmpDaemon

  • 10 –

class12.ppt

Unix startup (2)

init [1] [0]

for the console forks a getty (get tty or get terminal)

getty

e.g. snmpDaemons/etc/inittab

the /etc/inittab file init forks new processes as per

  • 11 –

class12.ppt

Unix startup (3)

init [1] [0]

getty execs a login program

login

  • 13 –

class12.ppt

Loading and running programs

from a shell

while (read(stdin, buffer, /* read command line until EOF */

(^) numchars)) {

if (<command line contains ‘&’

(^) >)

amper (^) = 1;

else amper (^) = 0;

if (fork() == 0) {/* for commands not in the shell command language */} execl(cmd, cmd, 0)

if (amper == 0)} retpid

(^) = wait(&status);

}

  • 14 –

class12.ppt

Process memory image (Alpha)

Reserved for kernel

Reserved for shared libraries Available for heapand dynamic loader

Heap (via malloc() or sbrk()

Grows up

Text segmentData segmentBss segment Stack

Available for stack Not accessible

Grows down to zero

Not accessible by convention

(64KB)

$sp $gp

0x (^0000) (^0000) (^0000)

0x (^0000) (^0000) (^) ffff

0x (^0000) (^0001) (^0000)

0x (^0000) (^) 1fff (^) ffff

0x (^0001) (^2000) (^0000)

0x (^) 03ff (^) 7fff (^) ffff

0x (^) 03ff (^8000) (^0000)

0x (^) 03ff (^) ffff (^) ffff

0x (^0400) (^0000) (^0000)

0xffff (^) fbff (^) ffff (^) ffff

0xffff (^) fc (^0000) (^0000)

0xffff (^) ffff (^) ffff (^) ffff

  • 16 –

class12.ppt

User and kernel modes

User mode

  • execute its own instructions and access its own data.Process can
  • access kernel data or data from other processes. – execute kernel instructions or privileged instructions (e.g. halt)Process cannot

Kernel mode

  • access kernel and user addresses – execute kernel instructions and privileged instructionsProcess can

Processes transition from user to kernel mode via

interrupts

and exceptions

system calls (traps)

  • 17 –

class12.ppt

System call interface

System calls (traps) are

expected program events

e.g., fork(), exec(), wait(), getpid()

User code

call user-level library function,

  • e.g. syscall(id)executes special syscall instruction

switch from user mode to kernel mode

transfer control to kernel system call interface

System call interface

find entry in syscall table corresponding to id

determine number of parameters

copy parameters from user member to kernel memory

save current process context (in case of abortive return)

invoke appropriate function in kernel

  • 19 –

class12.ppt

The context of a process is the state that is necessary^ Process control: Context of a process

• to restart the process if its interrupted. Union of ...

user-level context

register context

system-level context.

User-level context

text, data, and bss segments, and user stack

Register context

mode, kernel stack pointer, process table address, ...PC, general purpose integer and floating point regs, IEEE rounding

System-level context

various OS tables process and memory tables, kernel stack, ...

  • 20 –

class12.ppt Process control: Context switch

The kernel can decide to pass control to another

• process if:

the current process puts itself to sleep

the current process exits

when the current process returns from a system call

when the current process returns after being interrupted

Control passed to new process via

context switch:

save current process context.

select new process (scheduling)

restore (previously save) context of new process

pass control to new process