How to write a shell script

Introduction

A shell is a command line interpretor. It takes commands and executes them. As such, it implements a programming language. The Bourne shell is used to create shell scripts -- ie. programs that are interpreted/executed by the shell. You can write shell scripts with the C-shell; however, this is not covered here.

Creating a Script

Suppose you often type the command

    find . -name file -print

and you'd rather type a simple command, say

    sfind file

Create a shell script

    % cd ~/bin

    % emacs sfind

    % page sfind

    find . -name $1 -print

    % chmod a+x sfind

    % rehash

    % cd /usr/local/bin

    % sfind tcsh

    ./shells/tcsh

Observations

This quick example is far from adequate but some observations:

  1. Shell scripts are simple text files created with an editor.
  2. Shell scripts are marked as executeable

3.      %chmod a+x sfind

  1. Should be located in your search path and ~/bin should be in your search path.
  2. You likely need to rehash if you're a Csh (tcsh) user (but not again when you login).
  3. Arguments are passed from the command line and referenced. For example, as $1.

#!/bin/sh

All Bourne Shell scripts should begin with the sequence

    #!/bin/sh

From the man page for exec(2):

"On the first line of an interpreter script, following the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."

You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpretors -- Csh, Ksh, Bash, and others.

Comments

Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line.

Search Path

All shell scripts should include a search path specifica- tion:

    PATH=/usr/ucb:/usr/bin:/bin; export PATH

A PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path.

The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command.

Argument Checking

A good shell script should verify that the arguments sup- plied (if any) are correct.

 

    if [ $# -ne 3 ]; then

         echo 1>&2 Usage: $0 19 Oct 91

         exit 127

    fi

This script requires three arguments and gripes accordingly.

Exit status

All Unix utilities should return an exit status.

    # is the year out of range for me?

 

    if [ $year -lt 1901  -o  $year -gt 2099 ]; then

         echo 1>&2 Year \"$year\" out of range

         exit 127

    fi

 

    etc...

 

    # All done, exit ok

 

    exit 0

A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected.

On BSD systems there's been an attempt to categorize some of the more common exit status codes. See /usr/include/sysexits.h.

Using exit status

Exit codes are important for those who use your code. Many constructs test on the exit status of a command.

The conditional construct is:

    if command; then

         command

    fi

For example,

    if tty -s; then

         echo Enter text end with \^D

    fi

Your code should be written with the expectation that others will use it. Making sure you return a meaningful exit status will help.

Stdin, Stdout, Stderr

Standard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and should be used accordingly:

    # is the year out of range for me?

 

    if [ $year -lt 1901  -o  $year -gt 2099 ]; then

         echo 1>&2 Year \"$year\" out of my range

         exit 127

    fi

 

    etc...

 

    # ok, you have the number of days since Jan 1, ...

 

    case `expr $days % 7` in

    0)

         echo Mon;;

    1)

         echo Tue;;

 

    etc...

Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue:

    # give the fellow a chance to quit

 

    if tty -s ; then

         echo This will remove all files in $* since ...

         echo $n Ok to procede? $c;      read ans

         case "$ans" in

              n*|N*)

    echo File purge abandoned;

    exit 0   ;;

         esac

         RM="rm -rfi"

    else

         RM="rm -rf"

    fi

Note: this code behaves differently if there's a user to communicate with (ie. if the standard input is a tty rather than a pipe, or file, or etc. See tty(1)).

Language Constructs

For loop iteration

Substitute values for variable and perform task:

    for variable in word ...

    do

         command

    done

For example:

    for i in `cat $LOGS`

    do

            mv $i $i.$TODAY

            cp /dev/null $i

            chmod 664 $i

    done

Alternatively you may see:

    for variable in word ...; do command; done

  • Case

Switch to statements depending on pattern match

    case word in

    [ pattern [ | pattern ... ] )

         command ;; ] ...

    esac

For example:

 

    case "$year" in

 

    [0-9][0-9])

            year=19${year}

            years=`expr $year - 1901`

            ;;

    [0-9][0-9][0-9][0-9])

            years=`expr $year - 1901`

            ;;

    *)

            echo 1>&2 Year \"$year\" out of range ...

            exit 127

            ;;

    esac

  • Conditional Execution

Test exit status of command and branch

    if command

    then

         command

    [ else

         command ]

    fi

For example:

    if [ $# -ne 3 ]; then

            echo 1>&2 Usage: $0 19 Oct 91

            exit 127

    fi

Alternatively you may see:

    if command; then command; [ else command; ] fi

  • While/Until Iteration

Repeat task while command returns good exit status.

    {while | until} command

    do

         command

    done

For example:

    # for each argument mentioned, purge that directory

 

    while [ $# -ge 1 ]; do

            _purge $1

            shift

    done

Alternatively you may see:

    while command; do command; done

  • Variables

Variables are sequences of letters, digits, or underscores beginning with a letter or underscore. To get the contents of a variable you must prepend the name with a $.

Numeric variables (eg. like $1, etc.) are positional vari- ables for argument communication.

    • Variable Assignment

Assign a value to a variable by variable=value. For example:

    PATH=/usr/ucb:/usr/bin:/bin; export PATH

or

    TODAY=`(set \`date\`; echo $1)`

    • Exporting Variables

Variables are not exported to children unless explicitly marked.

    # We MUST have a DISPLAY environment variable

 

    if [ "$DISPLAY" = "" ]; then

            if tty -s ; then

     echo "DISPLAY (`hostname`:0.0)? \c";

     read DISPLAY

            fi

            if [ "$DISPLAY" = "" ]; then

     DISPLAY=`hostname`:0.0

            fi

            export DISPLAY

    fi

Likewise, for variables like the PRINTER which you want hon- ored by lpr(1). From a user's .profile:

    PRINTER=PostScript; export PRINTER

Note: that the Cshell exports all environment variables.

    • Referencing Variables

Use $variable (or, if necessary, ${variable}) to reference the value.

    # Most user's have a /bin of their own

 

    if [ "$USER" != "root" ]; then

            PATH=$HOME/bin:$PATH

    else

            PATH=/etc:/usr/etc:$PATH

    fi

The braces are required for concatenation constructs.

$p_01

The value of the variable "p_01".

${p}_01

The value of the variable "p" with "_01" pasted onto the end.

    • Conditional Reference

o    ${variable-word}

If the variable has been set, use it's value, else use word.

POSTSCRIPT=${POSTSCRIPT-PostScript};

export POSTSCRIPT

 

${variable:-word}

If the variable has been set and is not null, use it's value, else use word.

These are useful constructions for honoring the user envi- ronment. Ie. the user of the script can override variable assignments. Cf. programs like lpr(1) honor the PRINTER environment variable, you can do the same trick with your shell scripts.

${variable:?word}

If variable is set use it's value, else print out word and exit. Useful for bailing out.

    • Arguments

Command line arguments to shell scripts are positional vari- ables:

$0, $1, ...

The command and arguments. With $0 the command and the rest the arguments.

$#

The number of arguments.

$*, $@

All the arguments as a blank separated string. Watch out for "$*" vs. "$@". 
And, some commands:

shift

Shift the postional variables down one and decrement number of arguments.

set arg arg ...

Set the positional variables to the argument list.

Command line parsing uses shift:

    # parse argument list

 

    while [ $# -ge 1 ]; do

            case $1 in

         process arguments...

            esac

            shift

    done

A use of the set command:

    # figure out what day it is

 

    TODAY=`(set \`date\`; echo $1)`

 

    cd $SPOOL

 

    for i in `cat $LOGS`

    do

            mv $i $i.$TODAY

            cp /dev/null $i

            chmod 664 $i

    done

    • Special Variables

o    $$

Current process id. This is very useful for constructing temporary files.

         tmp=/tmp/cal0$$

         trap "rm -f $tmp /tmp/cal1$$ /tmp/cal2$$"

         trap exit 1 2 13 15

         /usr/lib/calprog >$tmp

 

$?

The exit status of the last command.

         $command

         # Run target file if no errors and ...

 

         if [ $? -eq 0 ]

         then

  etc...

         fi

 

  • Quotes/Special Characters

Special characters to terminate words:

      ; & ( ) | ^ < > new-line space tab

These are for command sequences, background jobs, etc. To quote any of these use a backslash (\) or bracket with quote marks ("" or '').

Single Quotes

Within single quotes all characters are quoted -- including the backslash. The result is one word.

 

         grep :${gid}: /etc/group | awk -F: '{print $1}'

Double Quotes

Within double quotes you have variable subsitution (ie. the dollar sign is interpreted) but no file name generation (ie. * and ? are quoted). The result is one word.

         if [ ! "${parent}" ]; then

           parent=${people}/${group}/${user}

         fi

Back Quotes

Back quotes mean run the command and substitute the output.

 

         if [ "`echo -n`" = "-n" ]; then

    n=""

    c="\c"

         else

    n="-n"

    c=""

         fi

and

         TODAY=`(set \`date\`; echo $1)`

  • Functions

Functions are a powerful feature that aren't used often enough. Syntax is

    name ()

    {

         commands

    }

For example:

 

    # Purge a directory

 

    _purge()

    {

            # there had better be a directory

 

            if [ ! -d $1 ]; then

     echo $1: No such directory 1>&2

     return

            fi

 

         etc...

    }

Within a function the positional parmeters $0, $1, etc. are the arguments to the function (not the arguments to the script).

Within a function use return instead of exit.

Functions are good for encapsulations. You can pipe, redi- rect input, etc. to functions. For example:

    # deal with a file, add people one at a time

 

    do_file()

    {

            while parse_one

 

            etc...

    }

 

    etc...

 

    # take standard input (or a specified file) and do it.

 

    if [ "$1" != "" ]; then

            cat $1 | do_file

    else

            do_file

    fi

  • Sourcing commands

You can execute shell scripts from within shell scripts. A couple of choices:

sh command

This runs the shell script as a separate shell. For example, on Sun machines in /etc/rc:

         sh /etc/rc.local

command

This runs the shell script from within the current shell script. For example:

         # Read in configuration information

         .  /etc/hostconfig

What are the virtues of each? What's the difference? The second form is useful for configuration files where environment variable are set for the script. For example:

    for HOST in $HOSTS; do

 

      # is there a config file for this host?

 

      if [ -r ${BACKUPHOME}/${HOST} ]; then

.  ${BACKUPHOME}/${HOST}

      fi

    etc...

Using configuration files in this manner makes it possible to write scripts that are automatically tailored for differ- ent situations.

Some Tricks

  • Test

The most powerful command is test(1).

    if test expression; then

 

         etc...

and (note the matching bracket argument)

    if [ expression ]; then

 

         etc...

On System V machines this is a builtin (check out the com- mand /bin/test).

On BSD systems (like the Suns) compare the command /usr/bin/test with /usr/bin/[.

Useful expressions are:

test { -w, -r, -x, -s, ... } filename

is file writeable, readable, executeable, empty, etc?

test n1 { -eq, -ne, -gt, ... } n2

are numbers equal, not equal, greater than, etc.?

test s1 { =, != } s2

Are strings the same or different?

test cond1 { -o, -a } cond2

Binary or; binary and; use ! for unary negation.

For example

    if [ $year -lt 1901  -o  $year -gt 2099 ]; then

         echo 1>&2 Year \"$year\" out of range

         exit 127

    fi

Learn this command inside out! It does a lot for you.

  • String matching

The test command provides limited string matching tests. A more powerful trick is to match strings with the case switch.

    # parse argument list

 

    while [ $# -ge 1 ]; do

            case $1 in

            -c*)    rate=`echo $1 | cut -c3-`;;

            -c)     shift;  rate=$1 ;;

            -p*)    prefix=`echo $1 | cut -c3-`;;

            -p)     shift;  prefix=$1 ;;

            -*)     echo $Usage; exit 1 ;;

            *)      disks=$*;       break   ;;

            esac

 

            shift

 

    done

Of course getopt would work much better.

  • SysV vs BSD echo

On BSD systems to get a prompt you'd say:

    echo -n Ok to procede?;  read ans

On SysV systems you'd say:

    echo Ok to procede? \c; read ans

In an effort to produce portable code we've been using:

    # figure out what kind of echo to use

 

    if [ "`echo -n`" = "-n" ]; then

            n="";  c="\c"

    else

            n="-n";     c=""

    fi

 

    etc...

 

    echo $n Ok to procede? $c; read ans

  • Is there a person?

The Unix tradition is that programs should execute as qui- etly as possible. Especially for pipelines, cron jobs, etc.

User prompts aren't required if there's no user.

    # If there's a person out there, prod him a bit.

 

    if tty -s; then

         echo Enter text end with \^D

    fi

The tradition also extends to output.

    # If the output is to a terminal, be verbose

 

    if tty -s <&1; then

         verbose=true

    else

         verbose=false

    fi

Beware: just because stdin is a tty that doesn't mean that stdout is too. User prompts should be directed to the user terminal.

    # If there's a person out there, prod him a bit.

 

    if tty -s; then

         echo Enter text end with \^D >&0

    fi

Have you ever had a program stop waiting for keyboard input when the output is directed elsewhere?

  • Creating Input

We're familiar with redirecting input. For example:

    # take standard input (or a specified file) and do it.

 

    if [ "$1" != "" ]; then

            cat $1 | do_file

    else

            do_file

    fi

alternatively, redirection from a file:

    # take standard input (or a specified file) and do it.

 

    if [ "$1" != "" ]; then

            do_file < $1

    else

            do_file

    fi

You can also construct files on the fly.

    rmail bsmtp <<$1@newshost.uwo.ca>

    rcpt to:

    data

    from: <$1@newshost.uwo.ca>

    to:

    Subject: Signon $2

 

    subscribe $2 Usenet Feeder at UWO

    .

    quit

    EOF

Note: that variables are expanded in the input.

  • String Manipulations

One of the more common things you'll need to do is parse strings. Some tricks

 

    TIME=`date | cut -c12-19`

 

    TIME=`date | sed 's/.* .* .* \(.*\) .* .*/\1/'`

 

    TIME=`date | awk '{print $4}'`

 

    TIME=`set \`date\`; echo $4`

 

    TIME=`date | (read u v w x y z; echo $x)`

