Widely Vision

  • Real History of Israel

  • Poll

  • Get a PDF version

  • Follow This Blog

  • October 2017
    S M T W T F S
    « Mar    
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  

Archive for the ‘linux’ Category

Memory overcommit in Linux Memory management

Posted by aboelnour on September 16, 2011


What’s happening when a program request more memory on machine running OS with Linux kernel?

last days I get the answer.When you use malloc() to reserve more memory it has a return value.

from malloc() man page:

 malloc() returns a pointer to the allocated memory,

which is suitably aligned for any kind of variable.

On error,these functions return NULL.

NULL is returned when error occur in allocating the block of memory that include the system didn’t find enough space to allocate it to your program.

With Linux kernel by default malloc() always succeed. Linux deal with memory allocating like airline when sell tickets, it sells more tickets than they have actual seats, in the hopes that some of the passengers don’t show up.

Linux assuming you’re not “really” going to use all of the memory you just asked for. The malloc()‘s will continue to succeed, but not until you actually try to use the memory you allocated will the kernel “really” allocate it.

As an example, consider the fork() system call, which copies all of a process’s memory for the new child process. In fact, all it does is to mark the memory as “copy on write” and allow parent and child to share it. Should either change a page shared in this way, a true copy is made.

This called “Memory overcommit

“Memory overcommit is a Linux kernel feature that lets applications allocate more memory than is actually available. The idea behind this feature is that some applications allocate large amounts of memory “just in case”, but never actually use it. Thus, memory overcommit allows you to run more applications than actually fit in your memory, provided the applications don’t actually use the memory they’ve allocated. If they do, then the kernel terminates the application.”

To make things more clearer you can try this examples:

[WARNING] “This examples may cause harm to your computer if you don’t know how to rescue your system from process eating all of your memory don’t run it”

example 1: allocate memory without using

#include <stdio.h>
#include <stdlib.h>

int main (void) {
int n = 0;

while (1) {
if (malloc(1<<20) == NULL) {
printf(“malloc failure after %d MiB\n”, n);
return 0;
}
printf (“got %d MiB\n”, ++n);
}
}

example 2: allocate memory and actually touch it all.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
int n = 0;
char *p;

while (1) {
if ((p = malloc(1<<20)) == NULL) {
printf(“malloc failure after %d MiB\n”, n);
return 0;
}
memset (p, 0, (1<<20));
printf (“got %d MiB\n”, ++n);
}
}

example 3: first allocate, and use later.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N       10000

int main (void) {
int i, n = 0;
char *pp[N];

for (n = 0; n < N; n++) {
pp[n] = malloc(1<<20);
if (pp[n] == NULL)
break;
}
printf(“malloc failure after %d MiB\n”, n);

for (i = 0; i < n; i++) {
memset (pp[i], 0, (1<<20));
printf(“%d\n”, i+1);
}

return 0;
}

In example 1 you will notice that the program will print smoothly without harming your computer and it will stop when the program reach your ram boundary and exit successfully.

Explanation:

the program doesn’t use the memory he allocate it,so Linux doesn’t care to allocate it.

——–

In example 2 you will notice that program make the computer print slower.

Explanation:

the program after allocating each block he use it, so Linux allocate it after each memset() so it takes alot of time to reach ram boundary.

——–

In example 3 you will notice that the program print quickly and when he reach the ram boundary the computer will be slower.

Explanation:

the program will allocate the whole block of memory after malloc() finishes.program will print smoothly and after printing “malloc failure after MiB” the computer will become slower.

———————————

So what’s happing when process request for more memory and there no free memory to allocate it for the process?

Linux start a job called OOM killer “Out Of Memory” killer, Its job is to kill processes and free up some memory. Getting it to kill the right processes has been an ongoing challenge, however. One person’s useless memory hog is another’s crucial application.

This challenge is like:

