Knowledge about Linux (Atexit (), fork (), sticky bit)

Source: Internet
Author: User

1.atexit () function

Function Name: atexit

Header files: #include <stdlib.h>

Function: register termination function (that is, function called after main execution ends)

Usage: int atexit (void (*func) (void));

Note: A process can register 32 functions, which are called automatically by exit, and these functions are called termination handlers, and the Atexit function can enlist these functions. Exit calls the order of termination handlers in the reverse order of atexit registrations, and is called multiple times if a function is enlisted more than once.

program Example:

#include <stdio.h>

#include <stdlib.h>

void Exit1 ()

{

printf ("Exit1 called\n");

}

void Exit2 ()

{

printf ("Exit2 called\n");

} int main (void)

{

Atexit (EXIT1);

Atexit (EXIT2);

return 0;

}

Output:

Exit2 called

Exit1 called

The result shows that the order of the registered functions is the opposite of the order of the calling function

How the process terminates:

There are 8 ways to terminate the process, with the first 5 terminating normally, which are

  1: Return from main

  2: Call exit

  3: Call _exit or _exit

4: The last thread returned from its startup routine

5: Last thread called Pthread_exit

There are 3 kinds of abnormal termination, they are

6: Call Abort

7: Received a signal and terminated

8: Last thread responds to cancellation request

2, Sticky bit

The Sticky bit is an access flag bit that can be used to mark files and paths. The most common usage is to set the sticky bit on the directory, so that only the owner or root of the file in the directory can delete or move the file. If you do not set a sticky bit for the directory, any user with the write and execute permission for that directory can delete and move the files in it. In practical applications, sticky bits are generally used in the/TMP directory to prevent ordinary users from deleting or moving other users ' files.

The sticky bits of the normal file are ignored by the Linux kernel,
The sticky bit of the directory indicates that files in this directory can only be deleted by owner and Root

For example: add sticky bits to path/usr/local/tmp,

1. chmod +t/usr/local/tmp

2. chmod 1777/usr/local/tmp

3. Fork () function

Fork function
#include <unistd.h>
pid_t fork (void);
An existing process can call fork to create a new process.
Return value: 0 is returned in the child process, the child process ID is returned in the parent process, and an error returns-1.

Note: A child process is a copy of the parent process. For example, a child process obtains a copy of the parent process data space, heap, and stack (primarily a copy of the data structure). The parent-child process does not share these storage space portions. Parent-child processes share body segments.

Analyze the Code:

Analysis

Related knowledge in Linux (Atexit (), fork (), sticky bit)

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.