With some care, redefining the input field separators can help.

 

    #!/bin/sh

    # convert IP number to in-addr.arpa name

 

    name()

    {    set `IFS=".";echo $1`

         echo $4.$3.$2.$1.in-addr.arpa

    }

 

    if [ $# -ne 1 ]; then

         echo 1>&2 Usage: bynum IP-address

         exit 127

    fi

 

    add=`name $1`

 

    nslookup < < EOF | grep "$add" | sed 's/.*= //'

    set type=any

    $add

    EOF

  • Debugging

The shell has a number of flags that make debugging easier:

sh -n command

Read the shell script but don't execute the commands. IE. check syntax.

sh -x command

Display commands and arguments as they're executed. In a lot of my shell scripts you'll see

    # Uncomment the next line for testing

    # set -x

Based on An Introduction to Shell Programing by:

Reg Quinton

Computing and Communications Services

The University of Western Ontario

London, Ontario N6A 5B7

Canada


Press here to return to the General Unix Software Menu.

Posted by 두장
2009. 5. 26. 17:36
Linux Run Level

RunLevel 0 : 시스템 종료(halt)
RunLevel 1 : 단일 사용자, 싱글 모드
RunLevel 2 : NFS를 지원하지 않는 다중 사용자 모드
RunLevel 3 : 모든 기능을 포함한 다중 사용자 모드(X윈도우 지원안함)
RunLevel 4 : 사용되지 않는 실행모드(사용자가 직접정의하여 사용)
RunLevel 5 : X윈도우 부팅, GUI환경
RunLevel 6 : 시스템 재부팅

RunLevel 3이라면 /etc/rc.d/rc3.d 디렉토리에 있는 심볼릭 링크 스크립트(실제 스크립트 파일은 /etc/rc.d/init.d 디렉토리에 있음)를 실행함.

init 명령어를 사용하여 RunLevel을 전환 할수 있다.

 [root@localhost ~]#  init 3

기본적으로 부팅되는 RunLevel을 변경하기 위해서는 /etc/inittab 파일을 수정해야 한다.

#

# inittab       This file describes how the INIT process should set up

#               the system in a certain run-level.

#

# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>

#               Modified for RHS Linux by Marc Ewing and Donnie Barnes

#

 

# Default runlevel. The runlevels used by RHS are:

#   0 - halt (Do NOT set initdefault to this)

#   1 - Single user mode

#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

#   3 - Full multiuser mode

#   4 - unused

#   5 - X11

#   6 - reboot (Do NOT set initdefault to this)

#

id:5:initdefault:     # 부팅시 기본적으로 적용되는 RunLevel

 

# System initialization.

si::sysinit:/etc/rc.d/rc.sysinit

 l0:0:wait:/etc/rc.d/rc 0

l1:1:wait:/etc/rc.d/rc 1

l2:2:wait:/etc/rc.d/rc 2

l3:3:wait:/etc/rc.d/rc 3

l4:4:wait:/etc/rc.d/rc 4


실행 레벨 설정 (chkconfig 명령어)

 [root@localhost ~]#  chkconfig --list 
모든 데몬들의 실행 레벨을 확인

 [root@localhost ~]#  chkconfig --list httpd
httpd 데몬의 실행 레벨을 확인

 [root@localhost ~]#  chkconfig --level 2345 httpd on
httpd 데몬의 실행 레벨 2,3,4,5 번 활성화 시킴


Linux Root 계정의 PassWord를 잊어버렸을 경우 Linux 부팅시 RunLevel 1로 전환 후 부팅을 하면
Linux 부팅후 로그인 과정 없이 접속할수 있다.
 Linux 를 부팅시 커널 버전을 선택할수 있는 Grub 화면이 뜨면
부팅 하고자 하는 커널 버전을 선택한후 'e'를 입력하면
RunLevel을 수정할수 있는 화면이 나온다..
kernel /vmlinuz-2.6.9-34.EL ro root=LABEL=/ 1
여기서 위와 같이 "root=LABEL=/" 뒤에 원하는 RunLevel "1"을 추가한후 부팅하면 된다

Posted by 두장

리눅스에서 현재 시스템 시간 확인

# date


시스템의 하드웨어에 입력되어 있는 시간 출력

# clock


시스템 시간을 하드웨어 시간에 입력

# clock -w


실제 현재 시간과 시스템에 설정된 시간이 다를 경우가 있다.
이때 보라넷 서버 시간과 일치시키기 위해 아래와 같이 하면된다.
(보라넷 서버 시간은 실제 현재 시간과 일치하기때문에)

# rdate -s time.bora.net && clock -w


아래와 같이 하면 리눅스의 시간을 임의로 설정 할수도 있다.

다음은 2009년 06월 30일 15시 55분으로 시스템의 시간을 설정하는 명령어이다. 

# date 063015522009


Posted by 두장
정규표현식 (Regular Expression) 이란 무엇인가? 이 질문은 진짜 어렵다. 너도 나도 정규표현식을 얘기하지만 정작 정규표현식이 뭔지는 잘 안 나와 있다. (오토마타 수업을 들은 풍월로 굳이 얘기하자면 정규 문법에 의해 생성되는 정규 언어를 표현할 수 있는 표현식..이라고 하면 되려나) 어쨌거나, "하나 이상의 문자열을 한 번에 나타낼 수 있는 패턴"이 정규 표현식이다. 아래는 UNIX 서적이나 웬만한 웹사이트에 다 나오는 기본 정규 표현식이다.
a : 말 그대로 "a", b 는 당연히 "b", c 는...
. : 임의의 한 글자. 따라서 a.d 는 aad abd acd add aed afd...
[list] : list 중의 한 글자. 
  [adf] 는 a 또는 d 또는 f
  [a-f] 는 a, b, c, d, e, f
  [^adf] 는 a, d, f 를 제외한 나머지 중 한 글자. list 앞에 "^" 이 오면 뒤에 오는 것을 제외한 것을 의미한다.
  [^a-f] 는.. 말 안 해도 되겠지
 그러면 "^ 또는 a 또는 b"를 의미하고 싶을 때는? "^"를 list 의 제일 앞이 아닌 곳에 두면 된다.
  [ab^] - 마찬가지로 "-" 나 "]" 역시 [ab-] []df] 와 같이 쓴다.
* : 0번 이상의 임의 번 반복. a* 는 null string, a, aa, aaa, aaaa...

그런데 아무래도 저것만 가지고는 좀 불편하다. 그래서 "확장 정규 표현식"이 등장했다.

+ : 1번 이상의 임의 번 반복. a+ 는 a, aa, aaa, ... 즉 aa* 와 동일.
| : a|b 는 a 또는 b
() : group, (ab|cd)ef 는 abef 또는 cdef
? : 없거나 하나 있거나. ab? 는 a 또는 ab

위의 확장 정규 표현식은 Perl에서는 그냥 쓰면 되고, ViEditor 에서는 앞에 백슬래쉬를 붙여 a\+ 와 같이 사용한다.

임의 번 반복 대신이 구체적으로 숫자를 줄 수도 있다.

{n,m} : n번 이상 m번 이하 반복 (가능한 많이)
{n} : n번 반복
{n,} : n번 이상 반복 (가능한 많이)
{,m} : m번 이하 반복 (가능한 많이)
{} : 0 번 이상. * 와 동일

{-n,m} : n번 이상 m번 이하 (가능한 적게)
{-n} : n번
{-n,} : n번 이상 (가능한 적게)
{-,m} : m번 이하 (가능한 적게)
{-} : 0번 이상 (가능한 적게)

역시 vi 에서는 앞에 백슬래쉬를 붙여서 a\{2,5} 등과 같이 사용한다.

위에서 "가능한 많이"와 "가능한 적게"는 뭔가? abcdebcdebcde 라는 예로 들면, a.*d 는 abcdebcdebcd 에 매칭되고, a\{-}d 는 abcd 에 매칭된다.

참고:

Posted by 두장
linux vi 에디터에서 문자(열)을 찾아서 바꾸기
 :(시작줄),(끝줄)s/찾을패턴/바꿀스트링/옵션

- 시작줄, 끝줄 : 바꾸기를 할 범위를 행번호로 지정한다. "."는 현재 커서가 있는 행을 의미, "$"는 제일 마지막 행을 의미한다.
- 찾을 패턴, 바꿀 스트링 : 찾을 패턴은 정규 표현식으로 지정하고, 바꿀 스트링은 string지정한다.
- 옵션 : 
g (global) - 한줄에 일치하는 패턴이 여러개 나오면 모두 바꾼다. 지정하지 않으면 첫번째 패턴만 바꾼다.
i (ignore case) - 대소문자 구분을 하지 않는다
c (confirm) - 검색된 문자열에 대해서 바꿀지 말지를 물어본다.

Example
 :5,10s/a/b/      - 5번째 행부터 10번째 행까지 각 행의 첫번째 "a"를 "b"로 바꾼다.
 :.,.+10s/a/b/g  - 현재 행부터 (현재 행 번호 + 10)번째 행까지 모든 "a"를 "b"로 바꾼다.
 :1,$s/a/b/c     - 첫번째 행부터 마지막 행까지 (즉 문서 전체) 각 행의 "a"를 "b"로 바꾸되, 바꾸기 전 확인을 받는다.
 :%s/a/b/gi      - 문서 전체에서 "a"와 "A"를 "b"로 바꾼다.


Posted by 두장
보통의 경우그래픽 카드가 설치되어 있는 컴퓨터나 머신에는 직접 모니터를 연결하여 리눅스를 설치 할 것이다..

그럴 경우 시디롬으로 부팅한 후 부팅 옵션을 입력하는 화면이 나왔을 때 단지 Enter를 입력하거나 

"linux"라고 입력하고 GUI를 통해 리눅스를 설치한다.

하지만 그래픽 카드가 없는 컴퓨터나 머신에서 리눅스를 설치하기 위해서 모니를 직접 연결 할수 없으므로

콘솔을 터미널 서버에 연결해서 리눅스를 설치해야한다. 이때는 GUI를 통해 리눅스를 설치하는 것이 불가능하다..

이때도 똑같이 리눅스 시디를 시디롬에 넣고 시디롬 부팅을 한 후

부팅 옵션을 입력하라는 화면이 나오면 단순히 "linux"라고 해서는 안되고

이때 "linux text console=ttyS0,115200n8" 라고 부팅 옵션을 입력하면
("ttyS0, 115200n8" < -- 이부분은 각각의 머신마다 변경 될수 있다.)

텍스트 형식으로 화면을 보면서 리눅스를 설치할 수 있다.

이때는 설치 과정이 GUI로 마우스로 클릭할수 있는 화면이 아니라

설치과정이 text형식으로 나오기때문에 설치과정에 좀더 유의해야 한다...

Posted by 두장
2008. 12. 23. 13:36

1장. pthread_mutex_lock(3)

차례
1.1절. 사용법
1.2절. 설명
1.3절. 반환값
1.4절. 에러
1.5절. 예제
1.6절. 참고문헌

뮤텍스 잠금을 얻거나 해제한다.


1.1절. 사용법

#include <pthrad.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
		


1.2절. 설명

mutex는 MUTual EXclusion(상호 배제)devide의 줄임말로 쓰레드간 공유하는 데이터 영역을 보호하기 위해서 사용한다. 데이터 영역의 보호는 critical section(임계 영역)을 만들고 임계 영역내에 단하나의 쓰레드만이 진입가능 하도록 하는 방식을 사용한다.

보통 이 임계영역에는 보호하고자 하는 데이터에 대한 접근/수정 루틴이 들어간다. 데이터에 대한 접근/수정 루틴에 오직 하나의 쓰레드만 접근 가능하게 되므로 결국 데이터를 보호할 수 있게 된다.

뮤텍스는 단지 2개의 가능한 행동만이 정의되어 있다. unlock와 lock이 그건데, lock는 임계영역은 진입하기 위한 요청, unlock는 임계영역을 빠져나오면서 다른 쓰레드에게 임계영역을 되돌려주기 위해서 사용한다. 만약 쓰레드가 임계영역이 진입하기 위해서 lock를 시도 했는데, 다른 쓰레드가 이미 임계영역에 진입했다면 해당 쓰레드가 unlock를 해서 임계영역을 빠져나오기 전까지 기다리게 된다.

mutex는 fast recursive의 2가지 종류가 지원된다. 이것은 lock을 얻은 쓰레드가 다시 lock를 얻을 수 있도록 할 것인지를 결정하기 위해서 사용한다. 기본적으로 mutex의 종류는 fast 상태로 시작된다. mutex 종류에 대한 자세한 내용은 pthread_mutexattr_init(3)을 참고하기 바란다.

pthread_mutex_t는 뮤텍스의 특징을 결정하기 위해서 사용한다.PTHREAD_MUTEX_INITIALIZER(fast mutex)와PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP(recursive mutexe), PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP(mutx 에러 체크용)의 3가지 상수가 준비되어 있다. 이중 하나를 선택하면 된다.

pthread_mutex_lock()는 (임계영역에 진입하기 위함)뮤텍스 잠금을 요청한다. 만약 뮤텍스의 최근 상태가 unlocked라면 쓰레드는 잠금을 얻고 임계영역에 진입하게 되고 리턴한다. 다른 쓰레드가 뮤텍스 잠금을 얻은 상태라면 잠금을 얻을 수 있을 때까지 기다리게 된다.

pthread_mutex_unlock()는 뮤텍스잠금을 되돌려준다. 만약 fast 뮤텍스라면 pthread_mutex_unlock()는 언제나 unlocked 상태를 되돌려준다. recursive 뮤텍스라면 잠겨있는 뮤텍스의 수를 감소시키고 이 수가 0이 된다면 뮤텍스잠금을 되돌려주게 된다.

pthread_mutex_destory()는 뮤텍스 객체를 삭제하고 자원을 되돌려준다. 더이상 사용하지 않는 뮤텍스는 반드시 이 함수를 이용해서 삭제하도록 하자. 리눅스에서 쓰레드는 뮤텍스 객체와 별개로 되어 있다. 그러므로 쓰레드가 종료되었다고 하더라도 뮤텍스 객체는 여전히 남아 있게 된다. 이 함수를 호출해야지만 뮤텍스 객체가 삭제 된다.


1.3절. 반환값

pthread_mutex_init()는 언제나 0을 리턴한다. 다른 뮤텍스 함수들은 성공했다면 0, 실패했다면 0이 아닌 수를 리턴하고 errno를 설정한다.


1.4절. 에러

pthread_mutex_lock()함수는 아래의 에러코드를 반환한다.

EINVAL

뮤텍스가 잘못 초기화 되었다.

EDEADLK

이미 잠금을 얻은 쓰레드가 다시 잠금을 요청할 때 (error checking 뮤텍스일 경우 사용할 수 있다)

pthread_mutex_trylock()함수는 아래의 에러코드를 반환한다.

EBUSY

뮤텍스가 잠겨 있어서 잠금을 얻을 수 없다.

EINVAL

뮤텍스가 잘못 초기화 되었다.


1.5절. 예제

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>

using namespace std;

void *ping(void *);
void *pong(void *);

pthread_mutex_t sync_mutex;
pthread_cond_t  sync_cond;

pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  gcond  = PTHREAD_COND_INITIALIZER;

int main()
{
    vector<void *(*)(void *)> thread_list;
    vector<pthread_t> tident(10); 
    int thresult;
    int status;
    int i;

    pthread_mutex_init(&sync_mutex, NULL);
    pthread_cond_init(&sync_cond, NULL);

    thread_list.push_back(pong);
    thread_list.push_back(ping);

    for(i = 0; i < thread_list.size(); i++ )
    {
        pthread_mutex_lock(&ync_mutex);
        if (pthread_create(&tident[i], NULL, thread_list[i], (void *)NULL) <0)
        {
            perror("error:");
            exit(0);
        }
        pthread_cond_wait(&sync_cond, &sync_mutex);
        pthread_mutex_unlock(&sync_mutex);
    }
    for (i = 0; i < tident.size(); i++)
    {
        pthread_join(tident[i], (void **)&status);
    }
}

