SlideShare a Scribd company logo
1 of 33
Threads
Advance
SOUMEN SANTRA
MCA, M.Tech, SCJP, MCP
Process Descriptor Handling
union thread_union {
struct thread_info thread_info;
unsigned long stack[2048];
};
Two data structures in 8KB.
thread_info structure
Kernel mode process stack
Storing the thread_info Structure in the Process
Kernel Stack
Pid_hash Table and Chained Lists
 PID is use to search a process descriptor.
 Sequential search in the process list is inefficient.
 The pid_hash array contains four hash tables and
corresponding filed in the process descriptor.
pid: PIDTYPE_PID.
tgid: PIDTYPE_TGID (thread group leader).
pgrp: PIDTYPE_PGID (group leader).
session: PIDTYPE_SID (session leader).
 Chaining is used to handle PID collisions.
pid_hash Table
 Size of each pidhash table: dependent on the
available memory.
 PID is transformed into table index using pid_hashfn
macro.
Example of PID Hash Table and
Chained Lists
Continue
 pids field of the process descriptor: the pid data
structures:
nr: PID number.
pid_chain: links to the previous and the next
elements in the hash chain list.
pid_list: head of the per-PID list (in thread group).
PID Hash Tables
PID Hash Table Handling Functions
and Macros
 do_each_trask_pid(nr, type, task) : Execute each task.
 while_each_trask_pid(nr, type, task) : Execute each task.
 find_trask_by_pid_type(type, nr) : Find task by type & id.
 find_trask_by_pid(nr) : Find task by id.
 attach_pid(task, type, nr) : Add process on task.
 detach_pid(task, type) Delete process from task.
 next_thread(task) Show next Thread of a task.
How Processes are Organized
 Processes in TASK_STOPPED, EXIT_ZOMBIE,
EXIT_DEAD: not linked in lists.
 Processes in TASK_INTERRUPTABLE,
TASK_UNINTERRUPTABLE: wait queues
 Two kinds of sleeping processes are:
Exclusive process.
Non-exclusive process: always woken up by the
kernel when the event occurs.
Programs on Wait Queues
 struct wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
typedef struct _ _wait_queue_head
wait_queue_head_t;
 struct wait_queue {
unsigned int flags;
struct task_struct * task;
wait_queue_func_t func;
struct list_head task_list;
};
typedef struct wait_queue wait_queue_t;
Handling Wait Queues
 Wait queue handling functions:
 add_wait_queue()
 add_wait_queue_exclusive()
 remove_wait_queue()
 wait_queue_active()
 DECLARE_WAIT_QUEUE_HEAD(name)
 init_waitqueue_head()
 To wait:
 sleep_on()
 interruptible_sleep_on()
 sleep_on_timeout(), interruptible_sleep_on_timeout()
 Prepare_to_wait(), prepare_to_wait_exclusive(), finish_wait()
 Macros: wait_event, wait_event_interruptible
Handling Wait Queues
 To be woken up:
wake_up, wake_up_nr, wake_up_all
 wake_up_sync, wake_up_sync_nr
 wake_up_interruptible, wake_up_interruptible_nr
 wake_up_interruptible_all
 wake_up_interruptible_sync
 wake_up_interruptible_sync_nr
Process Resource Limits
 RLIMIT_AS
 RLIMIT_CORE
 RLIMIT_CPU
 RLIMIT_DATA
 RLIMIT_FSIZE
 RLIMIT_LOCKS
 RLIMIT_MEMLOCK
 RLIMIT_MSGQUEUE
 RLIMIT_NOFILE
 RLIMIT_NPROC
 RLIMIT_SIGPENDING
 RLIMIT_STACK
Process Switch
 Process switch, task switch, context switch.
Hardware context switch: far jump (in older Linux).
Software context switch: a sequence of mov
instructions.
It allows better control over the validity of data
being loaded.
The amount of time required is about the same.
 Performing the Process Switch
Switching the Page Global Directory.
Switching the Kernel Mode stack and the
hardware context.
Task State Segment
It is a specific segment architecture to store
hardware contexts with type in x86.
Creating Processes
 In traditional UNIX, resources owned by parent
process are duplicated.
Very slow and inefficient.
 Mechanisms to solve this problem:
Copy on Write: parent and child read the same
physical pages.
Lightweight process: parent and child share per-
process kernel data structures.
vfork() system call: parent and child share the
memory address space.
System Calls
 clone(fn, arg, flags, child_stack,ptid, ctid): creating
