• 沒有找到結果。

Process Termination

N/A
N/A
Protected

Academic year: 2022

Share "Process Termination"

Copied!
13
0
0

加載中.... (立即查看全文)

全文

(1)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Contents

1. Preface/Introduction

2. Standardization and Implementation 3. File I/O

4. Standard I/O Library 5. Files and Directories

6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control

9. Signals

10.Inter-process Communication

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

The Environment of a Unix Process

ƒ Objective:

ƒ How a process is executed and terminates?

ƒ What the typical memory layout is?

ƒ Related functions and resource limits.

ƒ int main(int argc, char *argv[])

ƒ A special start-up routine is called to set things up first before call main()

ƒ Set up by the link editor (invoked by the compiler)

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

How a C program is started and terminated.

kernel

C start-up routine

main function

user functions

exit function

exit handler

exit handler

standard I/O cleanup exec

user process

_exit

callreturn call

return exit

(doe s not return)

exit

(does not return)

exit

(does not return)

call

return call return call return _exit

_exit

Process Termination

ƒ Five ways to terminate:

ƒ Normal termination

ƒReturn from main().

ƒ exit(main(argc, argv));

ƒCall exit().

ƒCall _exit()

ƒ Abnormal termination

ƒCall abort()

ƒBe terminated by a signal.

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Process Termination

#include <stdlib.h>

void exit(int status);

ƒ ANSI C

ƒ Perform cleanup processing

ƒClose and flush I/O streams (fclose)

#include <unistd.h>

void _exit(int status);

ƒ POSIX.1

ƒ Undefined exit status:

ƒ Exit/return without status.

ƒ Main falls off the end.

#include <stdio.h>

main() {

printf(“hello world\n”);

}

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Process Termination

#include <stdlib.h>

int atexit(void (*func)(void));

ƒ Up to 32 functions called by exit – ANSI C, supported by SVR4&4.3+BSD

ƒ The same exit functions can be registered for several times.

ƒ Exit functions will be called in reverse order of their registration.

ƒ Program 7.1 – Page 165

ƒ Exit handlers

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Command-Line Arguments &

Environment Variables

ƒ Command-Line Arguments

ƒ argv[argc] is NULL under POSIX.1 & ANSI C

ƒ Program 7.2 – Page 166

ƒ Environment Variables

ƒ int main(int agrc, char **argv, char **envp);

ƒ getenv/putenv

extern char **environ;

NULL

HOME=/home/stevens\0 PATH=:/bin:/usr/bin\0 SHELL=/bin/sh\0 USER=stevens\0 LOGNAME=stevens\0 environment

list

environment strings

Memory Layout

ƒ Pieces of a process

ƒ Text Segment

ƒ Read-only usually, sharable

ƒ Initialized Data Segment

ƒ int maxcount = 10;

ƒ Uninitialized Data Segment – bss (Block Started by Symbol)

ƒ Initialized to 0 by exec

ƒ long sum[1000];

ƒ Stack – return addr, automatic var, etc.

ƒ Heap – dynamic memory allocation

Read from program file by exec

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Layout

ƒ 4.3BSD

text structure

proc[i]

entry

page

table Code Segment PC

heap user stack argv, argc,…

sp .u

per-process kernel stack

p_textp x_caddr

p_p0br

u_proc p_addr

Red Zone

Initialized Data Uninitialized Data (bss)

0x00000000 0x7fffffff

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Layout

ƒ 4.4BSD

proc[i]

entry

process grp

file descrptors

VM space region lists

page

table Code Segment

Initialized Data

heap user stack argv, argc,…

.u

per-process kernel stack

p_p0br

u_proc p_addr

Uninitialized Data (bss)

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Layout

ƒ > size ls1 hole

ls1: 6971 + 876 + 364 = 8211 hole: 6995 + 896 + 368 = 8259

text data bss

Shared Library

ƒ Why a shared library?

ƒ Remove common library routines from executable files.

ƒ Have an easy way for upgrading

ƒ Problems

ƒ More overheads

ƒ Remark:

ƒ compiling options – gcc

ƒ Supported by many Unix systems

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Allocation

ƒ Three ANSI C Functions:

ƒ malloc – allocate a specified number of bytes of memory. Initial values are indeterminate.

ƒ calloc – allocate a specified number of objects of a specified size. The space is initialized to all 0 bits.

ƒ realloc – change the size of a previously allocated area. The initial value of

increased space is indeterminate.

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Allocation

#include <stdlib.h>

void *malloc(size_t size);

void *calloc(size_t nobj, size_t size);

void *realloc(void *ptr, size_t newsize);

ƒ Suitable alignments for any data obj

ƒ Generic void * pointer

ƒ free(void *ptr) to release space to a pool.

ƒ Leaking problem

ƒ Free already-freed blocks or blocks not from alloc().

ƒ mallopt(M_GRAINSet, value), mallinfo

#include <stdio.h>

#include <stdlib.h>

main() { char *ptr;

ptr = malloc(100);

free(ptr);

free(ptr);

ptr[0] = 'a'; ptr[1]=0;

printf("%s - Done\n", ptr);

}

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Memory Allocation