void *ping(void *data)
{
    int i=0;
    pthread_mutex_lock(&sync_mutex);
    pthread_cond_signal(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        printf("%d : ping\n", i);
        pthread_cond_signal(&gcond);
        pthread_cond_wait(&gcond, &gmutex);
        pthread_mutex_unlock(&gmutex);
        usleep(random()%100);
        i++;
    }
}

void *pong(void *data)
{
    int i = 0;
    pthread_mutex_lock(&sync_mutex);
    sleep(1);
    pthread_cond_signal(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        pthread_cond_wait(&gcond, &gmutex);
        printf("%d : pong\n", i);
        pthread_cond_signal(&gcond);
        pthread_mutex_unlock(&gmutex);
        i++;
    }
}
		


Posted by 두장
article_Pthread_API_Reference위키 홈으로


Pthread API Reference

윤 상배

고친 과정
고침 0.9 2004년 6월 30일 12시
pthread 취소관련 api 추가
고침 0.8 2003년 10월 9일 12시
pthread 시그널 관련 api 추가

1. 소개

이 문서는 pthread 레퍼런스 문서이다. pthread 에서 제공하는 모든 함수의 레퍼런스를 제공하고 있지는 않지만, 자주 쓰일만한 대부분의 함수들은 정리되어 있음으로 참고할만한 가치가 있을것이다.

이 문서에 빠진 내용들은 계속 추가해 나갈 예정이다.


2. 기본 쓰레드 함수

주로 쓰레드 생성과 종료에 관련된 가장 기본적인 함수들이다.


2.1. pthread_create

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void *), void *arg);
			
쓰레드 생성을 위해서 사용한다. 첫번째 아규먼트인 thread 는 쓰레드가 성공적으로 생성되었을때 생성된 쓰레드를 식별하기 위해서 사용되는 쓰레드 식별자이다. 두번째 아규먼트인 attr 은 쓰레드 특성을 지정하기 위해서 사용하며, 기본 쓰레드 특성을 이용하고자 할경우에 NULL 을 사용한다. 3번째 아규먼트인 start_routine는 분기시켜서 실행할 쓰레드 함수이며, 4번째 아규먼는인 arg는 쓰레드 함수의 인자이다.

성공적으로 생성될경우 0을 리턴한다.

예제 : pthread_create.cc

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 쓰레드 함수
void *t_function(void *data)
{
    int id;
    int i = 0;
    id = *((int *)data);

    while(1)
    {
        printf("%d : %d\n", id, i);
        i++;
        sleep(1);
    }
}

int main()
{
    pthread_t p_thread[2];
    int thr_id;
    int status;
    int a = 1;
    int b = 2;

    // 쓰레드 생성 아규먼트로 1 을 넘긴다.  
    thr_id = pthread_create(&p_thread[0], NULL, t_function, (void *)&a);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }

    // 쓰레드 생성 아규먼트로 2 를 넘긴다. 
    thr_id = pthread_create(&p_thread[1], NULL, t_function, (void *)&b);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }

    // 쓰레드 종료를 기다린다. 
    pthread_join(p_thread[0], (void **)&status);
    pthread_join(p_thread[1], (void **)&status);

    return 0;
}
			
실행된 쓰레드에 대해서는 pthread_join 등의 함수를 이용해서 쓰레드 종료때까지 기다려줘야 한다. ptherad_join 은 일종의 fork 의 wait 와 비슷하게 작동하며, 쓰레드자원을 해제 시켜준다.


2.2. pthread_join

#include <pthread.h>
int pthread_join(pthread_t th, void **thread_return);
			
첫번째 아규먼트 th는 기다릴(join)할 쓰레드 식별자이며, 두번째 아규먼트 thread_return은 쓰레드의 리턴(return) 값이다. thread_return 이 NULL 이 아닐경우 해다 포인터로 쓰레드 리턴 값을 받아올수 있다.

pthread_joinc.c

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 쓰레드 함수 
// 1초를 기다린후 아규먼트^2 을 리턴한다. 
void *t_function(void *data)
{
    int num = *((int *)data);
    printf("num %d\n", num);
    sleep(1);
    return (void *)(num*num);
}

int main()
{
    pthread_t p_thread;
    int thr_id;
    int status;
    int a = 100;

    thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }
    // 쓰레드 식별자 p_thread 가 종료되길 기다렸다가 
    // 종료리턴값을 가져온다. 
    pthread_join(p_thread, (void *)&status);
    printf("thread join : %d\n", status);

    return 0;
}
			


2.3. pthread_detach

int pthread_detach(pthread_t th);
			
detach 는 "떼어내다" 라는 뜻을 가지며 main 쓰레드에서 pthread_create 를 이용해 생성된 쓰레드를 분리시킨다. 이 함수는 식별번호th인 쓰레드를 detach 시키는데, detach 되었을경우 해당(detach 된) 쓰레드가 종료될경우 pthread_joinc 을 호출하지 않더라도 즉시 모든 자원이 해제(free) 된다.

여기에서는 pthread_create 호출후 detach 하는 방법을 설명하고 있는데, pthread_create 호출시에 쓰레드가 detach 되도록 할수도 있다. 이에 대한 내용은 pthread_attr_setdetachstate 를 다루면서 설명하도록 하겠다.

예제 : pthread_detach.c

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 쓰레드 함수
// 1초를 기다린후 아규먼트^2 을 리턴한다.
void *t_function(void *data)
{
    char a[100000];
    int num = *((int *)data);
	printf("Thread Start\n");
    sleep(5);
	printf("Thread end\n");
}

int main()
{
    pthread_t p_thread;
    int thr_id;
    int status;
    int a = 100;

	printf("Before Thread\n"); 
    thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }
    // 식별번호 p_thread 를 가지는 쓰레드를 detach 
    // 시켜준다. 
    pthread_detach(p_thread);
    pause();
    return 0;
}
			
위의 쏘쓰 코드에서 detach 시켰을때와 그렇지 않았을때의 메모리 상황을 비교해보기 바란다. detatach 를 했을경우 프로세스의 메모리 사용율과 detache 를 주석 처리했을경우의 메모리 사용율의 변화를 서로 비교해보면 되는데, detach 를 사용하지 않았을경우 t_function 이 종료가 되더라도 자원이 해제되지 않음을 볼수 있을것이다. 테스트는 간단한 스크립트를 이용하도록 한다.
[root@localhost test]# while [ 1 ]; do ps -aux | grep pthread | grep -v grep | grep -v vim; sleep 1; done
root      2668  0.0  0.1  1436  292 pts/8    S    18:37   0:00 ./pthread_detach
root      2668  0.0  0.1  1436  292 pts/8    S    18:37   0:00 ./pthread_detach
			
위의 ps 내용에서 5번째 필드의 변화를 확인하면 된다.


2.4. pthread_exit

void pthread_exit(void *retval);
			
pthread_exit 는 현재 실행중인 쓰레드를 종료시키고자 할때 사용한다. 만약pthread_cleanup_push 가 정의되어 있다면, pthread_exit 가 호출될경우 cleanup handler 가 호출된다. 보통 이 cleanup handler 은 메모리를 정리하는 등의 일을 하게 된다.

예제 : pthread_exit.c

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

// 쓰레드 함수
// 1초를 기다린후 아규먼트^2 을 리턴한다.
void *t_function(void *data)
{
    int num = *((int *)data);
    int i = 0;
    while(1)
    {
        if (i == 3)
            pthread_exit(0);
        printf("loop %d\n", i);
        i++;
        sleep(1);
    }
}

int main()
{
    pthread_t p_thread;
    int thr_id;
    int status;
    int a = 100;


    thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }
    pthread_join(p_thread, (void **)&status);
    return 0;
}
			


2.5. pthread_cleanup_push

void pthrad_cleanup_push(void (*routine) (void *), void *arg);
			
이것은 cleanup handlers 를 인스톨하기 위해서 사용된다. pthread_exit(3) 가 호출되어서 쓰레드가 종료될때 pthread_cleanup_push 에 의해서 인스톨된 함수가 호출된다. routine이 쓰레드가 종료될때 호출되는 함수이다. arg는 아규먼트이다.

cleanup handlers 는 주로 자원을 되돌려주거나, mutex 잠금등의 해제를 위한 용도로 사용된다. 만약 mutex 영역에서 pthread_exit 가 호출되어 버릴경우 다른쓰레드에서 영원히 block 될수 있기 때문이다. 또한 malloc 으로 할당받은 메모리, 열린 파일지정자를 닫기 위해서도 사용한다.

예제 : pthread_cleanup.c

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

// 쓰레드 함수
// 1초를 기다린후 아규먼트^2 을 리턴한다.
//

char *mydata;
void cleanup(void *);
void *t_function(void *data)
{
    int num = *((int *)data);
    int i = 0;
    int a = 1;
    // cleanup handler 로 cleanup 함수를 
    // 지정한다. 
    pthread_cleanup_push(cleanup, (void *)&a);
    mydata = (char *)malloc(1000);
    while(1)
    {
        if (i == 3)
        {
            // pthread_exit 가 호출되면서 
            // cleanup 을 호출하게 된다. 
            pthread_exit(0);
            return 1;
        }
        printf("loop %d\n", i);
        i++;
        sleep(1);
    }
    pthread_cleanup_pop(0);
}


int main()
{
    pthread_t p_thread;
    int thr_id;
    int status;
    int a = 100;


    thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }
    pthread_join(p_thread, (void **)&status);
    printf("Join finish\n");
}

// cleanup handler
void cleanup(void *myarg)
{
    printf("thread is clean up\n");
    printf("resource free\n");
    free(mydata);
}
			


2.6. pthread_cleanup_pop

pthread_cleanup_push 와 함께 사용되며, install 된 cleanup handler 을 제거하기 위해서 사용된다.

void pthread_cleanup_pop(int execute);
			
만약 execute 가 0 이라면, pthread_cleanup_push 에 의해 인스톨된 cleanup handler 를 (실행시키지 않고)삭제만 시킨다. 0 이 아닌 숫자라면 cleanup handler 을 실행시키고 삭제 된다. 사용예제는 2.5절을 참고하라.

그리고 pthread_cleanup_push 와 pthread_cleanup_pop 은 반드시 같은 함수내의 같은 레벨의 블럭에서 한쌍으로 사용해야 한다.


2.7. pthread_self

pthread_t pthread_self(void);
			
pthread_self를 호출하는 현재 쓰래드의 쓰레드식별자를 되돌려준다.

예제 : pthread_self.c

#include <pthread.h>
#include <stdio.h>

void *func(void *a)
{
    pthread_t id;
    id = pthread_self();
    printf("->%d\n", id);
}

int main(int argc, char **argv)
{
    pthread_t p_thread;
    pthread_create(&p_thread, NULL, func, (void *)NULL);
    printf("%d\n", p_thread);
    pthread_create(&p_thread, NULL, func, (void *)NULL);
    printf("%d\n", p_thread);

	return 1;
}
			


3. 쓰레드 동기화 함수

쓰레드 동기화와 관련된 함수들이다.


3.1. pthread_mutex_init

int pthread_mutex_init(pthread_mutex_t * mutex, 
           const pthread_mutex_attr *attr); 
			
mutex 는 여러개의 쓰레드가 공유하는 데이타를 보호하기 위해서 사용되는 도구로써, 보호하고자 하는 데이타를 다루는 코드영역을 단지 한번에 하나의 쓰레드만 실행가능 하도록 하는 방법으로 공유되는 데이타를 보호한다. 이러한 코드영역(하나의 쓰레드만 점유가능한)을 critical section 이라고 하며, mutex 관련 API 를 이용해서 관리할수 있다.

pthread_mutex_init 는 mutex 객체를 초기화 시키기 위해서 사용한다. 첫번째 인자로 주어지는 mutex 객체 mutex를 초기화시키며, 두번째 인자인 attr 를 이용해서 mutex 특성을 변경할수 있다. 기본 mutex 특성을 이용하기 원한다면 NULL 을 사용하면 된다.

mutex 특성(종류) 에는 "fast", "recurisev", "error checking" 의 종류가 있으며, 기본으로 "fast" 가 사용된다.

// 뮤텍스 객체 선언
pthread_mutex_t mutex_lock;
...
void *t_function()
{
    pthread_mutex_lock(&mutex_lock);
    // critical section
    pthread_mutex_unlock(&mutex_lock);
}
int main()
{
    pthread_t p_thread;
    int state;
    // 뮤텍스 객체 초기화, 기본 특성으로 초기화 했음
    pthread_mutex_init(&mutex_lock, NULL);
    pthread_create(&p_thread, NULL, t_function, (void *)&a);
    ...
    pthread_joinc(&p_thread, (void **)&status);
}
			


3.2. pthread_mutex_destory

int pthread_mutex_destory(pthread_mutex_t *mutex);
			
인자로 주어진 뮤텍스 객체 mutex 를 제거하기 위해서 사용된다. mutex  pthread_mutex_init()함수를 이용해서 생성된 뮤텍스 객체이다.

pthread_mutex_destory 를 이용해서 제대로 mutex 를 삭제하려면 이 mutex 는 반드시 unlock 상태이여야 한다.


3.3. pthread_mutex_lock

int pthread_mutex_lock(pthread_mutex_t *mutex);
			
pthread_mutex_lock 는 critcal section 에 들어가기 위해서 mutex lock 을 요청한다. 만약 이미 다른 쓰레드에서 mutex lock 를 얻어서 사용하고 있다면 다른 쓰레드에서 mutex lock(뮤텍스 잠금) 을 해제할때까지(사용할수 있을때까지) 블럭 된다.

만약 다른 어떤 쓰레드에서도 mutex lock 을 사용하고 있지 않다면, 즉시 mutex lock 을 얻을수 있게 되고 critcal section 에 진입하게 된다. critcal section 에서의 모든 작업을 마쳐서 사용하고 있는 mutex lock 이 더이상 필요 없다면 pthread_mutex_unlock 를 호출해서 mtuex lock 를 되돌려준다.


3.4. pthread_mutex_unlock

int pthread_mutex_unlock(pthread_mutex_t *mutex); 
			
critical section 에서의 모든 작업을 마치고 mutex lock 을 돌려주기 위해서 사용한다. pthread_mutex_unlock 를 이용해서 mutex lock 를 되돌려주면 다른 쓰레드에서 mutex lock 를 얻을수 있는 상태가 된다.


3.5. pthread_cond_init

int pthread_cond_init(pthread_cond_t *cond, 
                    const pthread_cond_attr *attr);
			
pthread_cond_init는 조견변수 (condition variable)cond를 초기화하기 위해서 사용한다. attr 를 이용해서 조건변수의 특성을 변경할수 있으며, NULL 을 줄경우 기본특성으로 초기화된다.

조건변수 cond는 상수 PTHREAD_COND_INITIALIZER 을 이용해서도 초기화 할수 있다. 즉 다음과 같은 2가지 초기화 방법이 존재한다.

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
or
pthread_cond_init(&cond, NULL);
			


