Search

Adding "Open as administrator" to the nautilus context menu

In Linux the root acts as the administrator of the system and a number of operations like installation of new software,update of kernel etc can be performed only by the root of the user having the admin privileges.

Files that are created by the root or the admin can only be modified by admin only and no user is allowed to modify the files

While working on the terminal we can use the command sudo to get admin privileges but while working with GUI we usually do not have any option to use sudo command, thus we might have to learn the commands to open files from the terminal to be able to open files as the admin or root.

The work around for this in nautilus is to install the package



Once this package is installed, we get an option "open as administrator" in right click menu as shown in the figure below.



Thus with this option we can open any file as root of the admin with out having to use the terminal.


10pt loadable: Metric (TFM) file not found

While using latex in debian 6.0 we might come across the following error.



This is because the font the latex is trying to use is not installed in the system

The workaround for the problem is to install the package



In debian we can do it using the command



After installing the package the above error should not appear while using latex.

Answers to Quiz on chapter-2 of book Linux Device Drivers

Answers to Quiz on chapter-2 of book Linux Device Drivers

1.Function passed to module_init is invoked when
Ans. Module is loaded into the kernel

2.The utility used to compile kernel modules is
Ans. make

3.A module can not call functions defined in
Ans. libc

4.A module always runs in the
Ans. Kernel space

5.current returns a pointer to
Ans. struct task_struct

6.struct task_struct is defined in
Ans. linux/sched.h

7.The option -C passed to make causes it to
Ans. Change the directory

8.modporbe not only loads the module passed to it but also
Ans. loads other modules on which the given module depends

9.command to list the currently loaded modules in the kernel is
Ans. lsmod

10.module compiled for one kernel version can not be loaded in kernel of other version.
Ans. True


Quiz on chapter-2 of book Linux Device Drivers

Quiz on chapter-2 of book ldd3

Quiz on chapter-2 of book ldd3

  1. Function passed to module_init is invoked when

  2. Module is loaded into the kernel
    Module is removed from the kernel
    Module is compiled
    Module is deleted from the kernel

  3. The utility used to compile kernel modules is

  4. made
    kernel_make
    make
    gcc

  5. A module can not call functions defined in

  6. libc
    kernel
    With in the module
    Another module

  7. A module always runs in the

  8. Kernel space
    User space
    User Space and kernel Space
    Module space

  9. current returns a pointer to

  10. struct task_struct
    struct current
    struct task
    struct kernel

  11. struct task_struct is defined in

  12. linux/sched.h
    linux/current.h
    linux/struct.h
    linux/task.h

  13. The option -C passed to make causes it to

  14. Compile the module
    Change the directory
    Create object file
    Clean the object file

  15. modporbe not only loads the module passed to it but also

  16. Loads other modules present in the current working directory
    Removes any older modules with the same name
    Loads other modules on which the given module depends
    Restarts the system

  17. command to list the currently loaded modules in the kernel is

  18. listmod
    mod
    lkmod
    lsmod

  19. module compiled for one kernel version can not be loaded into kernel of other version.

  20. True
    False
    True Sometimes
    Depends on the module



Related Posts :

Quiz on Chapter-1 of Linux Device Drivers
Linux Basics commands quiz

Using Spinlock in linux : Example

In the post we see the various functions available for spinlock in Linux. Let us look at an example and see how it can be used in Linux.

In the following example we create two threads, thread1 and thread2. We initialize a spinlock "my_lock" and both the threads try to acquire the lock .

Thread1 is made to get to the lock first. After acquiring the lock thread1 waits for one minute before releasing the lock.

During this time thread2 attempts to acquire the lock too. We have used the function spin_trylock, so if the thread2 is unable to hold the spinlock it will return immediately with a return value of non zero.

Thus we will define a spinlock using



Starting with the spinlock in unlocked state.

We will create the two threads thread_fn1 and thread_fn2 using the function kthread_create.

The first thread function thread_fn1 will hold the lock and loop for approximately on minute the code for which is



The second thread function thread_fn2 will try to hold try to hold the lock using spin_trylock.A sleep of 100ms is added at the beginning to make sure that the first thread gets to the lock first.Thus the code for the second thread function will be



If the thread_fn2 is able to get the lock we will see the message "Lock acquired" in the kernel log. If the thread_fn2 is unable to get the lock we will see the message "Unable to hold lock" in the kernel log.

The complete code will be

spinlock_example.c



Save the file as spinlock_example.c

Makefile required for compilation.



Compile and load the file



To check if second thread was able to get the lock run the command dmesg



Thus we can see that the spin_trylock faile to acquire the lock and returned the error message. Instead of spin_trylock if we use spin_lock, the second thread will wait on the lock as long as the thread does not become available.

Note: Do not try changing the code from spin_trylock to spin_lock if you have a uniprocessor system. It might freeze your system and force you to do hard reboot.


searching in pdf files using grep : pdfgrep

Just as we use grep to search for patterns in a text file we can use pdfgrep to search for strings in a pdf file. In debian based systems we can install the package from the package manager or from the terminal using



Once installed we can use this command from the terminal to search for the strings in pdf files. The syntax for use of the command is



Let us say we are searching for string "ioctl" in a pdf file name ch03.pdf (which is the third chapter from Linux device drivers book) .



