linux编程基础——文件、进程

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

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值