Search

Transfrming a photo to an old looking photo

We can use gimp to make any photo look like as if it was taken many years ago, in the days of black and white cameras.

To turn any photo into an old photo, open gimp and open the photo in gimp using

file->open.

1_old_open photo 1_open.jpg

Let us take this as the example photo.

image photo 2_image.jpg

Now click on

filters->decor->old photo

3_old_photo photo 3_old_photo_menu.jpg

This will prompt a menu as below

4_old_menu photo 4_old_menu.jpg

The options are :

defocus: To reduce the focus of the image to make it appear slightly blurred as in older photos

sepia: To remove the colors of the photo make it appear almost balck and white.

Mottle: To add dotted effects on the photo.

work on copy: To make sure that the original image is not modified check this option so that gimp creates a new image modified as required.

After selecting the required options click on ok and wait for gimp to finish its work.

Soon gimp will launch a new window with the modified image as below. (The sepia option has been selected)

5_old_final photo old_sepia.jpg

Thus making a new color photo appear as if it were a many years old photo.

Commands to find the CPU temprature.

As CPUs operate they always generate heat but for every cpu the temprature at which it operates is very important, and all cpus have an upper limit beyond which they should not be allowed to heat.

Here are various ways in which we can find the temprature of the CPU core at any given time.

1.



The first set of the readings is the core temprature used by the ACPI. The second is of a PCI adapter.

In the first set of reading we can see that the critical temprature is mentioned as 90.0°C, operating the CPU beyond that tempratiure would harm the CPU and mACPI would always shut the system down as soon as the critical temprature is reached.
2.



Norte the "V" is in upper case The fourh line gives the temprature of the core and the fifth line the critical temprature.

The above command will work only if you have ACPI enabled in the system.

3.



The above proc file system will exist only if ACPI is enabled. Other than temprature we can find the critical temprature using



passive trip point is one at which the hardware is cooled by lowering the cpu freuency etc. Not all hardware support this.

working of the fuction atomic_add_return

Atomic instructions are useful for executing instructions with out interrupt. Theory of the same is given in the post. Let us look into the Linux kernel, and see how atomic functions are implemented in the kernel. The atomic variables are declared as type atomic_t which is nothing but a structure defined in "include/Linux/types.h" as given below. Atomic structure :



File: include/Linux/types.h The value of the atomic variable is updated in the counter member of the structure. Atomic functions are written in different ways for different architectures. In this post We will be looking at atomic functions for x86 architecture. One of the atomic functions defined in atomic.h is atomic_add_return. The function is defined as follows.



The first line "#ifdef CONFIG_M386" is to check which CPU is the kernel being executed on. This is because in modern x86 CPU there are special instructions available that can be used for atomic operations.

If the architecture is 386, then we check if the instruction xadd is available in the CPU. If xadd is not available then we have to implement the atomic operation manually and we ump to the label no_xadd.

If xadd instruction is available or if the CPU is a modern 486 or above CPU, which always has the xadd instruction, then we make use of the instruction and pass the numbers for addition to xadd. Note that the first argument is the atomic number and the second is the number to be added to the atomic number.

If the system has i486 processor it will be defined in file: include/generated/autoconf.h as

The function xadd is implemented in the file arch/x86/include/asm/cmpxchg.h



From the comment above we note that xadd adds the second argument to the first argument and returns the previous value of the first argument. But note that the value of the first argument is updated to the new value.

xadd has been #defined to __xadd, which in turn has been #defined to __xchg_op. To the function __xcgh_op we pass the two numbers to be added, the instruction "xadd" and the keyword lock to signify that the instruction has to be executed in atomic mode.

The __xchg_op is implemented in the same file as



The four arguments passed __xcgh_op are

ptr: Pointer to the first argument, for xadd this the atomic variable which needs to be updated. arg: The second argument and for xadd the value to be added to the atomic variable op: The instruction to be executed, in this case "xadd" lock: To signify that the instruction is to be executed atomically.

The four case statements implement the addition for four different sizes of data. Let us take the first case of addition of single byte.



The above code implements the instruction using "asm" feature of gcc.

asm volatile: Instructs gcc not to do any optimizations to the code that follows.

lock: keyword to make the instruction execute atomically

op: In this case the "op", the assembly instruction, will be equal to xadd

b %b0, %1: b signifies argument is byte sized and %0,%1 are the arguments to the instruction.

"+q" (__ret), "+m" (*(ptr)): Being the first set of variables after the ":" these are the output variables.

"+" signifies the variable is going to read as well as written

q signifies the value is going to be stored in a register

__ret: Is the first argument to the instructions

m: signifies the value will be stored in memory

*(ptr): Is the second argument to instruction.

