32
exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use the one most suited to needs.

exec Function calls

  • Upload
    alder

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

exec Function calls. Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use the one most suited to needs. exec Function calls. int execv( char *path, char *argv[]) ; - PowerPoint PPT Presentation

Citation preview

Page 1: exec Function calls

exec Function calls

Used to begin a processes execution.

Accomplished by overwriting process imaged of caller with that of called.

Several flavors, use the one most suited to needs.

Page 2: exec Function calls

exec Function calls

int execv( char *path, char *argv[]) ;

path: directory path to executable image. Can be your program, or system program.

argv: Array of pointers to null terminated strings. These are the arguments to the program being executed.

Page 3: exec Function calls

exec Function calls

two conventions with argv:

1) argv[0] should hold the name of the program to be executed.2) The last element must be NULL.

Page 4: exec Function calls

main (int argc, *argv[])

{ int pid ; char *args[2] ; pid = fork() ; if (pid ==0)

{ args[0] = “./a.out” ; All executed by args[1] = NULL ; child process i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ;}

elseprintf(“Not a problem!\n”) ; Executed by parent

}

Page 5: exec Function calls

int pid ;char *args[2] ;pid = fork() ;

Page 6: exec Function calls

int pid ;char *args[2] ;pid = ?

fork

Parent

int pid ; char *args[2] ;

pid = 0

Child

Page 7: exec Function calls

Child

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; }else printf(“Not ….”);

Page 8: exec Function calls

Parent

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; }else printf(“Not ….”);

Page 9: exec Function calls

Parent

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; }else printf(“Not ….”);

Child

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; }else printf(“Not ….”);

Page 10: exec Function calls

Parent

int pid ;

char *args[2] ;

pid = fork()

if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; }else printf(“Not ….”);

a.out

Page 11: exec Function calls

Processes: program + execution state Pseudoparallelism Multiprogramming

Many processes active at once With switching:

process execution is not repeatable processes should make no assumptions about

timing Process consists of:

Process’ core image: program, data, run-time stack Program counter, registers, stack pointer OS bookkeeping information

Page 12: exec Function calls

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 13: exec Function calls

Diagram of Process State

Page 14: exec Function calls

Diagram of Process State

X

Page 15: exec Function calls

Diagram of Process State

XX

Page 16: exec Function calls

Diagram of Process State

XX

X

Page 17: exec Function calls

Process Table

One entry per process.

PCB PCB PCBPCB PCB

Pr 0 Pr 1 Pr 2 Pr 4 …..Pr N

Page 18: exec Function calls

PCB: Process Management

registers, program counter, program status word, stack pointer

process state time process started, CPU time used,

children’s CPU time used alarm clock setting pending signal bits pid message queue pointers, other flag bits

Page 19: exec Function calls

PCB: Memory-Related Information

pointers to text Data stack segments

Pointer to page table

Page 20: exec Function calls

PCB: File-Related Information

root, working directory file descriptors User ID Group ID

Page 21: exec Function calls

Process Control Block (PCB)

Page 22: exec Function calls

Context Switch (or Process Switch)

Currently executing process looses control of CPU

Its “context” must be saved (if not terminated) into PCS.

New process is chosen for execution. New process context is restored. New process is given control of the CPU.

Page 23: exec Function calls

CPU Switch From Process to Process

Page 24: exec Function calls

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 25: exec Function calls

Ready Queue And Various I/O Device Queues

Page 26: exec Function calls

Representation of Process Scheduling

Page 27: exec Function calls

Low Level Interrupt Processing

Each HW device has slot in interrupt vector (IV). On interrupt, HW pushes PC, PSW, one or more registers. Loads in new PC from IV. END OF HW Assembly code to save registers and info on stack (to

PCB). Assembly sets up new stack for handling process. Calls C interrupt handling routing to complete interrupt

processing. Scheduler is called and determines next process to run. Assembly language restores registers and other

necessary items (e.g., memory map) of selected process and begins its execution.

Page 28: exec Function calls

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 29: exec Function calls

Schedulers (Cont.)

Short-term scheduler is invoked very frequently (milliseconds) (must be fast).

Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow).

The long-term scheduler controls the degree of multiprogramming.

Page 30: exec Function calls

Schedulers (Cont.)

Processes can be described as either: I/O-bound process – spends more

time doing I/O than computations, many short CPU bursts.

CPU-bound process – spends more time doing computations; few very long CPU bursts.

Page 31: exec Function calls

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.

Time dependent on hardware support.

Page 32: exec Function calls

#include <unistd.h>#include <stdio.h>main()

{ char *ptr ;

char input[1024] ;

char *tmp, *tmp1 ; int i ;

while(1)

{ i = read(0, input, 512) ; printf("Ret %d\n", i) ;

ptr = input ; while (*ptr != '\n') { if (*ptr == ' ') printf("Space!\n") ; else printf("Read %c\n", *ptr) ; ptr++ ; } }}