Analysis and program design based on the object-oriented operating system development platform (OSKit) (2)
Source: Internet
Author: User
Article title: analysis and program design based on the object-oriented operating system development platform (OSKit) (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: Tang Haijing
Thread initialization in thread analysis
This article is the second article on analysis and program design based on the object-oriented operating system development platform (OSKIT). The author will introduce thread initialization for us.
As we have mentioned earlier, in OSKit, the unit of resource allocation and scheduling is a thread rather than a process in the traditional sense. Therefore, it is necessary for us to spend a lot of time analyzing its thread mechanism comprehensively. Of course, to analyze the thread part, we should first start with thread initialization before it involves inter-thread communication and thread scheduling. Therefore, this chapter will give you a comprehensive and systematic introduction to OSKit thread initialization.
2.1 thread initialization analysis
2.1.1 thread creation
In OSKit, the only way to create a new thread is to call the pthread_create thread of the created thread.
The kernel calls pthread_create for the system to complete the following operations:
Allocate an empty slot to the new thread in the thread table.
Assign a unique ID to the subthread.
Make a logical copy of the context of the parent thread. Because some parts of a thread, such as the body area, may be shared by several threads, the kernel only needs to increase the reference count of a certain area, instead of copying the region to a new physical memory zone.
Increase the number of file tables and index node tables related to this thread.
Returns the thread number of the subthread to the parent thread.
The system has a limit on the number of threads that a user can run simultaneously (this limit can be changed). therefore, no user can use too many thread table items. otherwise, this will prevent other users from creating new threads. in order to prevent deadlocks, OSKit specifies that common users cannot occupy the last thread in the thread table. this rule evolved from UNIX.
2.1.2 Thread storage
The OSKit thread storage solution is very similar to UNIX and consists of three logical segments: the body segment, data segment, and stack. The body segment contains a set of commands executed by a thread. the address of the body segment includes the body address (used for branch and subroutine calls) and the data address (used to access global data variables) and stack address (To access the data structure of the subroutine ).
If the machine regards the generated address as the address in the physical memory, it is impossible for two threads to execute concurrently when the address set they have generated overlaps. Although the compiler can generate a non-overlapping address between programs, such a program does not work for general-purpose computers, because the storage capacity on one machine is limited, all programs that may be compiled are infinite. This method can avoid data loss to a large extent.
2.1.3 Thread data structure
The following lists the complete data structure of threads in OSKit, most of which are inherited from UNIX, but some of which are OSKit's own, for example, to make the CPU resource allocation part provided by the example program.
To make it clear to you, I have made all comments on the data structure and divided them into several parts according to their different application fields, which are added to the annotations below.
Queue_chain_t runq;/* release the queue chain */
Pcb_t * ppcb;/* pointer to PCB */
Oskit_u32_t * pstk;/* pointer to the allocated stack */
Void * (* func) (void *);/* pointer to the execution function */
Size_t ssize;/* size of the allocated stack */
Size_t guardsize;/* alert stack size */
Pthread_t tid;/* thread number */
Oskit_u32_t flags;/* indicates the thread status */
Pthread_lock_t lock;/* lock */
Queue_chain_t chain;/* queue chain */
Int preempt;/* thread priority */
/* The following is used to handle deadlocks */
Pthread_mutex_t mutex;/* mutex (deadlock protection )*/
Pthread_cond_t cond;/* wait for the deadlock sign = 1 */
Int dead;/* deadlock */
/* Resources used in the example */
Int cputime;/* CPU usage time */
Int cpticks;/* The number of beats occupied by the previous second */
Oskit_u32_t pctcpu;/* CPU usage */
Int childtime;/* CPU overhead occupied by sub-threads */
/* Variables used to send information */
Pthread_lock_t waitlock;/* wait lock */
Oskit_u32_t waitflags;/* wait for flag Space */
Pthread_cond_t * waitcond;/* wait condition variable */
Struct osenv_sleeprec * sleeprec;/* a thread in osenv_sleep */
/* The following are the processes used for communication */
Void * msg;/* pointer to the message to be sent */
Oskit_size_t msg_size;/* Message Size */
Pthread_t tid;/* thread number */
Void * reply;/* pointer to the response information */
Oskit_size_t reply_size;/* response content size */
Queue_head_t senders;/* sender in the sending queue */
Queue_chain_t senders_chain;/* send queue pointer */
/* The following is the clock used */
Struct oskit_timer * condtimer;
/* The sleep and wake-up of a thread are implemented using a separate timer */
Struct oskit_timer * sleeptimer;
/* The following are the signals used */
Pthread_lock_t siglock;/* protection lock signal */
Sigset_t sigmask;/* blocking signal */
Sigset_t sigpending;/* pending signal */
Sigset_t sigwaiting;/* signal of the waiting signal */
Oskit_u32_t eip;/* handle fault signal page */
/* The key value is usually a fixed queue */
Void * keyvalues [PTHREAD_KEYS_MAX];/* key value */
/* The following is the cleanup operation */
Pthread_cleanup_t * cleanups;/* clear operation chain */
Char cancelstate;/* cancel state */
Char canceltype;/* cancel type */
/* The following is a separate lock for scheduling */
Pthread_lock_t schedlock;
Void * rtai_priv;
Int policy;/* scheduling policy */
Int priority;/* Current priority */
Struct scheduler_entry * scheduler;/* scheduler entry */
Int policy;/* scheduling policy */
Int priority;/* Current priority */
Int base_priority;/* initial priority */
Int ticks;/* The cycle saved by scheduling */
Oskit_timespec_t start;/* next running time */
Oskit_timespec_t deadline;/* next running time */
Oskit_timespec_t period;/* interval between two executions */
Queue_head_t waiters;/* the waiting thread */
Queue_chain_t waiters_chain;/* thread queue chain */
Struct pthread_thread * waiting_for;/* the waiting thread */
Struct pthread_thread * inherits_from;/* where the thread inherits */
/* The following is the inheritance of CPU */
Struct pthread_thread * schedct;/* thread scheduler population */
Schedmsg_t unblockmsg;
Schedmsg_queue_t * msgqueue;
Sched_wakecond_t wakeup_cond;/* wake-up condition */
Schedflags_t schedflags;/* Flag */
Int donate_rc;/* value returned from the donation thread */
Int timeout;/* millisecond */
Queue_head_t donors;/* resource donation thread */
Queue_chain_t donors_chain;/* donation queue chain */
Struct pthread_thread * donating_to;/* donated thread */
Struct pthread_thread * inherits_from;/* where the thread inherits */
Struct pthread_thread * nextup;/* next thread to be executed */
2.2 pthreads/pthread_create.c
This source code file includes a complete thread creation mechanism, which is the root of all threads. by reading the following function analysis, it will help you understand what functions OSKit uses to create threads. I think the analysis of code under the guidance of the theory in the previous section is easier for readers to accept than to elaborate on the general theory, and better impressed the readers.
2.2.1 create thread
Note: This is the thread creation function we mentioned in section 1. all user-completed thread creation operations must be implemented by calling it.
Tid: pointer to the thread storage location
Attr: pointer to the thread attribute
* (* Function) (void *): The function called when the thread is initialized.
* Argument: function
2.2.2 create an internal thread
Note: This function is used to create core threads in the core part of the system. general users do not have the right to call it. only the core of the system can be called.
Note: When an internal thread is created, all signal sigfillset (& pthread-> sigmask) will be blocked)
2.2.3 create a backup thread for the master process
Note: for system security reasons, OSKit defines this function to back up the main thread. Because the thread creates a subthread and the subthread creates its own subthread, a tree structure with the main thread as the root node will be generated, so once the main thread is lost, the system may crash, so you should back up the system.
2.2.4 initialize the thread for thread creation
Note: This is somewhat similar to the process used to create a process in UNIX. its only job is to create a subthread for the main thread and each thread contains a subthread.
2.2.5 create a waiting time for the waiting and sleep threads
Note: Due to scheduling, it is generally impossible for a thread to occupy the CPU all the time or to be suspended forever,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.