Pages

using pthread_mutex_timedlock in linux.

We saw in the post " " how we can use mutexes in pthreads to achieve synchronization among various threads. Along with the the three functions we saw, there is a fourth mutex function that we can use when we want to wait on a mutex for only a specific duration and then continue irrespective of whether we are able to get a lock on the mutex or not. The function for timed operation on mutex it pthread_mutex_timedlock whose syntax is



The timeout is the time for which we want to wait on the mutex. To specify the timeout we need to use the structure timepsec which has two fields one for seconds and other for nanoseconds as shown below.



The mutex if available will be locked immediately, if the mutex is not available the thread will wait for the duration of time mentioned in the abs_timeout and then exit from the wait.

The following program shows an example of using the pthread_mutex_timedlock. In the program thread1 takes a value as input from the user and assigns it to a variable "temp". In thread2 the same variable is accessed and its value is printed. Thread1 locks a mutex while getting the value for temp from the user, but thread2 waits only for 5 seconds for the mutex to get unlocked after which it exits with a time out message and does not print the value of the variable temp.

To mention the timeout we need to first allocate memory for a timespec structure



Thus now we have a timespec structure with 5 seconds and 0 nanoseconds as the time values, which can be passed to the function pthread_mutex_timedlock to achieve a 5 second timeout.
The whole function looks as follows



Save the program as mutex_timedlock.c, and compile it with the flag -lpthread



While executing the program when the prompt for entering a value for the variable appears, if you wait for more than 5 seonds for entering the value, you will noticed the timed out message from the second thread will appear on the other hand if we enter the value with in 5 seconds the value entered is printed by the second thread. case1: Waiting for more than 5 seconds before entering the value





Thus we are able to achieve a mutex lock for a specific duration using the pthread_mutex_timedlock function.

Animated gif image of linux using gimp

Here are steps of how we can create the following animated linux gif image using gimp.



Note: This was created using gmip 2.6.10

Open gimp and go to



This will show a menu as shown below.
In the field marked "text" , enter L, choose the font size that you would like and then click on the gradient to pick the color gradient you wish for the text.

The animation shown below uses the incandescent.

This will create a image as shown below.

Save the image as L.jpg. While saving remeber to take the file type as .jpg.

Following the same steps create the other images with the following text



Now Open gimp again.





choose the size of the template you want, (Base it on the size of the font that you have created).

Now drag and drop the 4 jpg images that we created above in the order



Download an image of tux, the penguin, from net if you want to and add that too to the gimp.

You should see all the six images in six layers as shown below.



Now to create an animation using these layers, click on save as and choose the image type to be .gif and add the extention to the name while saving as shown below



Before saving a window will appear, confirming whether you want to save as animation or just as an image, choose save as animation, and click on export



In the next window shown below, you can choose how much dealy should be given between every frame. The more delay you give the slower the animation will look. Check the loop for ever button to keep running the animation infinietly and click on save.



Now your gif animated image should be ready.

ussing barriers in pthreads

pthreads allows us to create and manage threads in a program. When multiple threads are working together, it might be required that the threads wait for each other at a certain event or point in the program before proceeding ahead.

Let us say we have four threads, each of which is going to initialize a global variable. The 4 variables in turn might be used by all the four threads. Thus it would be feasible that all the threads wait for each other to finish the initialization of the variables before proceeding.

Such operations can be implemented by adding a barrier in the thread. A barrier is a point where the thread is going to wait for other threads and will proceed further only when predefined number of threads reach the same barrier in their respective programs.

To use a barrier we need to use variables of the type pthread_barrier_t. A barrier can be initialized using the function pthread_barrier_init, whose syntax is



Once a barrier in initialized, a thread can be made to wait on the barrier for other threads using the function pthread_barrier_wait whose syntax is



Where the barrier is the same variable initialized using pthread_barrier_init.

A thread will keep waiting till the "count" number of threads passed during init do not call the wait function on the same barrier.

In the example below, we create 4 threads, and each thread assigns a value to one of he global variables and then later all the threads print the value of all the four variables. Thus to be able to print the value of all the variables the threads have to wait until all threads have not finished assigning the values. Thus we can make use of barriers to make the threads wait.