“An aircraft company discovered that it was cheaper to fly its planes with less fuel on board. The planes would be lighter and use less fuel and money was saved. On rare occasions however the amount of fuel was insufficient, and the plane would crash. This problem was solved by the engineers of the company by the development of a special OOF (out-of-fuel) mechanism. In emergency cases a passenger was selected and thrown out of the plane. (When necessary, the procedure was repeated.) A large body of theory was developed and many publications were devoted to the problem of properly selecting the victim to be ejected. Should the victim be chosen at random? Or should one choose the heaviest person? Or the oldest? Should passengers pay in order not to be ejected, so that the victim would be the poorest on board? And if for example the heaviest person was chosen, should there be a special exception in case that was the pilot? Should first class passengers be exempted? Now that the OOF mechanism existed, it would be activated every now and then, and eject passengers even when there was no fuel shortage. The engineers are still studying precisely how this malfunction is caused.”

———–

Refrences:

http://lwn.net/Articles/104179/

http://opsmonkey.blogspot.com/2007/01/linux-memory-overcommit.html

http://www.win.tue.nl/~aeb/linux/lk/lk-9.html

http://www.gnu.org/software/gnusound/Documentation/ar01s05.html#id2572059

———–

I hope that any one find this post useful. any questions or feedbacks are welcomed.

regards,

aboelnour

Advertisements

Posted in C/C++, linux | Leave a Comment »

How a linux geek play with google guitar

Posted by aboelnour on June 9, 2011


Hi,

I guess you saw the google guitar on google.com

I faced a problem when I try to run the clips which my friends posted on facebook and buzz Because I press keys very slow and I can’t know which clip is this due to the Guitar doesn’t response to the numbers keys which located at the side numpad it only response to the numbers key on the top which I’m not familiar with it. so i wrote this C code to handle my problem and simulate keys pressing :

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <stdio.h>
#include <unistd.h>

int
main (int argc, char *argv[])
{
sleep(2);
Display *display;
unsigned int keycode;
display = XOpenDisplay(NULL);
char* temp = argv[1];

while(*temp != 0)
{
//    printf(“TEST # %d\n”,*temp); //to debug
keycode = XKeysymToKeycode(display, *temp); // get key code

XTestFakeKeyEvent(display, keycode, True, 0); // Generate regular key press
XTestFakeKeyEvent(display, keycode, False, 0);// Generate regular key release
XFlush(display);
temp++;
usleep(500000);
}
return 0;
}

I used the X11 library  to simulate key press and release.

to compile you should link with X11 and Xtst

gcc test.c -lX11 -lXtst

Executable file for linux platforms here.

you should run the code in this format

./test 12-34-454-123

more samples:

‎48-89-89-87-78-78-76-67-6765-4343

2-45-2-45-2-56-12-678-12-678

8786-8786-8786-7-7675-7675-7675-6-6564-6564-6564-5

144-144-345-64-678-654-6564-354

987-67-6-56-7-676-987-7789-7-00-989-878-987

585-3453-12345-975-597-785-23465-321

Now open google.com and your terminal and run the program inside you terminal and quickly make your browser on the top.

best wishes.

Posted in C/C++, linux | Leave a Comment »

Deeper in Libraries With GCC(4)

Posted by aboelnour on February 19, 2010


peace be upon you:

After making your library you will need to edit it or just read it’s Contents.
—————————-
Listing Symbols Names in Object Files:
The nm utility can be used to list all the symbols defined in (or referenced from) an object file, a static archive library, or a shared library. If no file is named on the command line, the file name a.out is assumed

run:

nm libc.a

look the man page for more info about options.
——————-
Removing Unused Information from Object Files:
The strip utility removes the debugging symbol table information from the object file or files named on the command line. The object file can be a static library, a shared library, or a .o file produced by the compiler. Depending on how much debugging information has been included in the file, stripping can dramatically reduce the size of the file.

run:

strip main.o libglom.a

this command will strip all debugging information
from the object file main.o and all the object files in the library libglom.a

The strip utility replaces the existing file with the stripped version, so if you want to be able to restore the original unstripped versions, you will need to save the files before stripping them or use the -o option to produce the output in a different file.
——————-
Listing Shared Library Dependencies:
The ldd utility reads through the object files in the binary executable or shared library named on the command line and lists all the shared library dependencies.

