2010/03/25 19:32
/*
# 프로그램 순서
사용자로부터 시간 간격을 입력 받는다.
입력 받은 시간 간격마다 fork()함수로 프로세스를 복제한다.
복제된 자식 프로세스는 execl()함수를 이용하여 date명령을 수행하는  자식 프로세스를 생성하고 종료된다.
입력 받은 시간간격으로 현재 시간과 날짜를 반복 출력한다.
# 요구사항
프로그램을 작성 후 Makefile을 만들어서 컴파일 한다.

# 부연설명
fork는 자신과 동일한 프로세스만 생성 가능하지만, execl은 자신과 다른 프로세스를 생성 가능하다.
fork는 새로운 프로세스를 생성하더라도 종료되지 않고 자식 프로세스와 동시에 수행되지만, execl은 새로운 프로세스를 생성하면 자신은 종료되어야 한다.
fork로 자식 프로세스를 생성한 후 자식 프로세스 가 execl로 새로운 프로세스를 생성한 후 종료됨.(fork의 장점과 execl의 장점을 동시에 취함)
*/

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

int main(void) {

    int reTime = 0;
    pid_t pid = 0;

    printf("\nEnter repeating time : ");
    scanf("%d", &reTime);

    printf("\nCurrent running process\n");
    system("ps -l");
    printf("\n");

    while (1) {
        
        pid = fork();

        if ( pid == 0) { // fork()가 제대로 수행되었을 때. 즉 자식 프로세스 생성이 되었을 때.
            
            printf("The %d is child's PID\n", (int) getpid() );
            execl("/bin/date", "date", (char *)0);

        }

        else if ( pid  == -1 ) printf("Error....\n\n"); // 제대로 fork()가 수행되지 않았을 때

        else printf("The %d is parent's PPID\n", (int) getppid() );

        // printf("\n");
        system("ps -l");
        printf("\n");

        sleep(reTime);

        // printf("\n");
        // system("ps -l"); // test
        // printf("\n");

    }
    
    return 0;

}


크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 딸기맛a사탕b
2010/03/25 19:21
/*

# 프로그램 순서

사용자로부터 프로세스 생성 개수를 입력 받는다.
입력 받은 개수만큼 프로세스(“sleep 10000” 사용)를 생성한다. (생성시 백그라운드 설정)
생성된 프로세스 목록을 보여준다. (ps 명령어에 옵션 사용)
사용자로부터 프로세스 아이디(PID)를 입력 받는다.
입력 받은 프로세스를 삭제하고, 남은 프로세스 목록을 보여주며 프로그램을 종료한다.

# 요구사항

프로그램을 작성 후 Makefile을 만들어서 컴파일 한다.
시스템 프로세스를 비롯한 다른 것들을 삭제하지 않도록 한다.

*/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

int main(void) {

    int numofPro=0, pidofPro=0;
    int i=0;

    printf("\n");

    printf("Viewing active process\n\n");
    system("ps -l");

    printf("\n");

    printf("Enter making number of process : ");
    scanf("%d", &numofPro);

    i = numofPro;

    while (numofPro > 0) {

        system("sleep 10000 &");
        numofPro--;

    }

    printf("\n");

    printf("List of process after making process\n\n");
    system("ps -l");

    printf("\n");

    printf("Select killing PID of process : ");
    scanf("%d", &pidofPro);

    for ( ; i>0 ; i--) {
    
        kill(pidofPro, SIGTERM);
        pidofPro += 2;
    
    }

    printf("\n");

    printf("List of process after killing process\n\n");
    system("ps -l");

    printf("\n");

    return 0;
}



크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 딸기맛a사탕b
2009/08/24 20:09



♬ Lifehouse - Everything ♬

Find Me Here
Speak To Me
I want to feel you
I need to hear you
You are the light
That's leading me
To the place where I find peace again.

You are the strength, that keeps me walking.
You are the hope, that keeps me trusting.
You are the light to my soul.
You are my purpose...you're everything.

How can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?

