Contiki-An example of a process

Source: Internet
Author: User

Process Scheduler

The role of the process scheduler is to invoke processes. The process scheduler invokes a process by calling a function that implements the process thread. All processes in the Contiki are designed to respond to events passed into the process, or to the polling requested by the appropriate process. The process Scheduler passes the event identifier and an opaque pointer to the process when it dispatches the process, which is provided by the process caller and can be set to null (the event does not need to pass data). When the process polls, no data is passed.

Start process

1 /**2 * Start a process.3  *4 * \param p a pointer to A process structure.5  *6 * \param arg an argument pointer the can is passed to the new7 * Process8  *9  */TenCcifvoidProcess_start (structProcess *p,Const Char*ARG);
1 void2Process_start (structProcess *p,Const Char*Arg)3 {4   structProcess *Q;5 6   /*First make sure that we don ' t try to start a process that is7 already running.*/8    for(q = process_list; Q! = P && Q! = NULL; q = q->next);9 Ten   /*If We found the process on the process list, we bail out.*/ One   if(q = =p) { A     return; -   } -   /*Put on the procs list.*/ theP->next =process_list; -Process_list =p; -P->state =process_state_running; -Pt_init (&p->PT); +  -PRINTF ("process:starting '%s ' \ n", Process_name_string (p)); +  A   /*Post A synchronous initialization event to the process.*/ at Process_post_synch (P, Process_event_init, (process_data_t) arg); -}

Process_start () begins a process. The purpose of this function is to set up the process control block, add the process to the kernel active process list, and then invoke the initialization code in the process thread. Finally, a synchronous initialization event is sent to the process. After the Process_start () is called, the process begins to run.

The Process_start () function first makes a sensible check to see if the process already exists in the process chain list. If yes, indicates that the process has been run, the Process_start () function returns directly. After confirming that the process has not started running, the kernel joins the process to the list and sets the process control block. The state of the process is set to process_state_running, and the thread of the process is initialized by Pt_init (). Finally, the kernel sends a process to a synchronous event process_event_init, and passes an opaque pointer to the process, which is passed in by the process that calls Process_start () and is passed to some information about the process to run. However, this pointer is generally set to null. When a process receives its first event process_event_init, the process executes the first part of the process thread. Typically, this section contains the initialization code that will run at the beginning of the process.

Exit process and kill process

There are two ways to exit a process: The process exits automatically and is killed by other processes. The process exits when the function process_exit () is called or when the process thread executes to the Process_end () statement.

A process can call the function process_exit () to kill another process.

When a process exits (whether it is an active exit or a passive exit), the Contiki kernel sends an event to notify the other process, which allows other processes to release the resources that the exit process occupies. The event process_event_exited will be sent to all other processes in the same way as synchronous events.

When one is killed by another Stephen, the killed process will receive a synchronous event process_event_exit that notifies the process that will be killed and the process that can release the allocated resources, or any other process that will be exited.

The process is removed from the process list after the Contiki kernel sends an event notification that the process will be exited.

Self-starting process

Contiki provides a mechanism to automatically run processes when the system starts up, or when the module containing the process is loaded. This mechanism is implemented by the self-starter module. This mechanism allows the module developer to tell the system what process is included in the module. When the module is removed from memory, the system can also be processed accordingly.

Since the startup process is saved in a linked list, the self-starter module uses the linked list to automate the process. The order in which the process starts is consistent with the order in which it is in the list.

There are two kinds of timing for starting a process: When the system starts and when the module is loaded. All processes that need to be self-initiated at system startup must be included in a single system-level linked list. This self-starting list is provided by the user and is typically in a user module. When this module is used as a loadable module, the list allows the system to know what process needs to be run when the module is loaded.

When a module is loaded, the module loader looks for a list of self-initiated processes and initiates a process in the list after the module is loaded into memory. When the module is about to be unloaded, the module loader uses the list to kill the process that was started when the module was loaded.

An example of a process that receives an event and prints its number :

1#include"contiki.h"  2    3PROCESS (Example_process,"Example Process"); 4Autostart_processes (&example_process); 5    6 process_thread (example_process, Ev, data)7  {  8 Process_begin (); 9    Ten     while(1) {   One process_wait_event ();  Aprintf"Got Event Number%d\n", Ev);  -    }   -     the Process_end ();  -}

The above code is a Contiki complete process. The process is declared, defined, and run automatically.

In the third line, we define the process control block. The process Control block defines the name of the process Control block example_process and the text, the user-readable process name, example processes. After defining the process control block, we can use the variable name in other expressions.

Line four, the statement autostart_processes () tells Contiki to start the process example_process automatically when the system starts. The self-starting list consists of pointers to the process control block, so you need to add & address characters before the variable example_process.

Line six, we define the process thread. It contains the variable name of the process example_process and the variable EV and data that pass the event.

Line eighth, we used Process_begin () to define the beginning of a process. The definition flags the start of the process thread. The code above the declaration statement runs every time the process is scheduled to run. In most cases, you don't need to put any code on top of Process_begin ().

Line tenth, start the main loop of the process. The Contiki process cannot contain a dead loop that never ends. But in the code above, such a dead loop is safe because the process will wait for time. When a Contiki process waits for an event, it returns the controller to the Contiki kernel. During the process wait, the kernel will serve other processes.

Line 11th, the process waits for the event to occur. The expression process_wait_event () returns control to the Contiki kernel and waits for the kernel to pass events to the process. After the Contiki kernel passes events to the process, the code behind Process_wait_event () is executed. After the process is awakened, the print statement for Line 12th is executed. The purpose of this line is to print the event number that the process receives. If you pass in a pointer at the same time, the pointer variable is the data (? )。

Line 15th, Process_end () identifies the end of the process. Each Contiki process must contain Process_begin () and Process_end (). When executed to Process_end (), the process exits automatically and is removed from the process chain list in the kernel. However, due to the existence of a dead loop, it is never performed to Process_end (). Only when the system is switched off or the process is killed by Process_exit () will it be stopped.

A function that initiates a process and sends an event

1 Static CharMsg[] ="Data"; 2   3 Static void  4Example_function (void)  5 {  6   /*Start "Example process", and send it a NULL7 pointer.*/  8   9Process_start (&example_process, NULL); Ten     One   /*Send the process_event_msg EVENT synchronously to A "Example process", with a pointer to the message in the - array ' msg '.*/   -Process_post_synch (&example_process, the process_event_continue, msg);  -     -   /*Send The process_event_msg EVENT asynchronously to - "Example process", with a pointer to the message in the + array ' msg '.*/   -Process_post (&example_process, + process_event_continue, msg);  A    at   /*Poll "Example process".*/   -Process_poll (&example_process);  -}

Two processes through events to complete an interaction. The function in the example above initiates a process and sends a synchronous event and polling request to it.

Summarize

The process communicates with other processes by sending an event implementation.

Contiki-An example of a process

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.