概述
文件操作
文章目录
stat()
access()
chmod()
truncate()
link()
1.1 stat 函数
#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
用于获取文件的属性
参数说明:
path:文件路径;
buf:接收获取到的文件属性;文件属性存储在inode中,函数从inode结构体中获取文件信息。
返回描述:
成功:0
不成功:-1并设置errno
案例分析
代码如下(示例):
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(){
struct stat tempSBuf;
int tempRet = 2;
tempRet = stat("a.out", &tempSBuf);
if(tempRet == -1){
perror("stat error:");
exit(1);
}//of if
printf("len = %dn", tempSBuf.st_size);
return 0;
}//of main
运行截图:
1.2 access 函数
#include <unistd.h>
int access(const char *pathname, int mode);
用于测试文件是否拥有某种权限
参数说明:
pathname:文件名;
mode:取值有4个:R_OK, W_OK, X_OK, F_OK;前面3个是测试文件是否有读、写、执行权限,最后一个测试文件是否存在。
返回描述:
成功:0,表示文件存在或具有某种权限。
不成功:-1并设置errno
1.3 chmod函数
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
用于修改文件的访问权限
参数说明:
path:路径名;
mode:传递修改后的权限。
返回描述:
成功:0,表示文件存在或具有某种权限;
不成功:-1并设置errno。
1.4 truncate函数
#include <sys/stat.h>
int truncate(const char *path, off_t length);
用于修改文件大小,常用于扩展文件,其功能与lseek函数类似
参数说明:
path:路径名;
length:设置文件大小。
返回描述:
成功:0;
不成功:-1并设置errno。
进程控制
2.1 创建进程
#include <unistd.h>
pid_t fork(void);
用于创建进程函数执行后,系统会创建一个与原进程几乎相同的进程,之后父子进程都继续执行,如图所示:
返回说明:
成功:返回两个值,子进程创建成功后,原程序会被复制,就有了两个fork函数。父进程的fork函数会返回子进程的pid,子进程的fork函数会返回0.
不成功:若子进程创建失败,原程序不会复制,父进程的fork函数返回-1。
【案例 1】使用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 = %dn", getpid(), getppid());
}else{//child
printf("child process, pid = %d, ppid = %dn", getpid(), getppid());
}//of if
printf("......finish......");
return 0;
}//of main
运行截图:
【思考 1】多次执行test_fork会发现,child process后输出的ppid不等于parent process的pid,而等于1。请说明原因。
【提示】出现这种情况,是因为父进程先于子进程终止,子进程变成“孤儿进程”,后面由init进程来接收。
2.2 创建多个进程
int i;
for(i = 0; i < 2; i ++){
tempPid = fork();
}//of for i
每次调用fork函数,系统会复制原程序
i=0,第一次循环,会有两份test_fork文件
i=1,第二次循环,第一份test_fork文件又会有两份test_fork文件,第二份test_fork文件也会有两份
每一次循环,进程的总数是当前进程数量的两倍,2次循环则为2 2 = 4 2^2 = 42
2
=4个进程。
【案例 2】
#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 = %dn", getpid(), getppid());
}else{//child
printf("I am child process = %d, pid = %d, ppid = %dn", i + 1, getpid(), getppid());
}//of if
printf("......finish......");
return 0;
}//of main
运行截图:
2.3 进程的执行顺序:利用sleep函数,暂缓进程执行
【案例 3】
#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 = %dn", getpid(), getppid());
}else{//child
sleep(i);
printf("I am child process = %d, pid = %d, ppid = %dn", i + 1, getpid(), getppid());
}//of if
printf("......finish......");
return 0;
}//of main
运行截图:
最后
以上就是野性大船为你收集整理的Linux编程基础 2.2:文件操作&3.1:进程管理-1(学后心得)的全部内容,希望文章能够帮你解决Linux编程基础 2.2:文件操作&3.1:进程管理-1(学后心得)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复