Giter VIP home page Giter VIP logo

fp_sisop19_b02's Introduction

sisop_FP

Soal:

Buatlah program C yang menyerupai crontab menggunakan daemon dan thread. Ada sebuah file crontab.data untuk menyimpan config dari crontab. Setiap ada perubahan file tersebut maka secara otomatis program menjalankan config yang sesuai dengan perubahan tersebut tanpa perlu diberhentikan. Config hanya sebatas * dan 0-9 (tidak perlu /,- dan yang lainnya).

Jawab:

Check : Full SourceCode

Penjelasan:

  1. Menampilkan nama dari direktori yang saat ini sedang dibuka menggunakan getcwd
    if(getcwd(curPath, sizeof(curPath)) == NULL){
 
       perror("[error - getcwd]");
    
       }
  1. Menyalin dan menggabungkan 2 buah string
    strcpy(tabfile, curPath);
    strcat(tabfile, "/crontab.data");
  1. Menampilkan direktori yang aktif
   if ((chdir("/")) < 0) {
        exit(EXIT_FAILURE);
    }
  1. Menampilkan yang error
if (tmstmp->tm_sec == 0){
            FILE *confile = fopen(tabfile, "r");
            if(confile == NULL){
                perror("[error - fopen]");
  1. Mendapatkan hari,tanggal, waktu
 char min[3], hour[3], mday[3], mon[3], wday[2], cmd[1000];
                int cnt = 0;
                while(EOF != fscanf(confile, "%2s %2s %2s %2s %2s %[^\r\n]", min, hour, mday, mon, wday, cmd)){
                    if (fgetc(confile) != '\n') 
                        getc(confile);
                    if((strcmp(min, "*") == 0? 1 : atoi(min) == tmstmp->tm_min) &&
                        (strcmp(hour, "*") == 0? 1 : atoi(hour) == tmstmp->tm_hour) &&
                        (strcmp(mday, "*") == 0? 1 : atoi(mday) == tmstmp->tm_mday) &&
                        (strcmp(mon, "*") == 0? 1 : atoi(mon) == (tmstmp->tm_mon + 1)) &&
                        (strcmp(wday, "*") == 0? 1 : atoi(wday) == tmstmp->tm_wday)
                    ){    
  1. Membuat thread
int terr;
                       terr = pthread_create(&(tid[cnt]), NULL, &doWork, &cmd);
                       if(terr!=0){
                           printf("can't create thread[%d] : [%s]\n",cnt, strerror(terr));
                       }else{
                           printf("create thread[%d] success\n", cnt);
                       }
                       terr = pthread_join(tid[cnt], NULL);
                       if(terr != 0){
                           printf("can't join thread[%d] : [%s]\n",cnt, strerror(terr));
                       }else{
                           printf("join thread[%d] success\n", cnt);
                       }
                       cnt++;
  1. Untuk mengetahui apakah exec sudah berjalan dan sudah selesai dijalankan
void* doWork(void *args){
    char *cmd = (char *) args;
    printf("mulai exec - %s\n", cmd);
    system(cmd);
    printf("exec selesai\n");
    return NULL;
}

Reference:

fp_sisop19_b02's People

Contributors

rizanw avatar sherlyrosa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.