You calm the storms, and you give me rest.
You hold me in your hands, you won't let me fall.
You still my heart, and you take my breath away.
Would you take me in? Take me deeper now?

How can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?
And how can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?

Cause you're all I want, You're all I need
You're everything,everything
You're all I want your all I need
You're everything, everything.
You're all I want you're all I need.
You're everything, everything
You're all I want you're all I need, you're everything, everything.

And How can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?
How can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?

[background]
How can I stand here with you and not be moved by you?
Would you tell me how could it be any better than this?

Would you tell me how could it be any better than this?





크리에이티브 커먼즈 라이선스
Creative Commons License

'temp' 카테고리의 다른 글

everything  (0) 2009/08/24
사랑합니다 나의 예수님  (0) 2009/06/10
XP Administrator(관리자)계정 암호 삭제 방법  (0) 2008/07/22
Posted by 딸기맛a사탕b
2009/06/10 00:37
사용자 삽입 이미지



크리에이티브 커먼즈 라이선스
Creative Commons License

'temp' 카테고리의 다른 글

everything  (0) 2009/08/24
사랑합니다 나의 예수님  (0) 2009/06/10
XP Administrator(관리자)계정 암호 삭제 방법  (0) 2008/07/22
Posted by 딸기맛a사탕b
2009/04/07 19:15
[28-D16] Collections Framework - Hashtable, Iterator, Generics

[01] Collections Framework - Hashtable 클래스

    - Key, 값으로 되어 있는 구조 입니다.

    - 중복된 키 값을 허용하지 않습니다. 만약 사용하게되면 기존의 값이 삭제됩니다.

    - 검색 결과가 없으면 null을 리턴합니다.

    - 다수의 스레드가 접속하더라도 동기화 처리가 되어 있어서 값을 안정적으로
      변경 할 수 있습니다.


java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable

모든 구현 인터페이스:
Cloneable , Map , Serializable

직계의 기존의 서브 클래스:
Properties , UIDefaults


>>>>> TestHashTable.java

import java.util.Hashtable;
import java.util.Map;


public class TestHashTable {

    public static void main(String[] args) {
        // Hashtable ht = new Hashtable();
        Map ht = new Hashtable();
        
        // 키, 값의 구조
        ht.put("AREA01", "대한민국");
        ht.put("AREA02", "러시아");
        ht.put("AREA03", new Integer(1000));
        ht.put("AREA04", "일본");
        
        String area = (String)ht.get("AREA01");
        // String area = (String)ht.get("AREA05");
        
        if ( area != null){
            System.out.println(area);
        }else{
            System.out.println("검색 지역이 없습니다.");
        }
        
        Integer integer = (Integer)ht.get("AREA03");
        System.out.println(integer);
    }
}







[02] Iterator
     - Collection Framework안에 저장된 객체를 하나씩 추출하는 기능을 합니다.

1. List - Vector에서의 사용

>>>>> IteratorVector.java

import java.util.Vector;
import java.util.Iterator;

public class IteratorVector {
    public static void main(String args[]){
        //Vector에 요소 저장
        Vector v = new Vector();
        
        v.addElement("관악산");
        v.addElement("북한산");
        v.addElement("도봉산");        
        v.addElement("수락산");
        v.addElement("불암산");
        v.addElement("사패산");

        //vector에 저장된 sungjuk객체 추출하여 출력
        Iterator iter = v.iterator();
        
        while(iter.hasNext() == true){
            // 처음부터 마지막까지 요소 추출
            String s = (String)iter.next();
            System.out.println(s);
        }
        
    }
}





2. Map - HashTable에서의 사용

>>>>> IteratorHashTable.java

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class IteratorHashTable {

    public static void main(String[] args) {
        // Hashtable ht = new Hashtable();
        Map ht = new Hashtable();
        
        // 코드, 값의 구조
        ht.put("AREA01", "대한민국");
        ht.put("AREA02", "러시아");
        ht.put("AREA03", "중국");
        ht.put("AREA04", "일본");
        
        //Iterator iter = ht.keySet().iterator();
        
        // 키들로만 구성된 집합을 추출
        Set keySet = ht.keySet();
        Iterator iter = keySet.iterator();
        
        // 다음키가 있는지 검사합니다.
        while(iter.hasNext() == true){
            
            // 키를 순차적으로 읽어 옵니다.
            String key = (String)iter.next();
            
            // 키에 해당하는 값을 추출합니다.
            String str = (String)ht.get(key);
            System.out.println(key + " - " + str);
        }
    }
}