lightweight process
A wrapper function in C library
Uses clone() system call
 fork() and vfork() system calls: implemented by
clone() with different parameters.
 Each invokes do_fork() function.
Kernel Threads
 They run only in kernel mode.
 They use only linear addresses greater than
PAGE_OFFSET.
 kernel_thread(): To create a kernel thread.
 Example kernel threads:
Process 0 (swapper process), the ancestor of all
processes.
Process 1 (init process).
Others: keventd, kswapd, kflushd (also bdflush),
kupdated etc.
Destroying Processes
 exit() library function
Two system calls in Linux 2.6
 _exit() system call :Handled by do_exit() function.
 exit_group() system call : Handled by do_group_exit()
function.
 Process removal
Releasing the process descriptor of a zombie process
by release_task().
Normal function call
Threaded function call
Pthread Operations
POSIX function Description
pthread_create create a thread
pthread_detach set thread to release resources
pthread_equal test two thread IDs for equality
pthread_exit exit a thread without exiting process
pthread_kill send a signal to a thread
pthread_join wait for a thread
pthread_self find out own thread ID
Thread Packages
 Kernel thread packages
Implemented and supported at kernel level
 User-level thread packages
Implemented at user level
POSIX threads on GNU/Linux
 GNU/Linux, threads are implemented as processes.
 pthread_create to create a new thread.
 Linux creates a new process that runs that thread.
 A POSIX thread is not the same as a process you
would create with fork.
 It shares the same address space and resources as
the original process rather than receiving copies.
 Each thread maps to a kernel scheduling entity.
POSIX threads on GNU/Linux
Continue
 The Linux clone system call is a generalized form of
fork.
 pthread_create that allows the caller to specify which
resources are shared between the calling process
and the newly created process.
 Clone system call should not ordinarily be used in
programs.
 Use fork to create new processes or pthread_create
to create threads.
pthread_t identifier
 Process created ptherad_t and it is not visible outside.
 For instance, send a pthread_kill to a thread of another
process.
 More details for Linux:
 pthread_self() will get you an identifier that is unique across your
program, but not across your system.
 Thread is a system object.
 The system is unaware of the identifier POSIX library allocated for
the thread.
 Linux identifies threads with PID like number called TID.
 These numbers are system-wide.
Example Program
#include <pthread.h>
#include <stdio.h>
void *threadex(void *);
int main()
{
pthread_t tid; /* stores the new thread ID */
pthread_create(&tid, NULL, threadex, NULL); /*creates a new thread*/
pthread_join(tid, NULL); /*main thread waits for termination new thread */
return 0; /* exits main thread */
}
void *threadex(void *arg) /*thread subroutine*/
{
int t;
for (t=0; t<10; t++)
fprintf(stderr, "Hello, world of Threads ! n ");
return NULL;
}
Creating a thread with pthread
 A thread is created with
int pthread_create(
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *),
void *arg);
 The creating process (or thread) must provide a location
for storage of the thread id.
 The third parameter is just the name of the function for the
thread to run.
 The last parameter (void *arg) is the sole argument
