Understanding filesystem presence for a simple C program when executing

In our last two posts, we tried to learn writing of a very minimal simple C program and how its its execution happens in real time. In this post, we will try to understand, when the program is actually executing, i.e. when program becomes a running process, what kind of files it creates in filesystem and information exported by the kernel related to this program.

Lets write a simple program creating a loop as additional to simple program we written in previous posts,

 vim minimum-c-program.c 
main() {
        while(1);
}

Above program just creates infinite loop using while(1) inside main, so that this program keeps on executing forever and not exits soon after executing.

 $ gcc -o minimum-c-program_exe minimum-c-program.c 

Now, lets execute this binary created using above program as,

 $ ./minimum-c-program_exe 

This above program will keep on executing due to while(1), so now we need to open another terminal and need to check what is the process id assigned to this running program,

 $ ps -ax | grep minimum-c-program_exe
13027 pts/1    R+     0:05 ./minimum-c-program_exe

So this program is running as process id “13027” as seen above.

Every running process in Linux has one directory created in /proc with its process Id as directory name.

cd /proc/13027

 $ ls
attr        comm             fd        map_files   net            personality  setgroups  syscall
autogroup   coredump_filter  fdinfo    maps        ns             projid_map   smaps      task
auxv        cpuset           gid_map   mem         oom_adj        root         stack      timers
cgroup      cwd              io        mountinfo   oom_score      sched        stat       uid_map
clear_refs  environ          limits    mounts      oom_score_adj  schedstat    statm      wchan
cmdline     exe              loginuid  mountstats  pagemap        sessionid    status

Now if we check the “cmdline” file contents using cat, it will show how we had started this program.

 $ cat cmdline 
./minimum-c-program_exe 
Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply