ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux基础实验

2021-12-23 02:04:04  阅读:243  来源: 互联网

标签:file2 file1 int printf 基础 实验 Linux fd include


实验一
1.创建文件file1,写入字符串“abcdefghijklmn”;
2.创建文件file2,写入字符串“ABCDEFGHIJKLMN”;
3.读取file1中的内容,写入file2,使file2中的字符串内容为“ ABCDEFGHIJKLMNabcdefghijklmn”

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include <fcntl.h>

int main()

{

int file1,file2;

char str[20]={'0'};

file1 = open("./file1.txt",O_RDWR|O_CREAT,0777);

file2 = open("./file2.txt",O_RDWR|O_CREAT,0777);

write(file1,"abcdefghijklmn",14);   

lseek(file2, 17, SEEK_SET);

write(file2,"ABCDEFGHIJKLMN",14);    

lseek(file1, 0, SEEK_SET);

read(file1,str,14);

lseek(file2, 0, SEEK_SET);

write(file2,str,14);

close(file1);

close(file2);

return 0;

}

实验二
 编写代码,完成以下功能:
1.创建新文件,该文件具有用户读写权限。
2.采用dup/dup2/fcntl复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”字符串;
3.通过原有的文件描述符读取文件中的内容,并且打印显示;

#include<stdio.h>

#include<unistd.h>

#include<stdlib.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<string.h>

int main(int argc,char argv[])

{

int fd;

fd=open("test2.file",O_CREAT|O_WRONLY,S_IREAD|S_IWRITE);

char *name="class_name";

int fd2 = dup(fd);

if(fd2<0){

perror("dup");

}

write(fd2,name,strlen(name));

lseek(fd,0,SEEK_SET);

char str[12];

read(fd,str,12);

printf("%s\n",str);

close(fd);

return 0;

}

实验三:

编写代码实现以下功能:

1.打印字符串“hello world!”

2.在打印字符串“hello world!”前调用三次fork,分析打印结果。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

int main()

{

int i;

for(i=0;i<3;i++)

{

fork();

}

printf("hello world!!!\n");

return 0;

}

 

 

实验六
编写程序完成以下功能:
1.递归遍历/home目录,打印出所有文件和子目录名称及节点号。
2.判断文件类型,如果是子目录,继续进行递归遍历,直到遍历完所有子目录为止.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <errno.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <dirent.h>

int show (char * path)

{

char p[500];

DIR *dir;

struct stat statbuf;

struct dirent *dire;

lstat (path,&statbuf);

if (S_ISDIR(statbuf.st_mode))

{

dir = opendir (path);

if (dir)

{

while( (dire = readdir(dir) ) !=NULL)

{

if(( dire ->d_name[0] )=='.')

continue;

sprintf(p,"%s/%s",path,dire->d_name);

lstat(p,&statbuf);

printf ("\t该目录文件名为: %s \n",p);

printf ("\t该目录文件节点号为: %ld \n",statbuf.st_ino);

show (p);

}

}

}

if (S_ISREG(statbuf.st_mode)){

printf ("该文件名为: %s \n",path);//输出文件名

printf ("该文件节点号为: %ld \n",statbuf.st_ino);

}

}

int main()

{

show("/home");

return 0;

}

1.在子进程中打开文件file1,写入自己的“班级_姓名_学号”,

2.父进程读取file1中的内容,并且打印显示。

3.在父进程中获取已经结束的子进程的状态信息,打印该信息,并且打印结束的子进程的进程号。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/wait.h>

int main()

{

int fd,pid;

fd = open("file",O_CREAT|O_RDWR,S_IRWXU);

if(fd< 0){

perror("open");}

pid = fork();

if(pid == 0)

{

printf("This is the child!\n");

char str[128]= "zhinengsanban_liaoxianqiang_1915925583";

if(write(fd,str,128) < 0){

perror("write");}

exit(5);

}

else

{

printf("This is the father!\n");

char buf[128];

int n,status;

if(read(fd,buf,128) < 0){

perror("read");}

printf("The buf is: %s\n",buf);

if(wait(&status) < 0){

perror("perror");}

if(WIFEXITED(status)){

n = WEXITSTATUS(status);}

else{

printf("wait error!\n");}

printf("The child's pid is: %d\n",pid);

printf("The child exit status is: %d\n",n);

}

return 0;

}

3.编写程序实现以下功能:

利用有名管道文件实现进程间通信,要求

写进程向有名管道文件写入10次“hello world”;

读进程读取有名管道文件中的内容,并依次打印

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/stat.h>

int main()

{

int pid,fd;

if(mkfifo("fifotest",0666) < 0)

  perror("mkfifo");

pid = fork();

if(pid < 0)

  perror("fork");

else if(pid == 0)

{

printf("This is the write process!\n");

int fd = open("fifotest",0666);

for(int i = 0; i < 10;i++)

{

if(write(fd,"hello world",12) < 0)

perror("write");

sleep(1);

}

close(fd);

}

else

{

char str[128];

printf("This is the read process!\n");

int fd1 = open("fifotest",0666);

 

for(int i = 0;i < 10;i++)

{

if(read(fd1,str,128) < 0)

  perror("read");

else

  printf("%s\n",str);

}

system("rm -f fifotest");

}

}

标签:file2,file1,int,printf,基础,实验,Linux,fd,include
来源: https://www.cnblogs.com/w0112y/p/qwee.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有