passed to created thread.
Example 1: Thread Creation
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 10
void *PrintHelloThread (void *threadid) {
int tid;
tid = (int)threadid;
printf("Hello World of threads : #%d!n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
pthread_t threads[NUM_THREADS];
int count, i;
for(i=0; i<NUM_THREADS; i++){
printf("In main: creating thread %dn", i);
count = pthread_create(&threads[i], NULL, PrintHelloThread, (void *)i);
if (count) {
printf("ERROR code is %dn", count);
exit(1);
}
}
pthread_exit(NULL);
}
Example 2: Passing Parameters to a
Thread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 10
void *PrintHelloThread (void *ptr)
{
char *filename;
int t;
filename = (char *) ptr;
while (1)
{
printf("Hello World of threads : %s!n", filename);
for (t=1; t++; t<500);
}
pthread_exit(NULL);
}
Example 2: Passing Parameters to a
Thread (Continue)
int main (int argc, char *argv[])
{
pthread_t thread[10];
int err_code, count=0;
char *filename;
printf ("Enter thread name at any time to create threadn");
while (count <= 9) {
filename = (char *) malloc (80*sizeof(char));
scanf ("%s", filename);
printf(“From main- creating threads %dn", count);
err_code = pthread_create(&thread[count], NULL, PrintHelloThread, (void *)filename);
if (err_code){
printf("ERROR code is %dn", err_code);
exit(1);
}
count++;
}
pthread_exit(NULL);
}
THANK YOU
Give Feedback
Please Go Through
Thread Basic

More Related Content

What's hot

Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2DataStax
 
Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Angel Boy
 
Новый InterSystems: open-source, митапы, хакатоны
Новый InterSystems: open-source, митапы, хакатоныНовый InterSystems: open-source, митапы, хакатоны
Новый InterSystems: open-source, митапы, хакатоныTimur Safin
 
The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202Mahmoud Samir Fayed
 
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki LinnakangasPG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangaspgdayrussia
 
WiredTiger In-Memory vs WiredTiger B-Tree
WiredTiger In-Memory vs WiredTiger B-TreeWiredTiger In-Memory vs WiredTiger B-Tree
WiredTiger In-Memory vs WiredTiger B-TreeSveta Smirnova
 
HandlerSocket plugin for MySQL (English)
HandlerSocket plugin for MySQL (English)HandlerSocket plugin for MySQL (English)
HandlerSocket plugin for MySQL (English)akirahiguchi
 
The Ring programming language version 1.7 book - Part 112 of 196
The Ring programming language version 1.7 book - Part 112 of 196The Ring programming language version 1.7 book - Part 112 of 196
The Ring programming language version 1.7 book - Part 112 of 196Mahmoud Samir Fayed
 
Distributed systems at ok.ru #rigadevday
Distributed systems at ok.ru #rigadevdayDistributed systems at ok.ru #rigadevday
Distributed systems at ok.ru #rigadevdayodnoklassniki.ru
 
The Ring programming language version 1.7 book - Part 52 of 196
The Ring programming language version 1.7 book - Part 52 of 196The Ring programming language version 1.7 book - Part 52 of 196
The Ring programming language version 1.7 book - Part 52 of 196Mahmoud Samir Fayed
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrentRoger Xia
 
hbaseconasia2019 OpenTSDB at Xiaomi
hbaseconasia2019 OpenTSDB at Xiaomihbaseconasia2019 OpenTSDB at Xiaomi
hbaseconasia2019 OpenTSDB at XiaomiMichael Stack
 
The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184Mahmoud Samir Fayed
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4Benux Wei
 
groovy databases
groovy databasesgroovy databases
groovy databasesPaul King
 

What's hot (20)

Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
 
Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)
 
Новый InterSystems: open-source, митапы, хакатоны
Новый InterSystems: open-source, митапы, хакатоныНовый InterSystems: open-source, митапы, хакатоны
Новый InterSystems: open-source, митапы, хакатоны
 
The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210The Ring programming language version 1.9 book - Part 11 of 210
The Ring programming language version 1.9 book - Part 11 of 210
 
The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202
 
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki LinnakangasPG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
PG Day'14 Russia, PostgreSQL System Architecture, Heikki Linnakangas
 
WiredTiger In-Memory vs WiredTiger B-Tree
WiredTiger In-Memory vs WiredTiger B-TreeWiredTiger In-Memory vs WiredTiger B-Tree
WiredTiger In-Memory vs WiredTiger B-Tree
 
HandlerSocket plugin for MySQL (English)
HandlerSocket plugin for MySQL (English)HandlerSocket plugin for MySQL (English)
HandlerSocket plugin for MySQL (English)
 
Sysprog 14
Sysprog 14Sysprog 14
Sysprog 14
 
The Ring programming language version 1.7 book - Part 112 of 196
The Ring programming language version 1.7 book - Part 112 of 196The Ring programming language version 1.7 book - Part 112 of 196
The Ring programming language version 1.7 book - Part 112 of 196
 
Distributed systems at ok.ru #rigadevday
Distributed systems at ok.ru #rigadevdayDistributed systems at ok.ru #rigadevday
Distributed systems at ok.ru #rigadevday
 
The Ring programming language version 1.7 book - Part 52 of 196
The Ring programming language version 1.7 book - Part 52 of 196The Ring programming language version 1.7 book - Part 52 of 196
The Ring programming language version 1.7 book - Part 52 of 196
 
Kernel Pool
Kernel PoolKernel Pool
Kernel Pool
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
hbaseconasia2019 OpenTSDB at Xiaomi
hbaseconasia2019 OpenTSDB at Xiaomihbaseconasia2019 OpenTSDB at Xiaomi
hbaseconasia2019 OpenTSDB at Xiaomi
 
Threads
ThreadsThreads
Threads
 
The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184The Ring programming language version 1.5.3 book - Part 26 of 184
The Ring programming language version 1.5.3 book - Part 26 of 184
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4
 
groovy databases
groovy databasesgroovy databases
groovy databases
 

Similar to Threads Advance in System Administration with Linux

Tutorial4 Threads
Tutorial4  ThreadsTutorial4  Threads
Tutorial4 Threadstech2click
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsashukiller7
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programmingMohammed Farrag
 
Parallel program design
Parallel program designParallel program design
Parallel program designZongYing Lyu
 
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating systemHarrytoye2
 
ch 7 POSIX.pptx
ch 7 POSIX.pptxch 7 POSIX.pptx
ch 7 POSIX.pptxsibokac
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی Mohammad Reza Kamalifard
 
Contiki introduction II-from what to how
Contiki introduction II-from what to howContiki introduction II-from what to how
Contiki introduction II-from what to howDingxin Xu
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2Stanley Ho
 
OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3Peter Tröger
 
Generators & Decorators.pptx
Generators & Decorators.pptxGenerators & Decorators.pptx
Generators & Decorators.pptxIrfanShaik98
 

Similar to Threads Advance in System Administration with Linux (20)

P threads
P threadsP threads
P threads
 
Tutorial4 Threads
Tutorial4  ThreadsTutorial4  Threads
Tutorial4 Threads
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
PDCCLECTUREE.pptx
PDCCLECTUREE.pptxPDCCLECTUREE.pptx
PDCCLECTUREE.pptx
 
posix.pdf
posix.pdfposix.pdf
posix.pdf
 
Project Jugaad
Project JugaadProject Jugaad
Project Jugaad
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating system
 
ch 7 POSIX.pptx
ch 7 POSIX.pptxch 7 POSIX.pptx
ch 7 POSIX.pptx
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
Contiki introduction II-from what to how
Contiki introduction II-from what to howContiki introduction II-from what to how
Contiki introduction II-from what to how
 
1032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.21032 cs208 g operation system ip camera case share.v0.2
1032 cs208 g operation system ip camera case share.v0.2
 
OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3
 
Posix Threads
Posix ThreadsPosix Threads
Posix Threads
 
P4
P4P4
P4
 
Generators & Decorators.pptx
Generators & Decorators.pptxGenerators & Decorators.pptx
Generators & Decorators.pptx
 

More from Soumen Santra

Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Soumen Santra
 
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxPPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxSoumen Santra
 
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Soumen Santra
 
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Soumen Santra
 
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Soumen Santra
 
A Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologyA Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologySoumen Santra
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Soumen Santra
 
Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Soumen Santra
 
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Soumen Santra
 
Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Soumen Santra
 
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsPURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsSoumen Santra
 
Carrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CACarrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CASoumen Santra
 
RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)Soumen Santra
 
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION  SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION Soumen Santra
 