For example,the following command lists the shared libraries used by the bash shell program on a Linux system:

ldd /bin/bash

output:

linux-gate.so.1 =>  (0xb8022000)
libncurses.so.5 => /lib/libncurses.so.5 (0xb7fc3000)
libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7fbf000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e60000)
/lib/ld-linux.so.2 (0xb8008000)

this is the shared objects which the bash use it the ldd utility determine the library and it’s path on your hard.
—————————-

that’s the end of our series.I hope that any one find it helpful. Any question is welcomed.

regards,

aboelnour.

Posted in C/C++, linux | 2 Comments »

Deeper in Libraries With GCC(3)

Posted by aboelnour on February 19, 2010


peace be upon you:
In last Post we talk about how to deal with the dynamic library in this post isA we will talk about how to make your Dynamic library and how to configure it.
———————————————
Example:
We will make a library contain 2 functions one print “Hello” the second print a given String.
here is the Files:

/*sayhello.c*/
#include <stdio.h>

void sayhello()
{
printf(“Hello from a loaded function\n”);
}

/*saysomething.c*/
#include <stdio.h>
void saysomething(char *string)
{
printf(“%s\n”,string);
}

/*say.c*/
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc,char *argv[])
{
void *handle;
char *error;
void (*sayhello)(void);
void (*saysomething)(char *);
handle = dlopen(“libsay.so”,RTLD_LAZY);
if(error = dlerror()) {
printf(“%s\n”,error);
exit(1);
}
sayhello = dlsym(handle,”sayhello”);
if(error = dlerror()) {
printf(“%s\n”,error);
exit(1);
}
saysomething = dlsym(handle,”saysomething”);
if(error = dlerror()) {
printf(“%s\n”,error);
exit(1);
}
sayhello();
saysomething(“This is something”);
dlclose(handle);
}

then run:

gcc -fpic -shared sayhello.c asaysomething.c -o libsay.so

to make your shaerd object.

then to build your prog:

gcc -ldl say.c

to run:

./a.out

this message will appear:

libsay.so: cannot open shared object file: No such file or directory

that’s mean that the Linker couldn’t find your library that’s lead to we have to know how the linker search for the libraries.
the Dynamic Linker search by default in the directories /lib    /usr/lib.
and there is an environment variable called LD_LIBRARY_PATH.
this var contain the paths which the DL will search throw it so you need to edit it with the path of the directory which contains the library.
run:

export LD_LIBRARY_PATH=”/home/fakeroot/Desktop”

the run

./a.out

you will notice that the program works.

Another important environment variable called LD_PRELOAD this var is very useful because the linker load any library locate in this var before loading any library he should load.
With this var you can override some libraries in Linux like the libraries which is Responsible of detecting the user’s id 😀

————————-

Configuring the Search for Shared Libraries:
In this part we will talk about the ldconfig utility.
The ldconfig utility performs two fundamental functions dealing with shared libraries.
First, it creates links so that references to shared libraries are always to the latest version.
Second, it stores a complete list of the available shared libraries in the file /etc/ld.so.cache.

The ldconfig utility reads the file /etc/ld.so.conf, which is a list of
directories containing shared libraries, and uses these directory names (along with the directories /lib and /usr/lib) to locate the libraries to be linked and listed in /etc/ld.so.cache.
The following command will create all the new links necessary and generate
a new version of the file /etc/ld.so.cache (you must be root):

ldconfig -v

for more information:

man ldconfig

—————————-

In next post I will finish this series isA.

regards,

aboelnour

Posted in C/C++, linux | Leave a Comment »

Deeper in Libraries With GCC(2)

Posted by aboelnour on February 9, 2010


peace be upon you:

It is a long time since i wrote here. sorry for the few who following this blog 😀

In last post we talked about libraries and how to make a static one.

today we will continue our series in the libraries.

———————————–
Example on static libraries:

libhello.c File:

#include <stdio.h>

void hello(void) {
printf(“Hello, library world.\n”);
}

libhello.h File:

void hello(void);

demo_use.c File:

#include “libhello.h”

int main(void) {
hello();
return 0;
}

