• 沒有找到結果。

call call …

N/A
N/A
Protected

Academic year: 2022

Share "call call …"

Copied!
26
0
0

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

全文

(1)

系統程式

郭大維教授/施吉昇教授 臺灣大學資訊工程系

(2)

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

(3)

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)

(4)

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.

(5)

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

(does n

ot return)

exit

(does not return)

exit

(does not return)

call

return call

return call return _exit

_exit

(6)

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”);

}

(7)

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

(8)

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

(9)

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

(10)

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

(11)

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)

(12)

Memory Layout

ƒ > size ls1 hole

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

text data bss

(13)

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

(14)

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.

(15)

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);

}

(16)

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

(17)

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.

(18)

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.

(19)

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.

(20)

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

(21)

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

(22)

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!

(23)

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.

(24)

getrlimit and setrlimit

#include <sys/time.h>

#include <sys/resource.h>

int getrlimit(int resource, struct rlimit *rlptr);

int setrlimit(int 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 */

}

(25)

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)

(26)

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)

參考文獻

相關文件

Lemma 2 An American call or a European call on a non-dividend-paying stock is never worth less than its intrinsic value.. • An American call cannot be worth less than its

• The XYZ.com bonds are equivalent to a default-free zero-coupon bond with $X par value plus n written European puts on Merck at a strike price of $30.. – By the

• The XYZ.com bonds are equivalent to a default-free zero-coupon bond with $X par value plus n written European puts on Merck at a strike price of $30.. – By the

• The XYZ.com bonds are equivalent to a default-free zero-coupon bond with $X par value plus n written European puts on Merck at a strike price of $30. – By the

• The XYZ.com bonds are equivalent to a default-free zero-coupon bond with $X par value plus n written European puts on Merck at a strike price of $30. – By the

• The XYZ.com bonds are equivalent to a default-free zero-coupon bond with $X par value plus n written European puts on Merck at a strike price of $30.. – By the

A call to SetLoadLevelTarget() implicitely stops ramping if applicable (&#34;last action wins&#34;), resetting the state variables as defined by

L1:add eax,C_minutesInDay ; totalMinutes+=minutesInDay call WriteString ; display str1 (offset in EDX) call WriteInt ; display totalMinutes (EAX) call Crlf. inc days