Threads Basic : Features, Types & Implementation
Threads Basic : Features, Types  & ImplementationThreads Basic : Features, Types  & Implementation
Threads Basic : Features, Types & ImplementationSoumen Santra
 
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...Soumen Santra
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationSoumen Santra
 

More from Soumen Santra (20)

Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
 
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptxPPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
 
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
 
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
 
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
A Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance TechnologyA Novel Real Time Home Automation System with Google Assistance Technology
A Novel Real Time Home Automation System with Google Assistance Technology
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 
Java Basic PART I
Java Basic PART IJava Basic PART I
Java Basic PART I
 
Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)Frequency Division Multiplexing Access (FDMA)
Frequency Division Multiplexing Access (FDMA)
 
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
 
Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)Code-Division Multiple Access (CDMA)
Code-Division Multiple Access (CDMA)
 
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : DetailsPURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
 
Carrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CACarrier-sense multiple access with collision avoidance CSMA/CA
Carrier-sense multiple access with collision avoidance CSMA/CA
 
RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)RFID (RADIO FREQUENCY IDENTIFICATION)
RFID (RADIO FREQUENCY IDENTIFICATION)
 
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION  SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
 
Threads Basic : Features, Types & Implementation
Threads Basic : Features, Types  & ImplementationThreads Basic : Features, Types  & Implementation
Threads Basic : Features, Types & Implementation
 
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
CLOUD COMPUTING : BASIC CONCEPT REGARDING LOAD BALANCING AND Virtual Machine ...
 
