Archive for the ‘ Programming ’ Category

System Call

fork :

호출 프로세스와 똑같은 새로운 프로세스를 하나 생성한다. fork는 가장 기본적인 프로세스 생성 프리미티브이다.

exec :

한 집단의 라이브러리 루틴과 하나의 시스템 호출로, 각각은 동일한 기능 즉, 한 프로세스의 기억공간을 새로운 프로그램으로 대치시킴으로써 프로세스를 변환시키는 기능을 수행한다. exec 호출들 각각의 차이는 그들의 인수 리스트가 어떤 방법으로 작성되는가에 있다.

wait :

이 호출은 초보적인 프로세스 동기화(synchronization)를 제공한다. 한 프로세스로 하여금 연관된 다른 프로세스가 끝날 때까지 기다릴 수 있게 한다.

exit :

프로세스를 종료할 때 사용된다.

java reflectioin exam

http://java.sun.com/developer/technicalArticles/ALT/Reflection/

URL 주소가 가리키는 파일 읽어서 저장

package com.test.url;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLSaver {
    public static void main(String[] args) {
        if(args.length != 2) {
            System.out.println("사용법 : java URLSaver URL filename");
            System.exit(1);
        }

        URL url = null;

        try {
            url = new URL(args[0]);
        } catch (MalformedURLException e) {
            System.out.println("잘못된 URL 형식입니다.");
            System.exit(1);
            e.printStackTrace();
        }

        FileOutputStream fos = null;

        try {
            URLConnection urlConnection = url.openConnection();
            InputStream in = urlConnection.getInputStream();

            fos = new FileOutputStream(args[1]);
            byte[] buffer = new byte[512];
            int readcount = 0;

            System.out.println("읽기 시작");
            while((readcount = in.read(buffer)) != -1) {
                fos.write(buffer, 0, readcount);
            }
            System.out.println("파일 저장 완료");
        } catch (IOException e) {
             e.printStackTrace();
        } finally {
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}