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
/*
 프로그램에 경로를 주고 경로가 디렉토리인지 아닌지 확인
 다음 중 하나를 선택해 디렉토리를 점검한다
 -DIR 구조체
 -fopen()
 -stat 구조체
 
 디렉토리면 yes 아니면 no
 
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int isDir(char *);
int main(int argc,char *argv[])
 
{
    if(argc!=2){ exit(1);}
 
 
    if(isDir(argv[1]))
    {
        printf("%s not exist\n",argv[1]);
    }
    else
    {
        printf("%s exist\n",argv[1]);
        
    }
}
 
int isDir(char *path){
    DIR *dirp;
    dirp=opendir(path);
    closedir(dirp);
    return dirp == NULL ? 1:0;
}
 
 
 
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

 

 

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
51
52
/*
   두 양수를 입력 받아 다음 작업을 만들어보세요
   1.부모의 프로세스 x의 y제곱 계산
   2. 자식 프로세스 x에서 y까지 합을 계싼
   3. 두 값의 합을 출력
 */
 
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
 
int main(int argc,char *argv[])
{
 
    int status;
    pid_t pid;
    int i;
    double result=1;
    int sum=0;
    int num1,num2;
    if(argc!=3){printf("error"); exit(1);}
    num1=atoi(argv[1]);
    num2=atoi(argv[2]);
 
    pid=fork();
    if(pid>0){ //부모
        for(i=1;i<=num2;i++)
        {
            result*=num1;
        }
        wait(&status);
 
        status=status>>8;
        printf("sqrt:%lf +  sum:%d = %lf\n",result,status,result+status);
 
    }
    else if(pid==0)
    {
        for(i=num1;i<=num2;i++)
        {
            sum+=i;
        }
        exit(sum);
    }
    else
    {
        printf("fail to fork child\n");
    }
    return 0;
}
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

 

 

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
51
52
53
54
55
56
57
58
/*
   자식 프로세스에서 10초 간격으로 현재 시각을 파일
   "current_time.txt"에 저장하는 프로그램을 작성
   -단, 세션이 종료되도 프로그램이 실행되도록 한다.
 
 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
 
int main()
{
    pid_t pid;
    int i;
    FILE *fp;
    time_t now_time;
    struct tm *now_data;
    char buf[100];
    int status;
    time_t result;
    pid=fork();
    if(pid>0)
    {
        sleep(1);
        exit(1);
 
 
    }
    else if(pid==0)
    {
        
        setsid();
        fp=open("./current_time.txt",O_RDWR | O_CREAT,0644);
 
 
        while(1)
        {
            result=time(NULL);
            strcpy(buf,asctime(localtime(&result)));
            printf("%s",buf);
            write(fp,buf,strlen(buf));
            sleep(2);
        }
    
    }
    else
    {
        printf("tail to fork child\n");
    }
 
    return 0;
 
}
 
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

+ Recent posts