then run in terminal:

gcc -Wall -g -c -o libhello-static.o libhello.c

to obtain the object file of the source file which we want to make it a library.

ar rcs libhello-static.a libhello-static.o

to create the library.

gcc -Wall -g -c demo_use.c -o demo_use.o

to obtain the object file of the source which use the libarary.

gcc -g -o demo_use_static demo_use.o -L. -lhello-static

to compile the object file with the libarary.

./demo_use_static

to run the program.

and here you can make your own static lib and deal with it.

——————————–

Dynamic Linking:

In the static Linking we locating the library in the linking time this operation done by the linker which called by the compiler.

But there is another technique in linking calling dynamic linking which locating the libraries in the Run Time.

Once a program has been linked to use shared libraries, it must be able to find the shared library when it runs. The libraries are located by name, not by directory.

The loading of the libraries in the run time done by the Operating System.

to make the explanation easy we will start with an Example:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dlfcn.h>
  4. int main(int argc, char **argv) {
  5. void *handle;
  6. double (*cosine)(double);
  7. char *error;
  8. handle = dlopen (“/lib/libm.so.6”, RTLD_LAZY);
  9. if(error = dlerror()) {
  10. printf(“%s\n”,error);
  11. exit(1);
  12. }
  13. cosine = dlsym(handle, “cos”);
  14. if(error = dlerror()) {
  15. printf(“%s\n”,error);
  16. exit(1);
  17. }
  18. printf (“%f\n”, (*cosine)(2.0));
  19. dlclose(handle);
  20. }

In this Example we will calculate the cosine of (2.0) using a library called “libm.so.6” located in /lib.

Explanation of the code:

1-2: We include the standard library and standard I/O library.

3: We include the header dlfcn.h which contain the functions: dlopen(),dlsym(),dlerror(),dlclose() we will explain them later.

5-7: Declaration of void-char pointer 6: Declaration of Function pointer.

8: here we call the dlopen() function which loads the shared library into memory (if it’s not already there) and returns a handle that can be used to address it the first parameter is the library and the second one is a flag  passing to the Operating System.

9-12 ,14-17 : Handle Errors the dlerror() function returns a descriptive string describing the error that occurred on the most recent call to any one of the other functions(dlopen(), dlsym(), dlclose()). The dlerror() function returns NULL if no error occurred.

13: Here we call the dlsym() which return the addresses of the functions the first parameter is the address of the library and the second parameter is the name of the function which you want to use it from the library .

18: here we get the value of the cosine(2) with the function which we get from the library which loaded in the memory.

19: here we call the dlclose() Functions that detaches the current program from the shared library. If no other programs are attached to it,the dynamic library is unloaded from memory.

Note: The flag used as the second argument on the call to dlopen() can be RTLD_NOW,which causes all the functions in the library to be loaded into memory and become immediately available. The other option is to specify RTLD_LAZY, which will delay the actual loading of each function until it is referenced on a call to dlsym(). Either of these flags can be OR‘ed with RTLD_GLOBAL, which allows any external references in this library to be resolved by calling functions found in other (also loaded) dynamic libraries.

to build your program run:

gcc test.c -ldl -o testth

then to run:

./test

the option -ldl indicate that this program will need the Dynamic linker of the OS.

———————————–

so here we are we will cover in the next part how to make your own dynamic library and how to deal with the Environment variables and how to configure your library.

regards,

aboelnour 🙂

Posted in C/C++, linux | 3 Comments »

Deeper in Libraries With GCC(1)

Posted by aboelnour on November 29, 2009


peace upon you:
when you open a new file to write some C code you must to include some headers and libraries.
So in this article we will talk about this headers and libraries and how to deal with it throw the gcc with the terminal.
————————————————————-
Any Compiler consist of many parts one of this part is the linker.

To know what we mean with the linker we must know some basics about compiling stages.
when you call the compiler to compile some code it first turn your code to a thing called object file.
this object file contains the machine code which will run your machine.

you can get the object file by this

gcc -c main.c

