0% found this document useful (0 votes)
30 views3 pages

WSL and POSIX Threads Lab Guide

The document outlines a lab exercise for students to learn about Windows Subsystem for Linux (WSL) and POSIX threads (pthreads). It includes instructions for setting up WSL, writing and executing a multithreaded program, and understanding key concepts related to threads. The lab aims to familiarize students with compiling and running Linux programs on Windows and implementing basic multithreading techniques.

Uploaded by

awais.raza0381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

WSL and POSIX Threads Lab Guide

The document outlines a lab exercise for students to learn about Windows Subsystem for Linux (WSL) and POSIX threads (pthreads). It includes instructions for setting up WSL, writing and executing a multithreaded program, and understanding key concepts related to threads. The lab aims to familiarize students with compiling and running Linux programs on Windows and implementing basic multithreading techniques.

Uploaded by

awais.raza0381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name Muhammad Awais Raza Department: Computer Eng Tech

Roll No: 24017322-047 Section:A

Lab 1: Introduction to WSL and POSIX


Threads
Experiment Statement

This lab introduces students to WSL (Windows Subsystem for Linux) and the POSIX
threads (pthreads) library. Students will learn to compile and run Linux programs on
Windows using WSL and create multithreaded programs using pthreads.

Aim

• To familiarize students with the WSL environment for running Linux-based


applications on Windows.
• To understand POSIX threads (pthreads) and implement a basic multithreaded
program.

Objectives

By the end of this lab, students will be able to:

1. Install and configure WSL on a Windows machine.


2. Compile and run C programs in WSL.
3. Understand the concept of threads and multithreading.
4. Implement and execute a POSIX thread program.
5. Analyze and interpret the output of multithreaded programs.

Prerequisites

• Basic knowledge of C programming.


• Understanding of processes and threads.

Software Required

• Windows 10/11 with WSL installed (Ubuntu preferred).


• GCC compiler (gcc) installed in WSL.
• Text editor (VS Code, nano, vim, etc.).

Procedure

Pre-requisite to proceed: Please watch some tutorial videos on Youtube for basic
commands of Power Shell (CLI) and Ubuntu (Terminal)

Part A: Setting up WSL

1. Open PowerShell as Administrator.


2. Run the command:
3. wsl --install
4. Install Ubuntu from the Microsoft Store.
5. Open Ubuntu terminal and update packages:
6. sudo apt update && sudo apt upgrade
7. Check GCC installation:
8. gcc --version

Part B: Writing a POSIX Thread Program

1. Open a text editor in WSL:

nano thread_example.c
1. Write the following sample code:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void* thread_function(void* arg) {


int id = *((int*)arg);
printf("Hello from thread %d\n", id);
return NULL;
}

int main() {
pthread_t threads[3];
int thread_ids[3];

for(int i=0; i<3; i++) {


thread_ids[i] = i+1;
pthread_create(&threads[i], NULL, thread_function,
(void*)&thread_ids[i]);
}

for(int i=0; i<3; i++) {


pthread_join(threads[i], NULL);
}

printf("All threads completed.\n");


return 0;
}

3. Save and compile the program:


2. gcc -pthread thread_example.c -o thread_example
3. Run the program:
1. ./thread_example

Expected Results

• Each thread prints a unique greeting message with its thread ID.
• The main program waits for all threads to complete before printing:
• Hello from thread 1
• Hello from thread 2
• Hello from thread 3
• All threads completed.
• Thread execution order may vary, showing concurrent execution.

Q1. What is the purpose of pthread_create and pthread_join?


Ans:

 pthread_create is used to start a new thread.


 pthread_join is used to wait for a thread to finish before continuing.

Q2. Why might the thread output order differ on each run?
Ans:
Because threads run concurrently, and the CPU schedules them randomly, so the order of
execution can change every time.
Q3. Explain the difference between processes and threads.
Ans:

 A process has its own memory space and resources.


 A thread is a smaller unit inside a process that shares memory with other threads of
the same process.

Q4. Mo

dify the program to create 5 threads instead of 3. What changes do you observe?
Ans:
When we create 5 threads, we see 5 "Hello from thread" messages.
The order still varies, showing concurrent execution of multiple threads.

Q5. How does WSL facilitate running Linux programs on Windows?


Ans:
WSL allows Linux executables to run directly on Windows without using a virtual
machine.
It provides a Linux-like environment where we can use Linux commands, tools, and
compilers easily.
WSL Notes

• WSL allows running Linux executables natively on Windows.


• Use Ubuntu or any other preferred Linux distribution.
• Access Windows files from WSL at /mnt/c/ (e.g., C:\Users\Username).

POSIX Threads Notes

• Threads are lightweight processes sharing the same memory.


• pthread_create: Starts a new thread.
• pthread_join: Waits for a thread to finish.
• Use -pthread flag during compilation.

You might also like