3.6. pthread_cond_signal

int pthread_cond_signal(pthread_cond_t *cond);
			
조건변수 cond에 시그날을 보낸다. 시그날을 보낼경우 cond에서 기다리는(wait) 쓰레드가 있다면 쓰레드를 깨우게 된다(봉쇄가 풀림). 만약 조건변수 cond를 기다리는 쓰레드가 없다면, 아무런 일도 일어나지 않게되며, 여러개의 쓰레드가 기다리고 있다면 그중 하나의 쓰레드에게만 전달된다. 이때 어떤 쓰레드에게 신호가 전달될지는 알수 없다.


3.7. pthread_cond_boradcast

int pthread_cond_broadcast(pthread_cond_t *cond);
			
조건변수 cond에서 기다리는(wait) 모든 쓰레드에게 신호를 보내서, 깨운다는 점을 제외하고는pthread_cond_signal과 동일하게 작동한다.


3.8. pthread_cond_wait

int pthread_cond_wait(pthread_cond_t cond, pthread_mutex_t *mutex); 
			
조건변수 cond를 통해서 신호가 전달될때까지 블럭된다. 만약 신호가 전달되지 않는다면 영원히 블럭될수도 있다. pthread_cond_wait는 블럭되기 전에 mutex 잠금을 자동으로 되돌려준다.


3.9. pthread_cond_timewait

int pthread_cond_timedwait(pthread_cont_t *cond, pthread_mutex_t *mutex, 
                           const struct timespec *abstime);
			
조건변수 cond를 통해서 신호가 전달될때까지 블럭되며 자동으로 mutex을 돌려주는 점에서는pthread_cond_wait와 동일하다. 그러나 시간체크가 가능해서 abstime시간동안 신호가 도착하지 않는다면 error 를 발생하면서 리턴한다. 이때 리턴값은 ETIMEDOUT 이다. errno 가 세팅되는게 아닌, 리턴값으로 에러가 넘어오는것에 주의해야 한다.

또한 pthread_cond_timedwait함수는 다른 signal 에 의해서 interrupted 될수 있으며 이때 EINTR 을 리턴한다. 이 함수를 쓸때는 interrupted 상황에 대한 처리를 해주어야 한다.


3.10. pthread_cond_destroy

int pthread_cond_destroy(pthread_cond_t *cond);
			
pthread_cond_init를 통해서 생성한 조건변수cond에 대한 자원을 해제한다. destroy 함수를 호출하기 전에 어떤 쓰레드도 cond에서의 시그널을 기다리지 않는걸 확인해야 한다. 만약 cond 시그널을 기다리는 쓰레드가 존재한다면 이 함수는 실패하고 EBUSY 를 리턴한다.


3.11. 예제코드

이번장에서 설명한 쓰레드 동기화 관련 함수의 이해를 돕기 위해서 간단한 예제를 준비했다. 설명은 주석으로 대신한다.

예제 : pthrad_sync_api.c

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>

using namespace std;

void *ping(void *);
void *pong(void *);

pthread_mutex_t sync_mutex;
pthread_cond_t  sync_cond;

pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  gcond  = PTHREAD_COND_INITIALIZER;

int main()
{
    vector<void *(*)(void *)> thread_list;
    vector<pthread_t> tident(10); 
    int thresult;
    int status;
    int i;

    pthread_mutex_init(&sync_mutex, NULL);
    pthread_cond_init(&sync_cond, NULL);

    thread_list.push_back(pong);
    thread_list.push_back(ping);

    for(i = 0; i < thread_list.size(); i++ )
    {
        pthread_mutex_lock(&sync_mutex);
        if (pthread_create(&tident[i], NULL, thread_list[i], (void *)NULL) <0)
        {
            perror("error:");
            exit(0);
        }
        pthread_cond_wait(&sync_cond, &sync_mutex);
        pthread_mutex_unlock(&sync_mutex);
    }
    for (i = 0; i < tident.size(); i++)
    {
        pthread_join(tident[i], (void **)&status);
    }
}

void *ping(void *data)
{
    int i=0;
    pthread_mutex_lock(&sync_mutex);
    pthread_cond_signal(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        printf("%d : ping\n", i);
        pthread_cond_signal(&gcond);
        pthread_cond_wait(&gcond, &gmutex);
        pthread_mutex_unlock(&gmutex);
        usleep(random()%100);
        i++;
    }
}

void *pong(void *data)
{
    int i = 0;
    pthread_mutex_lock(&sync_mutex);
    sleep(1);
    pthread_cond_signal(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        pthread_cond_wait(&gcond, &gmutex);
        printf("%d : pong\n", i);
        pthread_cond_signal(&gcond);
        pthread_mutex_unlock(&gmutex);
        i++;
    }
}
			

위의 예제는 ping&pong 프로그램으로 ping 쓰레드와 pong 쓰레드가 각각 번갈아가면서 "ping", "pong" 을 날리는 프로그램이다. 2개의 영역에 걸쳐서 크리티컬섹션이 지정되어 있으며 각 크리티컬섹션안에는 쓰레드 동기화를 위해서 ptread_cond_signal 이 쓰여지고 있다.

위의 코드는 기본적으로 pong 쓰레드가 먼저 시그널을 대기하고 있다가 그 후 ping 쓰레드가 진입해서 "ping"을 날리고 시그널을 발생시키면 "pong" 메시지를 발생시키도록 되어 있다. 그렇다면 while 문에 있는 크리티컬 섹션에 반드시 pong 쓰레드가 먼저 진입할수 있도록 만들어줘야 할것이다. 그래서 위의 코드에서는 pong 쓰레드를 먼저 생성시켰다. 그러나 이것만으로는 충분하지 않다. 예를들어서 pong 쓰레드에서 크리티컬섹션에 들어가기 위해서 어떤 부가적인 작업이 있다고 했을때(메모리초기화, 기타 다른 함수 호출과 같은, 위에서는 sleep 으로 대신했다), 우리가 의도했던 바와는 다르게 ping 가 먼저 크리티컬섹션에 진입할수도 있다. 이럴경우 2개의 쓰레드는 교착상태에 빠지게 된다.

ping 쓰레드가 크리티컬섹션에 먼저 진입했을경우 ping 쓰레드는 "ping" 출력시키고 시그널을 발생시킬 것이고 pong 쓰레드가 "pong"를 출력시키고 시그널을 발생시킬때까지 시그널대기 하게 된다. ping 쓰레드가 시그널대기 하게 되면, 크리티컬섹션에 대한 뮤텍스 잠금이 해제됨으로 뒤늦게 크리티컬섹셔네 진입을 시도하던 pong 가 크리티컬섹션에 진입하고 ping 쓰레드에서부터 신호가 있는지 기다리게 될것이다. 그러나 ping 쓰레드는 이미 신호를 날려버렸음으로, pong 쓰레드는 결코 도착하지 않을 신호를 기다리며 영원히 시그널대기 하게 될것이다. 이런식으로 2개의 쓰레드는 교착상태에 빠져 버린다.

이 문제는 쓰레드간 동기화를 이용해서 해결할수 있으며, 위 코드에서는 mutex 잠금과, 조건변수를 이용해서 해결하고 있다. 물론 쓰레드간 동기화를 위해서 사용할수 있는 원시?적인 방법으로 sleep 나 usleep 같은 함수를 호출하는 방법도 있긴 하지만, ping 쓰레드에서 크리티컬 섹션에 진입하기전 1초 정도 sleep 을 주는 식으로 사용가능하지만 추천할만하진 않다. (간혹 간단하게 사용할수는 으며, 가장 확실한 방법을 제공해 주기도 한다)


4. Thread Attribute 함수

4.1. pthread_attr_init

int pthread_attr_init(pthread_attr_t *attr);
			
pthread_attr_init는 thread attribute 객체인 attr을 디폴트 값으로 초기화 시킨다.

성공할경우 0을 돌려주고 실패할경우 -1 을 되돌려준다.


4.2. pthread_attr_distroy

int pthread_attr_destroy(pthread_attr_t *attr);
			
pthread_attr_init에 의해 생성된 thread attribute 객체인 attr을 제거한다. 제거된 attr 을 다시 사용하기 위해서는 pthread_attr_init를 이용해서 다시 초기화 해주어야 한다.


4.3. pthread_attr_getscope

int pthread_attr_getscope(const pthread_attr_t *attr, 
             int *scope);
			
쓰레드가 어떤 영역(scope)에서 다루어지고 있는지를 얻어오기 위해서 사용된다. PTHREAD_SCOPE_SYSTEM과 PTHREAD_SCOPE_PROCESS 의 2가지 영역중에 선택할수 있다. SYSTEM 영역 쓰레드는 user 모드 쓰레드라고 불리우며, PROCESS 쓰레드는 커널모드 쓰레드라고 불리운다. 리눅스의 경우 유저모드 쓰레드인데, 즉 커널에서 쓰레드를 스케쥴링하는 방식이 아닌 쓰레드 라이브러리를 통해서 쓰레드를 스케쥴링 하는 방식을 사용한다.

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>


int main()
{
    pthread_attr_t pattr;
    int scope;

    pthread_attr_init(&pattr);

    pthread_attr_getscope(&pattr, &scope);
    if (scope == PTHREAD_SCOPE_SYSTEM)
    {
        printf("user mode thread\n");
    }
    else if (scope ==  PTHREAD_SCOPE_PROCESS)
    {
        printf("Kernel mode thread\n");
    }

    return 1;
}
			
위 프로그램을 컴파일한후 Linux 에서 실행시키면 "user mode thread"를 출력하고 솔라리스 상에서 실행시키면 "kernel mode thread"를 출력한다.


4.4. pthread_attr_setscope

int pthread_attr_setscope(pthread_attr_t *attr, int scope);
			
쓰레드가 어떤 영역(scope)에서 작동하게 할것인지 결정하기 위해서 사용한다. 리눅스의 경우 Kernel mode 쓰레드를 지원하지 않음으로 오직 PTHREAD_SCOPE_SYSTEM 만을 선택할수 있다. 반면 솔라리스는 유저모드와 커널모드중 선택이 가능하다.

pthread_attr_setscope.c

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>


int main()
{
    pthread_attr_t pattr;
    int scope;

    pthread_attr_init(&pattr);

    pthread_attr_setscope(&pattr, PTHREAD_SCOPE_PROCESS);
    pthread_attr_getscope(&pattr, &scope);
    if (scope == PTHREAD_SCOPE_SYSTEM)
    {
        printf("user mode thread\n");
    }
    else if (scope ==  PTHREAD_SCOPE_PROCESS)
    {
        printf("Kernel mode thread\n");
    }

    return 1;
}
			
위코드에서 쓰레드가 커널 모드에서 작동하도록 지정을 했다. 리눅스에서 실행시킬경우에는 비록 커널모드로 지정을 했다고 하더라도 유저모드 쓰레드로 작동하게 된다. 솔라리스의 경우에는 setscope 로 지정한대로 커널모드에서 작동하게 된다.


4.5. pthread_attr_getdetachstate

int pthread_attr_getdetachstate(pthread_attr_t *attr,
           int detachstate);
			
쓰레드가 join 가능한 상태(PTHREAD_CREATE_JOINABLE) 인지 detached 상태인지 (PTHREAD_CREATE_DETACHED) 인지를 알아낸다. 알아낸 값은 아규먼트 detachstate 에 저장된다.

기본은 PTHREAD_CREATE_JOINABLE 이며, pthread_detach를 이용해서 생성된 쓰레드를 detach 상태로 만들었을경우 또는 pthread_attr_setdetachstate함수를 이용해서 쓰레드를 detache 상태로 변경시켰을경우 PTHREAD_CREATE_DETACHED 상태가 된다.

예제 : pthread_attr_getdetachstate.c

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

pthread_attr_t attr;
void *test(void *a)
{
    int policy;
    printf("Thread Create\n");
    pthread_attr_getdetachstate(&attr, &policy);
    if (policy == PTHREAD_CREATE_JOINABLE)
    {
        printf ("Join able\n");
    }
    else if (policy == PTHREAD_CREATE_DETACHED)
    {
        printf ("Detache\n");
    }
}
int main()
{
    int status;
    pthread_t p_thread;
    pthread_attr_init(&attr);
    if (pthread_create(&p_thread, NULL, test, (void *)NULL) < 0)
    {
        exit(0);
    }

    pthread_join(p_thread, (void **)&status);
}
			
위의 프로그램을 실행시키면 분명 "Join able"를 출력할것이다.


4.6. pthread_attr_setdetachstate

int  pthread_attr_setdetachstate(pthread_attr_t *attr, 
             int detachstate);
			
쓰레드의 상태를 PTHREAD_CREATE_JOINABLE 혹은 PTHREAD_CREATE_DETACHED 상태로 변경시키기 위해서 사용된다. 아래와 같은 방법으로 사용하면 된다.
pthread_attr_t attr;
...
// JOINABLE 상태로 변경하고자 할때 
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// DETACHED 상태로 변경하고자 할때
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
			


5. 쓰레드 시그널 관련

쓰레드간 프로세스와 쓰레드간 시그널 전달관련 API들이다. 자세한 내용은 쓰레드와 시그널을 참고하기 바란다.


5.1. pthread_sigmask

#include <pthread.h>
#include <signal.h>

int pthread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask);
			
쓰레드에서 시그널은 서로 공유된다. 그런이유로 만약 프로세스에 시그널이 전달되면 프로세스가 생성된 모든 쓰레드로 시그널이 전달된다. 그러나 특정 쓰레드만 시그널을 받도록 하고 싶을 때가 있을 것이다. 이경우 이 함수를 이용하면 된다.


5.2. pthread_kill

#include <pthread.h>
#include <signal.h>

int pthread_kill(pthread_t thread, int signo);
			
쓰레드 식별번호 thread signo 번호의 시그널을 전달한다.


5.3. sigwait

#include <pthread.h>
#include >signal.h>

int sigwait(const sigset_t *set, int *sig);
			
시그널 전달을 동기적으로 기다린다.


6. 쓰레드 취소

자세한 내용은 쓰레드 취소와 종료 pthread_cancel(3)을 참고하기 바란다. 여기에서는 인덱스만 제공한다.


6.1. pthread_cancel

#include <pthread.h>

int pthread_cancel(pthread_t thread);
			


6.2. pthread_setcancelstate

#include <pthread.h>

int pthread_setcancelstate(int state, int *oldstate);
			


6.3. pthread_setcancelstate

#include <pthread.h>

int pthread_setcancelstate(int state, int *oldstate);
			


6.4. pthread_setcanceltype

#include <pthread.h>

int pthread_setcanceltype(int type, int *oldtype);
			


6.5. pthread_testcancel

#include <pthread.h>

void pthread_testcancel(void);
			

참고 문서 #

  1. Pthread 뮤텍스와 조건변수
  2. pthread 1
  3. pthread 모델
  4. pthread 3
Posted by 두장

 문자 평가 함수 