It lists out all the lines that contain the string "ioctl". To make the output look more easier to read we can prefix each line with the page number on which it occurs using the option "-n".



If we want to count the number of occurrences instead of viewing the lines on which they appear we can use the option "-c"



We can also prefix each line of output with the name of the file in which the line appears, which is useful when searching in multiple files, by using the option -H.



We can see that the file names have been prefixed to each line of the output. Note that when searching in multiple files the default behavior of pdfgrep is to prefix the filenames to each line.

Spinlocks in linux

We saw in the post "Introduction to spinlocks" how spinlock can be used to achieve mutual exclusion.

Let us further look at the practical implementation of spinlocks in Linux.

Spinlocks are declared in Linux using the datatype "spinlock_t". The spin lock variable can one of the two values



We can initialize the value of the spinlock to either of the values by just eqating it to the value, i.e.



Thus the lock starts in an unlocked state. We can also assign the value to the spinlock at runtime by using the function spin_lock_init



After the initialization we can use the spin lock any where in the code. To lock the spinlock before entering the critical section we can use one of the following functions

Locking:

Locking of a spinlock is nothing but changing the value of the spinlock variable from SPIN_LOCK_UNLOCKED to SPIN_LOCK_LOCKED.

The functions available to acquire the lock are as follows.

spin_lock :



Locks the spinlock if it is not already locked. If unable to obtain the lock, it will keep spinning on the lock until the lock becomes free.

spin_trylock:



Locks the spinlock if it is not already locked. If unable to obtain the lock it exits with an error and does not spin lock.

spin_lock_irq :



Locks the spinlock if it is not already locked and also disables the interrupts after locking. If lock is not available it continues to spin on the lock until it becomes available

spin_lock_irqsave :



Locks the spinlock if it is not already locked and also disables the interrupts, storing the state of the interrupts in the variable flags which is an unsigned long datatype, after locking. If lock is not available it continues to spin on the lock until it becomes available.

Before exiting the critical section any process that locks the spinlock has to unlock the spinlock. For every lock function that we saw above there are corresponding unlock functions as listed below.

Unlock:

Unlocking a spinlock is nothing but changing the value of the lock variable from SPIN_LOCK_LOCKED to SPIN_LOCK_UNLOCKED.

The functions available for unlocking a spinlock are as follows

spin_unlock:



Unlock the spinlock, changing the value of the spinlock variable from SPIN_LOCK_UNLOCKED to SPIN_LOCKED.

spin_unlock_bh:

spin_unlock_irq:



If the spinlock was locked using spin_lock_irq then the interrupts would have been disabled. Using this unlock function makes sure that the spinlock is unlocked as well as the interrupts are enabled.

spin_unlock_irqrestore:



If the spinlock was locked using the function spin_lock_irqsave, then the state of the interrupts which was stored also needs to be restored. Thus this function not only unlocks the function but also restores the state of interrupts to the same state as they were before they were disabled which was saved in the variable flags.


Introduction to spinlocks

Spinlocks are one of the ways of achieving mutual exclusion that is, preventing multiple processes from entering the critical section simultaneously.
A spinlock has only two possible states, LOCKED and UNLOCKED.

A critical section can be surrounded by spinlock as shown below.



Let us say two processes, A and B, want to execute the critical section and both reach the step of locking the spinlock at the same time.

Let us say process A is able to get a lock on the spinlock before process B. The process A can enter the critical section.
The process B will try to lock the spinlock but will fail as process A would have already locked it. As a result the process B will go into into an infinite spin on the lock, that is, it will keep checking for the availability of the lock repeatedly until the spinlock gets unlocked by process A. Because of this behavior of continuously spinning on the lock until it becomes available these are called as spinlocks

After finishing the execution of critical section process A will unlock the spinlock at which time the process B will be able to get the lock and enter the critical section.

Thus if a critical section is surrounded by a spinlock only one process can enter the critical section at any given time. Any number of processes can be spinning on the lock but only of the processes will be allowed to enter the critical section once the lock becomes available.

The following animation shows the working of a spinlock as explained above.

Spinlock animation

 photo spin_lock1.gif

djpeg to convert jpeg images to .gif,.bmp etc

djpeg(Decompress jpeg) command can be used to convert the jpg or jpeg format images into .bmp ,.gif and other formats.

For example let us say we have a jpg file by the name temp.jpg. We can find the size of the file using the command du.



We can use the command djpeg on this file to convert it to a .gif file.



options

"-gif" : To specify the output the file to be of type .gif
"-outfilename" : To specify the output file name which is given above as temp.gif

After executing the command we can see the new temp.gif file in the same folder.



Thus we can see that the new file "temp.gif" has been created and it is bigger than the .jpg file as it is a decompressed file.

Insted of "-gif" we can pass the option "-bmp" to convert the image to .bmp format.

We can also remove the color from the image and make it a grayscale image from the .jpg image by passing the option "-grayscale"



We can also scale images,make them smaller, using the option -scale, where the value of scale can be any number between 1 and 16, which is the scaling factor.



The file created above will be scaled by a factor 2/8, where 8 is the default DCT size.



We can see above that temp2.gif is of smaller size than temp.gif as it has been scaled.