C & Unix/Linux

Basic Definitions

▪ C language:

- B: K. Tompson in Bell Lab. (1970)

- C: Kernighan & Ritchie in Bell Lab. (1972)

▪ Standard C library in C

- A library of frequently used functions stored in /lib.

ex) sin(), log() in test.c

% cc test.c -lm -o test

'linking' math lib.

ex) in Visual C++

MFC (Microsoft Foundation Class): class "library"

ex) in Java

import java.io.*;

import java.net.*;

- Library function에서 쓰이는 header files (data format) in /include.

#include <stdio.h>: Standard I/O library.

#include <math.h>: Math library.

▪ Two sets of "functions"

ex) fopen(), printf(), fclose()

open(), read(), write()

① C library routine: C 컴파일러에 의해 제공되는 함수.

ex) malloc(), scanf(), ...

⇒ Any OS에서 사용.

② Unix system call: OS 커널에서 제공하는 service.

ex) fork(), exit(). ioctl(), pipe(), ...

⇒ Unix에서만 사용.

▪ Kernel : The OS itself.

User Program

system call

file system

buffer

char.  block

device drivers

Interprocess

communication

scheduler

memory

management

process

control

subsystem

libraries

socket

N/W

subsystem

buffer

device drivers

H/W control

H/W - CPU, momory, disk, N/W card, …

user

kernel

H/W

TCP/UDP

modules

▪ Program

- Executable file stored in directory.

ex)	commands - Stored in /etc/bin(ary)

⇔ c:\WINDOWS\system32 in Windows (*.exe)

compiled user programs - Stored in user directory.

▪ Process

- Instance of a program that is being executed by the kernel.

- Program → Process: exec()

Process → Process: fork()

Terminate a process: exit()

- Multitasking OS can execute more than one task (process) at a time by time-sharing.

ex) Unix, Windows, Linux 등.

- It is possible for a single program to be executing as many different processes at the same time.

ex) Shared: Text

⇒ By text table in kernel area

Not Shared: Data

/bin/vi

u1

u2

un

:

:

vi

vi

:

:

Process 1

Process n

ex) IE Web browser를 여러 개 실행시킬 경우.

▪ System calls

- Services from the kernel process.

⇒ user process가 kernel process에게 서비스를 요청.

⇒ System call 수행시 user process는 user mode에서 kernel mode로 바뀌어서 수행됨.

ex)   …	: user mode

fork()	: kernel mode ⇒ Kernel memory area access 가능.

…	: user mode

open()	: kernel mode

- Return -1 IF an error occurs

ELSE returns a value ≥ 0

ex) errno: a global integer which contains the system error no.

▪ Program ↔ Process: Shell

- ‘Command line interpreter’ that read commands from a user at a terminal or from a shell program file and execute the commands. (명령어 실행기)

ex) command.com in DOS.

- the Bourn shell : /bin/sh	; version 7

the Korn  shell : /bin/ksh

the C     shell : /bin/csh

the Bourn again shell (GNU bash): /bin/bash	; Linux

Ex) 자신의 shell 확인하기

$ cat /etc/passwd | grep test01

$ echo $SHELL

- login shell is defined in the password file.

ex) $more /etc/passwd

▪ Passing Data: Kernel Process → User Process

ex) % more test.out : Shell process(%)로부터 User process가 생성

⇒ Pass 2 arguments (argv[0] - more, argv[1] - test.out)

to C startoff routine.

- Argument list: A variable length argument list passed to the process.

- 구조체: An array of pointers to char. strings.

ex) main(argc, argv)

int argc;       /* the number of arguments */

char *argv[];   /* the pointer of an array of pointers to char. string */

ex)

% echo hello world ↲

↗   ↑    ↖

argv[0]  argv[1]  argv[2]

argv

argv[0]

argv[1]

argv[2]

"echo\0"

"hello\0"

"world\0"

▪ Environment list

- A variable length list of environment variables : process끼리 공유하는 값들.

- Setting environment variables.

ex) In .cshrc	환경변수       환경변수값

setenv OPENWINHOME /usr/openwin

ex) 환경변수와 변수값 보기 (Unix/Linux)

$ env

ex) 환경변수와 변수값 보기 (Windows)

제어판 > 시스템 > 환경 변수 >

∙ DB Programming on the Web

- A process can obtain the value of a specific variable from the list.

- CGI (Common Gateway Interface)

Browser

Server

CGI pgm.

① User CGI program들을 compile하여 cgi-bin directory에 저장.

② Browser가 특정한 CGI program을 실행할 것을 Web server에 요청.

ex) <FORM METHOD=get ACTION="http://www.kol.net/cgi-bin/test-cgi?NAME=Kim&ID=tykim">

“CGI process에 전달하는 입력값”

③ Web server는 HTML 문서에 내장된 특정 CGI program을 cgi-bin에서 찾아서 실행.

④ Web server의 “환경변수” 중의 하나인 QUERY STRING을 이용하여 user CGI program에 입력값을 전달.

⇒ FORM tag에 METHOD=GET이거나 생략한 경우.

(METHOD가 POST인 경우는 stdin(char. special file)을 통해서 전달됨.)

ex) Web Server Process의 환경변수 값들

SERVER_SOFTWARE = Apache/1/1/1

SERVER_NAME = www.kol.net

GATEWAY_INTERFACE = CGI/1.1

SERVER_PROTOCOL = HTTP/1.0

SERVER_PORT = 80

REQUEST_METHOD = GET

……

QUERY_STRING = NAME=Kim&ID=tykim

REMOTE_HOST = pc80.kol.co.kr

REMOTE_ADDR = 204.252.145.180

⑤ CGI program은 getenv()를 이용하여 변수 값을 전달 받음.

ex)  getenv(QUERY_STRING);

getenv(REMOTE_ADDR);

- CGI: User의 request마다 새로운 process를 생성

- Servlet

□모든 Servlet들은 하나의 Servlet engine process(예: Tomcat)에서 수행됨.

□User의 request마다 JVM상에서 thread를 생성하여 수행.

⇒ 같은 process 내에 있는 thread들은 서로 resource를 공유.

⇒ thread는 process보다 resource를 적게 사용.

⇒ performance enhancement

Browser

Client

Server

User

HTML files

Web Server

①request

③response

HTTP

Serv. Engine

DB

servlet

②execute

#80

#8080