[Linux] comments on a small program of the Ucontext library @ C language

Source: Internet
Author: User

In Wikipedia https://en.wikipedia.org/wiki/Setcontext there is the following procedure, understand this applet, basically to ucontext this execution context switch of the library can also understand a similar.

 #include <stdio.h> #include  <ucontext.h> #include  <unistd.h>int  Main (int  argc, const  char  *argv[]) {ucontext_t context;    GetContext ( &context); Puts (  hello World   " );    Sleep ( 1  );    SetContext ( &context);  return  0  ;}  

Let's look at the logic of this program. GetContext (&context); This statement is to place the current context into the variable ucontext_t context. and SetContext (&context); This statement is interpreted in English as: This function transfers control to the context in ucontext_t context. Execution continues from the point at which the context is stored in ucontext_t context. Chinese translation here means: the current function (that is, the main function The transfer is controlled to the context stored in the contextual variable, and execution continues from the store context to the point of the context variable. So the result of this app is: Infinite loop print Hello World, do you understand? To understand that basically Ucontext also understand a lot of.

[Linux] comments on a small program of the Ucontext library @ C language

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.