linux-sysprog · beginner · ~15 min

Correct flag type for a handler

Know the one correct type for a handler flag.

Challenge

A flag shared between a signal handler and the main code must be volatile sig_atomic_t: sig_atomic_t guarantees atomic access and volatile stops the compiler caching it. Validate the declared type.

Task

Implement int flag_type_ok(const char *type) that returns 1 only if type is exactly the correct handler-flag type.

Input

  • type: a C type spelled as a string.

Output

1 only for "volatile sig_atomic_t"; 0 for anything else (e.g. "int", "volatile int").

Example

flag_type_ok("volatile sig_atomic_t")   ->   1
flag_type_ok("int")                     ->   0
flag_type_ok("volatile int")            ->   0

Input format

type: a C type spelled as a string.

Output format

1 only for "volatile sig_atomic_t", else 0.

Constraints

Exact string match required.

Starter code

#include <string.h>

int flag_type_ok(const char *type) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.