① 문자 평가 함수들은 한 개 문자의 종류를 검사 또는 평가하는 함수로서 1개의 문자가 숫자인지, 문자인지, 제어문자인지, 대문자, 소문자인지의 여부를 평가한다. 

② 문자평가함수는 is-로 시작되며, is-로 시작되는 함수는 헤더파일인"ctype.h"에 정의되어 있다. 

(1)isalnum( ) 함수 

① 영문자(A∼Z, a∼z) 또는 숫자(0∼9)인지를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을 돌려준다.

② 형식 

#include <ctype.h>

int isalnum(c);

int c;


【 예제13-1 】하나의 문자를 읽어 들여 영문자 또는 숫자이면 alphanumeric를 출력하는 프로그램(ch13-1.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input data : ");

c = getchar(); 

if(isalnum(c) != 0)

printf("\n %c = alphanumeric\n", c);

else

printf("\n %c = Not alphanumeric\n", c);

getch();

}

▶ 실행결과

Input data : a

a = alphanumeric

Input data : #

a = Not alphanumeric



(2)isalpha( ) 함수 

① 영문자(A∼Z, a∼z)인가를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을 돌려준다.

② 형식

#include <ctype.h>

int isalpha(c);

int c;


【 예제13-2 】하나의 문자를 읽어 들여 영문자이면 alphabetic을 출력하는 프로그램(ch13-2.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input data : ");

c = getchar(); 

if(isalpha(c) != 0)

printf("\n %c = alphabetic\n", c);

else

printf("\n %c = Not alphabetic\n", c); 

getch();

}

▶ 실행결과

Input data : a

a = alphabetic

Input data : 3

a = Not alphabetic



(3) iscntrl( ) 함수 

① 제어문자인가를 평가하여 제어문자로 참이면 0이외의 값을 돌려주고, 거짓이면 0을

돌려준다.

② 형식

#include <ctype.h>

int iscntrl(c);

int c;


【 예제13-3 】하나의 문자를 읽어 들여 제어문자이면 Input data is control character를 출력하는 프로그램(ch13-3.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input data :");

c = getchar(); 

if(iscntrl(c) != 0)

printf("input data is conrol character\n");

else

printf("input data is not control character\n"); 

getch();

}

▶ 실행결과

Input data : a

Input data is not control character

Input data : ^A

Input data is control character



(4) isdigit( ) 함수 

① 0∼9인가를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을 돌려준다.

② 형식

#include <ctype.h>

int isdigit(c);

int c;


【 예제13-4 】하나의 문자를 읽어들여 숫자이면 digit을 출력하는 프로그램(ch13-4.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr();

printf("Input data :"); 

if(isdigit(c) != 0)

printf("\n %d = digit\n", c);

else

printf("\n %d = Not digit\n", c); 

getch();

}

▶ 실행결과

Input data : a

a = Not digit 

Input data : 9

a = digit



(5) isupper( ) 함수 

① 영문 대문자(A∼Z)인가를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을 돌려준다.

② 형식

#include <ctype.h>

int isupper(c);

int c;

【 예제13-5 】하나의 문자를 읽어 들여 영문 대문자이면 upper alphabetic을 평가하는 프로그램(ch13-5.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr();

printf("Input data :"); 

c = getchar(); 

if(isupper(c) != 0)

printf("\n %c = upper alphabetic\n", c);

else

printf("\n %c = Not upper alphanumeric\n", c); 

getch();

}

▶ 실행결과

Input data : a

a = Not upper alphabetic

Input data : A

a = upper alphabetic



(6) islower( ) 함수 

① 영문 소문자(a∼z)인가를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을

돌려준다.

② 형식

#include <ctype.h>

int islower(c);

int c;

【 예제13-6 】다음과 같이 선언한 문자 중에서 소문자들을 출력하는 프로그램(ch13-6.c)

char a[9] = {'K', 'b', 's', 'M', 'b', 'c', 'S', 'b', 's'};

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

char c[9] = {'K', 'b', 's', 'M', 'b', 'c', 'S', 'b', 's'};

int i;

clrscr();

for(i=0; i<9; i++)

if(islower(c[i]) != 0)

printf("%c", c[i]);

getch();

}

▶ 실행결과

bsbcbs



(7) isspace( ) 함수 

① 공백인가를 평가하여 참이면 0이외의 값을 돌려주고, 거짓이면 0을 돌려준다.

② 형식

#include <ctype.h>

int isspace(c);

int c;


【 예제13-7 】공백이 입력되기 전까지 문자를 출력하는 프로그램(ch13-7.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

char c;

clrscr();

printf("Input string : ");

c = getchar();

do

{

putchar(c);

c = getchar();

}

while(isspace(c) == 0);

getch();

}

▶ 실행결과

Input string : seoul korea

seoul




13-2. 문자 변환 함수 

① 소문자를 대문자로, 대문자를 소문자로 변환하거나 또는 ASCII 코드가 아닌 값을 ASCII 코드 값으로 바꾸어 준다.

② 문자변환함수는 헤더파일인 "ctype.h"에 정의되어 있다. 

(1) toascii( ) 함수 

① ASCII 코드로 변환시켜 주는 함수

② 형식

#include <ctype.h>

int toascii(c);

int c;


【 예제13-8 】지정 문자를 ASCII 코드로 변환시켜 주는 프로그램(ch13-8.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input character :");

c = getchar();

printf("ASCII CODE = %x\n", toascii(c));

printf("DECIMAL NUMBER = %d\n", c);

getch();

}

▶ 실행결과

Input character : A

ASCII CODE = 41

DECIMAL NUMBER = 65

(2) tolower( ) 함수 

① c가 대문자이면 소문자로 변환시켜 준다.

② 형식 

#include <ctype.h>

int tolower(c);

int c;



【 예제13-9 】지정문자가 대문자이면 소문자로 변환시켜 주는 프로그램(ch13-9.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input sring :"); 

while((c = getchar()) != EOF)

putchar(tolower(c)); 

getch();

}

▶ 실행결과

input string : MBC

mbc


(3) toupper( ) 함수 

① c가 소문자이면 대문자로 변환시켜 준다.

② 형식 

#include <ctype.h>

int toupper(c);

int c;



【 예제13-10 】지정문자가 소문자이면 대문자로 변환시켜 주는 프로그램(ch13-10.c)

▶ 프로그램

#include <stdio.h>

#include <ctype.h>

main()

{

int c;

clrscr(); 

printf("Input sring :"); 

while((c = getchar()) != EOF)

putchar(toupper(c)); 

getch();

}

▶ 실행결과

input string : mbc

MBC


13-3. 문자열의 조작함수 

문자열 조작 함수는 헤더파일인 "string.h"에 정의되어 있다. 

(1) strcpy( ) 함수 및 strncpy( ) 함수 

① 문자열을 복사하는 함수

② 형식

#include <string.h>

char *strcpy(cd, str);

char *strncpy(cd, str, len);

char *cd; ☞ 문자열이 복사되는 배열

char *str; ☞ 복사하는 문자열

int len; ☞ 복사하는 문자열의 길이

③ strcpy( )함수

"cd"가 가리키는 버퍼에 "str"이 가리키는 문자열을 복사하고, 함수값으로 "cd"의 값을 반환한다.

④ strncpy( ) 함수

앞에서부터 최대 len 문자까지를 복사한다. 이때 "cd"는 "복사 문자열의 길이 + 1"크기 이상을 확보해 두어야 한다. 

【 예제13-11 】문자열 str을 배열 cd에 copy하는 프로그램(ch13-11.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char cd[20] = " ";

char *str="Temple of the king";

clrscr();

printf("Before cd[20] data = %s\n", cd);

strcpy(cd, str);

printf("After cd[20] data = %s\n", cd);

getch();

}

▶ 실행결과

Before cd[20] data =

After cd[20] data = Temple of the king

【 예제13-12 】문자열 str에서 6개의 문자를 cd에 복사하는 프로그램(ch13-12.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *cd;

char *str="Temple of the king";

int len = 6;

clrscr(); 

printf("Original *str data = %s\n", str);

printf("Before cd[20] data = %s\n", cd); 

strncpy(cd, str, len);

printf("After cd[20] data = %s\n", cd); 

getch();

}

▶ 실행결과

Original *str data = Temple of the king

Before cd[20] data =

After cd[20] data = Temple



(2) strcat( ) 및 strncat( ) 함수 

① 문자열을 연결시키는 함수

② 형식

#include <string.h>

char *strcat(buf, str);

char *strncat(buf, str, len);

char *cd;

char *str;

char *buf;

int len;

③ strcat( ) 함수

"buf"가 가리키는 버퍼에 들어있는 문자열의 뒷부분에 "str"이 가리키는 문자열을 연결시킨다.

④ strncat( ) 함수

"str"이 가리키는 문자열의 최대 "len" 문자까지만 "buf"가 가리키는 버퍼의 문자열에 연결시켜 준다.

【 예제13-13 】문자열 buf와 문자열 str을 연결하여 출력하는 프로그램이다.(ch13-13.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *buf = "Temple";

char *str=" of the king";

clrscr(); 

printf("Original *buf data = %s\n", buf);

printf("Original *str data = %s\n", str); 

strcat(buf, str);

printf("After *buf data = %s\n", buf);

getch();

}

▶ 실행결과

Original *buf data = Temple

Original *str data = of the king

After *buf data = Temple of the king


【 예제13-14 】문자열 buf와 문자열 str의 11개의 문자를 연결하는 프로그램(ch13-14.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *buf = "Temple";

char *str=" of the king";

char *p;

int len = 11;

clrscr(); 

printf("Original *buf data = %s\n", buf);

printf("Original *str data = %s\n", str); 

p = strncat(buf, str, len); 

printf("After *p data = %s\n", p);

getch();

}

▶ 실행결과

Original *buf data = Temple

Original *str data = of the king

After *p data = Temple of the kin

(3) strchr( ) 함수 및 strrchr( ) 함수 

① 지정된 문자의 최초 위치 탐색과 최후 위치 탐색을 위한 함수

② 형식

#include <string.h>

char *strchr(string, c);

char *strrchr(string, c);

char *string; ☞ 탐색되는 문자열

char *c; ☞ 찾고 싶은 문자열

③ strchr( ) 함수

"string"내에서 찾고 싶은 문자 c의 내용이 발견되면 발견된 최초의 위치를 반환하고 발견되지 않으면 null 값을 반환한다. 즉, 최초의 문자가 발견된 위치를 반환시켜 준다.

④ strrchr( ) 함수

"string"내에서 찾고 싶은 문자 c의 내용이 발견되면 발견된 최후 위치를 반환하고 발견되지 않으면 null값을 반환한다. 즉, 최후의 문자가 발견된 위치를 반환시켜 준다. 

【 예제13-15 】문자열 string에서 e가 발견된 최초의 주소를 구하는 프로그램(ch13-15.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="Temple of the king";

char *c;

clrscr(); 

printf("*string data = %s\n", string);

printf("*string, address = %x\n", string); 

c = strchr(string, 'e');

printf("'e', address = %x\n", c);

printf("c = %s\n", c); 

getch();

}

▶ 실행결과

*string data = Temple of the king

*string, address = 194

'e', address = 195

【 예제13-16 】문자열 string에서 e가 기억된 마지막 주소를 구하는 프로그램(ch13-16.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="Temple of the king";

char *c;

clrscr();

printf("*string data = %s\n", string);

printf("*string, address = %x\n", string); 

c = strrchr(string, 'e');

printf("last address,'e' = %x\n", c);

getch();

}

▶ 실행결과

*string data = Temple of the king

*string, address = 194

last address,'e' = 1a0



(4) strcmp( ) 함수 및 strncmp( ) 함수 

① 문자열을 비교하는 함수로 앞에서 한 개의 문자씩 사전식 순서로 비교하여 그 결과를

반환한다.

② 형식

#include <string.h>

int strcmp(string1, string2);

int strncmp(string1, string2, size);

char *string1, *string2; ☞ 비교할 문자열

unsigned size; ☞ 비교할 길이

③ 2개의 문자열을 비교한 후 다음과 같이 결과를 반환한다.

  • string1 > string2 이면 양수값을 반환한다.
  • string1 = string2 이면 0을 반환한다.
  • string1 < string2 이면 음수값을 반환한다.

④ strcmp(p, q) 함수

문자열 p와 q를 비교하여 p의 문자열이 알파벳순으로 q보다 앞이면 음수값, 뒤이면 양수값을 반환한다.

【 예제13-17 】문자열 string1과 문자열 string2를 비교하는 프로그램(ch13-17.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string1="America";

char *string2="Korea";

int i;

clrscr(); 

printf("*string1 data = %s\n", string1);

printf("*string2 data = %s\n", string2); 

i = strcmp(string1, string2);

printf("value i = %d\n", i); 

if(i != 0)

printf("string1 not equal to string2\n");

else

printf("string1 equal to string2\n"); 

getch();

}

▶ 실행결과

*string1 data = America

*string2 data = Korea

value i = -10

string1 not equal to string2


【 예제13-18 】문자열 string1과 문자열 string2를 비교하는 프로그램(ch13-18.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string1="Banana";

char *string2="Bingo";

int i;

unsigned len = 5;

clrscr(); 

printf("*string1 data = %s\n", string1);

printf("*string2 data = %s\n", string2); 

i = strncmp(string1, string2, len);

printf("value i = %d\n", i); 

if(i != 0)

printf("string1 not equal to string2\n");

else

printf("string1 equal to string2\n"); 

getch();

}

▶ 실행결과

*string1 data = Banana

*string2 data = Bingo

value i = -8

string1 not equal to string2

(5) strset( )함수 및 strnset( ) 함수 

① 문자열을 변경시키는 함수

② 형식

#include <string.h>

char *strset(string, c);

char *strnset(string, c, len);

char *string;

int c;

int len;

③ strset( ) 함수

문자열 string이 가리키는 곳의 모든 문자를 c와 같은 문자로 전부 변경시킨다.

④ strnset( ) 함수

문자열 string이 가리키는 곳의 첫 번째 문자부터 n개의 문자만 c와 같은 문자로 전부 변경시킨다. 

【 예제13-19 】포인터 string이 가리키는 "SONATA" 문자열을 "KKKKKK"로 바꾸는 프로그램(ch13-19.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="SONATA";

clrscr(); 

printf("*string data = %s\n", string); 

strset(string, 'K');

printf("after string data = %s\n", string); 

getch();

}

▶ 실행결과

*string data = SONATA

after string data = KKKKKK

【 예제13-20 】포인터 string이 가리키는 "SONATA" 문자열을 "SONKKK"로 바꾸는 프로그램(ch13-20.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="SONATA";

clrscr(); 

printf("*string data = %s\n", string); 

strset(string+3, 'K');

printf("after string data = %s\n", string); 

getch();

}

▶ 실행결과

*string data = SONATA

after string data = SONKKK


【 예제13-21 】포인터 string이 가리키는 "SONATA" 문자열을 "KKKATA"로 바꾸는 프로그램(ch13-21.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="SONATA";

int len = 3;

clrscr(); 

printf("*string data = %s\n", string); 

strnset(string, 'K', len);

printf("after string data = %s\n", string); 

getch();

}

