0% found this document useful (0 votes)
10 views10 pages

Lab 5

The document provides an overview of threading in C programming, including how to identify threads, compile programs using the pthread library, and debug individual threads using GDB. It also includes algorithms and sample programs for creating threads, managing user accounts, and using the touch command for file management. Additionally, it outlines specific lab tasks related to threading and user management commands.
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)
10 views10 pages

Lab 5

The document provides an overview of threading in C programming, including how to identify threads, compile programs using the pthread library, and debug individual threads using GDB. It also includes algorithms and sample programs for creating threads, managing user accounts, and using the touch command for file management. Additionally, it outlines specific lab tasks related to threading and user management commands.
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
You are on page 1/ 10

Lab # 5

Threading
A thread is a sequence of such instructions within a program that can be executed independently
of other code.

A. Identifying a thread
Each thread identified by an ID, which is known as Thread ID. Thread ID is quite different
from Process ID. A Thread ID is unique in the current process, while a Process ID is unique
across the system.
Thread ID is represented by type pthread_t

a) Header file(s)
The header file which needs to be included to access thread functions
#include<pthread.h>
Then, how to compile C program with pthread.h library?
To compile C program with pthread.h library, you have to put -lpthread just after the compile
command gcc thread.c -o thread, this command will tell to the compiler to execute program
with pthread.h library.

b) The command is:


gcc thread.c -o thread -lpthread

➢ gcc is the compiler command.


➢ thread.c is the name of c program source file.
➢ -o is option to make object file (o/p file or executable file).
➢ thread is the name of object file.
➢ -lpthread is option to execute pthread.h library file.

1) Create a thread and display numbers from 1 to 100;


OUTPUT
2) Create two threads Thread1 and Thread2. Display even number in Thread1 and odd
numbers in Thread2 from 1 to 100.Debug them and display their outputs by single
stepping (running individual threads).

B. Debugging Individual Threads


GDB has the ability to debug individual threads, and to manipulate and examine them
independently. This functionality is not enabled by default. To do so use set non-stop on and
set target-async on. These can be added to .gdbinit. Once that functionality is turned on, GDB
is ready to conduct thread debugging.
For example, the following program creates two threads.
OUTPUT

First set breakpoints on all thread functions; thread1, thread2, and main.

Then run the program.


Fig. 1 Thread1: To display even numbers from 1-100

C. To write a c program to implement Threading and


Synchronization Applications.
ALGORITHM:

Step 1: Start the process


Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition.
Step 5: Check the error state of the thread.
Step 6: Display the completed thread process.
Step 7: Stop the process
PROGRAM:

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

pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
{
printf("\n can't create thread :[%s]", strerror(err));
}
else
{
printf("\n Thread created successfully\n");
}
i++;
}
sleep(5);
return 0;
}
D. User Management:
1. Checking all/specific user accounts
$ lslogins < --user-accs/--system-accs>
$ lslogins <username>
Example: $ lslogins bilal

2. Locating executables files


$ which <filename>
Example: $ which find
Example: $ which
Example: $ which find
Which command show that where is the file in the directory.

3. Finding binary and source files for commands


$ whereis <name>
Example: $ whereis find
Example: $ whereis cat

4. To show currently logged in users in the system


$ who
$w
$ users
$ last
$ top

5. To show current user


$ whoiam

6. To Substitute user with user name


$ su -<username> (if logged in as root then no password is required)
Example $ su –bilal

7. To Substitute user as root


$ su - root

8. To execute command with Substitute user


$ su – c <command> – s <shell> <user>
Example $ su – c “echo Hello World” –s /bin/bash bilal

D. touch Command:
1. Create file using touch command
$ touch <-c> <filename1> <filename2> <filename3>
-c : to suppress creation if file does not exist
-t : to create file using specified date and time
Example: $ touch OSLab5.1 OSLab5.2

2. Create file using touch command by specifying date & time


$ touch <-t> <YYMMDDHHMM> <filename1>
-t : to create file using specified date and time
Example: $ touch –t 2401231513 OSLab5.3

3. Updating access and modification timings


$ touch <-c-d|-a|-m|-d|-r|> <filename1>
-c-d : to update access and modification time
-a : to update access time only
-m : to update last modification time only
-d : to update modification date only
-r : uses timestamp of one file to the other one
Example: $ touch –d “05 May 2023” test.txt
$ touch –r source.txt destination.txt
Lab Tasks

1. Create a file OSLabFile5.1 with date & time 25th January, 2025 3:00 PM.

2. Create a file OSLab5.4 dated 01 Jan 2024 and copy its date to OSLab5.1

3. Find the binary and source file location of “ls” command

4. Create a user as testuser, and then execute command for crearing file OSLabFile5.5
using testuser and shell /bin/bash

5. Creates 5 threads each thread prints a "Hello World!" message, and then terminates.
PROGRAM
using System;
using System.Threading;
public class MyThread
{
public void Thread1()
{
Console.WriteLine("Hello World!");
}
}
public class ThreadExample
{
public static void Main()
{
MyThread mt = new MyThread();
Thread t1 = new Thread(new ThreadStart(mt.Thread1));
Thread t2 = new Thread(new ThreadStart(mt.Thread1));
Thread t3 = new Thread(new ThreadStart(mt.Thread1));
Thread t4 = new Thread(new ThreadStart(mt.Thread1));
Thread t5 = new Thread(new ThreadStart(mt.Thread1));
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
}
}

OUTPUT

You might also like