stat()函数获取文件属性
运用:使用stat()函数获取文件属性,输出文件大小。
写入文件内容
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(){
struct stat tempSBuf;
int tempRet = 2;
tempRet = stat("f1.txt", &tempSBuf);
if(tempRet == -1){
perror("< start error! >");
exit(1);
}//of if
printf("size = %ld\n", tempSBuf.st_size);
return 0;
}//of main
输出文件大小:
access()函数测试文件权限
运用:测试文件是否存在,是否有读、写、执行权限
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(){
struct stat tempSBuf;
int tempR = 2;
int tempW = 2;
int tempX = 2;
int tempF = 2;
tempR = access("f1.txt", R_OK);
tempW = access("f1.txt", W_OK);
tempX = access("f1.txt", X_OK);
tempF = access("f1.txt", F_OK);
if(tempF == -1){
perror("The file is not exist!\n");
exit(1);
}else{
printf("The file is exist\n");
}
if(tempR == -1){
perror("Read error!\n");
exit(1);
}else{
printf("Have Read mode\n");
}
if (tempW == -1){
perror("Write error!\n");
exit(1);
}else{
printf("Have Write mode\n");
}
if (tempX == -1){
perror("Execute error!\n");
exit(1);
}else{
printf("Have Execute mode\n");
}
//of if
return 0;
}//of main
输出结果:
修改文件访问权限函数:
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
运用:修改文件权限为所有者可读、不可写、不可执行,其他人有所有权限
在之前的代码上进行添加
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(){
struct stat tempSBuf;
int tempR = 2;
int tempW = 2;
int tempX = 2;
int tempF = 2;
int temp_change = 2;
temp_change = chmod("f1.txt", 0477); //修改权限
tempR = access("f1.txt", R_OK);
tempW = access("f1.txt", W_OK);
tempX = access("f1.txt", X_OK);
tempF = access("f1.txt", F_OK);
if (temp_change == -1){
perror("Change error!\n");
exit(1);
}else{
printf("Have changed\n");
}
if(tempF == -1){
perror("The file is not exist!\n");
exit(1);
}else{
printf("The file is exist\n");
}
if(tempR == -1){
perror("Read error!\n");
exit(1);
}else{
printf("Have Read mode\n");
}
if (tempW == -1){
perror("Write error!\n");
exit(1);
}else{
printf("Have Write mode\n");
}
if (tempX == -1){
perror("Execute error!\n");
exit(1);
}else{
printf("Have Execute mode\n");
}
//of if
return 0;
}//of main
运行结果:因为文件所有者只有读权限,所以测试写权限拒绝。
修改文件大小,与修改权限类似,不作示例。
#include <sys/stat.h>
int truncate(const char *path, off_t length);
进程
利用fork函数创建进程
运用:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
pid_t tempPid;
tempPid = fork();
if(tempPid == -1){
perror("fork error");
}else if(tempPid > 0){//parent
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
}else{//child
printf("child process, pid = %d, ppid = %d\n", getpid(), getppid());
}//of if
printf("---finish---\n");
return 0;
}//of main
输出结果:
循环创建多个子进程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
pid_t tempPid;
int i;
for(i = 0; i < 2; i ++){
if((tempPid = fork()) == 0){
break;
}//of if
}//of for i
if(tempPid == -1){
perror("fork error");
}else if(tempPid > 0){//parent
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
}else{//child
printf("I am child process = %d, pid = %d, ppid = %d\n", i + 1, getpid(), getppid());
}//of if
printf("---finish---\n");
return 0;
}//of main
运行结果:
在多次运行后出现终端出现后依旧输出进程信息,因为终端与其它进程公平竞争CPU因此终端出现后可能仍然有进程信息输出
为了避免上面的的情况,可以使用sleep函数来暂缓进程执行
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
pid_t tempPid;
int i;
for(i = 0; i < 2; i ++){
if((tempPid = fork()) == 0){
break;
}//of if
}//of for i
if(tempPid == -1){
perror("fork error");
}else if(tempPid > 0){//parent
sleep(2);
printf("parent process, pid = %d, ppid = %d\n", getpid(), getppid());
}else{//child
sleep(i);
printf("I am child process = %d, pid = %d, ppid = %d\n", i + 1, getpid(), getppid());
}//of if
printf("---finish---\n");
return 0;
}//of main
运行结果: