10. SP - Threads
10. SP - Threads
Threads
shchoi@kw.ac.kr
• Process
– a program in execution
– program image + environment
➢ text(code) /data / stack / heap segment
➢ kernel data structure, address space, open files, ...
Process Review
/* test_program.c */
#include <stdio.h>
int a,b;
int glob_var = 3;
void test_func(void) {
int local_var, *buf;
a = i + 1;
printf(“value of a = %d\n”, a); $ gcc –o test_program test_program.c
test_func();
}
Process Review
• $ ./test_program
Program Process
PCB
text
text
data Open files,
data
bss Terminal,
bss …
stack
…
heap
…
Disk
Memory
* bss: Block Started by Symbol
PCB: Process Control Block
Process Review
$ ps –e
PID TTY TIME CMD
1? 00:00:00 init
2? 00:00:00 migration/0
3? 00:00:00 ksoftirqd/0
4? 00:00:00 watchdog/0
5? 00:00:00 migration/1
6? 00:00:00 ksoftirqd/1
7? 00:00:00 watchdog/1
8? 00:00:00 migration/2
…
29122 pts/9 00:00:00 bash
29151 ? 00:00:00 sshd
29170 ? 00:00:00 sshd
29171 pts/10 00:00:00 bash
30465 ? 00:00:00 httpd
$
Process Review
process
Ready Queue
A B C A CPU
Process Review
• State of a process
Processes and Threads
• Processes
– Each process has a separate resources (separate
address memory space) to execute a program
– At least one thread of execution
➢can have multiple threads
– No protection between threads
• Threads
– An entity within a process that can be scheduled
for execution
– All threads of a process share the address space and
resources
Processes and Threads (cont.)
• Processes
– Process contains a single address space
– A separate address space of a process (protection)
• Threads
– A sequentially executing block within a process
(Sometimes called a lightweight process)
– No protection between threads
– A thread of a process (concurrency)
Threads concepts
• Thread
– An independent and schedulable execution unit
– A process can be divided into two or more running
threads
– A single thread of control(= a UNIX process)
➢Each process is doing only one thing at a time
– Multiple threads of control in a process
➢The process can do more than one thing at a time
– Multithreading is possible on even uni-processor
➢by time-division multiplexing
Threads concepts
• A typical example
– Apache web server
…
Server Client M
thread N
Threads concepts
• Advantages of thread
– Easy to share information
➢the memory address space and file descriptors
– More interactive
➢The separated threads can deal with user
input/output
Threads concepts
Code
Stack Stack Stack
Stack
Threads concepts
• A thread-specific information
– Thread ID
– Register values
– Stack
– Scheduling priority
– A signal mask
• What is pthread?
– IEEE POSIX 1003.1c standards
• Thread ID
– Identifier of thread (similar to process ID.)
– A thread ID is represented by pthread_t data type
➢Unsigned long integer in Linux
➢A pointer to the pthread structure in FreeBSD
Thread identification
#include <pthread.h>
pthread_t pthread_self(void);
Returns: the thread ID of the calling thread
#include <pthread.h>
#include <pthread.h>
• Example
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
Thread creation
• Example (cont.)
int main(void)
{
int err;
• 실행
In Solaris
When a thread is created, there is no guarantee which runs first.
$ ./a.out
main thread: pid 7225 tid 1 (0x1)
new thread: pid 7225 tid 4 (0x4)
$
In FreeBSD
$ ./a.out FreeBSD uses a pointer to the thread data structure for its thread ID.
main thread: pid 14954 tid 134529024 (0x804c000)
new thread: pid 14954 tid 134530048 (0x804c400)
$
In Linux
$ ./a.out
main thread: pid 17874 tid 140693894424320 (0x7ff5d9996700)
new thread: pid 17874 tid 140693886129920 (0x7ff5d91ad700)
$
Thread termination
#include <pthread.h>
#include <pthread.h>
• Example
#include "apue.h"
#include <pthread.h>
• Example (cont.)
int main(void)
{
int err;
pthread_t tid1, tid2;
void *tret;
• Example (cont.)
exit(0);
}
Thread termination
• 실행
$ ./a.out
thread 1 returning
thread 2 exiting
thread 1 exit code 1
thread 2 exit code 2
$
Thread termination
#include <pthread.h>
• A synchronization example
– Two threads try to modify the same variable
simultaneously
➢i is initialized to 5
➢Two threads perform “i++;”, respectively
Thread A Thread B
i++; i++;
• A synchronization example
Mutex
• Mutex
– It is a lock that we set (lock) before accessing a
shared resource and release (unlock) when we’re
done
– While it is set, any other thread that tries to set it
will block until it is released
– If more than one thread is blocked when the mutex
is unlocked, then only one will be able to set the
lock
Mutex
#include <pthread.h>
• Initialize a mutex
– initialize the mutex referenced by mutex with
attributes specified by attr
– attr is generally set to NULL
• Static initialization
– pthread_mutex_t mutex =
PTHREAD_MUTEX_INITIALIZER;
Mutex
#include <pthread.h>
• Destroy a mutex
– destroy the mutex object referenced by mutex
Mutex
#include <pthread.h>
• Example (mutex_test.c)
#include <pthread.h>
#define NLOOP 50
exit(0);
}
Mutex
• Example
• 실행
[kwangwoon@kw test]$ gcc –o mutex_test –lpthread mutex_test.c
[kwangwoon@kw test]$ ./mutex_test
1093278016: 1
1103767872: 2
1093278016: 3
1103767872: 4
1093278016: 5
1103767872: 6
1093278016: 7
1103767872: 8
1093278016: 9
1103767872: 10
1093278016: 11
…
1093278016: 99
1103767872: 100
[kwangwoon@kw test]$