-
- Write a function add(int, int) and spawn a new thread to call this function.
- Print the output on the console from the thread.
- Create a thread routine that adds all the numbers passed to it as argument and return the result to the main thread.
-
- Write a program having a global variable x, which is initialised with some
- value, say 10. Now create two threads and increment value of x from both the threads. Also, print the value of x inside thread routines and before main routine finishes. What do you observe? What is the behaviour when program is run multiple times?
-
- Write a program having a global variable x, which is initialised with some
- value, say 10. Now create two threads and increment value of x from both the threads. Protect the global variable x using a locking mechanism. Use mutex.
-
- Write a program having a global variable x, which is initialised with some
- value, say 10. Now create two threads and increment value of x from both the threads. Protect the global variable x using a locking mechanism. Use spinlock.
-
- Open a file and use 5 threads to write to the file. Increase elements in a
- loop in the threads in a loop and write the elements to the file. What is happening here. What should be the data in the file.
-
- Open a file and use 5 threads to write to the file. Increase elements in a
- loop in the threads in a loop and write the elements to the file. What is happening here. Make sure that the threads write to the file one by one - for example if the threads are A, B, C, D and E - write the program such that the content of the file is in the order ABCDEABCDE.
-
- Using multithreading add all the element of a array. Take a large array say,
- 10^8 elements and divide the work across all the threads equally. Compare the time take by a single threaded program.
-
- Using multithreading sort a array in ascending order using Merge sort. Take a
- large array, say 1068 elements and compare the working with a single threaded program.
-
- Though you are writing multi threaded code - you might observe that some of
- your processor cores are not getting used, and some of your threads are sleeping. How will you - 1. Make sure that all the threads get maximum available resources on your desktop. 2. All the processor cores are busy all the time.
-
- What are thread safe functions? Write a function which has a static variable
- and it increments it. Make it thread safe.
-
- What are re-entrant functions? Write a function which has a static variable
- and it increments it. Make it re-entrant.
-
- What are atomic instructions - why are they needed, how do you have atomicity
- in the code. For example in an addition function which has a counter of “How many times it was called”.
-
- Open and read a file, keep writing some data using a thread to the file. Now
- whenever you get a keyboard interuppt (for example say ctrl+w) count the number of characters in the file and display it on the screen. This counting has to be done in a new thread. Similiary another thread should calculate the number of words and the number of vowels in the file.
-
- Write a multithreaded program where one thread is writing some data to a file,
- and on receiving some interrupt from keyboard (say, ctrl+w), perform following operations using separate threads and display the results on screen: a. Count number of characters written to the file b. Count number of words in the file c. Count number of vowels in the fileWhile solving the above question you will observe that you have to take lock on the file. When you take lock - the write thread will be blocked. What do you suggest. How can we minimize the blocking of the write thread. Code them as well.
- Simulate the consumer - producer problem.
- Simulate a deadlock situation, the deadlock should be due to improper lock ordering.
- Implement a mutex library.
- Implement a spinlock library.
Thanks Adheer Chandravanshi<adheerchandravanshi@gmail.com> for reviewing the questions.
Monday, March 2, 2015
Problems For Learning Multithreading.
Friday, February 13, 2015
Excellence In Programming
Introduction
Whether you are a seasoned expert on programming or a newbie, sometimes you need
to redo the basic. This small document is written for redoing the whole thing,
and doing it right, and doing it right in the first attempt.
This book is language independent. Whatever language you are comfortable with -
solve the questions.
The initial programs are quite simple and it lets you spend some time with
basics and slowly picks up pace.
This book is and will be always in construction. I request all the readers to
send me reviews, questions and answers on pinnacle-mentors@googlegroups.com.
Whenever you encounter or solve a small programing question - convert it to a
question and send that to me. I will give proper credits and add the question.
Use the repository https://github.com/abrrba/pinnacle-blogs for checking-in the code.
Loops
Print the following patterns.
Arrays
Character/Sorting Arrays
Write a program for
Data Structures - Bitmap
Data Structures - Arrays
Data Structures - Trees
Data Structures - Hashing
Files
Read About
More Problems
Whether you are a seasoned expert on programming or a newbie, sometimes you need
to redo the basic. This small document is written for redoing the whole thing,
and doing it right, and doing it right in the first attempt.
This book is language independent. Whatever language you are comfortable with -
solve the questions.
The initial programs are quite simple and it lets you spend some time with
basics and slowly picks up pace.
This book is and will be always in construction. I request all the readers to
send me reviews, questions and answers on pinnacle-mentors@googlegroups.com.
Whenever you encounter or solve a small programing question - convert it to a
question and send that to me. I will give proper credits and add the question.
Use the repository https://github.com/abrrba/pinnacle-blogs for checking-in the code.
Loops
Print the following patterns.
******** ******** ******** ******** ********
***** **** *** ** *
* ** *** **** *****
####* ###** ##*** #**** *****
* ** *** **** *****
*********** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ***********
* *** ***** ******* ********* ******* ***** *** *
********* ******* ***** *** *
Arrays
- Find the sum of all the elements stored in the array?
- Find the product of all the elements stored in the array? What can be the possible problems here.
- Find the maximum and minimum element of an array.
- Find the average of the array.
- Find the median of the array.
- Find the element which has occurred for the maximum number of times in an array. Similarly find the element which has occurred minimum number of times.
- Fill an array with the multiples of 3, then remove the numbers which are divisible by 5. Fill the empty spaces so generated with -1.
- Fill an array with random elements.
- How will you solve the second problem of the basic level.
- Sort the elements stored in the array using all the sorting methods like Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort.
- Write a program to multiply two 2-D Arrays.
- Write a program to find the sum of a 2D array
- Write a program to find the transpose of a 2D Array.
- Write a program to find the inverse of an array.
- Write a program to find all the prime numbers in a range of 1 to N. N is the user input.
- Write a program to find the factorial of a given number.
- Write a program to find the sum of al the numbers till N ( N is the user input).
- Print the geomatric progressoion, Take the relevant input.
- Print the arthimatic progression, take the relevant input.
- Write a program to convert the epoch to date.
- Write a program to print the spiral of numbers.
- Write a program to print the sum of all the numbers in a 2 D array.
- Write a program to print the multiplication of all the elements in a 2 D array.
Character/Sorting Arrays
Write a program for
- Finding the character which is alphabetically highest, use ASCII values.
- Find wether a string is a palindrome or not.
- Concat the same string to itself. (Eg. HelloWorld -> HelloWorldHelloWorld) #. Insert the word “Hello” in the middle of the words in an sentence. (Eg. How are You? to How Hello are Hello You hello?)
- In the above program do not use too much of extra space, do not use a new array.
- Find if the other string is a substring of the other string.
- Find the number of words in a string. (use space as a word separator).
- In the above program, make sure that multiple spaces are counted as one ” ” is counted as ” “
- Parse a string and find the number of vowels, spaces, special characters.
- Parse a string and reverse all the words in the string.
- Reverse the characters of string.
Data Structures - Bitmap
- Make a library for bitmap. The function calls will be void * init_bitmap(size_of_bitmap). set_bit(int), unset_bit(int), check_bit(int).
- Use the bitmap to check for the characters present in a string.
- Use the bitmap to check for the numbers in a array of integers (1 to N).
Data Structures - Arrays
- Implement a small dictionary application where the users can search for the meaning of a word. use linear search.
- Sort the elements and do not binary search. Implement the above dictionary, find the runtime.
- Sort the elements and do binary search, check the runtime.
Data Structures - Trees
- Implement a binary tree.
- Implement a binary search tree.
- Implement a binary search tree library.
- Using the binary search tree library, make a dictionary. Check the runtime for searching the words
- If your language support tree as data strutures use the feature and implment a dictionary, See the difference between the runtime. Find out why there is a diference.
Data Structures - Hashing
- Implement hashing - use a simple hashing function, make sure that you can handle collisions.
- Using the above code of hashing, insert the elements of the dictionary in your hash. Check the number of collisions. How can you reduce the collisions.
- Now with a better algorithm for hashing, test the dictionary words and see the number of collisions.
- Check the runtime, when you are using your new function.
- Now use the hsearch() for creating a hash, Now check the runtime.
- Now use the SHA1 hashing for creating a hash, now check the runtime.
Files
- Reverse all the characters in a given file. Use the above written code for string reverse.
- Reverse all the sentences in a file. Use the above written code. (I love you to you love I).
- Learn how to read/write a file using C/Java Programming (fopen, fread, fwrite). Write a simple data base application where you save the name, birthDate of students. make another table to save the marks in maths, chemistry, and physics of the students.
- In the above program, generate the following reports. Number of students failed/passed in the subjects. Topper and the lowest scorer. Find the number of students whose aggregate is more than 90%, 80%. List the students.
- If you can easily generate graphs (like in Java) then use graphs to show the results. This will make your looping concepts better.
- Use a file to save the word and the meaning. Whenever user searches for a word, read the file, line by line and then check for the element. Observe the runtime.
- Use a file to save the word and the meaning, but this time, read the whole file, take the whole contents in ram and then search for the contents.
- Read the same file, now save the contents in a Hashing table and then search. See the amount of code you could reuse which you wrote earlier. Can that be improved. How? Re-implement the above written code if required.
- Read the same file, save the contents in a tree and then search. See the amount of code you could reuse which you wrote earlier. Can that be improved. How? Re-implement the above written code if required.
- Read the same file and save the contents in an array and then search. See the amount of code you could reuse which you wrote earlier. Can that be improved. How? Re-implement the above written code if required.
- Observe the difference in the run time of the above 3 approaches of writing a dictionary.
Read About
- Sieve of Eratosthenes
- Rabin-Karp algorithm
More Problems
- Find the Prime numbers using Sieve of Eratosthenes
- Find the substring using Rabin-Karp algorithm.
Subscribe to:
Posts (Atom)