Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int main(int argc, char *argv[], char *envp[])
  10. {
  11. int i = 0 ; //contador do loop
  12. unsigned long long k;
  13. int pid = fork(); //criando o processo filho e guardando o identificador
  14. char convert[200], comand[200]; //convert -> variavel pra armazenar o pid convertido pra string
  15. //comand -> o comando final do linux para monitorar
  16. if(pid < 0)
  17. {
  18. perror("Erro: ");
  19. exit(-1);
  20. }
  21. else if(pid == 0) //filho
  22. {
  23. if(strcmp(argv[1], "cpu") == 0)
  24. {
  25. for(k = 0; k < 999999999999; ++k)
  26. {
  27.  
  28. }
  29. }
  30. else if(strcmp(argv[1], "cpu-mem") == 0)
  31. {
  32. for(k = 0; k < 99; ++k)
  33. {
  34. malloc(sizeof(char) / 8);
  35. }
  36. }
  37. }
  38. else //pai
  39. {
  40. sprintf(convert, "%d", pid);
  41. if(strcmp(argv[1], "cpu") == 0)
  42. {
  43. printf(" PID / CPU(%%)\n");
  44. strcpy(comand, "ps -e -o pid,pcpu | grep ");//coloca uma parte do comando linux na string comand
  45. strcat(comand, convert);//concatena a string comand com o pid ja transformado em string
  46. }
  47. else if(strcmp(argv[1], "cpu-mem") == 0)
  48. {
  49. printf(" PID / CPU%%\n");
  50. strcpy(comand, "ps -e -o pid,pcpu | grep ");////coloca uma parte do comando linux na string comand
  51. //strcat(comand, convert);
  52. //sprintf(comand,"ps -e -o pid,pcpu | grep %s ",convert);
  53. sprintf(comand, "ps -e -o pid,pcpu | grep %s ;pmap %s |grep -i total",convert,convert);
  54. }
  55.  
  56. while(i < 10) //10 segundos
  57. {
  58. system(comand);
  59. sleep(1); //espera 1 segundo
  60. i++;
  61. }
  62.  
  63. strcpy(comand, "kill "); //guardando a string kill na string comand, para matar o pid do filho
  64. strcat(comand, convert); //concatenando a string comand("kill ") com o pid do filho
  65. system(comand); //matando o filho
  66.  
  67. }
  68.  
  69. exit(0) ; /* encerra o processo com sucesso (código 0) */
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement