Search

Getting information regarding cpu frequency from command line

If you want to figure out what is he the frequency of the your processor/processors and various other information related to the frequency of your system's processor, from the command line then the command cpufreq-info will come in handy.

cpufreq-info displays the frequency at which the various processors in the system are running at and also what is the possible range of frequencies for the processor.

For eg :



The other set of commands related to cpu frequency are

cpufreq-set : Which allows to modify the frequency settings for eg: the maximum frequency or the minimum frequency etc.

cpufreq-selector: This command allows you to set the frequency to one of the available values for the processor.

Have fun playing with the frequencies :-)

Reversing the contents of a file

The command cat is used to view the contents of a file with out opening it, the contents are displayes from top to bottom in the usual way. In case you want to view the contents in the reverse that is from last line to first here are a few ways to do it.

Let us say we have a file named test with the following contents

test :



1. tac

The command tac takes a filename as input and displays the contetns in the bottom up fashion.



2. Using awk :

The following awk script takes a file name as input and displays the same in the reverse order .



3. Using sed :

The following sed script takes a file name as input and reveres the contents, displaying it from bottomt to top.



qemu does not launch : vnc server started

Problem : When qemu is launched from the command line the GUI of the virtual machine does not launch but only a message

VNC server started at 127.0.0.1:5869

appears on the terminal.

Solution : The problem most probably is because the sdl libraries have not been installed. Hence install the same using the package manager of your respective distro. For eg if you are running debian(ubuntu) based systems then run the following commands in the terminal

sudo apt-get install libsdl1.2-dev

Now try running the qemu and GUI should appear.

Couting the number of blank lines in a file

Counting the number of blank lines in a file.

Here are a few ways to count the number of blank lines in a file using shell scripting.

Assume we have a file named test which has the following contents :



Note that the above file has 10 blank lines includin the one at the end. Let see how we can count the same using commands or scripts

1. Using grep :



When -c option is used with grep, it counts the number of occurrences of the search string and outputs the same. 2. Using awk:



Awk reads one line of the file at a time, hence we compare each line with the blank line i.e. ^$ and increment a count whenever there is match and print the final count before exiting.

3. Using shell scripting :



Save the script as line_count.sh . Give it execute permission using

chmod 777 line_count.sh

And then execute it



