Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why cp fails to copy /proc/stat file?

Tags:

linux

unix

procfs

In my linux machine if i try to copy /proc/stat it is creating 0 byte file. but if i do cat /proc/stat it has data. but the size always shows as 0.

 cp /proc/stat statfile

is creating zero byte file. If i write a program to copy then it worked. why is it so ?

  int main() 
  {
    std::ifstream procFile("/proc/stat");
    std::ofstream outfile("statfile");
    char buf[1024];
    while (!procFile.eof() && procFile.is_open())
    {
            procFile.getline(buf, 1024);
            outfile << buf<<endl;
    }

    procFile.close();
    outfile.close();
    return 0;
  }
like image 510
Dinesh Reddy Avatar asked Nov 04 '13 12:11

Dinesh Reddy


People also ask

What does CP Cannot stat mean?

This means cp: cannot stat 'mock/*': No such file or directory unable to copy all files from mock folder because file or directory not exists on relevant path. Follow this answer to receive notifications.

Where is the proc filesystem stored?

The Linux /proc File System is a virtual filesystem that exists in RAM (i.e., it is not stored on the hard drive). That means that it exists only when the computer is turned on and running.

How often is proc PID stat updated?

In test environment, this file refreshed 1 ~ 2 sec, so I assume this file often updated by system at least 1 sec. But, In commercial servers environment, process run with high load (cpu and file IO), /proc/[pid]/stat file update period increasing even 20~60 sec!!

What is proc file in Android?

/proc – This is a virtual file system used to provide information about the system. For each file there is a corresponding set of functions that are called in the kernel. A device driver can install a handler for a particular file name in the /proc folder.


1 Answers

/proc is a pseudo-filesystem. It indicates a size of 0 for the file /proc/stat.

This is why copying doesn't work (cp does first look at the size of the file it has to copy), but you can easily read the information and write it back to a file.

$> cat /proc/stat > statfile

This was fixed in GNU coreutils 7.3.

like image 88
Theolodis Avatar answered Oct 07 '22 12:10

Theolodis