piątek, 13 kwietnia 2018

The class returns the program name for the process - /proc (g++/linux)

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
class get_Name
{
    private:
    int read_value;
    char *start_file;
    char *end_file;
    pid_t pid_value;
    char name[128];
    char info_value[1024];
    char *text_value;
    int file;
    public:
    get_Name(pid_t m_pid)
    {
      int int_pid;   
      pid_value=m_pid;
      int_pid=(int)pid_value;   
      snprintf(name,sizeof(name),"/proc/%d/stat",int_pid);
      file=open(name,O_RDONLY);
      read_value=read(file,info_value,sizeof(info_value)-1);
      close(file);
      info_value[read_value]='\0';
     
    }
    char *get_Text()
    {
        start_file=strchr(info_value,'(');
        end_file=strchr(info_value,')');
        text_value=new char[end_file-start_file];
        strncpy(text_value,start_file+1,end_file-start_file-1);
        text_value[end_file-start_file-1]='\0';
        return text_value;
    }
    ~get_Name()
    {
        delete text_value;
    }
};

int main(int argc, char **argv)
{
    pid_t test_pid=(pid_t) atoi(argv[1]);
    get_Name Get(test_pid);
    cout<<Get.get_Text()<<endl;
    return 0;
}

Brak komentarzy:

Prześlij komentarz