linux系统编程

dup 与 dup2函数

dup用来复制参数oldfd所指的文件描述符。当复制成功是,返回最小的
尚未被使用过的文件描述符,若有错误则返回-1.错误代码存入errno中
返回的新文件描述符和参数oldfd指向同一个文件,这两个描述符共享
同一个数据结构,共享所有的锁定,读写指针和各项权限或标志位。

dup2与dup区别是dup2可以用参数newfd指定新文件描述符的数值。
若参数newfd已经被程序使用,则系统就会将newfd所指的文件关闭,
若newfd等于oldfd,则返回newfd,而不关闭newfd所指的文件。
dup2所复制的文件描述符与原来的文件描述符共享各种文件状态。
共享所有的锁定,读写位置和各项权限或flags等.
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd = open("a.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    printf("file open fd = %d\n", fd);

    // 找到进程文件描述表中 ==第一个== 可用的文件描述符
    // 将参数指定的文件复制到该描述符后,返回这个描述符
    int ret = dup(fd);
    if(ret == -1)
    {
        perror("dup");
        exit(1);
    }
    printf("dup fd = %d\n", ret);
    char* buf = "你是猴子派来的救兵吗????\n";
    char* buf1 = "你大爷的,我是程序猿!!!\n";
    write(fd, buf, strlen(buf));
    write(ret, buf1, strlen(buf1));

    close(fd);
    return 0;
}

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int fd = open("english.txt", O_RDWR);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    int fd1 = open("a.txt", O_RDWR);
    if(fd1 == -1)
    {
        perror("open");
        exit(1);
    }

    printf("fd = %d\n", fd);
    printf("fd1 = %d\n", fd1);

    int ret = dup2(fd1, fd);
    if(ret == -1)
    {
        perror("dup2");
        exit(1);
    }
    printf("current fd = %d\n", ret);
    char* buf = "主要看气质 ^_^!!!!!!!!!!\n";
    write(fd, buf, strlen(buf));
    write(fd1, "hello, world!", 13);

    close(fd);
    close(fd1);
    return 0;
}

fcntl函数,改变已打开文件的属性

image
image

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    int fd;
    int flag;

    // 测试字符串
    char *p = "我们是一个有中国特色的社会主义国家!!!!!!";
    char *q = "社会主义好哇。。。。。。";    

    // 只写的方式打开文件
    fd = open("test.txt", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(1);
    }

    // 输入新的内容,该部分会覆盖原来旧的内容
    if(write(fd, p, strlen(p)) == -1) {
        perror("write");
        exit(1);
    }

    // 使用 F_GETFL 命令得到文件状态标志
    flag = fcntl(fd, F_GETFL, 0);
    if(flag == -1) {
        perror("fcntl");
        exit(1);
    }

    // 将文件状态标志添加 ”追加写“ 选项
    flag |= O_APPEND;
    // 将文件状态修改为追加写
    if(fcntl(fd, F_SETFL, flag) == -1) {
        perror("fcntl -- append write");
        exit(1);
    }

    // 再次输入新内容,该内容会追加到旧内容的后面
    if(write(fd, q, strlen(q)) == -1) {
        perror("write again");
        exit(1);
    }

    // 关闭文件
    close(fd);

    return 0;
}

虚拟地址: 可用的地址空间 有 4G

CPU

image

MMU:内存管理单元,位于CPU内部

image

PCB 本质是一个结构体

image

image

环境变量:
环境变量,是指在操作系统中用来指定操作系统运行环境的一些参数。
通常具备以下特征:
① 字符串(本质) ② 有统一的格式:名=值[:值] ③ 值用来描述进程环境信息。
存储形式:与命令行参数类似。char *[]数组,数组名environ,内部存储字符串,NULL作为哨兵结尾。
使用形式:与命令行参数类似。
加载位置:与命令行参数类似。位于用户区,高于stack的起始位置。
引入环境变量表:须声明环境变量。extern char ** environ;
练习:打印当前进程的所有环境变量。	

打印当前环境所有的环境变量

点击查看代码
#include <stdio.h>
#include <unistd.h>

extern char **environ;

int main(void)
{
    int i;

    for (i = 0; environ[i] != NULL; i++)
        printf("%s\n", environ[i]);

    return 0;
}

setenv,getenv,unsetenv函数示例

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	char *val;
	// 使用ABD避免与真正的环境变量冲突,避免污染环境变量
	const char *name = "ABD";

	val = getenv(name);
	printf("1, %s = %s\n", name, val);

	setenv(name, "haha-day-and-night", 1);

	val = getenv(name);
	printf("2, %s = %s\n", name, val);

#if 1
	int ret = unsetenv("ABD=");
    printf("ret = %d\n", ret);

	val = getenv(name);
	printf("3, %s = %s\n", name, val);

#else
	int ret = unsetenv("ABD");  //name=value:value
	printf("ret = %d\n", ret);

	val = getenv(name);
	printf("3, %s = %s\n", name, val);

#endif

	return 0;
}

posted @ 2022-10-26 20:19  mnst  阅读(57)  评论(0)    收藏  举报