Linux system programming, the main use of C language, C ++ is a superset of C, but also possible

The full case code has been uploaded to github: github.com/neatlife/my…

Obtaining the Available Environment

You can use a virtual machine to install a Linux operating system for Linux development, although the MAC OS is very similar to Linux, but there are many differences with Linux, installing a virtual machine is the most convenient. elementary.io/zh_CN/

Install Linux 4.x or later

The interface effect is as follows:

C language standard library

The standard library contains the following libraries

  • assert.h
  • ctype.h
  • errno.h
  • float.h
  • limits.h
  • locale.h
  • math.h
  • setjmp.h
  • signal.h
  • stdarg.h
  • stddef.h
  • stdio.h
  • stdlib.h
  • string.h
  • time.h

Threading library pthread. H default support Linux, Windows need additional installation, reference: sourceforge.net/projects/pt…

Process creation and destruction (fork, wait) are in the unistd.h library, which supports Linux by default

An example – replacement signal processing

Pressing Ctrl + C on a running program interrupts the program by default and causes it to exit

#include <stdio.h>
#include <signal.h>

int
main(int argc, char *argv[])
{
    printf("Hello World!");
    sleep(300);
}
Copy the code

Results the following

You can see that the program exits immediately when CTRL + C is pressed, because the shell terminal sends SIGINT to the program when CTRL + C is pressed. The default SIGINT handler terminates the program. Instead of the default behavior, you can register a new SIGINT handler using the signal function in the signal.h library, such as simply printing a string upon receiving a SIGINT

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

static void
mySigHandler(int sig)
{
    printf("in signal handler\n");
}

int
main(int argc, char *argv[])
{
    if (signal(SIGINT, mySigHandler) == SIG_ERR) {
        printf("Replacement signal processor exception \n");
        exit(1);
    }

    printf("Hello World! \n");

    while (1) {
        sleep(300); }}Copy the code

Results the following

You can also use the kill command to send a SIGINT signal to a program. SIGINT is defined in the signum-generic.h header

A couple of points to note

When SIGINT is sent to the program, it wakes up from its sleep state and can keep the program running through an infinite loop

while (1) {
    sleep(300);
}
Copy the code

Disable the energy saving option for the VM to prevent the VM from entering the sleep or exit state frequently

gsettings reset org.gnome.desktop.interface monospace-font-name
gsettings set org.gnome.desktop.interface cursor-blink false
gsettings set io.elementary.files.preferences single-click false
Copy the code

SIGINT signal has been replaced, CTRL + C can not terminate the program, you can use SIGKILL signal (code 9) terminate namely kill -9 PID, SIGKILL signal processing function is not replaced, rest easy to use.

You can choose Ubuntu as your development environment so that there are fewer environmental issues

The resources

  1. Blog.csdn.net/u012422855/…