cpu:PC AR
open()
create()
O_RDONLY:只讀方式打開文件
O_WRONLY:只寫方式打開文件
O_RDWR:讀寫方式打開文件
O_APPEND
O_CREAT
O_DIRECTORY
O_EXCL
- 一個簡單的文件copy的程序:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#define BUFFER_SIZE 100
int main(int argc,char *argv[])
{
if(argc != 3)
{
printf("usage:%s<src_file> <dst_file>\n",argv[0]);
return 1;
}
int src_fd = 0;
int dst_fd = 0;
int n = 0;
char buf[BUFFER_SIZE] = {"\0"};
char *src_file = argv[1];
char *dst_file = argv[2];
char cover_dir = '\0';
char cover_filename[BUFFER_SIZE] = {'\0'};
if(access(dst_file,F_OK) == 0)
{
printf("directorie is exist!!,,Do you want to cover, y or n:");
scanf("%c",&cover_dir);
switch(cover_dir)
{
case 'y':
case 'Y':
{
//dst_fd = open(dst_file,O_WRONLY | O_TRUNC,S_IRUSR | S_IWUSR);
if((dst_fd = open(dst_file,O_WRONLY | O_TRUNC)) == -1)
{
perror("open dst error");
return -1;
}
}
break;
case 'n':
case 'N':
{
printf("Please resume load of filename:");
getchar();
scanf("%s",cover_filename);
if((dst_fd = open(cover_filename,O_WRONLY | O_TRUNC | O_CREAT,S_IRUSR | S_IWUSR)) == -1)
{
perror("open dst error\n");
return -1;
}
break;
}
break;
default:
printf("Please input again!!!\n");
return -1;
}
}
else{
if((dst_fd = open(dst_file,O_WRONLY | O_TRUNC | O_CREAT,S_IRUSR | S_IWUSR)) == -1)
{
perror("open dst error");
return -1;
}
}
if((src_fd = open(src_file,O_RDONLY)) == -1)
{
perror("open src error");
return -1;
}
while((n = read(src_fd,buf,BUFFER_SIZE)) > 0)
{
write(dst_fd,buf,n);
}
close(src_fd);
close(dst_fd);
//1.open
//2.1 read data form src_file
//2.2 write data to dst_file
//3.close
return 0;
}
簡單模仿一個shell的ls命令
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("usage : %s <filename>\n", argv[0]);
return 1;
}
struct stat file_stat;
struct passwd *pw = NULL;
struct group *grp = NULL;
memset(&file_stat, 0, sizeof(file_stat));
if(stat(argv[1], &file_stat) == -1)
{
perror("stat error");
return 1;
}
// 打印文件類型和權(quán)限信息
// 思路:通過預(yù)定義的宏函數(shù)分析st_mode的值
if(S_ISREG(file_stat.st_mode))
printf("-");
if(S_ISDIR(file_stat.st_mode))
printf("d");
if(S_ISCHR(file_stat.st_mode))
printf("c");
// ...
if(file_stat.st_mode & S_IRUSR)
printf("r");
else
printf("-");
if(file_stat.st_mode & S_IWUSR)
printf("w");
else
printf("-");
if(file_stat.st_mode & S_IXUSR)
printf("x");
else
printf("-");
// 打印硬鏈接數(shù)目
printf("%d\t", file_stat.st_nlink);
// 通過文件所有者ID獲取文件所有者名稱
pw = getpwuid(file_stat.st_uid);
printf("%s\t", pw->pw_name);
// 通過文件所屬組ID獲取文件所屬組名稱
grp = getgrgid(file_stat.st_gid);
printf("%s\t", grp->gr_name);
// 打印文件大小
printf("%d\t", file_stat.st_size);
// 打印文件的最后修改時間
printf("%s\t", ctime(&(file_stat.st_mtime)));
printf("\n");
return 0;
}
fork通過copy的方式創(chuàng)建一個進程沿盅,父進程和子進程同事存在坎缭,子進程會執(zhí)行父進程中的內(nèi)容,執(zhí)行時不分先后順序滥玷。需要判讀其返回值來判斷來區(qū)分函數(shù)的字符進程。炉爆。exec系列函數(shù)創(chuàng)建進程的時候具有替代進程的屬性铝耻,參數(shù)有path路徑和 envo路徑尋找的方式
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<pwd.h>
#include <grp.h>
#define DEBUG_MODE 1
#define COM_MAX_LEN 128
int main(int argc,char *argv[])
{
pid_t pid = 0;
char command[COM_MAX_LEN] = {'\0'};
int status = 0;
char *delim = " ";
char *str_command[2];
struct passwd *pw = NULL;
struct stat file_stat;
memset(&file_stat, 0, sizeof(file_stat));
while(1)
{
// 通過文件所有者ID獲取文件所有者名稱
pw = getpwuid(file_stat.st_uid);
printf("%s>", pw->pw_name);
//printf(">>>>>>>>>");
//uid_t st_uid; /* user ID of owner */
fgets(command,COM_MAX_LEN,stdin);
command[strlen(command)-1] = '\0';
if(strcmp(command,".exit") == 0)
return 0;
str_command[0] = strtok(command,delim);
str_command[1] = strtok(NULL,delim);
//strcpy(str_command[],p);
if((pid = fork()) == 0)
{
printf("in child peocess :%d\n",getpid());
//利用exec函數(shù)將該子進程的執(zhí)行指令替換
execlp(str_command[0],str_command[0],str_command[1],NULL);
//在path路勁中收索執(zhí)行l(wèi)s命令
//2.在path路勁中搜索執(zhí)行l(wèi)s -l /命令
//execlp("ls","ls","-l",NULL);
return 0;
}
//防止僵尸進程的產(chǎn)生
pid = wait(&status);
#if DEBUG_MODE
printf("in parent process child %d process exit code: \n",pid);
#endif
}
return 0;
}
//練習(xí):
//參考上述代碼,完成自己的shell命令解釋器
//基本功能1:能夠輸入并執(zhí)行普通的不帶選項和參數(shù)的shell命令
//基本功能2:shell命令解釋器具備不同的
- 模仿shell的命令解釋器:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<pwd.h>
#include <grp.h>
#define DEBUG_MODE 1
#define COM_MAX_LEN 128
int main(int argc,char *argv[])
{
pid_t pid = 0;
char command[COM_MAX_LEN] = {'\0'};
int status = 0;
char *delim = " ";
char *str_command[2];
struct passwd *pw = NULL;
struct stat file_stat;
memset(&file_stat, 0, sizeof(file_stat));
while(1)
{
// 通過文件所有者ID獲取文件所有者名稱
pw = getpwuid(file_stat.st_uid);
printf("%s>", pw->pw_name);
//printf(">>>>>>>>>");
//uid_t st_uid; /* user ID of owner */
fgets(command,COM_MAX_LEN,stdin);
command[strlen(command)-1] = '\0';
if(strcmp(command,".exit") == 0)
return 0;
str_command[0] = strtok(command,delim);
str_command[1] = strtok(NULL,delim);
//strcpy(str_command[],p);
if((pid = fork()) == 0)
{
printf("in child peocess :%d\n",getpid());
//利用exec函數(shù)將該子進程的執(zhí)行指令替換
execlp(str_command[0],str_command[0],str_command[1],NULL);
//在path路勁中收索執(zhí)行l(wèi)s命令
//2.在path路勁中搜索執(zhí)行l(wèi)s -l /命令
//execlp("ls","ls","-l",NULL);
return 0;
}
//防止僵尸進程的產(chǎn)生
pid = wait(&status);
#if DEBUG_MODE
printf("in parent process child %d process exit code: \n",pid);
#endif
}
return 0;
}
進程的5種狀態(tài)贞远,創(chuàng)建畴博、等待、run蓝仲、關(guān)閉俱病、阻塞。袱结。
信號在只能在進程的間傳遞信號亮隙,起到激發(fā)的作用,不能傳遞信息垢夹。