The above script uses a while loop to pass through the file, one line at a time and on reading each line an "if" statement is used to comapre the line with the blank line. Note the use of double [ with the if statement. A single [ will not be able to interpret the regular expressions in an if statement. When the comparision is a success a count is incremented and the same is printed at the end.

Creating a folder under /proc and creating a entry under the folder.

In the posts "creating proc read entry",creating read/write proc entry we saw how to create read only proc entries and read write proc entries.
In both the examples we created the proc entry under /proc and not in any sub diretory in /proc.

In this post we will see how to create a folder under /proc and then create an entry in it.
We will use the same example as in the post create read write proc entry and extend it further.
To create folder under /proc we use the function
proc_mkdir(,struct proc_dir_entry *parent);
Arguments :
Folder name: The name of the folder that will be created under /proc.
Parent: In case the folder needs to be created in a sub folder under /proc a pointer to the same is passed else it can be left as NULL.

proc_mkdir in turn returns a pointer to the folder which will be of the kind "struct proc_dir_entry"
If we want to create folder by the name "newparent" then the function call will look as below
struct proc_dir_entry *proc_parent
proc_parent = proc_mkdir("newparent",NULL)

The call to create_proc_entry will become
proc_write_entry = create_proc_entry("hello",0666,proc_parent);

Thus "hello" is the name of the entry, by passing 0666 we are giving read and write permission to every one,


This returns a pointer to a structure of the kind proc_dir_entry. You can look into the contents of the structure in the file linux/proc_fs.h

The structure has various fields and we need to initialize only the ones we feel is relevant for our module.
In our example we want to implement read and write of proc hence we will have to initialize the read_proc and write_proc in the structure to point to the respective read and write functions.
Thus the initializations will be
proc_write_entry->read_proc = read_proc ;
proc_write_entry->write_proc = write_proc;

Thus we have to implement a function by the name read_proc that will be called when the proc entry is read and a function by the name write_proc that will be called when the proc entry is written to.
We will use an array by the name proc_data to store the data that will written into the proc entry. The read function thus will output the contents of the proc_data array whenever the proc entry is read as shown below. int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data ) { int len=0; len = sprintf(buf,"\n %s\n ",proc_data); return len; } The write function will have to written to accept data from the user and put it into the array proc_data as shown below.
int write_proc(struct file *file,const char *buf,int count,void *data ) { if(count > MAX_PROC_SIZE) count = MAX_PROC_SIZE; if(copy_from_user(proc_data, buf, count)) return -EFAULT; return count; }
Before writing into the proc entry we make sure that amount of write does not exceed the size of the array. In case the amount of write from user space is more than the size of the array then we truncate the data to the maximum size array

The full code of the module is as below.
proc_dir.c :

Makefile:

Now run the following commands as root
$ make

$ insmod proc_dir.ko

After inserting successfully, write some data into the proc entry as shown below.
$ echo "Hello World" > /proc/newparent/hello

You can see that the new entry in inside the folder "newparent" and not under /proc Now read the proc entry and see if the same data written above is shown or not.
$cat /proc/newparent/hello
Hello world

What ever you written into the proc etnry, you should be able to read it back using the cat command.

Reference Books



Removing blank lines from a file

Removing blank lines from a file is common requirement. Here are few simple ways we can do it.

1. Using cat and grep



The symbol ^ searches for a string that is followed from the beginning of the line. The symbol $ signifies the end of the line. Thus the combination ^$ means from begingin till the end there is nothing in the line, which would be the case in a blank line.

The same logic applies to all the examples below.

2. Using sed



3. Modifying the fine in place using sed

Please note that this will change the orginal file itself.



4. Using awk

Disaplying specific number of lines at a time from a file.

Disaplying specific number of lines from a file.

The command cat displays the complete contents of the file at a time, but in case you want to view a specific number of lines at a time

Here are a few methods to do it.

1. more :

This is probably the simplest way, the command more by default displays one page of contents at a time, to restrict the number of lines we need to pass the number as an option.

If we have a file by the name "test" with the contents



If we want to view one line at a time then, we can run more as follows



Each time you press enter the next line gets displayed, the blank lines are also treated as a line. You can change the "-1" to any number of your choice.

2. Here is a bash script that tries to achive the same as shown above.

The only way this script is better than more is that it is a little more interactive, in all other respects more is a better option, this is just for fun.



Let us save script lines.sh .

Give execute permission using



The script takes one filename as input and displays the count of number of lines present in it, before asking the user to enter the number of lines he/she wants to view at a time.

After displaying the number of lines user requested for it will prompt if the user wants to continue on reciving a confirmation it will display the next set of lines and so on till the end of the file.

If the user wants to stop in between he/she can say no when the scripts prompts for confirmation, on which the script will give the number of lines displayed and exit.

Here is a trial run on the same file used above.

Different commands to shutdown or reboot

Here are list of commands that can be used to shutdown or reboot a system.
Note: All the commands require superuser privileges, hence use sudo for all of them.

1. Halt

The command halt will shutdown the system immediately.

2. Reboot

The command reboots the system

3. shutdown

The command can be used to shutdown as well as reboot the system by passing the correct options as given below

To shutdown immediately


To shutdown after a time interval



This will shutdown the system after 15 minutes

To reboot :



To reboot after a time interval



This will reboot the system after 15 seconds.

4. init

To reboot using init run



This will reboot the system immediately.

5. poweroff

This will poweroff the system immediately just like the halt command

Adding line numbers to a file

There are multiple ways of adding line numbers to a file, here are a few of them. Please note that none of the methods below modify the original file and to save the output you will have to redirect the output to the required file.

1. nl :

The command nl adds line numbers to the filename passed to it.

nl filename for eg: if we have a file by the name test with the contents



Run the command nl on it as follows



You can add a symbol after the numbers using the options -s



You can explore a few more options of the command in the man pages

2. Using "cat".
cat with the option -n also outputs lines with its line numbers



For eg



3. Using awk

The script below will add line numbers to the test file using an awk script.



4. Using sed

The script below will add line numbers to the test file using a sed script. The "=" command prints the line numbers of line being processed by sed. But only "=" inserts a new line after the line number thus to remove the line number the line and the line number are parsed again through sed using a pipe and the new line character is removed.



5. Using a script

Line number can also be added using a script as shown below

Save the script as num_lines.sh and execute it by passing the file name on the command line as shown below



All the above commands and scripts add line numbers to blank lines too, which might not be required at times. Here are ways of adding line numbers to a file, but not to blank lines

6. Using awk to ignore blank lines

7. Using a script to ignore blank lines

Save the script as num_lines.sh and execute it by passing the file name on the command line as shown below



Adding a system call to linux

Adding a system call to linux 3.1.5
Here are the steps to be followed to add a system call in linux ( Thest steps have been tested with linux 3.1.5 )

Note: The steps are valid only till kernel version 3.3

1. Download the source of the kernel version to which you want to add the system call  e.g. linux-3.1.5.tar.bz2 or linux-3.1.5.tar.gz 2. Untar the source using



This should create a folder linux-3.1.5 in the present working directory 3. cd linux-3.1.5/arch/x86/kernel (This is to add a system call for x86 architecture for other architectures replace x86 by the corresponding folder name)

4. Open the file syscall_table_32.S  
  (Using vi or gedit or any suitable eidtor )
  This acts file as a table of all the system calls present in the kernel . 
To identify the number of a system call easily after every 5 system call entries the number of the call is written as a comment next to it.


5. In the 3.1.5 version there are 347 system calls by default numbering from 0 to 346. To add your system call move to the end of the file and add an entry at the end with the syntax same as previous lines. i.e. to add a system call by the name hello add the line.

Note in this case the number for this system call will be 347.

6. The next step is to inform the kernel about the system call number. This is done in the file linux-3.1.5/arch/x86/include/asm/unistd_32.h
open this file in an editor, you will notice a list of #deines of the kind#define __NR_ move to end of these #defines and add the #define for the your system call i.e.



7. Now it is the time to implement the program for the system call. The program needs to be put into the respective file int source code of the kernel, for example if it is related to schedulers put it in sched.c. All generic system calls are present in sys.c,hence we will add our implementation to sys.c . Open the file linux-3.1.5/kernel/sys.c

8. Add the code for the system call at the end of the file


SYSCALL_DEFINE0 signifies it is a system call with zero arguments. The argument passed to SYSCALL_DEFINE0 is the system call name. In this system call there is only a print statement, you can implement any other relevant kernel code.

9. Compile the kernel using the steps given in the post "Compiling a linux kernel" 10. To check the system call we need to call it from the user space. This would require the gcc to be informed about the system call and recompiling gcc too. But there is shortcut to test the system call i.e. the function syscall()

11. To test our system call create a c file test_syscall.c with the following contents


12. Now compile the code using


13. If there are no errors then executable test_syscall would have got created in the current working directory. run the same to check the output

14. ./test_syscall

15. Our system call had only a printk statement hence it would appear in the kernel logs and not on the screen. To check this run the command


16. In the output of the dmesg you should see the message   "Hello system call" i.e. the message we printed in our system call being printed in the logs Thus confirming the successful execution of our system call.