you will notice that a file called main.o appeared.
In this stage the linker start working it takes this object file and link it to the libraries and headers which the program use it and produce an execution file ready to execute.
you can to call the linker of gcc to deal with the object file like this

gcc main.o -o a.out
This will produce an execution version of your program called a.out you can run it:

./a.out

you can merge more than one source file in one object file:

gcc main.o inlet.o outlet.o genspru.o -o spinout

Object Files in a Static Library:

Object files can be stored in a static library and linked from there in much the same way as they can be linked from separate files, except the linker will automatically search through the contents of the library and include only the object files that are necessary.

If nothing in an object file is referenced from inside the program, it is not included as part
of the executable.

A static library containing object files is known as an archive file, and it’s constructed
and maintained by a utility named ar. The name of an archive file normally has a
prefix of lib and a suffix of .a.the following sequence of commands compiles three
object files and stores a copy of them in a library named libspin.a. Then the linker
uses the object file named main.o and the contents of the library to construct an executable program named spinner:

$ gcc -c inlet.c outlet.c genspru.c
$ ar -r libspin.a inlet.o outlet.o genspru.o
$ gcc main.c libspin.a -o spinner

The first gcc command produces the three object files that are inserted into the static
library by the ar command. The last command compiles main.c into main.o and then
invokes the linker, which reads the contents of libspin.a to try to resolve external
function and data references made in main.o. A module stored in libspin.a is
included as part of the final executable file only if it contains a function or data item
referred to from a module that has already been included as part of the executable.

Inside the static library, along with the object modules, is an index that lists all the
names of global data and functions defined in the library. The linker uses this index to
determine which modules to include and which ones to ignore. Normally, this index is
created by the ar utility when the library is created or updated, but options are available
on the ar utility that can suppress the creation of the index. This can be useful when
maintaining a large library—multiple changes can be made without bothering to update
the index until the modifications have been completed. To create an index or to update
an existing index, you can use the ranlib utility. For example, the following pair of
commands use the -q option of ar to quickly append files to an existing archive without
updating the index, and then it uses ranlib to update the index to reflect the current
status of the archive:

$ ar -q lib.a file1.o file2.o file3.o
$ ranlib lib.a

The order of appearance of the modules in the library can make a difference. If the
same symbol is defined in more than one module, then the linker will find and include
the first module if it is looking for that symbol.
————————————————————-
In this article we talked about the static libraries.
In comming articles isa we will talk about the dynamic libraries and how to deal with it.
any feedback is welcomed
best wishes,
aboelnour.

Posted in C/C++, linux | Leave a Comment »

HOWTO GCC & GDB

Posted by aboelnour on October 15, 2009


gcc_sm

peace upon you:

As they say Linux for developers.

so it’s an article to help any Beginner to Began developing C/C++ Under Linux.

————————-

Your distro comes often with the GCC (GNU Compiler Collection) and with the GDB (GNU Debugger) This tools is very important to any C/C++ developer turned to Linux.

1) The GCC:

First: Make sure the build-essential package is installed:

sudo apt-get install build-essential

Second: Compiling a Simple Program :

take this Hello World program and Copy and Paste it in a File Called main.c

#include<stdio.h>

int main()

{

printf(“Hello World!\n”);

return 0;

}

to Compile and make the output named test

gcc main.c -o test

If it work without any Compilation errors you can run your program:

./test

NOTES:

1) if you don’t Determine the output file the compiler will name him “a.out” and if there is another file have the same name the Compiler will replace it.

2) you can give the Compiler the option -Wall to make the Compiler print any error message he would say.

2) The GDB:

When you successfully write your code you will need to debugging it so there is the GDB. to debug any code

gdb ./test

where test is the binary code which the Compiler produce it.

this command will open the gdb prompt to take your Commands:

run

to run your code and stop at any break point you put it.

list

show you the source code of your program with numbered lines to put break points.

break LINE_NUMBER

put a break point in the line number which you determine.

continue

to continue running your code after stopping at break point.

step

next

move one step in your program.

delete

delete all the break point in your program.

clear LINE_NUMBER

clear the break point at this line.

until LINE_NUMBER

run  your program until a the Determined line.

