Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 2 min read

Support

A little known fact is that most C implementations have built-in primitives that can be used for cooperative multitasking / coroutines. They are setcontext and setjmp.

setjmp

The function setjmp is used in a pair with longjmp to transfer execution to a different point in the code. It relies on an existing jmp_buf declaration.

#include

int main (void)
{
jmp_buf buf;

if (setjmp(buf) == 0)
{
/* This code is executed on the first call to setjmp. */
longjmp(buf, 1);
} else {
/* This code is executed once longjmp is called. */
}
return 0;
}

setjmp() stores the current execution point in memory, which remains valid as long as the containing function doesn't return. It initially returns 0. Control is returned to setjmp once longjmp is called with the original jmp_buf and the replacement return value.

Note that jmp_buf is passed to setjmp without using the address-of operator.

The easiest way to understand setjmp and longjmp, is that setjmp stores the state of the CPU which includes program counter, stack pointer, all the registers, including the bits of the flags register, at the location pointed to by jmp_buf, which is defined some LEN+1, which is enough bytes to store the registers of whatever CPU is involved. The longjmp(buf), never returns, because it restores the execution context from the contents of struct jmp_buf buf previously set by the call to setjmp, so execution continues after setjmp was called, but the return value of setjmp is not 0, but with the value passed as the second parameter to longjmp. This is somewhat similar to the fork() system call, which returns 0 to the child process, and the PID of the child process to the parent.

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

C programming

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in C programming

CC BY-SA 4.0
C is an efficient, minimalist language that has some peculiarities that a programmer must be aware of. To address these, sometimes a good solution is to combine another language with C for added flexibility and power, like the combination of Emacs-LISP and C used for Emacs. Sometimes they can be add
CC BY-SA 4.0
Typically, their FILE-based definitions in stdio.h, as covered in an earlier section, are used to interact with them. These streams can also be interacted with through their raw file descriptors: | unistd.h symbol | stream | File descriptor | | --- | --- | --- | | STDIN_FILENO | stdin | 0 | | STDOUT
CC BY-SA 4.0
We can create the necessary subdirectory structure by running "h2xs" application (e.g. "h2xs -A -n Modulename"). This will create - among others - a Makefile.PL, a .pm Perl module and a .xs XSUB file in the subdirectory tree. We can edit the .xs file by adding our code to that, let's say: void hello
CC BY-SA 4.0
Since the C Programming-Language was not created with Object Oriented Programming in mind, it has no explicit support for classes, inheritance, polymorphism and other OO Concepts. Neither does it have its own Virtual Table, which is found in object-oriented languages such [C++]( [Java]( and [C#]( Th
CC BY-SA 4.0
A **statement** is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements. In C, a statement can be any of the following: ## Labeled statements A statement can be
CC BY-SA 4.0
GCC, Clang) to emit a diagnostic message: #define EMIT_MESSAGE(str) EMIT_PRAGMA(message(str)) #define EMIT_PRAGMA(content) _Pragma(#content) EMIT_MESSAGE("Hello, world!") Some compilers use a slight variant syntax; in particular, MSVC supports **__pragma** instead of **_Pragma**. Specific compilers