The init function :



initializes the barrier "our_barrier" to wait for four threads.

Thus till all the four threads have not called the pthread_barrier_wait the other threads will continue to wait.

Once pthread_barrier_wait has been called by 4 threads, all the threads will continue their execution of program.

A barrier can be destroyed using the function pthread_barrier_destroy, whose syntax is :



But note that the barrier should be destroyed only when no thread is executing a wait on the barrier.



Save the program as pthread_barrier.c and compile it by passing the -lpthread flag.



To stress on usefulness of the barrier, we can change barrier initialization in the above code to wait for only 3 threads i.e.


And remove the pthread_barrier_wait from the the 4th thread.

After the above changes, if we compile and execute the code the output would be



The first three threads could not get the value entered by the fourth thread as they moved over the barrier as soon as three threads executed the wait function.

Converting pdf to text in linux

The contents of a pdf file can be converted to a simple text file using the tool "pdftotext". The format of using pdftotext is



Let us say we have a pdf file by the name temp.pdf, of which we want to convert to text the page numbers 1 to 4 and create a text file by the name output.txt .



The file output.txt will have all the text contents of the temp.pdf, pages 1 to 4 but will not contain the images. The formatting by default in not maintained in the text file.

To be able to maintain the formatting also we need to pass the option -layout.



Now the text file will have almost the same format as the pdf file.

Using condition variables in pthreads

Along with mutexes, pthreads gives us another tool for synchronization between the threads, condition variables. Condition variables are variables of the kind pthread_cond_t.

When a thread is waiting on a mutex it will continuously keep polling on the mutex waiting for it to get unlocked. Such behavior could lead to wastage of CPU resources. This can be prevented by using the condition variables.

The condition variables can be initialized statically



or dynamically



Attribute can be left NULL if default attributes are sufficient. The only attribute condition variables have is process_shared, which indicates that the condition variable is visible to threads of other processes too.

Now to use the condition variable in a thread we have two functions



This is called when a thread wants to wait for specific task to be completed by another thread before continuing its operations. This function should be called after locking the mutex, which automatically gets unlocked once the thread goes in to wait state.



This function does the job of signaling to the thread that had called pthread_cond_wait. Note that the "condition variable" used in the signal and the wait functions should be the same. We need to lock the mutex that is shared by the threads before calling this function and unlock it after the call.

Once pthread_cond_signal is called the threas which was waiting on the function pthread_cond_wait will proceed with its execution.

The following program depicts the use of condition variables.
We create two threads
"fill": which fills values into an array "arr"
read: Which reads the values of the array.

Now we can read from the array unless it is not filled, thus the thread read calls pthread_cond_wait, waiting for the array to be filled. On the other hand the thread "fill" calls pthread_cond_signal once it is done filling the array.Thus making sure that the threads are in synchronization.



Save the program as pthread_cond.c, and compile using -lpthread flag



Execute the program



Thus we can see that the read thread was waiting until the fill thread did not call the signal, and only after receiving the signal the read thread continued execution.

Searching and highlighting in vi editor


In this post we will look into at the ways in which we can search in a program using the VI editor.

The searching always happens in the command mode so if you want to search for a string or a pattern in the program, first step is to go to the command mode by pressing "esc"

Let us take a simple hello world program as an example.


Let us say we come across the string printf once and want to search all the occurrences of printf after the current cursor position, then just move the cursor on printf and press "*". The cursor will automatically move to the next occurrence of printf and on each press of "*" it will move to the next occurrence of printf.

To search backwards, that is behind the current cursor position place the cursor on printf and press "#". The cursor will move to the previous occurence of the search string. The second way of searching is
Go to command mode and type

any thing that follows the "/" will be taken as the search string.

Then keep pressing "n" to move to the next occurrences of the search string. If you want to move back in the document, that is search for occurrences of the string before the current cursor position use the shift "n" , i.e "N" .
To see all the matching strings at the same time we can highlight all the matched strings by setting the option hlsearch i.e.



All the items which matched the previous search string will get highlighted. To remove the highlight run the command



To make the seach case insensitive we need to set the option "ignorecase" i.e



The search can be made case insensitive by adding \c to the search string, i.e



