Develop
2003.04.23 10:55
[linux] 프로세스 관련 시스템콜
Views 7875 Comment 0
system ( ) 함수: 쉘 명령어를 실행시킨다.
예제: system.c
1 #include <stdlib.h>
2
3 main() {
4 system("/usr/bin/ls -l");
5 }
#35 hyundai1:/export/home0/oracle/sql/sql00/c% a.out
총 14
-rwxr-xr-x 1 sql00 sql 5444 11월 6일 20:00 a.out
-rw-r--r-- 1 sql00 sql 60 11월 6일 20:00 system.c
--------------------------------------------------------------------------------
execl ( ) 함수: 프로그램을 실행시킨다.
예제: execl.c
1 main() {
2 int pid;
3 int status;
4
5 pid = fork();
6 if(pid > 0) {
7 wait(&status);
8 } else if(pid == 0) {
9 execl("/usr/bin/ls", "ls", "-l", 0);
10 } else {
11 printf("fork error.n");
12 }
13 }
#97 hyundai1:/export/home0/oracle/sql/sql00/c% a.out
총 26
-rwxr-xr-x 1 sql00 sql 5736 11월 6일 21:11 a.out
-rw-r--r-- 1 sql00 sql 185 11월 6일 21:11 execl.c
-rw-r--r-- 1 sql00 sql 69 11월 6일 20:03 fork.c
-rw-r--r-- 1 sql00 sql 196 11월 6일 21:03 fork2.c
-rw-r--r-- 1 sql00 sql 213 11월 6일 21:03 fork3.c
-rw-r--r-- 1 sql00 sql 1736 11월 6일 21:08 process.html
-rw-r--r-- 1 sql00 sql 60 11월 6일 20:00 system.c
<xmp>
--------------------------------------------------------------------------------
fork ( ) 함수: 새로운 프로세스를 생성한다.
예제: fork1.c
1 main() {
2 int pid;
3
4 pid = fork();
5 printf("my pid = %dn", pid);
6 }
#43 hyundai1:/export/home0/oracle/sql/sql00/c% a.out
my pid = 0
my pid = 9015
예제: fork2.c
1 main() {
2 int pid;
3
4 pid = fork();
5 if(pid > 0) {
6 printf("I am parent.n");
7 } else if(pid == 0) {
8 sleep(1);
9 printf("I am child.n");
10 } else {
11 printf("fork error.n");
12 }
13 }
#75 hyundai1:/export/home0/oracle/sql/sql00/c% a.out
I am parent.
#76 hyundai1:/export/home0/oracle/sql/sql00/c% I am child.
--------------------------------------------------------------------------------
wait ( ) 함수: 부모 프로세스가 자식 프로세스가 종료될 때까지 기다린다.
예제: fork3.c
1 main() {
2 int pid;
3 int status;
4
5 pid = fork();
6 if(pid > 0) {
7 printf("I am parent.n");
8 wait(&status);
9 } else if(pid == 0) {
10 sleep(2);
11 printf("I am child.n");
12 } else {
13 printf("fork error.n");
14 }
15 }
#87 hyundai1:/export/home0/oracle/sql/sql00/c% a.out
I am parent.
I am child.
--------------------------------------------------------------------------------
atexit ( ) 함수: 프로그램이 종료되기 전에 atexit에 등록된 함수가 수행된다.
사용법
#include
int atexit(void (*func)(void));
예제: atexit.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void cleanup() {
5 printf("Clean up before exit.n");
6 }
7
8 main() {
9 atexit(cleanup);
10 printf("Hello World !!n");
11 }
#19 enterprise:/data1/student/c9844000% a.out
Hello World !!
Clean up before exit.
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
309 | System/OS | [linux] 패킷의 소스 주소 바꾸기 | hooni | 2003.04.23 | 18866 |
308 | System/OS | [linux] 프로그램 설치방법 (내공쌓기) | hooni | 2003.04.23 | 13495 |
» | Develop | [linux] 프로세스 관련 시스템콜 | hooni | 2003.04.23 | 7875 |
306 | System/OS | [linux] 프로세스 상태확인(ps) | hooni | 2003.04.23 | 12776 |
305 | System/OS | [linux] 프로세스의 stat 상태에 대한 설명 | hooni | 2013.04.23 | 10839 |
304 | System/OS | [linux] 한글 URL 인식할 수 있게 아파치(Apache) 설정 (mod_url.c 설치) | hooni | 2013.04.23 | 13156 |
303 | System/OS | [macos] How to Fix ‘You Shut Down Your Computer Because of a Problem’ | hooni | 2022.06.01 | 975 |
302 | System/OS | [mac] Charlesproxy 간단한 설정 내용~ | hooni | 2013.11.12 | 12447 |
301 | System/OS | [mac] Homebrew/rvm/cocoapod setting | hooni | 2017.07.29 | 1185 |
300 | System/OS | [mac] How to uninstall MySQL on Mac OS. | hooni | 2017.11.08 | 1052 |
299 | System/OS | [mac] Mac OS 패키지 매니저, HomeBrew | hooni | 2015.01.03 | 1475 |
298 | System/OS | [mac] Mac OS에서 재생되는 사운드를 녹음하는 방법 | hooni | 2016.10.03 | 1746 |