해바

Lab #13 본문

System Programming

Lab #13

Bacha 2019. 11. 18. 13:19

popen()과 pclose를 사용하여 "who | sort"를 실행 시킬 수 있는 프로그램을 작성하라.

 

 

// pipe.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
    FILE* read_fp, *write_fp;
    char buf[BUFSIZ];

    read_fp = popen("who", "r");
    write_fp = popen("sort", "w");

    if(read_fp != NULL && write_fp != NULL) {
        fread(buf, sizeof(char), BUFSIZ, read_fp);
        fwrite(buf, sizeof(char), strlen(buf), write_fp);

        pclose(read_fp);
        pclose(write_fp);
        exit(0);
    }

    exit(1);
}

 

 

컴파일 :

%gcc pipe.c

 

실행 :

%./a.out

'System Programming' 카테고리의 다른 글

Lab #15  (0) 2019.12.02
Lab #14  (0) 2019.11.18
Lab #12  (0) 2019.11.18
Lab #11  (0) 2019.11.18
Lab #10  (0) 2019.11.18
Comments