1 / 13

Linux Programming Example: 6 – 4 & 6-17

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:

haruki
Download Presentation

Linux Programming Example: 6 – 4 & 6-17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Programming Example: 6 – 4 & 6-17 컴퓨터공학 2007242158 펠네르 파울로

  2. Linux Programming • Contents • 1. brk() & sbrk() • 2. Source analysis • 3. Execution • fork(), wait() & execlp() • Source analysis

  3. 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.

  4. 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.

  5. 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); 프로세서의 영역을 나타내는 외부 변수들을 선언한다. 프로세서의현재 택스트, 데이타 영역의 가상 주소를 출력한다.

  6. 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 증가시킨다. 브레이크 값을 데이다 스택 내의 주소로 설정한다.

  7. /* 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바이트 감소시킨다.

  8. Example: 6-17

  9. 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.

  10. 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.

  11. 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.

  12. 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)를 실행시킨다. 자식 프로세스가 종료하기를 기다린다.

  13. main() { printf(“%d\n“, command(“date > Data; cat Date“)); printf(“%d\n“, command(“ who am I”)); } date 명령어를 매개 변수로 하여 command 함수를 실행시킨다.

More Related