display EXPRESSION

to display the value of this expression.

info

this command give you more information about the gdb commands

quit

to quit from the gdb

IMP.NOTES

1) To use the debugger you should run the option -g while compelling

your code with gcc.

2) when you deal with C++ you can follow the same steps but instant using the gcc you will use the g++.

so that’s it any feedback is welcomed

best wishes ,

aboelnour.

archer

Posted in C/C++, linux | Leave a Comment »

“INIT” parent of your processes

Posted by aboelnour on September 27, 2009


init0

Peace be upon you:
Today we will talk about a process called Init .

Init  is  the  parent of all processes on the system, it is executed by
the kernel and is responsible for starting all other processes. it is the  parent  of all processes whose natural parents have died and it is responsible for reaping those when they die.

this process lunched by the kernel when the boot operation end and here is the steps:

1.  BIOS: The Basic Input/Output System is the lowest level interface between the computer and peripherals.
The BIOS performs integrity checks on memory and seeks instructions on the Master Boot Record (MBR) on the floppy drive or hard drive.

2. The MBR points to the boot loader (GRUB or LILO: Linux boot loader).

3. Boot loader (GRUB or LILO) will then ask for the OS label which will identify which kernel to run and where it is located (hard drive and partition specified). The installation process requires to creation/identification of partitions and where to install the OS. GRUB/LILO are also configured during this process. The boot loader then loads the Linux operating system.

4. The first thing the kernel does is to execute init program. Init is the root/parent of all processes executing on Linux.
5. Based on the appropriate run-level, scripts are executed to start various processes to run the system and make it functional.

After the kernel is booted and initialized, the kernel starts the first user-space application. This is the first program invoked that is compiled with the standard C library.

The init process is the last step in the boot procedure and identified by process id “1”. Init is responsible for starting system processes as defined in the /etc/inittab file. Upon shutdown, init controls the sequence and processes for shutdown. The init process is never shut down. It is a user process and not a kernel system process although it does run as root.

NOTE: In some modern distro’s there isn’t a /etc/inittab file like Ubuntu they used Upstart.
and there is /etc/event.d which configure init.

Linux init Run Levels:
~~~~~~~~~~~~~~~~~
there is a run levels that you can run your system on it.every runlevel has it’s Specifications:
runlevel 0 : System halt-> be safely powered down.
runlevel 1 : Single user-> login as root user.
runlevel 2 : Multiple users, no network.
runlevel 3 : Multiple users, command line -> the standard runlevel for most Linux-based server hardware.
runlevel 4 : User-definable
runlevel 5 : Multiple users, GUI -> the standard runlevel for most Linux-based desktop systems.
runlevel 6 : Reboot; used when restarting the system.

Every distro may has it’s own runlevel but it stay 7 runlevels from 0 to 6 for more detail you canCheck this .

By default Linux boots either to runlevel 3 or to runlevel 5. to know which runlevel you are in type:

runlevel

you can run your system on any runlevel by typping

init#

when # is the number of the runlevel you want.

Init in the / directory:
~~~~~~~~~~~~~~~~~
/etc/init.d: this directory contains every script that Init use it.

/etc/rc#.d : this directories contain the scripts which will be runed when you run your system in a specify runlevel

you can see the contents of this directories is a links which links to scripts in/etc/init.d.

/etc/rc#.d/README: this file contains an info about the parent directory.

/etc/init.d/README: this file contains an info about The Init process and the Scripts which located in /etc/init.d.

——

so it was a simple view over the Init process and how it works.

any feedback is welcomed.

Best wishes,

aboelnour

fig1

Posted in linux | Leave a Comment »

Freedom Day @ Alexandria University

Posted by aboelnour on September 6, 2009


n124455764728_397

Peace be upon you:

In last few Days a Big Event started at our university “SFD” Software Freedom Day.

It is a big and really good event it started 1st September and Last for 2 weeks.

The event contain presentations and study groups and a lot of nice things

talked about open source field .

Before the event started the Organizers announced that they need a speakers

to talk in Specific fields.

Among this fields there were”Linux” field.

