Bird
Raised Fist0
Operating Systemsknowledge~20 mins

Thread creation and management in Operating Systems - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Thread Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Thread Creation Methods

Which of the following is not a common method to create a thread in an operating system?

ACreating a thread by specifying a function to execute
BUsing a thread library function like pthread_create()
CCloning an existing process with fork()
DUsing a thread class constructor in high-level languages
Attempts:
2 left
💡 Hint

Think about the difference between processes and threads.

📋 Factual
intermediate
2:00remaining
Thread Lifecycle States

Which of the following is not a typical state in a thread's lifecycle?

AZombie
BSleeping
CRunning
DWaiting
Attempts:
2 left
💡 Hint

Consider which states apply to threads versus processes.

🔍 Analysis
advanced
2:00remaining
Effect of Thread Priority on Scheduling

Consider a system where threads have priorities. If a high-priority thread is ready to run, what is the most likely behavior of the scheduler?

AIt will always run the high-priority thread immediately, preempting any running lower-priority thread.
BIt will wait for the current thread to finish before running the high-priority thread.
CIt will run threads in the order they were created, ignoring priority.
DIt will randomly select any thread to run regardless of priority.
Attempts:
2 left
💡 Hint

Think about preemptive scheduling and priority.

Comparison
advanced
2:00remaining
User-Level vs Kernel-Level Threads

Which statement correctly compares user-level threads and kernel-level threads?

AKernel-level threads cannot run in parallel on multiple processors.
BKernel-level threads can be scheduled independently by the OS, but user-level threads cannot.
CUser-level threads require more overhead to switch than kernel-level threads.
DUser-level threads are managed by the kernel, while kernel-level threads are managed by user libraries.
Attempts:
2 left
💡 Hint

Consider who manages the threads and how scheduling works.

Reasoning
expert
2:00remaining
Deadlock Scenario in Thread Management

Given two threads T1 and T2 and two resources R1 and R2, which scenario can cause a deadlock?

Assume each thread locks one resource and waits for the other.

AT1 and T2 both wait for R1 without locking any resource.
BT1 locks R1, then releases it before locking R2; T2 locks R2 and then locks R1.
CT1 locks R1 and R2 sequentially without waiting; T2 waits for R1 only.
DT1 locks R1, T2 locks R2, then T1 waits for R2 and T2 waits for R1.
Attempts:
2 left
💡 Hint

Deadlock happens when threads wait on each other holding resources.

Practice

(1/5)
1. What is the main purpose of creating threads in an operating system?
easy
A. To increase the size of the program's memory
B. To allow a program to perform multiple tasks at the same time
C. To make the program run slower
D. To reduce the number of files a program can open

Solution

  1. Step 1: Understand what threads do

    Threads let a program split work into parts that run at the same time.
  2. Step 2: Identify the main benefit

    This helps the program do many tasks faster and be more responsive.
  3. Final Answer:

    To allow a program to perform multiple tasks at the same time -> Option B
  4. Quick Check:

    Threads = multitasking [OK]
Hint: Threads let programs multitask simultaneously [OK]
Common Mistakes:
  • Thinking threads increase memory size
  • Believing threads slow down programs
  • Confusing threads with file handling
2. Which of the following is the correct way to start a thread in many programming environments?
easy
A. Define the thread function and call start() on the thread object
B. Write the thread code and call run() directly
C. Create a thread and call stop() immediately
D. Use delete() to begin the thread

Solution

  1. Step 1: Understand thread starting methods

    Threads usually start by calling a special method like start() which runs the thread's code in parallel.
  2. Step 2: Identify correct usage

    Calling run() directly runs code in the current thread, not a new one. stop() and delete() are not used to start threads.
  3. Final Answer:

    Define the thread function and call start() on the thread object -> Option A
  4. Quick Check:

    Use start() to launch threads [OK]
Hint: Always use start() to launch a thread, not run() [OK]
Common Mistakes:
  • Calling run() instead of start()
  • Trying to stop a thread to start it
  • Using delete() to manage threads
3. Consider this pseudocode for thread creation:
thread = createThread(taskFunction)
thread.start()
thread.join()

What does thread.join() do?
medium
A. Waits for the thread to finish before continuing
B. Starts the thread execution
C. Stops the thread immediately
D. Creates a new thread

Solution

  1. Step 1: Understand thread.join()

    The join() method pauses the current program until the thread finishes its task.
  2. Step 2: Identify the effect of join()

    This ensures the program waits for the thread to complete before moving on.
  3. Final Answer:

    Waits for the thread to finish before continuing -> Option A
  4. Quick Check:

    join() = wait for thread end [OK]
Hint: join() waits for thread to finish before continuing [OK]
Common Mistakes:
  • Thinking join() starts the thread
  • Confusing join() with stopping the thread
  • Believing join() creates a new thread
4. What is wrong with this thread creation code snippet?
thread = createThread(taskFunction)
thread.run()

Choose the best explanation.
medium
A. The thread will start twice
B. run() is the correct way to start a thread
C. The thread will never execute
D. Calling run() runs the task in the current thread, not a new thread

Solution

  1. Step 1: Understand difference between run() and start()

    Calling run() directly executes the task in the current thread, not a new thread.
  2. Step 2: Identify the problem

    This means no new thread is created, so the program does not run tasks concurrently.
  3. Final Answer:

    Calling run() runs the task in the current thread, not a new thread -> Option D
  4. Quick Check:

    run() = no new thread [OK]
Hint: Use start(), not run(), to create a new thread [OK]
Common Mistakes:
  • Thinking run() starts a new thread
  • Assuming thread runs twice
  • Believing thread never executes
5. You want to create multiple threads to process a list of tasks, but ensure the main program waits until all threads finish. Which approach is best?
hard
A. Create threads but do not start or join them
B. Start each thread and immediately call run() on each
C. Start each thread and call join() on each one after starting all
D. Start one thread and ignore the others

Solution

  1. Step 1: Understand thread starting and joining

    Starting each thread runs tasks concurrently. Calling join() waits for each thread to finish.
  2. Step 2: Identify correct management

    Starting all threads first allows parallel work, then joining ensures the main program waits for all to complete.
  3. Final Answer:

    Start each thread and call join() on each one after starting all -> Option C
  4. Quick Check:

    Start all, then join all = proper thread management [OK]
Hint: Start all threads first, then join each to wait [OK]
Common Mistakes:
  • Calling run() instead of start()
  • Not joining threads, causing premature exit
  • Starting only one thread and ignoring others