fork() 실습
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
void cleanupaction(void);
int main(int argc,char **argv){
pid_t pid;
int i;
for(size_t i=0;i<3;i++){
printf("before fork[%d]\n",i);
sleep(1);
}
// fork
pid=fork();
if(pid>0){ //부모프로세스
for(size_t i=0;i<7;i++){
printf("parent [%d]\n",i);
sleep(1);
}
atexit(cleanupaction);
}
else if(pid==0) //자식 프로세스
{
for(size_t i=0;i<5;i++)
{
printf("child[%d]\n",i);
sleep(1);
execl("/bin/ls","ls","-l",(char *)0);
}
}
else
{
printf("fail to fork child\n");
}
return 0;
} //end -of main
void cleanupaction(void)
{
printf("Clean-up action\n");
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
waitpid() 실습
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void errors(char *msg, int code);
void cleanupaction(void);
int main(int argc,char **argv)
{
pid_t pid;
int status;
if((pid=fork())>0){ //부모프로세스가 수행하는 부분
while(!waitpid(pid,&status, WNOHANG)){
printf("parent stauts : %d\n",status++);
sleep(1);
}
printf("parent child exit:%d\n",status);
}
else if(pid==0){ //child
sleep(5);
printf("bye child\n");
exit(1);
}
else
{
printf("failed to fork\n");
}
printf("Buy buy\n");
return 0;
}
void cleanupaction(void)
{
printf("Clean-up action\n");
}
void error(char *msg, int code)
{
perror(msg);
exit(code);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
tms 리눅스 시스템에서는 프로세스의 수행시간을 clock_t 단위로 계산하는 times 함수 제공, times 함수는 strcut tms 형식 변수의 주소를 입력 인자로 받아 수행 시간을 채운다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
void do_in_child(int seq, int rval);
int main() {
struct tms buf;
srand((unsigned)time(0));
int i = 0;
for (i = 0; i < 5; i++)
do_in_child(i + 1, rand() % 10);
int rval = 0; pid_t cpid = 0;
while (i > 0) {
cpid = wait(&rval);
printf("cpid:%d exit status:%#x\n", cpid, rval);
i--;
}
for (i = 0; i < 100; i++) putchar('-');
times(&buf);
printf("USER CPU time:%d \n", buf.tms_utime);
printf("SYSTEM CPU time:%d \n", buf.tms_stime);
printf("Children's USER CPU time:%d \n", buf.tms_cutime);
printf("Children's SYSTEM CPU time:%d \n", buf.tms_cstime);
return 0;
}
void do_in_child(int seq, int rval) {
pid_t cpid = fork();
if (cpid == -1) {
perror("error fork");
return;
}
if (cpid > 0)
printf("fork child pid:%u\n", cpid);
else {
printf("pid:%d sleep %dseconds\n", getpid(), rval);
int i = 0;
for (i = 0; i < 100; i++) {
putchar('.');
}
exit(seq);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'딥러닝 기반 영상인식 개발 전문가 과정 > 리눅스' 카테고리의 다른 글
6월3일 시그널, Alarm,sleep (0) | 2019.06.03 |
---|---|
6월3일 sid 와 프로세스, 신호(Signal) (0) | 2019.06.03 |
5월 31일 프로세스, fork, wait (0) | 2019.05.31 |
5월30일 시스템 환경설정과 자원제한 (0) | 2019.05.30 |
5월 30일 사용자와 그룹 (0) | 2019.05.30 |