CIS 505 HW 1 用到的 functions

  • pipe() system call returns a pair of file handles and not FILE obj;
  • fdopen(3); Use this to convert file handles to FILE obj
FILE *fdopen(int fd, const char *mode);
  • pid_t is basically an int
The pid_t data type is a signed integer type which is 
capable of representing a process ID. In the GNU C Library, this is an int.
  • fdopen() + fgets() + fprintf() 组合建立父子通讯:
  • pipe[0] 为读取端口, pipe[1]为写入端口
//参考作业
//read
char rmsg[1000];
FILE *rstream;
rstream = fdopen(dnPipe[i][0], "r");
fgets(rmsg,1000,rstream);
printf("Kid revived %s\n",rmsg);
fclose(rstream);

//write
FILE *wstream;
wstream = fdopen(upPipe[i][1], "w");
fprintf (wstream,"blablabla");
fclose(wstream);
  • Read input from stdin
void LoadfromSTDIN(vector<long long> &input){
    char rmsg[1000];
    FILE *rstream;
    rstream = fdopen(STDIN_FILENO, "r");

    fgets(rmsg, 1000, rstream);
    int n = atoi(rmsg);
    cout << n <<endl;
    int k = 0;
    while(k<n){
        k++;
        fgets(rmsg, 1000, rstream);
        input.push_back(atoll(rmsg));       
    }
}
  • Parse input using getopt()
//for example ./mysort -n 4 -t input.txt input2.txt
//parse input using getopt()
//parse flagsfirst
char *nvalue = NULL;
int nflag = 0, tflag = 0;
int c = 0;
while ((c = getopt(argc, argv, "n:t")) !=-1){
    switch (c){
        case 'n':
            nvalue = optarg;
            nflag = 1;
            break;
        case 't':
            tflag = 1;
            break;
        case '?':
            if(optopt == 'n'){
                fprintf(stderr, "Option -%c requires an argument.\n", optopt);
            } else {
                fprintf(stderr, "Unknown option '-%c'.\n", optopt); 
            }
            exit(1);
        default:
            abort();
    }
}
N = atoi(nvalue);
//parse the following args
for(index = optind; index < argc; index++){
    // parse the input files
    vector<long long> input;
    readFile(input, argv[index]);
    Allnums.insert(end(Allnums), begin(input),end(input));
}
  • CHILD process don't have to call exec!!!!! for example, in this assignment I have
for(int i = 0; i< N; i++){
    pipe(upPipe[i]);
    pipe(dnPipe[i]);

    pid_t pid = fork();
    if(pid < 0){
        perror("fork failed\n");
        exit(1);
    } else if (pid == 0){
        //===============
        // Child process
        //===============
        vector<long long> input;
        //-------------------------------------
        // read from parent process from down pipe
        //-------------------------------------
        close(dnPipe[i][1]); //first close write end
        char rmsg[1000];
        FILE * rstream;
        rstream = fdopen(dnPipe[i][0], "r");
        fgets(rmsg, 1000, rstream);
        int n = atoi(rmsg); // the first line tells how many line this stream have
        for(int j = 0; j < n; j++){
            fgets(rmsg, 1000, rstream);
            input.push_back(atoll(rmsg));
        }
        fclose(rstream);   
        close(dnPipe[i][0]);
        //-------------------------------------
        // finish reading, bubble sort the input
        //-------------------------------------
        bubbleSort(input);
        //-------------------------------------
        // write sorted result to parent
        //-------------------------------------
        close(upPipe[i][0]); //first close read end
        FILE *wstream;
        wstream = fdopen(upPipe[i][1], "w");
        fprintf(wstream, "%d\n", (int) input.size());
        for(int j = 0; j < (int) input.size(); j++){
            fprintf(wstream, "%lld\n", input[j]);
        }
        fclose(wstream);
        close(upPipe[i][1]);
        exit(0);
    }
}
  • Multithread using pipe:
struct threadPipe_struct{
    int upPipe[2];
    int dnPipe[2];
};
...
void *threadBubble(void *arg){
    vector<long long> input;
    struct threadPipe_struct *arg_struct = (struct threadPipe_struct *) arg;
    //-------------------------------------
    // read from parent thread from down pipe
    //-------------------------------------
    //-------------------------------------
    // finish reading, bubble sort the input
    //-------------------------------------
    //-------------------------------------
    // write sorted result to parent
    //-------------------------------------
    pthread_exit(0);
}
...
    struct threadPipe_struct threadPipe[N];

    //Launch N threads
    pthread_t tids[N];
    for(int i = 0; i < N; i++){
        //create the communication pipe
        pipe(threadPipe[i].upPipe);
        pipe(threadPipe[i].dnPipe);

        pthread_attr_t attr;
        pthread_attr_init(&attr);

        // Child thread:
        if(pthread_create(&tids[i], &attr, threadBubble, &threadPipe[i]) != 0){
            fprintf(stderr, "Unable to create thread!\n");
        }
    }
...
  • A program that checks if seek is possible( if there is stdin!)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
    if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) {
        printf("Cannot do seek.\n"); fflush(stdout);
    }
    else {
        printf("Seek is possible.\n"); fflush(stdout);
    }
    return 0;
} /* End of main. */

results matching ""

    No results matching ""