The search can also be restrcted to specific lines. To restict the search only to a single line we need to use \%numl along with the search string. To restrict the search for strintg printf to 20th line we need to use



To restinct the search to lines after 20th line



To restinct the search to lines between 20th line and 30th lines



The same rules can be applied to columns instead of lines by replacing "l" in the command by "c".

We can also highlight patterns with color of our own wish by using the command "match". Let us say we want to highlight all the strings "printf" in red color. Then first we need to create a hightlight group by running the command



Instead of group1 we can use any group name we want, and instead of red we can use any other color name that is recognized by "vi".
Now use the command match to highlight the string printf.



group1 being the name we used while creating our group using highlight command. All the occurrences of string printf should get highlighted in red, or whatever color is chosen while running the highlight command.

To remove the highlight run the command

Enabling mouse in vi editor in linux

While using the "vi" editor, one of the common problmes that we face is the inability to the mouse to move around in the file. Here is simple way to enable to usage of mouse in vi editor.

Open the vi editor in the command mode (Press "esc" to enter command mode) run the command



Now you should be able to click any where in the document and the cursor will move to that location.

To enable the mouse every time "vi" is launched we need to open the file



And uncomment the line



Note: un-commenting is done by removing the ' " ' at the beginning of the line.

Save and close the file. From the next time any file is opened using "vi" editor the mouse will be enabled by default.

Extracting images from pdf file from command line in linux

if we want to extract only the images from a pdf file, we can use the command line tool pdfimages. pdfimages extracts the images from the given pdf, from the pages we mention and saves it in the .ppm format. The syntax for using the command is



Image-root is nothing but prefix for the filenames of the image files to be created.

Let us say we have a file by the name temp.pdf, from which we want to extract the images from the pages 2 to 4 assume there are three images.



Thus we can see that the three images are extracted and stored as image-000.ppm,image-001.ppm,image-002.ppm.

If the file is password locked we can pass the password using the option -upw along with the command.

Creating a password locked pdf document in linux

We can user pdftk tool to create a password locked pdf file i.e. to open the pdf file we need to open the supply a password.

The options that have to be passed to pdftk are



Let us say we have a file temp.pdf and we want to have the password "hello" for opening the file and we will call the new file by the name temp_passwd.pdf



Now if we try to open the file temp_passwd.pdf we will be prompted for a password and the file will open only if we pass the password "hello".

In the command we also use encrypt_128bit instead of encrypt_40bit.


Splitting a pdf file into seperate pdfs in linux

The pdftk tool can be used to split a given pdf document into separate individual pdf pages. The option to be used with pdftk to achieve this is "burst" i.e.



The pages are created with the names as pg_0001.pdf for first page,pg_0002.pdf for the second page etc. Example : Let us say we have a pdf file named "temp.pdf" with 10 pages.



Thus we can see the temp.pdf has been split into 10 pdf files. The file doc_data.txt has the information of the document.

Using pthread_mutex_trylock

In the mutex we saw in the post " " , if a mutex is locked by one thread, other threads will wait infinitesly for the mutex when pthread_mutex_lock is called. The other threads wil get out pf hte wait only when the mutex becomes available or the process is killed.
If such a behviour of infinite wait is undesirable we can use the variant pthread_mutex_trylock.

In this function the thread will try to lock the mutex, but in case the mutes in not available, it will return with an error EBUSY and will not try to acuire the lock again . If it is able to lock the mutex, the return value is 0 else it returns a non zero error value.

The following code shows the use of the function pthread_mutex_trylock. In the program, two threads are created one to write to a file and another to read from the file. The read thread is started with a little delay to allow the write thread to get access of the mutex.

Both the thread lock the muetx using pthread_mutex_trylock. The write thread is able to get access to the lock and continues with the execution of the thread. The read thread on the other hand tries to access the lock but is unaalbe to get a lock on the mutex as the write thread is holding the same. Thus it returns with an error value, because of which the thread has to be exited with our reading the file.



Save the file as mutex_trylock.c, and compile it using the flab -lpthread



We can see from the messages that the read thread has returned with an error and was unable to open the file as the pthread_mutex_trylock failed to get the lock.