▶ 실행결과

*string data = SONATA

after string data = KKKATA

(6)strlen( ) 함수 

① 문자열의 길이를 알아내는 함수

② "\0"을 포함하지 않은 문자열 길이를 함수값으로 반환시켜 준다.

③ 형식

#include <string.h>

unsigned strlen(string);

char *string;


【 예제13-22 】문자열 string의 문자열 길이를 구하는 프로그램(ch13-22.c)

▶ 프로그램

#include <stdio.h>

#include <string.h>

main()

{

char *string="SONATA";

int len ;

clrscr(); 

printf("*string data = %s\n", string); 

len = strlen(string);

printf("string length = %d\n", len); 

getch();

}

▶ 실행결과

*string data = SONATA

string length = 6


(7) atoi( ) 함수 

① 문자열을 정수로 변환시키는 함수

② 형식

#include <stdlib.h>

int atoi(string);

char *string;


【 예제13-23 】문자열 string1과 문자열 string2를 정수로 변환하여 덧셈하는 프로그램(ch13-23.c)

▶ 프로그램

#include <stdio.h>

#include <stdlib.h>

main()

{

char *string1="1234";

char *string2="4321";

int i;

clrscr(); 

printf("*string1 data = %s\n", string1);

printf("*string2 data = %s\n", string2); 

i = atoi(string1) + atoi(string2);

printf("after integer data = %d\n", i); 

getch();

}

▶ 실행결과

*string1 data = 1234

*string2 data = 4321

after integer data = 5555

(8) strlwr( ) 함수, strupr( ) 함수, strrev( ) 함수 

① strlwr( ) 함수 : 소문자화 하는 함수 

【 예제13-24 】"KBS"를 "kbs"로 변환하는 프로그램(ch13-24.c)

▶ 프로그램

#include <stdio.h>

#include <stdlib.h> 

main()

{

char *string1="KBS";

char *string2;

clrscr(); 

printf("*string1 data = %s\n", string1);

string2 = strlwr(string1);

printf("*string2 data = %s\n", string2);

getch();

}

▶ 실행결과

*string1 data = KBS

*string2 data = kbs


② strupr( ) 함수 : 대문자화 하는 함수 

【 예제13-25 】"kbs"를 "KBS"로 변환하는 프로그램(ch13-25.c)

▶ 프로그램

#include <stdio.h>

#include <stdlib.h>

main()

{

char *string1="kbs";

char *string2;

clrscr(); 

printf("*string1 data = %s\n", string1);

string2 = strupr(string1);

printf("*string2 data = %s\n", string2);

getch();

}

▶ 실행결과

*string1 data = kbs

*string2 data = KBS

③ strrev( ) 함수 : 문자 배열을 반대로 하는 함수 

【 예제13-26 】"Computer"를 "retupmoC"로 변환하는 프로그램(ch13-26.c)

▶ 프로그램

#include <stdio.h>

#include <stdlib.h>

main()

{

char *string1="Computer";

char *string2;

clrscr(); 

printf("*string1 data = %s\n", string1); 

string2 = strrev(string1);

printf("*string2 data = %s\n", string2); 

getch();

}

▶ 실행결과

*string1 data = Computer

*string2 data = retupmoC


 

13-4. 문자열 변환 함수 

출력하고자 하는 문자열을 주어진 형식에 따라 편집하여 주는 함수 

(1) sprintf( ) 함수 

① 문자열을 지정된 서식에 따라 출력을 변환하는 함수

② 형식

#include <stdlib.h>

int sprintf(buffer, format, 출력자료1, 출력자료2, …);

char *buffer; ☞ 출력할 문자열

char *format; ☞ 서식 제어 문자열

③ 출력 자료들을 "format"에 표시된 변환 규칙에 따라 buffer에 넣는다. 편집이 끝난 후에는 '\0'을 제일 끝에 추가시킨다. 

【 예제13-27 】다음 프로그램을 실행하라.(ch13-27.c)

char buf1[10], buf2[14], *p = "KOREA";

① sprintf(buf1, "seoul%d%c", 123, '!');

buf1의 내용:

s

e

o

u

l

1

2

3

!

\0

② sprinf(buf2, "%-5s = %5d", p, 123);

buf2의 내용:

K

O

R

E

A


=




1

2

3

\0

▶ 프로그램

#include <stdio.h>

#include <stdlib.h>

main()

{

char buf1[10], buf2[14];

char *p="KOREA";

clrscr();

sprintf(buf1, "seoul%d%c", 123, '!');

puts(buf1);

sprintf(buf2, "%5s = %5d",p, 123);

puts(buf2);

getch();

}

▶ 실행결과

seoul123!

KOREA = 123

(2) sscanf( ) 함수 

① 표준 입력에서 서식 지정된 값을 읽어 들인다.

② 형식

#include <stdlib.h>

int sscanf(buffer, format, 입력자료1, 입력자료2, …);

char *buffer; ☞ 입력할 문자열

char *format; ☞ 서식 제어 문자열

③ "buffer" 내의 자료를 "format"에 표시된 변환 규칙에 따라 입력자료1, 입력자료2, … 등에 입력시켜 준다. 이 함수의 반환 값은 입력이 성공되면 입력 변수의 개수가 되며, 실패시에는 -1이 된다. 

【 예제13-28 】다음 프로그램을 실행하여라.(ch13-28)

char *buf = "KOREA 12345 3.14", name[10];

float f;

int i, j;

sscanf(buf, "%s%3d%d%f", name, &i, &j, &f);

▷ buf의 내용 :

K

O

R

E

A



1

2

3

4

5


3

.

1

4

\0

▷ 문자 변수 name의 내용 :

K

O

R

E

A

\0

▷ 정수형 변수 i, j와 실수형 변수 f의 내용 :

i의 내용 :

1

2

3

 

j의 내용 :

4

5

f의 내용 :

3

.

1

4

▶ 프로그램

#include <stdio.h>

#include <stdlib.h>

main()

{

char *buf = "KOREA 12345 3.14", name[10];

float f;

int i, j;

clrscr();

sscanf(buf, "%s%3d%d%f", name, &i, &j, &f);

printf("name = %s\ni = %d\nj = %d\nf = %.2f\n", name, i, j, f);

getch();

}

▶ 실행결과

name = KOREA

i = 123

j = 45

f = 3.14

 

출처:http://www.daejin.or.kr/home/hslee2/TC/chap13/chap13-1.htm

Posted by 두장
원문 http://blog.naver.com/pjfile/40012816089

22 프로세스의 시동과 종료

    22.1 프로그램 인수들
    22.2 환경 변수들
    22.3 프로그램 종료

 

∵ getopt() 함수란?  유닉스에서는 프로그램 실행시 옵션으로 '-a','-b'등등을 줄 수 있는 것이

                             많은데 이 처리를 쉽게하기 위해 만들어진 함수가 입니다.
 

프로세스는 시스템 자원들의 할당을 위한 기본적인 동작 단위이다. 각 프로세스는 자신만의 주소 공간(address space)과 제어의 한 쓰레드를 갖는다. 한 프로세스는 한 프로그램을 실행한다; 당신이 같은 프로그램을 실행하는데 여러개의 프로세스를 가질 수 있지만, 각각의 프로세스는 자신의 주소공간에서 자신의 프로그램 복제본을 갖고 다른 프로그램 복사본과 독립적으로 실행된다.

 

일반적으로 우리는 하나의 프로세스를 단위로 하여 독립적인 작업을 수행하는 것으로 알고 있지만, 컴퓨터 기술의 발전으로 프로세스라는 단위는 너무나 큰 단위가 되어버리고 이를 대신하여 쓰레드라는 단위가 사용되게 되었다. 쓰레드를 사용하는 운영체제에서는 작업의 스케줄링을 수행할 때 쓰레드를 단위로 하여 스케줄링을 한다.

 

이 장은 당신의 프로그램에서 프로세스를 시동하고, 그것을 종료시키고, 부모프로세스로 부터 정보(인수들과 환경)를 받기 위하여 무엇을 해야만 하는지를 설명하고 있다.

--------------------------------------------------------------------------------

 

22. 1 프로그램 인수들

 

 시스템은 main 함수를 호출함으로써 C 프로그램을 시작한다. 그것은 당신이 프로그램을 만들 때 main 이라 이름지어진 함수를 꼭 만들어야 함을 의미하고_그것을 하지 않았을 때 에러없이 프로그램을 링크할 수 없을 것이다.

 

main 함수는 아무런 인수를 취하지 않거나, 커멘드 라인에서 받아들이는 인수를 나타내는 두 개의 인수를 취한다. 다음처럼.

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

 

커멘드 라인 인수들은 공백으로 분리된 토큰으로써 프로그램을 호출하도록 쉘 커맨드에 주어진다; 그래서, `cat foo bar'라고 했을 때 인수는 `foo' 와 `bar'가 된다. 어떤 프로그램에서 커맨드 라인에 인수를 넣는 방법은 오직 main함수의 인수를 통해서 가능하다. 만일 main 함수가 인수를 취하지 않는다면, 당신은 커맨드 라인에서 인수를 취할 수 없다.

 

argc 인수는 커맨드 라인 인수들의 개수를 말하고, argv 인수는 C 문자열의 벡터이다;
argv의 각 요소들은 공백으로 분리된 개별적인 커맨드 라인 인수 문자열이다.
실행시키려는 프로그램의 파일이름 또한 첫 번째 요소로써 벡터 안에 포함된다.
argc의 값은 이 요소들의 개수이다. 널 포인터가 항상 마지막 요소가 된다: 즉, argv[argc]는 널 포인터이다. 커맨드 `cat foo bar' 에서, argc는 3이고, argv는 "cat", "foo", 그리고 "bar" 라는 세 개의 요소들을 갖는다. 만일 당신의 프로그램에서 커맨드 라인 인수들의 구분이 간단하다면, 당신은 직접 argv로부터 간단하게 하나씩 인수를 가져올 수 있다. 당신의 프로그램이 한정된 인수들의 개수를 갖지않거나, 모든 인수들을 한꺼번에 취하지 않는다면 같은 방법으로 인수들을 취할 수 있지만, 그렇지 않다면 그것을 구문분석 하기 위해서 getopt를 사용해서 인수들을 취해야 할 것이다.

 

22. 1. 1 프로그램 인수 구문 관례들

 

POSIX에서 커맨드 라인 인수들을 위해서 다음과 같은 관례들을 권장하고 있다. getopt (22. 1. 2 [Parsing Options] 참조)는 다음의 권장사항을 쉽게 충족시키도록 만든다. 하이픈(hyphen (`_') ) 구획문자(delimiter)로 시작하는 인수들은 옵션이 있다.


한 개의 토큰에서 한 개의 하이픈 구획문자 뒤에 따르는 것은, 만일 그 옵션들이 인수들을 취하지 않는다면 다중 옵션이 된다. 그래서, `-abc'는 `-a -b -c'와 동등하다.
옵션 이름들은 단일한 알파벳 문자들이다( isalnum에 관하여; 4. 1절[Classification of Characters] 참조) 어떤 옵션들을 인수를 필요로 한다. 예를 들어, ld 명령의 `-o' 옵션은 출력 파일 이름으로 하나의 인수를 필요로 한다. 옵션과 옵션이 필요로 하는 인수들은 토큰으로 분리되어 나타날 수도 있고, 그렇지 않을 수도 있다. (즉, 그들을 분리하는 공백문자는 선택적이다. ) 그래서, `-o foo'
와 `-ofoo'는 동등한다.


옵션들은 전형적으로 다른 비-옵션 인수들에 선행된다.
GNU C 라이브러리에 있는 getopt의 구현은 당신의 프로그램을 사용하는 사용자가 심지어 옵션과 비-옵션 인수들을 혼합했을지라도, 구문분석을 위해서 모든 비-옵션 인수들 전에 옵션 인수들이 나타나도록 만든다. 인수 `--'는 모든 옵션들을 종결시킨다; 다음에 따르는 인수들은 그들이 하이픈으로 시작할지라도, 비-옵션 인수들로 취급된다.

 

한 개의 하이픈 문자가 들어가 있는 토큰은 보통 비-옵션 인수로써 취급된다. 관례상, 그것은 기본 입력과 출력 스트림으로 입력이나 출력을 정하기 위해서 사용된다. 옵션들은 어떤 순서대로 공급되어지고, 또는 여러번 중복해서 나타나기도 할 것이다. 해석은 특정한 응용 프로그램이 맨 왼쪽에 있다고 해석한다.

 

GNU 는 그들의 관례에 따라서 긴 옵션을 더한다. 긴 옵션들은 `--'뒤에 따르는 이름으로 구성되는데 그 이름은 영숫자와 대쉬들로 구성된다. 옵션 이름들은 전형적으로 하나에서 세 개 단어의 길이를 갖고, 그들은 하이픈으로 단어들을 분리한다. 사용자들은 옵션을 대표하는 약자가 단일하다면 얼마든지 옵션 이름들을 축약할 수 있다. 한 개의 긴 옵션을 위한 인수를 정하기 위해서는, `--name=value'라고 써라. 이 구문은 선택적인 인수를 받아들이는 긴 옵션을 가능하게 한다. GNU 시스템은 쉘에서 긴 옵션 이름을 지원할 것이다.

 

 

22. 1. 2 프로그램 옵션들을 구문 분석하기

이번에는 getopt 함수를 어떻게 호출하는지에 대해서 자세히 설명한다. 이 기능을 사용하기 위해서, 당신의 프로그램에 헤더파일 `unistd. h'를 포함시켜야만 한다.

 

변수 : int opterr

 

만일 이 변수의 값이 0 이 아니면, getopt는 만일 알지 못하는 옵션 문자가 들어오거나 인수를 필요로 하는 옵션이 옵션을 갖고있지 않다면 표준 에러 스트림에 에러메세지를 프린트한다. 이것은 디폴트 동작이다. 만일 당신이 이 변수를 0으로 설정하면, getopt는 아무런 메시지도 프린트하지는 않지만, 에러를 지적하기 위해서 여전히 ? 문자를 여전히 반환한다.

 

변수 : int optopt

 

getopt가 알려지지 않은 옵션 문자와 만나거나 필요한 인수가 빠져있는 옵션일 때, getopt는 그 옵션 문자를 이 변수에 저장한다. 당신은 자신만의 진단 메시지를 제공하기 이해서 이 변수를 사용할 수 있다.

변수 : int optind

 

이 변수는 앞으로 분석할 배열 argv의 다음 요소에 대한 인덱스로 getopt에 의해 설정된다. 일단 getopt가 옵션 인수들 모두를 발견했다면, 당신은 나머지 비-옵션 인수들이 어디에서 시작하는지 결정하기 위해서 이 변수를 사용할 수 있다. 이 변수의 초기값은 1이다.

 

변수 : chat *optarg

 

이 변수는 인수들을 받아들이는 옵션들을 위해서, 옵션 인수들의 값을 가리키는 포인터로써 getopt에 의해 설정된다.

 

함수 : int getopt(int argc, char **argv, const char *options)

 

getopt 함수는 argv와 argc 인수들에 의해 지정된 인수 목록으로부터 다음 옵션 인수를 얻는다. 보통, 그들 값들은 main에 의해 받아들여진 인수들로부터 직접적으로 온다. 옵션 인수는 이 프로그램에서 유용하게 쓰일 옵션 문자들을 지정한 문자열이다.

문자열 안에 있는 옵션 문자는 인수가 필요함을 알리기위해서 콜론(`: ')의 다음에 나타난다.

 

