Bird
Raised Fist0
Operating Systemsknowledge~10 mins

Thread creation and management in Operating Systems - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new thread using the POSIX pthread library.

Operating Systems
pthread_t thread_id;
pthread_create(&thread_id, NULL, [1], NULL);
Drag options to blanks, or click blank then click option'
Apthread_join
Bstart_thread
Cmain
Dpthread_exit
Attempts:
3 left
💡 Hint
Common Mistakes
Using pthread_join or pthread_exit as the thread start function.
Passing main as the thread function.
2fill in blank
medium

Complete the code to wait for a thread to finish execution.

Operating Systems
pthread_t tid;
// thread created earlier
pthread_join([1], NULL);
Drag options to blanks, or click blank then click option'
Athread_id
BNULL
C0
Dtid
Attempts:
3 left
💡 Hint
Common Mistakes
Passing NULL instead of the thread ID.
Using a variable name not declared as a thread ID.
3fill in blank
hard

Fix the error in the thread function declaration to match pthread requirements.

Operating Systems
void* [1](void* arg) {
    // thread code
    return NULL;
}
Drag options to blanks, or click blank then click option'
Athread_func
Bvoid thread_func
CthreadFunc()
Dint thread_func
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring the function with no parameters or wrong return type.
Using int or void return types instead of void*.
4fill in blank
hard

Fill both blanks to create a thread and wait for it to finish.

Operating Systems
pthread_t tid;
pthread_create(&tid, NULL, [1], NULL);
pthread_[2](tid, NULL);
Drag options to blanks, or click blank then click option'
Athread_func
Bjoin
Cexit
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using pthread_exit instead of pthread_join to wait.
Passing wrong function names.
5fill in blank
hard

Fill all three blanks to create a thread, pass an argument, and wait for it to finish.

Operating Systems
void* [1](void* arg) {
    int* num = (int*)[2];
    printf("Number: %d\n", *num);
    return NULL;
}

pthread_t tid;
int value = 5;
pthread_create(&tid, NULL, [3], &value);
pthread_join(tid, NULL);
Drag options to blanks, or click blank then click option'
Athread_func
Barg
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the argument pointer inside the function.
Passing wrong function names to pthread_create.

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