[03] Generics

   - 객체를 저장하는 기술인 Collection Framework의 단점을 개선한
     기능입니다. 기존의 1.4이하에서는 Vector등에 들어오는 객체의
     타입을 Design Time에 확인이 안되 에러를 잡기 힘들었으나 1.5부터는
     개발자가 개발시에 이클립스로부터 에러 상황을 바로 확인할 수 있습니다.

   - Class ArrayList<E>에서 'E'가 있는 곳에는 ArrayList를 선언하고
     생성할 때 사용할 실제타입이 들어갑니다.

   - E는 Element(원소)를 말하며, new ArrayList<String>() 이면 'E'는
     'String' 클래스를 가르키며, < > 안의 String은 유형 매개 변수
     (type parameter)이다.
     따라서 boolean add(Object o) 메소드는 boolean add(String o) 가 된다.

   - 'E'는 ArrayList의 인스턴스를 만들때 < > 안에 넣는 타입을 말합니다.

   - 'E'는 컬렉션에 저장하고 컬렉션에서 리턴할 원소의 타입을 가르킵니다.



※ Generics 관련 예제

1. 1.4이하의 패턴

>>>>> Old14.java

import java.util.Vector;


public class Old14 {

    public static void main(String[] args) {
        // 객체 10개를 저장할 수 있는 list 객체 변수 선언
        Vector list = new Vector(1000);
        
        list.add(new Integer(10)); //Integer 추가
        list.add(new Integer(50)); //Integer 추가
        list.add("List Test");     //String 추가, ERROR
        
        for(int i=0; i<list.size(); i++){
            //Integer 객체를 추출합니다.
            Integer iobj = (Integer)list.get(i);
            
            //객체가 가지고 있는 값에 1000을 더합니다.
            int tot = iobj.intValue() + 1000;
            System.out.println("tot= " + tot);
        }

    }

}





2. generics가 적용된 코드 (compile 시점에 에러가 체크됨 )

>>>>> Generics.java

import java.util.Vector;
import java.util.Iterator;
import java.util.List;

public class Generics {

    public static void main(String[] args) {
        //선언되는 list는 Integer 클래스 타입만 저장할 수 있습니다.
        Vector<Integer> list = new Vector<Integer>(10);
        list.add(new Integer(10));
        
        //ERROR 강제 발생 부분, Integer 타입만 가능,
        //Design Time에서 발견됩니다.
        list.add("List Test");

        Iterator i = list.iterator();
        Integer su = (Integer)i.next();
        System.out.println("su=" + su);
        
        su = (Integer)i.next();
        System.out.println("su=" + su);
    }
}





3. 메소드에서의 Generics 사용

>>>>> Generics2.java

import java.util.Vector;
import java.util.Iterator;

public class Generics2 {
    //list인수는 Integer 객체만 저장된 List 객체를
    //받을 수 있습니다.
    public void prn(Vector<Integer> list){
        Iterator i = list.iterator();
        
        for (int k=0; k < list.size(); k++){
            Integer su = (Integer)i.next();
            System.out.println("su=" + su);
            
        }
    }

    public static void main(String[] args) {
        Vector<Integer> list = new Vector<Integer>(10);
        
        list.add(new Integer(10));
        list.add(new Integer(20));
        // list.add(30); //권장 아님
        
        Generics2 g = new Generics2();
        g.prn(list);
        
        Vector<String> list2 = new Vector<String>(10);
        list2.add("월요일");
        list2.add("화요일");
        g.prn(list2); // Vector안에 Integer 객체가 저장된 것만 받음
        
    }
}
크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 딸기맛a사탕b