so I become very happy so I started to build my presentation about Linux.

Saturday 6 September 11 A.M. was my time to give the presentation and it’s

1 hour long.

so I went and give the presentation it was really a nice day and the Attendance and my friends have a good Impression.

thanks allah.

Many thanks to the Organizers of this event.

Especially my friends Khaled , Emara and every one help me many thanks.

to Download the presentation here

Best wishes,

aboelnour

Posted in linux | 4 Comments »

/proc Tuning Your System Performance

Posted by aboelnour on August 24, 2009


19571-radio-tuning

Peace be upon you:

we will talk about the /proc directory in your file system in linux,so enjoy 🙂

I know the article is long but it really Deserves.

======

The /proc directory is a strange beast. It doesn’t really exist, yet you can explore it. Its zero-length files are neither binary nor text, yet you can examine and display them. This special directory holds all the details about your Linux system, including its kernel, processes, and configuration parameters. By studying the /proc directory, you can learn how Linux commands work, and you can even do some administrative tasks.

Under Linux, everything is managed as a file; even devices are accessed as files (in the /dev directory). Although you might think that “normal” files are either text or binary (or possibly device or pipe files), the /proc directory contains a stranger type: virtual files. These files are listed, but don’t actually exist on disk; the operating system creates them on the fly if you try to read them.

The /proc directory itself is created every time you boot your box. You need to work as root to be able to examine the whole directory; some of the files (such as the process-related ones) are owned by the user who launched it. Although almost all the files are read-only, a few writable ones (notably in /proc/sys) allow you to change kernel parameters. (Of course, you must be careful if you do this.)

/proc directory organization :
1

The numbered directories (more on them later) correspond to each running process.Some virtual files provide hardware information, such as /proc/cpuinfo, /proc/meminfo, and /proc/interrupts. Others give file-related info, such as /proc/filesystems or /proc/partitions. The files under /proc/sys are related to kernel configuration parameters.

The cat /proc/meminfo command might bring up something like this:
2

If you try the top or free commands, you might recognize some of these numbers. In fact, several well-known utilities access the /proc directory to get their information. For example, if you want to know what kernel you’re running, you might try uname -srv, or go to the source and type cat /proc/version.

Some other interesting files include:

/proc/apm: Provides information on Advanced Power Management, if it’s installed.

/proc/acpi: A similar directory that offers plenty of data on the more modern Advanced Configuration and Power Interface. For example, to see if your laptop is connected to the AC power, you can use cat /proc/acpi/ac_adapter/AC/state to get either “on line” or “off line.”

/proc/cmdline: Shows the parameters that were passed to the kernel at boot time. In my case, it contains root=/dev/sda10 ro quiet
which tells me which partition is the root of the filesystem,and more.

/proc/cpuinfo: Provides data on the processor of your box. For example, in my laptop, cat /proc/cpuinfo gets me a listing that starts with:

/proc/filesystems: Shows which filesystem types are supported by your kernel. A portion of this file might look like this:
3

The first column shows whether the filesystem is mounted on a block device. In my case, I have partitions configured ext3 mounted.

/proc/mounts: Shows all the mounts used by your machine (its output looks much like /etc/mtab). Similarly, /proc/partitions and /proc/swaps show all partitions and swap space.

What’s in a process?

As I said, the numerical named directories represent all running processes. When a process ends, its /proc directory disappears automatically. If you check any of these directories while they exist, you will find plenty of files, such as:

cmdline: Contains the command that started the process, with all its parameters.

cwd: A symlink to the current working directory (CWD) for the process; exe links to the process executable, and root links to its root directory.

environ: Shows all environment variables for the process.

fd: Contains all file descriptors for a process, showing which files or devices it is using.

Conclusion

The /proc special directory provides full detailed information about the inner workings of Linux and lets you tuning many of its configuration. If you spend some time learning all the possibilities of this directory, you’ll be able to get a more perfect Linux box. And isn’t that something we all want?

======

it was a simple look on the /proc directory 😀 any feedback is welcomed.

regards,

aboelnour.

Posted in linux | Leave a Comment »

 
%d bloggers like this: