Download Understanding Linux Threads and Processes: POSIX Threads, clone(), and Linux structures and more Slides Advanced Operating Systems in PDF only on Docsity!
Overview of today’s lecture
- POSIX threads (pthreads) standard interface
and calls
- Simple pthreads Hello World program
- Linux processes and threads
- The clone() system call
- Fields in task_struct in Linux
- Process/threads states and FSM in Linux
- Looking ahead into the next lecture
- Re-cap of lecture
The Pthreads "hello, world" Program
/*
*** hello.c - Pthreads "hello, world" program /*
#include "csapp.h"
**void thread(void vargp);
int main() {
pthread_t tid;
Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0);
}
/ thread routine /
**void thread(void vargp) {
printf("Hello, world!\n"); return NULL;
}
Thread attributes (usually NULL)
*Thread arguments (void *p)*
**return value (void **p)**
Execution of Threaded“hello, world”
main thread
peer thread
main thread waits for return NULL;
peer thread to terminate
exit()
terminates
main thread and
any peer threads
call Pthread_create()
call Pthread_join()
Pthread_join() returns
printf()
(peer thread
terminates)
Pthread_create() returns
Main Flags for Linux clone
- CLONE_CLEARID Clear the task ID.
- CLONE_DETACHED The parent does not want a SIGCHLD signal sent on exit.
- CLONE_FILES Shares the table that identifies the open files.
- CLONE_FS Shares the table that identifies the root directory and the current working directory, as well as the value of the bit mask used to mask the initial file permissions of a new file.
- CLONE_IDLETASK Set PID to zero, which refers to an idle task. The idle task is employed when all available tasks are blocked waiting for resources.
- CLONE_PARENT Caller and new task share the same parent process.
- CLONE_PTRACE If the parent process is being traced, the child process will also be traced.
- CLONE_SIGHAND Shares the table that identifies the signal handlers.
- CLONE_THREAD Inserts this process into the same thread group of the parent. If this flag is true, it implicitly enforces CLONE_PARENT.
- CLONE_VFORK If set, the parent does not get scheduled for execution until the child invokes the execve() system call.
- CLONE_VM Shares the address space (memory descriptor and all page tables).
clone()
- fork() is implemented as a wrapper around
clone() with specific parameters
- __clone(fp, data, flags, stack)
- "__" means "don’t call this directly"
- fp is thread start function, data is params
- flags is of CLONE_ flags
- stack is address of user stack
- clone() calls do_fork() to do the work
Linux processes
- In Linux terminology, processes are called tasks.
- Linux has a list of process descriptors
- (which are of type task_struct defined in
include/linux/sched.h in Linux source tree).
- The maximum number of threads/processes allowed is
dependent upon the amount of memory in the system.
- Check /proc/sys/kernel/threads_max for the current
limit. By writing to that file, the limit can be changed on
the fly (by the superuser).
Linux Process Identity
- Users: pid; Kernel: address of descriptor
- Pids dynamically allocated, reused
- 16 bits – 32767, avoid immediate reuse
- Pid to address hash
- static task_array
- Statically limited # tasks
- This limitation removed in 2.
- current->pid (macro)
Linux data structures for processes
- The process control blocks are called descriptors
- They are kept in circular linked lists
- The linked list implementation in the kernel uses the
following structure as a node:
struct list_head *next, *prev;
};
- The linked lists are circular, so there is no head or tail
node. You can start at any node and traverse the whole list.
Task struct contents
- Scheduling information: Information needed by Linux to schedule processes. A
process can be normal or real time and has a priority. Real-time processes are scheduled before normal processes, and within each category, relative priorities can be used. A counter keeps track of the amount of time a process is allowed to execute.
- Identifiers: Each process has a unique process identifier and also has user and group
identifiers. A group identifier is used to assign resource access privileges to a group of processes.
- Interprocess communication: Linux supports the IPC mechanisms found in UNIX
SVR
- Links: Each process includes a link to its parent process, links to its siblings (processes
with the same parent), and links to all of its children.
- Times and timers: Includes process creation time and the amount of processor time
so far consumed by the process. A process may also have associated one or more interval timers. A process defines an interval timer by means of a system call; as a result a signal is sent to the process when the timer expires. A timer may be single use or periodic.
See /include/linux/sched.h for process states in the Linux kernel