ƒ Remark

ƒ realloc() could trigger moving of data Æ avoid pointers to that area!

ƒ prt == NULL Æ malloc()

ƒ sbrk() is used to expand or contract the heap of a process – a malloc pool

ƒ Record-keeping info is also reserved for memory allocation – do not move data inside.

ƒ alloca() allocates space from the stack frame of the current function!

ƒ No needs for free with potential portability problems

Environment Variables

ƒ Name=value

ƒ Interpretation is up to applications.

ƒ Setup automatically or manually

ƒ E.g., HOME, USER, MAILPATH, etc.

setenv FONTPATH $X11R6HOME/lib/X11/fonts\:$OPENWINHOME/lib/fonts

#include <stdlib.h>

char *getenv(const char *name);

ƒ Figure 7.4 – environment variables

ƒ ANSI C function, but no ANSI C environment variable.

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Environment Variables

#include <stdlib.h>

int putenv(const char *name-value);

ƒ Remove old definitions if they exist.

int setenv(const char *name, const char

*value, int rewrite);

ƒ rewrite = 0 Æ no removing of existing names.

int unsetenv(const char *name);

ƒ Remove any def of the name

ƒ No error msg if no such def exists.

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Environment Variables

ƒ Adding/Deleting/Modifying of Existing Strings

ƒ Modifying

ƒThe size of a new value <= the size of the existing value Æ overwriting

ƒOtherwise; malloc() & redirect the ptr

ƒ Adding

ƒThe first time we add a new name Æ malloc of pointer-list’s room & update envron

ƒOtherwise; copying. realloc() if needed.

ƒ The heap segment could be involved.

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

setjmp and longjmp

ƒ Objective:

ƒ goto to escape from a deeply nested function call!

ƒ Program 7.3 – Program Skeleton

ƒ What if cmd_add() suffers a fatal error?

How to return to main() to get the next line?

stack frame for main stack frame for do_line stack frame for cmd_add

Note: Automatic variables are allocated within the stack frames!

line cmd token

setjmp and longjmp

#include <setjmp.h>

int setjmp(jmp_buf env);

int longjmp(jmp_buf env, int val);

ƒ Return 0 if called directly; otherwise, it could return a value val from longjmp().

ƒ env tends to be a global variable.

ƒ longjmp() unwinds the stack and affect some variables.

ƒ Program 7.4 – Page 178

ƒ setjmp and longjmp

(11)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

setjmp and longjmp

ƒ Automatic, Register, and Volatile Variables

ƒ Compiler optimization

ƒRegister variables could be in memory.

ƒ Values are often indeterminate

ƒNormally no roll back on automatic and register variables

ƒShown in Program 7.5 later

ƒGlobal and static variables are left alone when longjmp is executed.

ƒ Portability Issues!

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

setjmp and longjmp

ƒ Program 7.5 – Page 179

ƒ Effects of longjmp

ƒ Variables stored in memory have their values unchanged – no optimization…

ƒ Potential Problems with Automatic Variables – Program 7.6 (Page 180)

ƒ Never be referenced after their stack frames are released.

(12)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

getrlimit and setrlimit

#include <sys/time.h>

#include <sys/resource.h>

int getrlimitint resource, struct rlimit *rlptr);

int setrlimitint resource, const struct rlimit

*rlptr);

ƒ Not POSIX.1, but supported by SVR4 and 4.3+BSD

ƒ Rules:

ƒSoft limit <= hard limit, the lowering of hard limits is irreversible.

ƒOnly a superuser can raise a hard limit.

struct rlimit {

rlim_t rlim_cur; /* soft limit */

rlim_t rlim_max; /* hard limit */

}

getrlimit and setrlimit

ƒ Resources (SVR4&4.3BSD)

ƒ RLIMIT_CORE (both), RLIMIT_CPU (both, SIGXCPU), RLIMIT_DATA (both),

RLIMIT_FSIZE (both , SIGXFSZ), RLIMIT_MEMLOCK (4.3+BSD, no

implementation), RLIMIT_NOFILE (SVR4, _SC_OPEN_MAX), RLIMIT_NPROC (4.3+BSD, _SC_CHILD_MAX), RLIMIT_OFILE

(4.3+BSD=RLIMIT_NOFILE), RLIMIT_RSS (4.3+BSD, max memory resident size), RLIMIT_STACK (both), RLIMIT_VMEM (SVR4)

(13)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

getrlimit and setrlimit

ƒ Resource Limits Æ inheritance by processes

ƒ Built-in commands in shells

ƒumask, chdir, limit (C shall), ulimit –H and –S (Bourne shell and KornShell)

ƒ Program 7.7 – Page 183

ƒ Resource limits

ƒ#define RLIM_NLIMITS 7

ƒ doit(RLIMIT_CORE) =

pr_limits(“RLIMIT_CORE”, RLIMIT_CORE)

ƒ#define doit(name) pr_limits(#name, name)

參考文獻

相關文件

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005!.

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University,