简介
- Linux POSIX
Linux POSIX 详解
在 POSIX 系统中,<unistd.h>
是一个重要的头文件,提供了许多对系统进行访问的标准符号常量、类型和函数原型。这个头文件对于系统级编程非常重要,它定义了 POSIX 操作系统 API 的许多核心功能。
以下是 <unistd.h>
中常见的一些功能和内容:
- 符号常量:
STDIN_FILENO
、STDOUT_FILENO
、STDERR_FILENO
:标准输入、标准输出和标准错误的文件描述符。F_OK
、R_OK
、W_OK
、X_OK
:用于文件访问权限的标志常量。- 其他常量用于系统调用、文件操作等。
- 数据类型:
ssize_t
:用于表示字节大小的有符号整数类型。off_t
:用于表示文件偏移量的类型。pid_t
:用于表示进程 ID 的类型。- 其他与系统相关的类型。
- 函数原型:
- 文件操作:
read()
、write()
、close()
等。 - 进程控制:
fork()
、exec()
、wait()
、exit()
等。 - 文件和目录操作:
access()
、mkdir()
、rmdir()
、chdir()
等。 - 系统调用和资源管理:
sleep()
、getpid()
、getcwd()
、getuid()
等。
- 文件操作:
<unistd.h>
头文件中的函数和符号常量是进行系统级编程的关键工具。这些函数和常量允许程序员对文件、进程、系统调用等进行操作,使得在 POSIX 系统上进行更底层的系统编程变得更加容易和标准化。
Linux POSIX open() 详解
在 POSIX 系统中,open()
是 <unistd.h>
头文件中定义的函数之一,用于打开或创建文件,并返回一个文件描述符,以便后续对文件进行读写操作。
函数原型如下:
1
int open(const char *pathname, int flags, mode_t mode);
参数说明:
pathname
:要打开或创建的文件的路径名。flags
:用于指定打开文件的标志,可以是O_RDONLY
(只读)、O_WRONLY
(只写)、O_RDWR
(读写)、O_CREAT
(若文件不存在则创建)、O_TRUNC
(清空文件内容)等标志的组合。mode
:用于指定创建文件时的权限,仅在O_CREAT
标志被指定时有效,通常以八进制数表示,比如0644
。
返回值为文件描述符 int
类型,表示打开文件的引用,如果出错则返回 -1。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd;
fd = open("file.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); // 打开或创建文件
if (fd == -1) {
perror("open");
return -1;
}
printf("File opened with file descriptor: %d\n", fd);
close(fd); // 关闭文件
return 0;
}
这个示例演示了如何使用 open()
函数打开或创建文件,并获取文件描述符。在这个例子中,file.txt
是要打开或创建的文件名。使用 O_WRONLY
表示只写模式,O_CREAT
表示若文件不存在则创建,O_TRUNC
表示清空文件内容。0644
是文件权限,指定文件所有者有读写权限,其他用户只有读权限。
open()
函数在实际应用中用于打开文件或创建文件,返回文件描述符以便后续对文件进行读写操作。
Linux POSIX read() 详解
在 POSIX 系统中,read()
是 <unistd.h>
头文件中定义的函数之一,用于从文件描述符中读取数据。该函数通常用于从文件、管道、套接字或其他 I/O 对象中读取数据。
函数原型如下:
1
ssize_t read(int fd, void *buf, size_t count);
参数说明:
fd
:要读取的文件描述符。buf
:用于存储读取数据的缓冲区。count
:要读取的字节数。
read()
函数的返回值 ssize_t
表示实际读取的字节数,或者在出错时返回负数。返回值为 0 表示已到达文件末尾(对于文件描述符到达末尾或连接被关闭的情况)。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd;
char buffer[1024];
ssize_t bytes_read;
fd = open("file.txt", O_RDONLY); // 打开文件
if (fd == -1) {
perror("open");
return -1;
}
bytes_read = read(fd, buffer, sizeof(buffer)); // 从文件中读取数据
if (bytes_read == -1) {
perror("read");
return -1;
}
printf("Read %zd bytes: %s\n", bytes_read, buffer);
close(fd); // 关闭文件
return 0;
}
这个示例演示了如何使用 read()
函数从文件中读取数据。首先,通过 open()
函数打开一个文件,并获取文件描述符。然后,使用 read()
函数从该文件中读取数据,并将读取的内容存储在 buffer
缓冲区中。最后,使用 printf()
函数打印读取的字节数和内容。
read()
函数在实际应用中经常用于从文件或其他 I/O 对象中读取数据,并将数据存储到指定的缓冲区中。
Linux POSIX write() 详解
在 POSIX 系统中,write()
是 <unistd.h>
头文件中定义的函数之一,用于向文件描述符中写入数据。这个函数通常用于将数据写入文件、管道、套接字或其他 I/O 对象。
函数原型如下:
1
ssize_t write(int fd, const void *buf, size_t count);
参数说明:
fd
:要写入的文件描述符。buf
:要写入的数据的缓冲区。count
:要写入的字节数。
write()
函数的返回值 ssize_t
表示实际写入的字节数,或者在出错时返回负数。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main() {
int fd;
char *data = "Hello, this is a test!";
ssize_t bytes_written;
fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); // 打开或创建文件
if (fd == -1) {
perror("open");
return -1;
}
bytes_written = write(fd, data, strlen(data)); // 向文件中写入数据
if (bytes_written == -1) {
perror("write");
return -1;
}
printf("Written %zd bytes to the file.\n", bytes_written);
close(fd); // 关闭文件
return 0;
}
这个示例演示了如何使用 write()
函数向文件中写入数据。首先,通过 open()
函数打开一个文件或创建一个新文件,并获取文件描述符。然后,使用 write()
函数将数据写入文件中。在这个例子中,字符串 “Hello, this is a test!” 被写入到文件中。最后,使用 printf()
函数打印写入的字节数。
write()
函数在实际应用中通常用于将数据写入到文件中,执行输入/输出操作。
Linux POSIX close() 详解
在 POSIX 系统中,close()
是 <unistd.h>
头文件中定义的函数之一,用于关闭先前打开的文件描述符,释放文件描述符所占用的资源。
函数原型如下:
1
int close(int fd);
参数 fd
是要关闭的文件描述符。
close()
函数的返回值为整型。成功关闭文件描述符时返回 0,失败时返回 -1,并设置全局变量 errno
以指示错误类型。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd;
fd = open("file.txt", O_RDONLY); // 打开文件
if (fd == -1) {
perror("open");
return -1;
}
// 在此对文件进行读取、写入等操作...
if (close(fd) == -1) { // 关闭文件
perror("close");
return -1;
}
return 0;
}
在示例中,close()
函数用于关闭先前使用 open()
打开的文件描述符 fd
。关闭文件描述符后,应用程序不再具有对文件的访问权限。
close()
函数在文件操作中是一个重要的步骤,它负责释放文件描述符所占用的资源,因此在使用完文件后,一般都会使用 close()
来关闭文件描述符,以避免资源泄漏。
Linux POSIX lseek() 详解
在 POSIX 系统中,lseek()
是 <unistd.h>
头文件中定义的函数之一,用于在文件中移动文件读写指针,以便定位到文件中的特定位置。
函数原型如下:
1
off_t lseek(int fd, off_t offset, int whence);
参数说明:
fd
:文件描述符,指示要操作的文件。offset
:偏移量,指定相对于whence
参数的位置进行偏移。whence
:指定偏移量的基准位置,可以是SEEK_SET
(从文件开头开始)、SEEK_CUR
(从当前位置开始)、SEEK_END
(从文件末尾开始)。
lseek()
函数返回 off_t
类型,表示从文件开头到指定位置的偏移量,或者在出错时返回 -1
。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd;
off_t new_offset;
fd = open("file.txt", O_RDONLY); // 打开文件
if (fd == -1) {
perror("open");
return -1;
}
// 移动文件读写指针到文件末尾
new_offset = lseek(fd, 0, SEEK_END);
if (new_offset == -1) {
perror("lseek");
close(fd);
return -1;
}
printf("File size: %lld bytes\n", (long long)new_offset);
close(fd); // 关闭文件
return 0;
}
在示例中,lseek()
函数被用于将文件读写指针移动到文件末尾。通过将 whence
参数设置为 SEEK_END
并将 offset
设置为 0,lseek()
将读写指针移动到文件末尾,并返回文件大小。然后,使用 printf()
打印文件的大小。
lseek()
函数在文件操作中可以用于定位到文件的特定位置,比如读取文件末尾、从指定位置开始读取等操作。
Linux POSIX fork() 详解
在 POSIX 系统中,fork()
是 <unistd.h>
头文件中定义的函数之一,用于创建一个新的进程,该进程是调用进程的副本,被称为子进程。
函数原型如下:
1
pid_t fork(void);
fork()
函数不接收任何参数,返回值是一个 pid_t
类型的值,表示进程 ID。在父进程中,fork()
返回子进程的进程 ID;而在子进程中,fork()
返回 0。如果出现错误,fork()
返回一个负数。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t child_pid;
child_pid = fork(); // 创建子进程
if (child_pid == -1) {
perror("fork");
return -1;
} else if (child_pid == 0) {
// 子进程执行的代码
printf("This is the child process (PID: %d)\n", getpid());
} else {
// 父进程执行的代码
printf("This is the parent process (Child PID: %d, Parent PID: %d)\n", child_pid, getpid());
}
return 0;
}
在这个示例中,fork()
被调用后会创建一个新的子进程。父进程中的 child_pid
变量存储了子进程的进程 ID。在父进程中,fork()
返回子进程的进程 ID;在子进程中,fork()
返回 0。通过检查返回值,可以区分父进程和子进程。
fork()
在创建子进程时,会复制父进程的内存映像。子进程是父进程的副本,但它有自己独立的内存空间。父子进程会同时执行 fork()
调用之后的代码,但在不同的进程上下文中。这使得通过 fork()
创建新进程成为了一种常见的并行执行代码的方式。
Linux POSIX exec() 详解
在 POSIX 系统中,exec()
函数族包括一组函数,用于在当前进程中执行新的程序,替换当前进程的内存映像为新程序的代码和数据。exec()
函数族包括多个变体,如 execl()
, execv()
, execle()
, execve()
, 等等。
这些函数允许程序动态地在当前进程中加载新的程序,并执行该程序的代码,取代当前进程的内容。
以下是其中两个常用的函数原型:
-
int execl(const char *path, const char *arg0, ... /* (char *) NULL */ );
path
:新程序的路径。arg0
:新程序的名称,通常被赋予被执行程序的名称。...
:可变数量的参数,用来传递给新程序的命令行参数,最后一个参数必须是(char *) NULL
。
-
int execv(const char *path, char *const argv[]);
path
:新程序的路径。argv
:一个字符指针数组,用于传递给新程序的命令行参数,以 NULL 结尾。
这些函数的返回值在执行成功时一般不会返回,如果返回,通常表示发生了错误。
示例用法(execv()
):
1
2
3
4
5
6
7
8
9
10
11
12
#include <unistd.h>
#include <stdio.h>
int main() {
char *args[] = {"/bin/ls", "-l", NULL}; // 命令行参数数组
if (execv("/bin/ls", args) == -1) { // 执行 ls 命令
perror("execv");
return -1;
}
printf("This line will not be executed because of execv()\n");
return 0;
}
在这个示例中,execv()
函数用于执行 /bin/ls
命令,传递了命令行参数 -l
。如果 execv()
执行成功,则会替换当前进程的内存映像为 ls
命令,并显示相应的输出。如果执行失败,则会输出错误信息。需要注意的是,如果 execv()
函数执行成功,则后续代码不会被执行,因为当前进程的内容已经被新程序替换。
Linux POSIX wait() 详解
在 POSIX 系统中,wait()
是 <unistd.h>
头文件中定义的函数之一,用于父进程等待子进程的终止。
函数原型如下:
1
pid_t wait(int *status);
参数 status
是一个指向整型的指针,用于存储子进程的退出状态信息。如果不关心子进程的退出状态,可以传入 NULL
。
wait()
函数的返回值为子进程的进程 ID,如果出错则返回 -1。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork(); // 创建子进程
if (pid == -1) {
perror("fork");
return -1;
} else if (pid == 0) {
// 子进程执行的代码
printf("Child process with PID: %d\n", getpid());
exit(42); // 子进程退出,退出状态为 42
} else {
// 父进程执行的代码
int status;
pid_t child_pid = wait(&status); // 等待子进程退出
if (child_pid == -1) {
perror("wait");
return -1;
}
if (WIFEXITED(status)) { // 检查子进程是否正常终止
printf("Child process %d exited with status: %d\n", child_pid, WEXITSTATUS(status));
}
}
return 0;
}
在这个示例中,父进程使用 fork()
创建了一个子进程。在子进程中,使用 exit(42)
退出,并返回状态码 42。在父进程中,使用 wait()
函数等待子进程退出,并获取子进程的退出状态信息。WIFEXITED(status)
用于检查子进程是否正常终止,WEXITSTATUS(status)
获取子进程的退出状态。
wait()
函数允许父进程等待子进程的终止,以便获取子进程的退出状态。
Linux POSIX exit() 详解
在 POSIX 系统中,exit()
是 <unistd.h>
头文件中定义的函数之一,用于正常终止当前进程。
函数原型如下:
1
void exit(int status);
参数 status
是整数类型,表示进程的退出状态。通常情况下,0 表示正常终止,非零值表示出现了错误。
exit()
函数没有返回值,它会终止当前进程的执行,并将进程的退出状态传递给父进程。进程终止后,操作系统会进行清理工作,包括关闭文件描述符、释放内存等。
示例用法:
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
printf("Before calling exit()\n");
exit(0); // 正常终止进程,退出状态为 0
printf("This line will not be executed\n");
return 0;
}
在示例中,exit()
函数被调用来正常终止进程。在调用 exit()
后的代码将不会被执行,因为进程已经终止。
exit()
函数用于结束当前进程的执行,常见的使用情况包括程序执行成功时返回 0,出现错误时返回非零值,以及在程序中遇到不可恢复的错误时终止程序。
Linux POSIX access() 详解
在 POSIX 系统中,access()
是 <unistd.h>
头文件中定义的函数之一,用于检查文件的权限。
函数原型如下:
1
int access(const char *pathname, int mode);
参数 pathname
是要检查权限的文件路径名,mode
是要检查的权限。mode
可以是以下值的按位或组合:
F_OK
:用于检查文件是否存在。R_OK
:用于检查文件是否可读。W_OK
:用于检查文件是否可写。X_OK
:用于检查文件是否可执行。
access()
函数返回整型值,表示文件权限检查的结果。如果权限检查成功,返回 0;否则返回 -1,并设置 errno
来指示具体的错误类型。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <unistd.h>
int main() {
const char *filename = "example.txt";
// 检查文件是否存在
if (access(filename, F_OK) == 0) {
printf("%s exists\n", filename);
} else {
printf("%s does not exist\n", filename);
}
// 检查文件是否可读
if (access(filename, R_OK) == 0) {
printf("%s is readable\n", filename);
} else {
printf("%s is not readable\n", filename);
}
// 检查文件是否可写
if (access(filename, W_OK) == 0) {
printf("%s is writable\n", filename);
} else {
printf("%s is not writable\n", filename);
}
// 检查文件是否可执行
if (access(filename, X_OK) == 0) {
printf("%s is executable\n", filename);
} else {
printf("%s is not executable\n", filename);
}
return 0;
}
在这个示例中,access()
函数被用于检查文件 example.txt
的存在性、可读性、可写性和可执行性。根据权限检查的结果,打印相应的信息。
Linux POSIX mkir() 详解
在 POSIX 系统中,创建新目录的函数是 mkdir()
,而不是 mkir()
。
mkdir()
函数用于创建新目录,其原型定义在 <sys/stat.h>
头文件中,而不是 <unistd.h>
。它的函数原型如下:
1
int mkdir(const char *pathname, mode_t mode);
pathname
是新目录的路径名。mode
是新目录的权限模式,通常以八进制表示,比如0755
。
函数返回值为整型,如果创建成功返回 0,创建失败返回 -1,并设置相应的错误码,可以通过 errno
查看具体错误信息。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main() {
const char *dirname = "new_directory";
// 创建新目录
if (mkdir(dirname, 0755) == 0) {
printf("Directory \"%s\" created successfully.\n", dirname);
} else {
perror("mkdir");
return -1;
}
return 0;
}
这个示例演示了如何使用 mkdir()
函数创建一个名为 new_directory
的新目录,并指定了权限为 0755
。如果目录创建成功,则会输出相应的成功信息,否则会输出相应的错误信息。
mkdir()
函数是 POSIX 系统中用于创建目录的常用函数之一。
Linux POSIX rmdir() 详解
在 POSIX 系统中,rmdir()
是 <unistd.h>
头文件中定义的函数之一,用于删除目录。
函数原型如下:
1
int rmdir(const char *pathname);
参数 pathname
是要删除的目录的路径名。
rmdir()
函数的返回值为整型,如果删除成功返回 0,删除失败返回 -1,并设置相应的错误码,可以通过 errno
查看具体错误信息。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <unistd.h>
int main() {
const char *dirname = "directory_to_delete";
// 删除目录
if (rmdir(dirname) == 0) {
printf("Directory \"%s\" deleted successfully.\n", dirname);
} else {
perror("rmdir");
return -1;
}
return 0;
}
这个示例演示了如何使用 rmdir()
函数删除一个名为 directory_to_delete
的目录。如果目录删除成功,则会输出相应的成功信息,否则会输出相应的错误信息。
需要注意的是,使用 rmdir()
删除目录时,目录必须为空。如果目录中包含文件或其他目录,则无法成功删除目录。因此,在删除目录之前,通常需要确保目录为空。
Linux POSIX chdir() 详解
在 POSIX 系统中,chdir()
是 <unistd.h>
头文件中定义的函数之一,用于改变当前工作目录。
函数原型如下:
1
int chdir(const char *path);
参数 path
是要设置为当前工作目录的路径名。
chdir()
函数的返回值为整型,如果成功改变工作目录则返回 0,失败返回 -1,并设置相应的错误码,可以通过 errno
查看具体错误信息。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <unistd.h>
int main() {
const char *new_directory = "/path/to/new_directory";
// 改变当前工作目录
if (chdir(new_directory) == 0) {
printf("Changed current working directory to: %s\n", new_directory);
} else {
perror("chdir");
return -1;
}
return 0;
}
这个示例演示了如何使用 chdir()
函数将当前工作目录更改为 /path/to/new_directory
。如果成功更改当前工作目录,则会输出相应的成功信息,否则会输出相应的错误信息。
chdir()
函数在 POSIX 系统中是用来改变当前工作目录的常用函数之一。
Linux POSIX sleep() 详解
在 POSIX 系统中,sleep()
是 <unistd.h>
头文件中定义的函数之一,用于让当前进程挂起指定的时间。
函数原型如下:
1
unsigned int sleep(unsigned int seconds);
参数 seconds
是要挂起的时间长度,单位为秒。该函数会使当前进程挂起指定的秒数,并且在等待的时间内会让出 CPU,直到指定的时间过去为止。
sleep()
函数的返回值为未休眠完的时间(剩余的时间)。如果休眠时间到期,则返回 0。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Sleeping for 3 seconds...\n");
unsigned int remaining_time = sleep(3); // 休眠 3 秒
if (remaining_time > 0) {
printf("Sleep interrupted, remaining time: %u seconds\n", remaining_time);
} else {
printf("Woke up after 3 seconds\n");
}
return 0;
}
在这个示例中,sleep(3)
使当前进程挂起 3 秒钟。如果休眠时间到期,则程序会打印”Woke up after 3 seconds”,否则如果在等待期间发生了信号中断或其他情况,则会打印”Sleep interrupted, remaining time: [剩余秒数] seconds”。
sleep()
函数通常用于需要暂停执行一段时间的情况,比如在程序中设置定时任务或者等待某些条件达成的情况下。
Linux POSIX getpid() 详解
在 POSIX 系统中,getpid()
是 <unistd.h>
头文件中定义的函数之一,用于获取当前进程的进程 ID(PID)。
函数原型如下:
1
2
3
#include <unistd.h>
pid_t getpid(void);
getpid()
函数不接受任何参数,直接返回调用进程的进程 ID(PID)。
示例用法:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t process_id = getpid(); // 获取当前进程的 PID
printf("Current process ID (PID): %d\n", process_id);
return 0;
}
在这个示例中,getpid()
函数被调用以获取当前进程的 PID,并将其打印输出。通常情况下,每个运行的进程都有一个唯一的 PID,可以用来标识和跟踪进程。
Linux POSIX getcwd() 详解
在 POSIX 系统中,getcwd()
是 <unistd.h>
头文件中定义的函数之一,用于获取当前工作目录的路径名。
函数原型如下:
1
2
3
#include <unistd.h>
char *getcwd(char *buf, size_t size);
buf
是一个字符指针,用于存储当前工作目录的路径名。size
是要存储路径名的缓冲区的大小。
getcwd()
函数的返回值是一个指向当前工作目录路径名的字符串指针,即 buf
参数的值。如果出现错误,则返回 NULL,并设置 errno
来指示具体的错误类型。
示例用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <unistd.h>
int main() {
char buffer[4096]; // 缓冲区大小
char *cwd = getcwd(buffer, sizeof(buffer)); // 获取当前工作目录
if (cwd != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd");
return -1;
}
return 0;
}
在这个示例中,getcwd()
函数被调用以获取当前工作目录的路径名,并将其存储在 buffer
缓冲区中。然后,通过判断返回值是否为 NULL,来输出当前工作目录的路径名。这个函数通常用于获取当前工作目录,以便程序在需要时操作特定的文件或目录。
Linux POSIX getuid() 详解
在 POSIX 系统中,getuid()
是 <unistd.h>
头文件中定义的函数之一,用于获取当前进程的实际用户 ID。
函数原型如下:
1
2
3
#include <unistd.h>
uid_t getuid(void);
getuid()
函数不接受任何参数,直接返回调用进程的实际用户 ID(UID)。
示例用法:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <unistd.h>
int main() {
uid_t user_id = getuid(); // 获取当前进程的实际用户 ID
printf("Current user ID: %d\n", user_id);
return 0;
}
在这个示例中,getuid()
函数被调用以获取当前进程的实际用户 ID,并将其打印输出。实际用户 ID 是唯一标识一个用户的整数值,用于在系统中识别和区分不同的用户。