만일 옵션 인수 문자열이 하이픈(`-')으로 시작한다면, 이것은 특별하게 취급된다. 그들은 옵션 문자가 `\0'인 것처럼 반환되어, 옵션이 없는 인수들을 허용한다. getopt 함수는 다음 커맨드 라인옵션의 옵션 문자를 반환한다. 더 이상 유용한 옵션 인수들이 없을 때, -1을 반환한다. 그곳은 이제 비-옵션 인수들이 있을 것이다; 당신은 이것을 체크하기 위해서 argc 파라미터에 대응하는 외부변수 optind를 비교해야만 한다. 만일 어떤 옵션이 인수를 가진다면, getopt 는 변수들 optarg안에 그것을 저장함으로써 인수를 반환한다. 당신이 덧씌워질지도 모르는 정적 변수가 아닌, 보통의 argv 배열을 가리키는 포인터를 라면, optarg 문자열을 복사할 필요가 없다. 만일 알려지지 않은 옵션문자를 사용하거나, 옵션 인수가 빠져있다면 getopt는 `?'를 반환하고 현재 옵션 문자로 외부변수 optopt를 설정한다. 만일 옵션의 첫 번째 문자가 콜론(`: ') 이라면, getopt는 옵션 인수가 빠져있음을 알리기위해서 `?' 대신에 `: '를 반환한다. 또한, 만일 외부변수 opterr이 0이 아니면(디폴트이다. ), getopt는 에러 메시지를 출력한다.

 

 

22. 1. 3 getopt를 사용해서 인수를 구문 분석하는 예제

 

다음은 getopt가 전형적으로 어떻게 사용되는지를 보여주는 예제이다. 핵심은 다음과 같다:

보통, getopt는 루프 안에서 호출된다. getopt가 더 이상 나타난 옵션이 없음을 알리기위해서 -1을 반환 하면, 루프는 종료한다. switch 구문은 getopt로부터의 반환값에 따른 각각의 처리에 사용된다. 각각의 경우는 프로그램에서 나중에 사용될 변수를 설정하게 된다. 두 번째 루프는 남아있는 비-옵션 인수들을 처리하는데 사용된다.

 

#include <unistd. h>

#include <stdio. h>

 


int main (int argc, char **argv)
{
    int aflag = 0;
    int bflag = 0;
    char *cvalue = NULL;
    int index;
    int c;
    opterr = 0;
    
    while ((c = getopt (argc, argv, "abc: ")) != -1) 
    {
        switch (c)
        {
            case 'a':
                aflag = 1;
                
                break;
                
            case 'b':
                bflag = 1;
                break;
            
            case 'c':
                cvalue = optarg;
                break;
            
            case '?':
            if (isprint (optopt))
                fprintf(stderr, "Unknown option `-%c'. \n", optopt);
            else
                fprintf (stderr,
            
            "Unknown option character `\\x%x'. \n", optopt);
            
            return 1;
            
            default:
            
            abort ();
            
        }
    }
    
    printf("aflag = %d, bflag = %d, cvalue = %s\n", aflag, bflag, cvalue);
        
    for (index = optind; index < argc; index++)
        printf ("Non-option argument %s\n", argv[index]);
    
    return 0;    
}

 

다음은 위의 프로그램을 여러 가지 인수들의 조합을 사용했을 때 어떤 결과를
나타내는지에 대한 예이다.

% testopt

aflag = 0, bflag = 0, cvalue = (null)

 

% testopt -a -b

aflag = 1, bflag = 1, cvalue = (null)

 

% testopt -ab

aflag = 1, bflag = 1, cvalue = (null)

 

% testopt -c foo

aflag = 0, bflag = 0, cvalue = foo

 

% testopt -cfoo

aflag = 0, bflag = 0, cvalue = foo

 

% testopt arg1

aflag = 0, bflag = 0, cvalue = (null)

Non-option argument arg1

 

% testopt -a arg1

aflag = 1, bflag = 0, cvalue = (null)

Non-option argument arg1

 

% testopt -c foo arg1

aflag = 0, bflag = 0, cvalue = foo

Non-option argument arg1

 

% testopt -a -- -b

aflag = 1, bflag = 0, cvalue = (null)

Non-option argument -b

 

% testopt -a -

aflag = 1, bflag = 0, cvalue = (null)

Non-option argument -

 

22. 1. 4 긴 옵션들을 구문 분석하기

단일한-문자 옵션들로만 되어있는 GNU-스타일의 긴 옵션들을 받아들이기 위해서는 getopt 대신에 getopt_long를 사용한다. 당신은 이런 가외의 작은 작업을 통해서 모든 프로그램이 긴 옵션들을 받아들이게 만들 수 있고, 초보자들에게 어떻게 그 프로그램을 사용하는지 도울 수 있다.

 

데이터 타입 : struct option

 

이 구조체는 getopt_long을 위한 단일한 긴 옵션 이름을 설명한다. 긴 옵션을 구성하는 옵션의 하나인 인수 longopts는 그들 구조체의 배열이 되어야만 한다. 그 배열은 0으로 채워진 요소로써 끝난다. struct option 구조체는 다음과 같은 필드를 갖는다.

 

const char *name

 

이 필드는 옵션의 이름으로 문자열이다.

int has_arg

이 필드는 옵션이 인수를 취하는지 또는 취하지 않는지의 여부를 알린다. 그것은 정수이고 그곳에 세 개의 합리적인 값들이 있다: no_argument, required argument, 그리고 optional_argument.

 

int *flag

int val

 

이 필드들은 그 옵션이 발생할 때 옵션에 대하여 어떻게 보고를 하거나 동작할 것인지를 제어한다. 만일 flag가 널 포인터라면, val은 이 옵션을 대표하는 값이다. 종종 그 값들은 특정한 긴 옵션들의 단일함을 확인하기 위해서 선택되어진다. 만일 플래그가 널 포인터가 아니라면, 그것은 이 옵션을 위한 플래그인 int형 변수의 주소가 되어질 것이다. val안의 값은 나타난 옵션을 지적하도록 플래그 안에 저장하기위한 값이다.

 

함수 : int getopt__long (int argc, char **argv, const char *shortopts, struct
option *longopts, int *indexptr)

 

벡터 argv ( 길이는 argc이다. )로부터 옵션들을 해독한다. 인수 shortopts는 getopt가 해독할 수 있는 만큼의 짧은 옵션을 나타내고, 인수 longopts는 받아들이기 위한 긴 옵션들을 나타낸다. getopt_long이 짧은 옵션을 만났을 때, 그것은 getopt가 하는것과 같은 일을 한다: 그것은 옵션을 위한 문자 코드를 반환하고, optarg에 옵션 인수들을(만일 그것이한 개를 가진다면) 저장한다.

getopt_long가 긴 옵션을 만났을 때, 그것은 그 옵션을 정의하는 flag 와 val 필드에 기초한 동작들을 취한다.

 

만일 flag가 널 포인터라면, getopt_long 은 발견한 옵션을 지적하도록 val의 내용을 반환한다. 당신이 다른 의미를 가진 옵션을 위해서 val 필드 안에 별개의 값들을 배열하면, 당신은 getopt_long이 반환한 후에 그들 값을 해독할 수 있다. 만일 긴 옵션이 짧은 옵션과 동일하다면, 당신은 짭은 옵션의 문자 코드를 사용할 수 있다.

 

만일 flag가 널 포인터가 아니라면, 단지 프로그램 안에서 flag가 설정된 이 옵션을 의미한다. flag는 당신이 정의한 int형의 변수이다. flag 필드 안에 그 플래그의 주소를 저장하라. flag에 저장하려는 옵션값을 val 필드 안에 저장하라. 이 경우, getopt_long는0을 반환한다.

 

어떤 긴 옵션에서, getopt_long는 배열 longopts안의 옵션 정의에 대한 인덱스를, *indexptr에 저장하여 알린다. 당신은 longopts[*indexptr]. name 어로 옵션의 이름을 얻을 수 있다. 그래서 당신은 그들의 val 필드 안에 있는 값이나, 그들의 인덱스에 의해서 긴 옵션들을 구분할 수 있다. 당신은 플래그를 설정한 긴 옵션들을 이 방법으로 구분할 수 있다.

 

어떤 긴 옵션이 인수를 가질 때, getopt_long는 반환하기 전에 변수 optarg안에 인수값을 저장한다. 어떤 옵션이 아무런 인수를 가지지 않을 때, optarg안의 값은 널 포인터이다. 이것은 공급된 인수가 옵션을 가졌는지 아닌지에 대한 여부를 당신에게 알리는 방법인 것이다. getopt_long는 더 이상 처리할 옵션들이 없을 때, -1을 반환하고, 다음 남아있는 인수의 argv 인덱스를 변수 optind 안에 남긴다.

 

22. 1. 5 긴 옵션들의 구분분석에 대한 예제

 

#include <stdio. h>
/* 플래그는 `--verbose' 에 의해 설정한다. */

static int verbose_flag;
int main (argc, argv)
int argc, char **argv;
{
    int c;

    while (1)
    {
        static struct option long_options[] =
        {
            /* 그들 옵션들은 플래그를 설정한다. */
            {"verbose", 0, &verbose_flag, 1}, {"brief", 0, &verbose_flag, 0},
            /* 그들 옵션들은 플래그를 설정하지 않는다. 우리는 그들의 인덱스에 의해 그들을 구분한다. */
            {"add", 1, 0, 0},{"append", 0, 0, 0},{"delete", 1, 0, 0},{"create", 0, 0, 0},
            {"file", 1, 0, 0},{0, 0, 0, 0}}
        };

        /* getopt_long는 이곳에 옵션 인덱스를 저장한다. */
        int option_index = 0;
        c = getopt_long(argc, argv, "abc: d: ", long_options, &option_index);

        /* 옵션들의 끝을 검출한다. */
        if (c == -1)   break;

        switch (c)
        {

            case 0:

                /* 만일 이 옵션이 플래그를 설정하면, 지금 아무런 일도 하지 말아라. */
                
                if (long_options[option_index]. flag != 0) break;
                
                printf("option %s", long_options[option_index]. name);
                
                if (optarg) printf(" with arg %s", optarg);
                
                printf("\n");
                
                break;

            case 'a':
                puts("option -a\n");
                break;
            
            case 'b':
                puts("option -b\n");
                break;

            case 'c':
                printf("option -c with value `%s'\n", optarg);
                break;

            case 'd':
                printf("option -d with value `%s'\n", optarg);
                break;

            case '?':
                /* getopt_long는 이미 에러 메시지를 프린트했다. */
                break;

            default:
                abort ();

        } /* switch 의 끝*/

    } /* while 의 끝 */


    /* 그들이 맞추었던 것으로 `--verbose'와 `--brief'를 보고하는 대신에, 우리는
    그들로부터 나온 결과로 마지막 상황을 보고한다. */
    
    if (verbose_flag)  
        puts ("verbose flag is set");
    
        
    /* 남아있는 코맨드 라인 인수들을( 옵션이 없는) 출력하라. */    
    if (optind < argc) 
    {
        printf ("non-option ARGV-elements: ");
    
        while (optind < argc)    
            printf ("%s ", argv[optind++]);
    
        putchar ('\n');    
    }

    exit (0);
}

--------------------------------------------------------------------------------

 

22. 2 환경 변수들

프로그램이 실행될 때, 두 가지 방법으로, 실행될 문맥(context)에 대한 정보를 받는다. 첫 번째 방법은 argv와 argc 인수를 메인 함수에서 사용하는 것으로써 이것에 대한 것은 22. 1절 [Program Arguments] 에서 설명하였다. 두 번째 방법은 환경변수를 사용하는 것인데 지금부터 설명할 것이다. argv를 사용하는 방법은 실행될 특정한 프로그램에 지정된 커맨드 라인 인수를 주기 위해서 전형적으로 사용된다. 다른 방법으로, 환경은, 드물게 변경되고 덜 빈번하게 억세스 되는, 많은 프로그램에 의해 분배될 정보를 기억한다.

이 절에서 논의되고 있는 환경변수들은 당신이 쉘에게 커맨드를 보내고 사용될 것을 배정하는 환경변수와 같은 것이다. 쉘로부터 실행된 프로그램들은 쉘로부터 환경변수 모두를 상속받는다. 표준 환경 변수들은 사용자의 홈 디렉토리, 터미널 타입, 현재 지역 등등에 대한 정보를 위해서 사용된다; 당신은 다른 목적으로 부가적인 환경변수를 정의할 수 있다. 모든 환경변수들의 가진 값들의 집합은 집합적으로 환경으로써 알려진다. 환경변수들의 이름은 대소문자를 구분하고 문자 `='를 포함해서는 안된다.  시스템-정의 환경변수들은 항상 대문자이다. 환경변수들의 값은 문자열로써 표현될 수 있는 어떤 것도 가능하다. 값에는 널 문자가 포함 되서는 안된다, 왜냐하면 이것은 문자열의 끝으로 간주되기 때문이다.

 

22. 2. 1 환경 검색

환경변수의 값은 getenv 함수를 통해서 억세스될 수 있다. 이것은 헤더 파일 `stdlib.h'에 선언되어 있다.

 

함수 : char *getenv (const char name)

 

이 함수는 환경변수 name의 값인 문자열을 반환한다. 당신은 이 문자열을 갱신할 수 없다. 어떤 GNU 라이브러리를 사용하지 않는 비-유닉스 시스템들은 getenv 함수의 연속적인 호출에 의해서 덮어 쓰여질지 모른다(다른 라이브러리 함수에 의하지 않는다. ) 만일 환경변수 name이 정의되지 않는다면, 그 값은 널 포인터이다.

 

함수 : int putenv (const char *string)

 

putenv 함수는 환경으로부터 정의를 더하거나 제한다. 만일 string이 `name=value'의 형식이라면, 그 정의는 환경에 더해진다. 그렇지 않다면, 그 string은 환경변수의 이름으로써 해석되고, 환경 안에서 이 변수에 대한 정의가 제거된다. GNU 라이브러리는 SVID와의 호환성을 위해서 이 함수를 제공하고 있고; 다른 시스템에서 이 함수 는 유용하지 않다.

당신은 환경에 더 많은 변수들을 더하기 위해서 환경 오브젝트의 내재된 표현을 직접적으로 다룰 수 있다( 예로, 당신이 실행하려는 것에 대해서 다른 프로그램으로 통신하기 위해서; 23. 5절 [Executing a File] 참조).

 

변수 : chat **environ

 

환경은 문자열의 배열로써 표현된다. 각 문자열은 `name=value'의 형식을 갖는다.
환경 안에 나타난 문자열의 순서는 중요하지 않지만, 같은 이름이 한번 이상 나타나서는 결코 안된다. 배열의 마지막 요소는 널 포인터이다. 이 변수는 헤더파일 `unistd. h'에 선언되어 있다. 만일 당신이 환경변수의 값을 얻기 원한다면 getenv를 사용하라.

 

22. 2. 2 표준 환경 변수들

그들 환경변수들은 표준 의미를 갖는다. 이것은 그들이 항상 환경 안에서만 나타남을 의미하지는 않는다; 그러나 그 변수들이 어디에서든 나타난다고 해도 그들은 그들만의 의미를 갖기 때문에 당신이 다른 목적으로 환경변수 이름을 사용하려고 시도해서는 안된다.

 

HOME

이것은 사용자의 홈 디렉토리나, 초기 디폴트 작업 디렉토리를 나타내는 문자열이다. 사용자는 어떤 값으로 HOME를 설정할 수 있다. 만일 당신이 어떤 특정한 사용자를 위해서 적당한 홈 디렉토리를 부여하기를 원한다면, HOME을 사용할 수 없고; 대신에, 사용자 데이터 베이스에서 사용자의 이름을 찾아보라 ( 25. 12절 [User Database] 참조). 대부분의 목적에서는, HOME을 사용하는 것이 좋다, 왜냐하면 정확하게 사용자가 그 값을 정하도록 허용하기 때문이다.

 

LOGNAME

이것은 로그 인 하기 위해서 사용되는 사용자 이름이다. 환경에서 이 값은 제멋대로 조정될 수 있기 때문에, 이것은 프로세스를 실행시키고 있는 사용자를 구분하기 위한 신뢰 가능한 방법이 아니다; getlogin과 같은 함수(25. 11 [Who Logged In] 참조)는 그러한 목적을 위해서는 오히려 더 낫다. 대부분의 목적을 위해서는 LOGNAME이 사용자가 그값을 지정하도록 하기 때문에 사용하기 더 좋다.

 

PATH

경로는 파일을 찾기 위해서 사용되는 디렉토리 이름들의 열(sequence) 이다. 변수 PATH는 실행시키려는 프로그램을 찾기 위해서 사용되는 경로는 저장하고 있다. execlp와 execvp 함수들은 (23. 5절 [Executing a File] 참조) 그들 함수들에 의하여 실행된 쉘과 다른 유틸리티에서, 이 환경변수를 사용한다. 경로의 구분은 콜론으로 분리된 디렉토리 이름들의 순차열(sequence) 이다. 디렉토리 이름 대신에 빈 문자열은 현재의 디렉토리를 나타낸다( 9. 1절 [Working Directory] 참조).

 

이 환경변수의 전형적인 값은 다음과 같은 문자열이 될 것이다.

: /bin: /etc: /usr/bin: /usr/new/X11: /usr/new: /usr/local/bin

 

TERM

이것은 프로그램 출력을 받아들이는 터미널의 종류를 지정한다. 어떤 프로그램들은 터미널의 특정한 종류에 의해 지원되는 특별한 이스케이프 시퀀스 또는 터미널모드들을 이용하기 위해 이 정보를 사용하게 만들 수 있다. termcap 라이브러리( Termcap 라이브러리 매뉴얼에서 "Finding a Terminal Description" 절을 참고)를 사용하는 많은 프로그램들은 TERM 환경변수를 사용한다.

 

TZ

이것은 시간대(time zone)를 지정한다. 이 문자열의 형식과 어떻게 그것을 사용하는지에 대한 자세한 정보는 17. 2. 5절 [TZ Variable] 참조. LANG 속성 범주를 사용하도록 디폴트 지역을 지정한다.

 

LC_ALL

범주를 위한 환경변수가 아니면 설정한다. 지역에 대한 자세한 정보를 19장 [Locales]
참조.

 

LC_COLLATE

이것은 문자열 정렬을 위해서 어떤 지역을 지정한다.

 

LC_CTYPE

이것은 문자 집합(sets)과 문자 분류를 위해 사용하도록 어떤 지역을 지정한다.

 

LC_MONETARY

이것은 형식화된 통화량 값을 사용하기 위한 어떤 지역을 지정한다.

 

LC_NUMERIC

이것은 형식화된 숫자들을 사용하기 위한 어떤 지역을 지정한다.

 

LC_TIME

이것은 형식화된 날짜/시간값들을 위해서 어떤 지역을 지정한다.

 

_POSIX_OPTION_ORDER

만일 이 환경변수가 정의되었다면, 그것은 getopt에 의한 커맨드 라인 인수의 재정리를 금지한다. 22. 1. 1절 [Argument Syntax] 참조)


--------------------------------------------------------------------------------

22. 3 프로그램 종료

프로그램을 종료시키는 평상적인 방법은 메인 함수가 반환하도록 하는 것이다. 메인
함수로부터 반환되는 분기(exit) 상황 값은 프로세스의 부모 프로세스나 쉘에게
정보를 보고하기 위해서 사용된다. 프로그램은 exit 함수를 호출함으로써 또한 종료할
수 있다. 또한, 프로그램은 시그널들에 의해서 종료될 수 있다; 이것은 21장 [Signal
Handling] 에 자세하게 설명되어 있다. abort함수는 프로그램을 죽이는(kill)
시그널을 발생시킨다.

 

22. 3. 1 보통의 종료

프로세스는 프로그램이 exit를 호출할 때 보통 종료된다. main 함수로부터의 반환은 exit 를 호출하는것과 같고, main이 반환한 값은 exit에 인수로써 사용된다.

 

함수 : void exit (int status)

 

exit 함수는 상황 status로 프로세스를 종료한다. 이 함수는 반환하지 않는다.

보통의 종료는 다음과 같은 동작들의 원인이 된다:

 

1. atexit 나 on_exit 함수들에 등록된 함수들은 등록의 역순으로 호출되어진다. 이 메커니즘은 프로그램이 종료할 때 수행되어지는 자신만의 "cleanup" 동작들을 지정하도록 당신의 응용프로그램에게 허용한다. 전형적으로, 이것은 파일에 프로그램 상황 정보를 저장하거나, 공유 데이터베이스에서 락들을 해제하는것과 같은 일들을 하는데 사용된다.

 

2. 버퍼된 출력 데이터가 모두 기록되어, 개방된 모든 스트림이 폐쇄되었다. 7. 4절 [Closing Streams] 참조. 또한, tmpfile 함수에의해 개방되었던 임시 파일들은 제거된다; 9. 10절 [Temporary Files] 참조.

 

3. 프로그램을 종료시키도록 _exit가 호출되었다. 22. 3. 5절 [Termination
Internals] 참조.

 

22. 3. 2 Exit 상황들

프로그램이 종료할 때, 그것은 분기 상황(exit status)을 사용해서, 종료의 원인에 대한 작은 양의 정보를 부모 프로세스에게 반환할 수 있다. 이것은 현존하는 프로세스가 exit에게 인수로써 주는 0에서 255사이의 값이다. 보통 당신은 성공이나 실패에 대한 매우 광범위한 정보를 보고하기 위해서 exit 상황을 사용할 것이다.  당신은 실패의 이유에 대해서 매우 자세하게는 정보를 줄 수는 없고, 대부분의 부모 프로세스도 더 이상 세밀한 정보를 원하지 않을 것이다.

 

어떤 프로그램이 반환하는 값은 어떤 부류의 상황 값에 속한다. 대부분의 관습은 성공하면 0이고 실패하면 1이다. 그러나 비교를 수행하는 프로그램은 다른 관습을 사용한다: 매치되지 않았음을 지적하기 위해서 1을 사용하고 비교 불가능임을 지적하기 위해서 2를 사용한다. 당신의 프로그램이 그것을 위한 종료관습을 이해하려면 종료관습을 따라야한다. 일반적인 관습은 상황 값으로 128개를 예약하고 특별한 목적을 위해서 확장한다.  특별하게, 128개의 값은 서브프로세스에서 다른 프로그램에게 실패를 지적하기 위해서 사용된다. 이 관습은 보편적으로 제공되지는 않지만, 당신의 프로그램이 그것을 따르도록 하는 것이 좋다.

 

주의 : 분기상황으로써 에러들의 개수를 사용하려고 시도하지 말아라. 이것은 실제로 매우 유용하지 않다; 부모 프로세스는 얼마나 많은 에러들이 발생했는지에 주의를 기울이지 않는다. 그것보다 더 심각한 경우로는, 상황 값이 8비트에서 짤리기 때문에 만일 프로그램에서 256개의 에러를 보고한다면, 부모 프로세스는 0개의 에러가 난 것으로 보고를 받을 것이고_결국 성공한 것으로 되어버린다. 같은 이유로, 255개를 초과할 수 있기 때문에 종료의 상황으로써 에러의 개수를 사용하여 작업하지 말아라.

 

이식성 노트: 어떤 비-POSIX 시스템들은 종료 상황 값들을 위해서 다른 관습을 사용한다. 좀더 좋은 이식성을 위해서는, 성공과 실패를 위한 상황 값으로 매크로 EXIT_SUCCESS 와 EXIT_FAILURE 를 사용할 수 있다. 그들은 헤더파일 `stdlib. h'에 선언되어 있다.

 

매크로 : int EXIT__SUCCESS

 

이 매크로는 성공적인 프로그램 수행을 알리기위해서 exit 함수와 함께 사용될 수 있다. POSIX 시스템에서, 이 매크로의 값은 0이고, 다른 시스템에서 이 값은 어떤 다른 정수 표현( 아마도 비-상수 )이 될 것이다.

 

매크로 : int EXIT__FAILURE

 

이 매크로는 비성공적인 프로그램 수행을 알리기위해서 exit 함수와 함께 사용될 수 있다. POSIX 시스템에서, 이 매크로의 값은 1이다. 다른 시스템들에서 그 값은 어떤 다른 정수 표현( 아마도 비-상수 )이 될 것이다. 다른 0이 아닌 상황 값들은 앞으로의 일을 알린다. 어떤 프로그램들은 특정한 종류의 "비-성공"을 알리기위해서 다른 0이 아닌 상황 값들을 사용한다. 예를 들어, diff는 파일들이 서로 다름을 의미할 때 상황 값 1을 사용하고, 파일을 개방하기가 어렵다면 2 또는 그 이상의 값을 사용한다.

 

22. 3. 3 종료시의 상황정리

당신의 프로그램에서 보통의 종료가 발생하면 그 자신만의 정리(cleanup) 함수를 실행하여 정돈할 수 있다. 만일 당신이 여러 개의 응용 프로그램에서 한 개의 라이브러리를 사용하고 있을 때, 모든 응용프로그램이 종료되기전에 라이브러리의 정리(cleanup) 함수들을 명시적으로 호출한다면 신뢰성이 없다. atexit 또는 on_exit를 사용해서 라이브러리 그 자체에서 정리 함수를 설정함으로써, 응용 프로그램에게는 보이지 않는 정리(cleanup)함수를 만드는 것이 더 신뢰성 있는
프로그램이 된다.

 

함수 : int atexit (void (*function) (void))

 

atexit 함수는 프로그램이 정상적으로 종료하면 호출되도록 함수 function을 등록한다. 그 function은 아무런 인수가 없이 호출된다. atexit로부터의 반환값은 성공하면 0이고 만일 그 함수가 등록될 수 없으면 0이 아닌 값을 반환한다. 

 

함수 : int on__exit (void (*function)(int status, void *arg), void *arg)

 

이 함수는 atexit 보다는 좀더 다양함을 구사할 수 있는 것이다. 이 함수는 두 개의 인수로써 함수 function과 포인터 arg를 받아들인다. 정상적이 프로그램 종료시하다, 그 function은 두 개의 인수들, 즉 exit에 주었던 상황 값과 arg와 함께 호출된다. 이 함수는 SunOS와의 호환성을 위해 GNU C 라이브러리에 포함되었으며, 다른 시스템에서는 지원되지 않을 것이다.

 

다음은 exit 와 atexit 의사용을 설명하는 프로그램 예제이다.

 

#include <stdio. h>

#include <stdlib. h>

 

void bye (void)

{

    puts ("Goodbye, cruel world. . . . ");

}

 

int main (void)

{

    atexit (bye);

    exit (EXIT_SUCCESS);

}

 

이 프로그램이 실행됐을 때, 단지 메시지를 프린트하고 종료한다.

 

22. 3. 4 프로그램 중지시키기

당신은 abort 함수를 사용하여 당신의 프로그램을 중지시킬 수 있다. 이 함수를 위한 프로토타입은 `stdlib. h'에 있다.

 

함수 : void abort (void)

 

abort 함수는 비정상적으로 프로그램을 종료시킨다. 이것은 atexit 또는 on_exit로 등록된 정리(cleanup)함수들을 실행하지 않는다. 이 함수는 SIGABRT 시그널을 발생시킴으로써 프로세스를 종료시키고, 당신의 프로그램은 이 시그널을 가로채서 처리하는 핸들러를 포함할 수 있다.

 

22. 3. 5 내부적 종료

_exit 함수는 exit에 의해서 프로세스를 종료하기 위해서 사용되는 기본 동작이다.
이것은 `unistd. h'에 선언되어 있다.

 

함수 : void __exit (int status)

 

_exit 함수는 상황 status로 프로세스가 종료되도록 하는 기본 동작(primitives)이다.
이 함수를 호출하는 것은 atexit 또는 on_exit에 등록된 정리(cleanup) 함수들이 실행되지 않는다.

명백히 종료를 호출하거나, 또는 시그널의 결과에 의한 종료이거나_어떤 이유에 의해서 프로세스가 종료될 때, 다음과 같은 일이 발생한다. 프로세스에서 모든 개방된 파일 기술자들은 폐쇄된다. 8장 [Low-Level I/O] 참조.

종료 상황 코드의 하위 8비트는 wait 나 waitpid를거쳐서 부모 프로세스에게 보고되도록 저장된다; 23. 6절 [Process Completion] 참조. 종료된 프로세스의 어느 자식 프로세스들은 새로운 부모 프로세스가 할당된다.
(이것은 프로세스 ID 1로서, 처음의 프로세스이다. )

SIGCHLD 시그널은 부로 프로세스에게 보내어진다.

만일 그 프로세스가 제어중인 터미널을 가진 세션 리더(session leader)라면, SIGHUP 시그널은 전면 작업에 있는 각각의 프로세스에게 보내어 지고, 제어중인 터미널은 세션으로부터 분열된다. 24장 [Job Control] 참조. 어떤 프로세스의 종료가 프로세스 그룹을 고아가 되도록 하고, 프로세스 그룹의 어떤 멤버를 멈추도록 하는 원인이 된다면, SIGHUP 시그널과 SIGCONT 시그널이 그룹에 있는 각각의 프로세스에게 보내어진다. 24장 [Job Control] 참조.

Posted by 두장
이전버튼 1 2 이전버튼