JavaScript with Syntax & Implementation
JavaScript with Syntax & ImplementationJavaScript with Syntax & Implementation
JavaScript with Syntax & Implementation
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 

Threads Advance in System Administration with Linux

  • 2. Process Descriptor Handling union thread_union { struct thread_info thread_info; unsigned long stack[2048]; }; Two data structures in 8KB. thread_info structure Kernel mode process stack
  • 3. Storing the thread_info Structure in the Process Kernel Stack
  • 4. Pid_hash Table and Chained Lists  PID is use to search a process descriptor.  Sequential search in the process list is inefficient.  The pid_hash array contains four hash tables and corresponding filed in the process descriptor. pid: PIDTYPE_PID. tgid: PIDTYPE_TGID (thread group leader). pgrp: PIDTYPE_PGID (group leader). session: PIDTYPE_SID (session leader).  Chaining is used to handle PID collisions.
  • 5. pid_hash Table  Size of each pidhash table: dependent on the available memory.  PID is transformed into table index using pid_hashfn macro.
  • 6. Example of PID Hash Table and Chained Lists
  • 7. Continue  pids field of the process descriptor: the pid data structures: nr: PID number. pid_chain: links to the previous and the next elements in the hash chain list. pid_list: head of the per-PID list (in thread group).
  • 9. PID Hash Table Handling Functions and Macros  do_each_trask_pid(nr, type, task) : Execute each task.  while_each_trask_pid(nr, type, task) : Execute each task.  find_trask_by_pid_type(type, nr) : Find task by type & id.  find_trask_by_pid(nr) : Find task by id.  attach_pid(task, type, nr) : Add process on task.  detach_pid(task, type) Delete process from task.  next_thread(task) Show next Thread of a task.
  • 10. How Processes are Organized  Processes in TASK_STOPPED, EXIT_ZOMBIE, EXIT_DEAD: not linked in lists.  Processes in TASK_INTERRUPTABLE, TASK_UNINTERRUPTABLE: wait queues  Two kinds of sleeping processes are: Exclusive process. Non-exclusive process: always woken up by the kernel when the event occurs.
  • 11. Programs on Wait Queues  struct wait_queue_head { spinlock_t lock; struct list_head task_list; }; typedef struct _ _wait_queue_head wait_queue_head_t;  struct wait_queue { unsigned int flags; struct task_struct * task; wait_queue_func_t func; struct list_head task_list; }; typedef struct wait_queue wait_queue_t;
  • 12. Handling Wait Queues  Wait queue handling functions:  add_wait_queue()  add_wait_queue_exclusive()  remove_wait_queue()  wait_queue_active()  DECLARE_WAIT_QUEUE_HEAD(name)  init_waitqueue_head()  To wait:  sleep_on()  interruptible_sleep_on()  sleep_on_timeout(), interruptible_sleep_on_timeout()  Prepare_to_wait(), prepare_to_wait_exclusive(), finish_wait()  Macros: wait_event, wait_event_interruptible
  • 13. Handling Wait Queues  To be woken up: wake_up, wake_up_nr, wake_up_all  wake_up_sync, wake_up_sync_nr  wake_up_interruptible, wake_up_interruptible_nr  wake_up_interruptible_all  wake_up_interruptible_sync  wake_up_interruptible_sync_nr
  • 14. Process Resource Limits  RLIMIT_AS  RLIMIT_CORE  RLIMIT_CPU  RLIMIT_DATA  RLIMIT_FSIZE  RLIMIT_LOCKS  RLIMIT_MEMLOCK  RLIMIT_MSGQUEUE  RLIMIT_NOFILE  RLIMIT_NPROC  RLIMIT_SIGPENDING  RLIMIT_STACK
  • 15. Process Switch  Process switch, task switch, context switch. Hardware context switch: far jump (in older Linux). Software context switch: a sequence of mov instructions. It allows better control over the validity of data being loaded. The amount of time required is about the same.  Performing the Process Switch Switching the Page Global Directory. Switching the Kernel Mode stack and the hardware context.
  • 16. Task State Segment It is a specific segment architecture to store hardware contexts with type in x86.
  • 17. Creating Processes  In traditional UNIX, resources owned by parent process are duplicated. Very slow and inefficient.  Mechanisms to solve this problem: Copy on Write: parent and child read the same physical pages. Lightweight process: parent and child share per- process kernel data structures. vfork() system call: parent and child share the memory address space.
  • 18. System Calls  clone(fn, arg, flags, child_stack,ptid, ctid): creating lightweight process A wrapper function in C library Uses clone() system call  fork() and vfork() system calls: implemented by clone() with different parameters.  Each invokes do_fork() function.
  • 19. Kernel Threads  They run only in kernel mode.  They use only linear addresses greater than PAGE_OFFSET.  kernel_thread(): To create a kernel thread.  Example kernel threads: Process 0 (swapper process), the ancestor of all processes. Process 1 (init process). Others: keventd, kswapd, kflushd (also bdflush), kupdated etc.
  • 20. Destroying Processes  exit() library function Two system calls in Linux 2.6  _exit() system call :Handled by do_exit() function.  exit_group() system call : Handled by do_group_exit() function.  Process removal Releasing the process descriptor of a zombie process by release_task().
  • 23. Pthread Operations POSIX function Description pthread_create create a thread pthread_detach set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_kill send a signal to a thread pthread_join wait for a thread pthread_self find out own thread ID
  • 24. Thread Packages  Kernel thread packages Implemented and supported at kernel level  User-level thread packages Implemented at user level
  • 25. POSIX threads on GNU/Linux  GNU/Linux, threads are implemented as processes.  pthread_create to create a new thread.  Linux creates a new process that runs that thread.  A POSIX thread is not the same as a process you would create with fork.  It shares the same address space and resources as the original process rather than receiving copies.  Each thread maps to a kernel scheduling entity.
  • 26. POSIX threads on GNU/Linux Continue  The Linux clone system call is a generalized form of fork.  pthread_create that allows the caller to specify which resources are shared between the calling process and the newly created process.  Clone system call should not ordinarily be used in programs.  Use fork to create new processes or pthread_create to create threads.
  • 27. pthread_t identifier  Process created ptherad_t and it is not visible outside.  For instance, send a pthread_kill to a thread of another process.  More details for Linux:  pthread_self() will get you an identifier that is unique across your program, but not across your system.  Thread is a system object.  The system is unaware of the identifier POSIX library allocated for the thread.  Linux identifies threads with PID like number called TID.  These numbers are system-wide.
  • 28. Example Program #include <pthread.h> #include <stdio.h> void *threadex(void *); int main() { pthread_t tid; /* stores the new thread ID */ pthread_create(&tid, NULL, threadex, NULL); /*creates a new thread*/ pthread_join(tid, NULL); /*main thread waits for termination new thread */ return 0; /* exits main thread */ } void *threadex(void *arg) /*thread subroutine*/ { int t; for (t=0; t<10; t++) fprintf(stderr, "Hello, world of Threads ! n "); return NULL; }
  • 29. Creating a thread with pthread  A thread is created with int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);  The creating process (or thread) must provide a location for storage of the thread id.  The third parameter is just the name of the function for the thread to run.  The last parameter (void *arg) is the sole argument passed to created thread.
  • 30. Example 1: Thread Creation #include <pthread.h> #include <stdio.h> #define NUM_THREADS 10 void *PrintHelloThread (void *threadid) { int tid; tid = (int)threadid; printf("Hello World of threads : #%d!n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int count, i; for(i=0; i<NUM_THREADS; i++){ printf("In main: creating thread %dn", i); count = pthread_create(&threads[i], NULL, PrintHelloThread, (void *)i); if (count) { printf("ERROR code is %dn", count); exit(1); } } pthread_exit(NULL); }
  • 31. Example 2: Passing Parameters to a Thread #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 10 void *PrintHelloThread (void *ptr) { char *filename; int t; filename = (char *) ptr; while (1) { printf("Hello World of threads : %s!n", filename); for (t=1; t++; t<500); } pthread_exit(NULL); }
  • 32. Example 2: Passing Parameters to a Thread (Continue) int main (int argc, char *argv[]) { pthread_t thread[10]; int err_code, count=0; char *filename; printf ("Enter thread name at any time to create threadn"); while (count <= 9) { filename = (char *) malloc (80*sizeof(char)); scanf ("%s", filename); printf(“From main- creating threads %dn", count); err_code = pthread_create(&thread[count], NULL, PrintHelloThread, (void *)filename); if (err_code){ printf("ERROR code is %dn", err_code); exit(1); } count++; } pthread_exit(NULL); }
  • 33. THANK YOU Give Feedback Please Go Through Thread Basic