问题大全——Linux进程、IO及网络编程(自用)

本文探讨了文件IO和标准IO的区别,包括操作对象、接口和缓冲机制。还展示了遍历文件夹并打印文件信息的方法,以及如何使用文件IO实现定时写入功能。此外,文中提到了TCP保活机制、僵尸进程和守护进程的概念,以及apt安装软件依赖问题的解决方案。
摘要由CSDN通过智能技术生成

目录

文件IO和标准IO

遍历文件夹里面所有文件,打印文件的大小和日期

使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”

TCP保活机制

僵尸进程

守护进程

apt安装软件时缺少依赖

\\无法访问某个ip

ubuntu环境变量不生效(交叉编译)


本文章持续更新中,当前更新时间2024-07-20

文件IO和标准IO

文件IO和标准IO在多个方面存在差异,以下是对它们的主要区别的详细解释:

  1. 操作对象与目的
  • 文件IO:与文件相关联,主要涉及对文件的读取和写入操作。
  • 标准IO:与标准输入(stdin)、标准输出(stdout)和标准错误(stderr)相关联,主要用于与终端或控制台的交互。
  1. 接口
  • 文件IO:使用文件IO函数,如fopen()、fclose()、write()、read()等,这些函数提供了文件的打开、读写和关闭功能。
  • 标准IO:使用标准IO函数,如printf()和scanf(),这些函数提供了简单的接口和格式化功能。
  1. 缓冲机制
  • 文件IO:在进行文件操作时,直接调用系统函数对文件进行读写,这种操作是无缓冲的。每次读写操作都会触发从用户态到内核态的切换,这可能导致较高的系统资源消耗。
  • 标准IO:在读写数据时,会先将数据存放在缓冲区中,等待缓冲区满或遇到换行符等特定条件时,再进行系统调用与文件进行数据交互。这种缓冲机制可以减少系统调用的次数,从而提高效率。

遍历文件夹里面所有文件,打印文件的大小和日期

#include <stdio.h>  
#include <dirent.h>  
#include <sys/stat.h>  
#include <time.h>  
#include <string.h>  
  
void print_file_info(const char *path) {  
    struct stat file_stat;  
    if (stat(path, &file_stat) == -1) {  
        perror("stat");  
        return;  
    }  
  
    char time_string[80];  
    struct tm *time_info = localtime(&(file_stat.st_mtime));  
    strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", time_info);  
  
    printf("File: %s\n", path);  
    printf("Size: %lld bytes\n", (long long)file_stat.st_size);  
    printf("Last modified: %s\n", time_string);  
    printf("\n");  
}  
  
void traverse_directory(const char *dir_path) {  
    DIR *dir;  
    struct dirent *entry;  
    char path[1024];  
  
    if ((dir = opendir(dir_path)) == NULL) {  
        perror("Could not open directory");  
        return;  
    }  
  
    while ((entry = readdir(dir)) != NULL) {  
        if (entry->d_type == DT_REG) { // Regular file  
            snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);  
            print_file_info(path);  
        } else if (entry->d_type == DT_DIR) { // Directory  
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {  
                continue; // Skip current and parent directory  
            }  
            snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);  
            traverse_directory(path); // Recursively traverse subdirectories  
        }  
    }  
  
    closedir(dir);  
}  
  
int main(int argc, char *argv[]) {  
    if (argc != 2) {  
        printf("Usage: %s <directory>\n", argv[0]);  
        return 1;  
    }  
  
    traverse_directory(argv[1]);  
    return 0;  
}

使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”

这个我以前学文件IO和标准IO的时候写的,有个小坑就是对一行数据的划分。 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
 
int main()
{
	int i = 0,fd,ret;
	time_t times;
	char buf[32];
	struct tm* ge;
    fd = open("test.txt",O_RDWR | O_CREAT|O_APPEND, 0666);
    if(fd<0){
       printf("open file err\n");
       return 0;
    }
/*	
	while(fgets(buf,32,fp) != NULL)
	{
		if(buf[strlen(buf)-1] == '\n')
		{
			i++;
		}	
	}
*/
	char buf1[32];
	while(read(fd,buf1,32))
	{
		for(int j = 0;j <32;j++)
			if(buf1[j] == '\n'){
				i++;
			}
	}
	lseek(fd,0,SEEK_SET);
	while(1){
	times = time(NULL);
	//printf("%d\n",(int)times);
	ge = localtime(&times);
	printf("%2d:%4d-%2d-%2d %2d:%2d:%2d\n",i,ge->tm_year+1900,ge->tm_mon+1,ge->tm_mday,ge->tm_hour,ge->tm_min,ge->tm_sec);
	sprintf(buf,"%2d:%4d-%2d-%2d %2d:%2d:%2d\n",i,ge->tm_year+1900,ge->tm_mon+1,ge->tm_mday,ge->tm_hour,ge->tm_min,ge->tm_sec);	
	ret=write(fd,buf,strlen(buf));
    if(ret<0){
       perror("write");
       goto END;
    }
	i++;
	sleep(1);	
	}
	END:
 
 
    close(fd);
}

TCP保活机制

TCP保活机制详解(KeepAlive) - 知乎

僵尸进程

        僵尸进程通常发生在子进程先于父进程结束时,父进程没有正确地回收子进程并释放其占用的资源。在这种情况下,子进程将保持一个已完成任务但在进程表中仍有条目的状态,被视为已死进程,需要父进程执行终止命令来结束。如果父进程持续循环而不结束,子进程可能会一直保持僵尸状态。然而,如果父进程退出,init进程(或在某些系统中是systemd)会接管并回收子进程占用的资源。僵尸进程的实现可以通过在代码中创建两个进程,并让子进程退出而父进程循环输出来完成。

守护进程

        守护进程则是另一种类型的进程,它通常在系统启动时开始运行,并在系统关闭时终止。守护进程在后台运行,没有控制终端,且通常负责执行特定的任务或提供某种服务,例如Web服务器、数据库服务、打印服务等。守护进程的设计理念是为了在系统启动后,持续提供服务或执行任务,而不需要交互式用户干预。

apt安装软件时缺少依赖

sudo apt --fix-broken install

执行这个命令可以自动修复

\\无法访问某个ip

sudo chmod -R go+rwx 目录

前提是安装了samba服务器

ubuntu环境变量不生效(交叉编译)

需要安装lib32z1和lib32ncurses5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇努力学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值