一.首先是一个小测试
要求:1. 创建一个目录 使用 mkdir 函数
2.在目录里创建文件 使用open 函数
3.在创建的文件里写入内容 在读取
4.关闭文件
5.删除文件
6.删除目录
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
if(!mkdir("gw",0777))
{
printf(" creat mkdir %s ok\n",argv[1]);
}
else
{
printf(" mkdir error\n ");
return -2;
}
system("ls -all");
system("cd gw");
int fp1 = open("ww.txt",O_RDWR |O_CREAT,0666);
if(fp1<0)
{
perror("open error \n");
return -3;
}
else
{
printf("open OK\n");
}
char W[100]={"adasdasdasda"};
int a =write(fp1,W,strlen(W));
printf("输入成功 输入%d 个字节 \n",a);
char W1[100]="";
lseek(fp1,0,SEEK_SET);
read(fp1,W1,sizeof(W1));
close(fp1);
system("ls -all");
int gg=unlink("ww.txt");
if(!gg)
{
printf("删除文件成功!\n");
}
else
{
perror("删除失败!\n");
return -4;
}
printf("W1 = %s\n",W1);
if( !rmdir("gw"))
{
printf("删除成功!\n");
}
else
{
printf("删除目录失败!\n");
}
return 0;
}
二.学习时间函数
时间函数:
char *asctime(const struct tm tm); // 把指针转化为字符串的内容
char *asctime_r(const struct tm *tm,char *buf);
char *ctime(const time_t *timep); // 把time_t 类型的总秒数转换为字符串
char *ctime_r(const time_t *timep,char *buf);
struct tm*gmtime(const time_t *timep); //获得国际时间
struct tm *gmtine_r(const time_t *timep,struct tn *result);
struct tm* localtime(const time_t *timep); //获得当地时间
struct tm *localtime_r(const time_t *timep,struct tn *result);
tine_t mktine(struct tm*to); //把 struct tm * 类型的数据 转换为总的秒数
从1900年1月1号0点 开始的计时(自己查一下)
代码:getime()函数
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
time_t tm2 =0;
time_t tm1 = time(&tm2); //输出函数
struct tm* timp =gmtime(&tm1);
printf("year: %d month: %d day:%d hour: %d min:%d sec:%d \n",timp->tm_year+1900,timp->tm_mon+1,timp->tm_mday,(timp->tm_hour+8)%24,timp->tm_min,timp->tm_sec);
char * timstr = asctime(timp); //国际时间
puts(timstr);
char * local_time =asctime(localtime(&tm2));
puts(local_time);
fputs(local_time,stdout);
return 0;
}
ctime()
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用: ctime 的使用方法
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
void time1() //函数封装 程序作用 每一秒输入一个时间到文件里面去!
{
FILE* fp =fopen("1.txt","a+");
time_t tm1 =time(NULL); //输出函
char *s = ctime(&tm1);
fprintf(fp,"%s\n",s);
fclose(fp);
}
int main(int argc,char *argv[])
{
while(1)
{
sleep(1);
time1();
}
return 0;
}
mktime()
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用: mktime 的使用
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
void time1()
{
time_t tm1 =time(NULL);
struct tm* timp =gmtime(&tm1);
time_t tm2 =mktime(timp);
printf("%ld\n",tm2);
}
int main(int argc,char *argv[])
{
time1();
return 0;
}
函数 fcntl fcntl 两个参数 F_DUPFD
程序的作用:
检测 fcntl 函数的 复制 文件描述符
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用: fcntl 两个参数 F_DUPFD
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
system("touch hello.txt"); //创建文件
int fd = open("hello.txt",O_RDWR); //打开文件
printf("fd = %d\n",fd);
char W[100]={"dasdasdasda"};
write(fd,W,strlen(W));
int fd1 =fcntl(fd,F_DUPFD); //复制文件描述符
printf("fd1 =%d\n",fd1);
write(fd1,W,strlen(W));
lseek(fd,0,SEEK_SET);
int fd2 =fcntl(fd1,F_DUPFD,12); //复制文件描述符 并且给写好的值(之后一个参数)
printf("fd2 =%d\n",fd2);
char W1[1000]="";
read(fd2,W1,sizeof(W1));
fputs(W1,stdout);
printf("\n");
close(fd);
close(fd1);
close(fd2);
return 0;
}
函数 fcntl fcntl 两个参数 F_GETFL F_SETFL
程序的作用:
把 文件描述符STDIN_FILENO 的终端堵塞输入 ,改为 不堵塞。
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:把堵塞 变为不堵塞
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
//堵塞
char buff[20]="";
read(STDIN_FILENO,buff,sizeof(buff));
printf("buff is %s\n",buff);
//获取和设定
int flg = fcntl(STDIN_FILENO,F_GETFL);
int newflg = flg| O_NONBLOCK;
int res = fcntl(STDIN_FILENO,F_SETFL,newflg);//转换为不堵塞
if(!res)
puts("fcntl nonblock is ok\n");
else
puts("fcntl nonblock is not ok\n");
//不堵塞
memset(buff ,0 ,sizeof(buff));
read(STDIN_FILENO,buff,sizeof(buff));
printf("buff is %s\n",buff);
return 0;
}
函数 fcntl fcntl 两个参数 F_GETFL F_SETFL
程序的作用:加写锁 和 解锁的实验
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
//1.申请一把文件锁
struct flock lock ={0}; //初始化
//2.初始化一把锁
lock.l_type= F_WRLCK; //写锁
lock.l_whence = SEEK_SET; //开始的位置
lock.l_len =0; //全部内容都锁
lock.l_pid = getpid(); //获得进程号
//打开文件
int fp =open("hello.ttx",O_RDWR |O_CREAT| O_APPEND,0664);
if(fp <0 )
{
perror(" open error !\n ");
return -1;
}
//3.上锁
while(fcntl(fp,F_SETLK,&lock))
{
perror("fcntl error\n");
sleep(1);
}
printf("fcntl lock ok\n");
//4.读写操作 //其它任务 不可以操作固定文件
printf("this is fcntl text\n");
getchar();
//5.解锁
lock.l_type =F_UNLCK;
int a =fcntl(fp,F_SETLK,&lock);
if(a==0)
{
puts("fcntl unlock ok\n");
}
else
{
puts("fcnt1 unlock not ok\n");
}
return 0;
}
函数 :1.getpid()//自己的进程号
2. getuid() //用户进程号
3.getppid() //父进程
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
//获取当前任务的id
pid_t pid =getpid();
printf("自己的进程号:pid = %d\n",pid);
//获得父进程的id
//1.为子任务分配空间
//2.子程序执行结束后父任务回收
printf("父进程:parent id :%d\n",getppid());
//用户的进程号id
printf("用户的进程号:uid :%d\n",getuid());
//组id
printf("组id gid: %d\n",getgid());
//id所在的组的组id
int a=getgid();
printf("所在的组的组id :pgid is : %d\n",getpgid(a));
//当前进程所在的进程组id
printf("当前进程所在的进程组id pgrp is : %d\n",getpgrp());
return 0;
}
定义函数: int getpriority(int which, int who);
getpriority0可用来取得进程、进程组和用户的进程执行优先权。参数 which 有三种数值,
setpriority()
分配优先级:反映线程的重要或紧急程度
线程的优先级用1~10 表示,1的优先级最低,10的优先级最高,默认值是5
代码:
/************************************************************************************************************************************************************************************************************************
*文件名:
*作 者:She001
*时 间:
*版 本:
*作 用:
****************************************************************************************************************************************************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/wait.h>
#include<a.out.h>
#include<signal.h>
#include<stdarg.h>
#include<sys/socket.h>
#include<utime.h>
#include<sys/utsname.h>
#include<sys/times.h>
#include<sys/un.h>
#include<ctype.h>
#include<dirent.h>
#include<sys/time.h>
#include<sys/resource.h>
int main(int argc,char *argv[])
{
/*函数说明:
定义函数: int getpriority(int which, int who);
getpriority0可用来取得进程、进程组和用户的进程执行优先权。参数 which 有三种数值,
参数who
则依 which值有不同定义。
which who代表的意义:
1、PRIO_PROCESS who:为进程识别码
2、PRIO_PGRP who:为进程的组识别码
3、PRIO_USER who:为用户识别码此函数返回的数值介于-20至20之间,代表进程执行
优先权,数值越低代表有较高的优先次序,执行会较频繁.
*/
//获取进程id优先级为 0
int pro = getpriority(PRIO_PROCESS,getpid());//进程号优先级增加
printf("pro is : %d\n",pro);
//组id优先级为 0
int pro1 = getpriority(PRIO_PGRP,getpid());//组的进程号优先级增加
printf("pro1 is : %d\n",pro1);
/*优先级表示重要程度或者紧急程度.但是能不能抢到资源也是不一定.
分配优先级:反映线程的重要或紧急程度
线程的优先级用1~10 表示,1的优先级最低,10的优先级最高,默认值是5
*/ //设定优先级:
//只有root可以
if(setpriority(PRIO_PROCESS,getpid(),-2))
{
perror("setpriority error\n");
return -1;
}
printf("setpriority is ok\n");
//再查看一次
pro = getpriority(PRIO_PROCESS,pro);
printf("newpro is : %d\n",pro);
return 0;
}
好像这就是今天的内容了!