The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
A
By Guest on 16th May 2019 05:27:56 AM | Syntax: TEXT | Views: 13



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <signal.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. int main() {
  10.    int fds[2], fds2[2];
  11.    int status, status2;
  12.    int pid;
  13.    char buffer[100];
  14.    char readmsg[100];
  15.    int total = 0, readtotal;
  16.    FILE *command;
  17.  
  18.    status = pipe(fds);
  19.    status2 = pipe(fds2);
  20.  
  21.    if (status == -1) {
  22.       printf("Gagal membuat pipe\n");
  23.       return 1;
  24.    }
  25.    if (status2 == -1) {
  26.       printf("Gagal membuat pipe\n");
  27.       return 1;
  28.    }
  29.  
  30.    pid = fork();
  31.    
  32.    // Child process
  33.    if (pid == 0) {
  34.       printf("Grabbing process......\n\n");
  35.       command = popen("ps -a -o pid,tty,command", "r");
  36.  
  37.       if (command == NULL){
  38.          printf("Gagal menjalankan command\n");
  39.          return -1;
  40.       }
  41.  
  42.       while (fgets(buffer, sizeof(buffer), command) != NULL){
  43.          write(fds[1], buffer, sizeof(buffer));
  44.          total++;
  45.       }
  46.  
  47.       write(fds2[1], &total, sizeof(total));
  48.       printf("Child Done.\n\n");
  49.       pclose(command);
  50.    } else { // Parent process
  51.       wait(NULL);
  52.       close(fds[1]);
  53.       close(fds2[1]);
  54.  
  55.       read(fds2[0], &readtotal, sizeof(readtotal));
  56.       printf("Parent ready : \n\n");
  57.       printf("-------- [ List process ] --------\n\n");
  58.       int i = 0;
  59.       while(1){
  60.          read(fds[0], readmsg, sizeof(readmsg));
  61.          printf("%s\n", readmsg);
  62.          i++;
  63.          if(i > readtotal) {
  64.             break;
  65.          }
  66.       }
  67.  
  68.       return 0;      
  69.    }
  70.  
  71.  
  72. }



  • Recent Pastes