150 likes | 413 Views
Linux Programming Example: 6 – 4 & 6-17. 컴퓨터공학 2007242158 펠네르 파울로. Linux Programming. Contents 1. brk () & sbrk () 2. Source analysis 3. Execution fork(), wait() & execlp () Source analysis. brk () & sbrk (). brk () & sbrk () – both change segment size. brk () Definition:
E N D
Linux Programming Example: 6 – 4 & 6-17 컴퓨터공학 2007242158 펠네르 파울로
Linux Programming • Contents • 1. brk() & sbrk() • 2. Source analysis • 3. Execution • fork(), wait() & execlp() • Source analysis
brk() & sbrk() brk() & sbrk() – both change segment size. brk() Definition: brk () sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory. On success: brk() returns zero. On error: brk() -1 is returned.
sbrk & sbrk sbrk() Definition: sbrk() increments the program’s data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break. On success: sbrk() returns the previous program break. ( if the break was increased, then this value is a pointer to the start of the start of the newly allocated memory). On error: sbrk() -1 is returned.
Programming explanation extern intetext, edata, end; main() { intbrk(), ret; char *sbrk(), *bv; system("clear"); printf("The program text ends at %07o\n", &etext); printf("The initialized data ends at %07o\n", &edata); printf("The uninitialized data ends at %07o\n", &end); 프로세서의 영역을 나타내는 외부 변수들을 선언한다. 프로세서의현재 택스트, 데이타 영역의 가상 주소를 출력한다.
bv = sbrk(0); printf("Current break value is %07o\n\n",bv); /* 01000 */ ret = brk(bv+512); printf("brk returned . . . . %d\n",ret); bv = sbrk(0); printf("Current break value is %07o\n\n",bv); ret = brk(&ret); printf("brk returned . . . . %d\n",ret); bv = sbrk(0); printf("Current break value is %07o\n\n",bv); 현재의 브레이크 값을 출력한다. 이때의 값은 &end 의 값과 같다. 브레이크 값을 512 증가시킨다. 브레이크 값을 데이다 스택 내의 주소로 설정한다.
/* 0100 */ bv = sbrk(64); printf("sbrk returned %07o\n",bv); bv = sbrk(0); printf("Current break value is %07o\n\n",bv); /* memory deallocation: -02000 */ bv = sbrk(-1024); printf("sbrk returned %07o\n",bv); bv = sbrk(0); printf("Current break value is %07o\n\n",bv); } 브레이크 값을 64만큼 증가시킨다. 데이타 세그먼트의 크기를 1024바이트 감소시킨다.
fork(), wait() & execlp() fork() fork () - creates a child process that differs from the parent process. Return value On success: The PID of the child process is returned in the parent, and 0 is returned in the child. On failure: -1 is returned in the parent, no child process is created.
fork(), wait() & execlp() wait() wait() - wait for process to change state. wait() – system call suspends execution of the calling process until one of its children terminates. Return value: On success: returns the process ID of the terminated child. On failure : -1 is returned.
fork(), wait() & execlp() execlp() execlp() – execute a file execlp(), the function duplicate the actions of the shell in searching for an executable file if the specified file name does not contain a slash(/) character. Return value The exec() functions only return if an error has have occurred. The return value is -1.
Programming explanation command(cmd) char *cmd; { intchpid, fork(); int w, status, wait(); if ((chpid= fork()) == 0) { execlp(“sh”, “sh“, “-c“, cmd,(char *) 0); exit(127); } while((w = wait(&status)) !=chpid && w !=-1); if(w ==-1) status =-1; return(status >> 8); } 자식 프로세스를 생성하여 셸(shell)을 수행시킨다. 이때 –c 옵션(option)을 다음의 인수가 명령어임을 뜻한다. 즉, 매개 변수로 받은 명령어(cmd)를 실행시킨다. 자식 프로세스가 종료하기를 기다린다.
main() { printf(“%d\n“, command(“date > Data; cat Date“)); printf(“%d\n“, command(“ who am I”)); } date 명령어를 매개 변수로 하여 command 함수를 실행시킨다.