There are no input arguments thus there are no arguments in the second set of : : .

The last set of arguments are clobbers which are.

memory: The instruction write to some memory and the values in registers should not be cached by gcc

cc: The instruction affects the condition codes.

The working of xadd in i486 is as below.



To understand how the asm implementation of xadd is working we can use the followin example code.



compile it using gcc and execute it.



We can see that the value of i has become equal to "11" which was the value of j and the value of j has become equal to 21 which is the addition of i and j.

Applying the same logic to the kernel code above we can see that the value of __ret will become equal to the value of *ptr, which is the atomic variable, and the value of *ptr will become the sum of *ptr and __ret.

__xchg_op at the end returns __ret which is the value of the *ptr before addition.

Thus in the function atomic_add_return the statement



Increments the value of atomic variable by "i" and returns, "i" + "value of atomic variable before increment", which is nothing but the new value of the atomic variable.

In case the processor does not have an xadd instruction then the following code gets executed.



In the absence of support for atomic instructions in the processor, we have to manually disable the interrupts to make the instruction execute atomically. raw_local_irq_save(flags) : Disabling all interrupts
__i = atomic_read(v) : Atomically read the value of the variable and assign it to __i.
atomic_set(v, i + __i): Set the value of atomic variable,v, to sum of __i ,i.
once the value of atomic variable is updated we can enable the interrupts.
raw_local_irq_restore(flags);

Then return the sum of i,__i which is same as the new value of the atomic variable.

This we can see that atomic_add_return is able to perform the addition atomically using either xadd instruction of the processor or by disabling the interrupts manually.


make: Nothing to be done for `default'.

While writing make files to compile kernel modules we might come across the error



The common cause for the error is lack of tab space before the command.
A make rule always has three parts a target,prerequisite and the command to generate the target from the prerequisite.



For make to be able to differentiate between a command and a target, the command always needs to be prefixed with one tab space. In case we fail to do so then make will not recognize it as a command and throw an error saying that there is no command specified for the target. It is the same error as given above where default is the target.

Thus to solve the problem just open the makefile and add a tabspace before the command for whichever target the error is being thrown.


Anwers to the pthreads basics quiz

Here are the answer to the quiz on pthread basics

1. Which header file needs to be used for pthread programming ?
Ans: pthread.h

2.Pthreads are not useful in uniprocessor systems
Ans: false

3. What are the flags that are passed while compiling a pthread program using gcc ?
Ans: -lpthread

4. Among the arguments passed to pthread API pthread_create the final argument, is.
Ans: Data being passed to the thread

5. The data type used for storing the thread id is
Ans: pthread_t

6. Which of the following APIs can be used for synchronization between threads
Ans: pthread_join

7. To create a detachable thread which of the following structures needs to be modified
Ans: pthread_attr_t

8. pthread_self returns
Ans: The thread id

9.Two threads can be compared using the API
Ans: pthread_equal

10.pthread_create(&threads_id, NULL, Hello_world, NULL); creates a thread
Ans:Called Hello_world with default attributes

Quiz on pthread basics

Pthreads Basics Quiz

Pthreads Basics Quiz

A quiz on basics of pthreads
  1. 1. Which header file needs to be used for pthread programming ?

  2. pthreads.h
    pthread.h
    p_thread.h
    posix.h

  3. Pthreads are not useful in uniprocessor systems

  4. False
    True
    Depends on the compiler
    Depends on the processor

  5. What are the flags that are passed while compiling a pthread program using gcc

  6. -pthread
    -lpthread
    -gccpthread
    -thread

  7. Among the arguments passed to pthread API pthread_create the final argument, is.

  8. Attributes of the thread
    Name of the thread
    Data being passed to the thread
    Thread id

  9. The data type used for storing the thread id is

  10. pthread
    pthread_t
    p_thread_t
    pthread_id

  11. Which of the following APIs can be used for synchronization between threads

  12. pthread_exit
    pthread_cancel
    pthread_self
    pthread_join

  13. To create a detachable thread which of the following structures needs to be modified

  14. pthread_attr_t
    pthread_t
    pthread_attribute
    attribute_t

  15. pthread_self returns

  16. The thread id
    The thread attribute
    Thread data
    Thread running time

  17. Two threads can be compared using the API

  18. compare_pthreads
    pthread_equal
    pthread_compare
    equal_pthread

  19. pthread_create(&threads_id, NULL, Hello_world, NULL); creates a thread

  20. Called Hello_world with default attributes
    Called Hello_world with all attributes set to NULL
    Called Hello with default attributes
    Called Hello with all attributes set to NULL

Answers the quiz are available at : Answers to pthreads basic quiz