編寫一個程序,檢測當前系統的進程最多能打開幾個文件。

更多資料請點擊:我的目錄
本篇僅用於記錄自己所學知識及應用,代碼仍可優化,僅供參考,如果發現有錯誤的地方,儘管留言於我,謝謝。

輸出結果: 在這裏插入圖片描述

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define SIZE 1024
int main(int argc, char **argv)
{
	//檢測輸入參數是否正確
	if(argc != 2)
	{
		perror("輸入錯誤!\n");
		exit(0);
	}	
	
	long src[SIZE], i = 0;
	
	while(1)
	{
		//open()系統IO打開指定的文件(只讀),讀取成功src[i] >= 0
		src[i] = open(argv[1], O_RDONLY);
		if ( src[i] == -1 )//讀取失敗src[i] = -1
		{	
			//strerror(errno)輸出錯誤信息
			printf("Error:%s\n當前系統的進程最多能打開%ld個文件。\n",strerror(errno),i);
			exit(0);
		}
		i++;
	}	
	return 0;
}

更多資料請點擊:我的目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章