1 / 9

프로그램 설명

프로그램 설명. 리눅스 시스템 프로그래밍. 함수설명. getopt(argc, argv, const char* options). 함수설명. ulimt(int cmd, long b). 함수설명. fprintf(FILE *stream, const char *Format. ….). 스트림으로 서식화된 출력을 행한다. Printf 와 모든 기능에 있어서 동일하며 사용하는 서식도 동일하다. 다른 점은 fprintf 는 지정한 스트림으로 출력이 나간다. 에러시 EOF(-1) 을 반환한다.

Download Presentation

프로그램 설명

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. 프로그램 설명 리눅스 시스템 프로그래밍

  2. 함수설명 getopt(argc, argv, const char* options)

  3. 함수설명 ulimt(int cmd, long b)

  4. 함수설명 fprintf(FILE *stream, const char *Format. ….) 스트림으로 서식화된 출력을 행한다. Printf와 모든 기능에 있어서 동일하며 사용하는 서식도 동일하다. 다른 점은 fprintf는 지정한 스트림으로 출력이 나간다 에러시EOF(-1)을 반환한다. stderr는에러 발생시 에러문 출력을 하게 해줌..

  5. 함수설명 long atol(const char *s) 문자열을 Long형 정수로 변환한다. 문자열로 부터 변환된 long형 정수 변환이 불가능할 경우 0리턴 사용예str =“1212ad”; long a = atol(str); a는 1212가 저장

  6. 함수설명 실행 방법 ./a.out –? ? = i, s, p, u, U 여러 문자 동시 사용가능 ./a.out –i –s –p –u 이런 식으로도 사용가능.. 출력결과는 시연참조..

  7. 소스 #include <stdio.h> // 다양한 출력문에 대한 해더파일 #include <unistd.h> //getopt에 대한 헤더파일 #define GET_FSLIM 1 //ulimit 함수의 명령어를 선언한다. #define SET_FSLIM 2 // ulimit 함수의 명령어를 선언한다. extern char *optarg; // ./a.out -?(i, s, p, u) optarg(뒤따르는 문자열.) extern int optind; // 구별자를 따라 옵션을 찾는다. main(int argc, char *argv[]) { int c; // int형으로변환된 문자를 저장하기 위한 변수 static char options[] = "ispuU:"; //프로그램에서 사용가능한 문자 if(argc < 2) // 입력값이 하나일 경우 예>./a.out 오류처리 fprintf(stderr,"Usage: %s [-i] [-s] [-p] [-u] [-Unewulimit]\n", argv[0]); ./a.out –i 2045 –s –p 이렇게 입력하면 argv[0] = a.out, argv[1] = -i, argv[2] = 2045, argv[3] = -s, argv[4] = -p가 된다. 이경우 처음 optind은 1이되고(-i 위치) getopt가 한번 실행 된뒤 3(-s 위치)이된다. 또 한번 getopt가 실행되면 4(-p 위치)가된다.

  8. 소스 while((c=getopt(argc,argv,options)) != EOF) //getopt함수를 이용하여 옵션이 존재하지 않을 때까지 반복 switch(c) { case 'i': //실사용자 및 유효 사용자 ID 그룹과 유효 그룹 ID출력 printf("real userid = %d\n",getuid()); //실사용자 printf("effective userid = %d\n",geteuid());//실사용자그룹 printf("real groupid = %d\n",getgid()); //유효사용자 printf("effective groupid = %d\n",getegid()); //유효사용자그룹 break;

  9. 소스 case 's': //setpgrp()를 이용하여 프로세스를 새로운 구룹에 포함 (void) setpgrp(); //프로세스 그룹변경 break; case 'p': //프로세스, 부모, 구룹 프로세스 ID 출력 printf("process number = %d\n",getpid()); //프로세스ID printf("parent process number = %d\n",getppid()); //부모 프로세스 ID printf("group process number = %d\n",getpgrp()); //그룹 프로세스 ID case 'U': //프로세스의 크기를 변경 if(ulimit(SET_FSLIM,atol(optarg)) == -1) fprintf(stderr,"Must be super-user to increase ulimit\n"); break; case 'u': //프로세스 크기에 대한 값을 새로 설정 printf("ulimit = %ld\n",ulimit(GET_FSLIM,0L)); break; } }

More Related