【无标题】

实验二,

第一题,

  • 创建文件file1,写入字符串“abcdefghijklmn”;
  • 创建文件file2,写入字符串“ABCDEFGHIJKLMN”;
  • 读取file1中的内容,写入file2,使file2中的字符  串内容为“ ABCDEFGHIJKLMNabcdefghijklmn

#include<stdio.h>

#include<fcntl.h>

int main(){

    int fd1,fd2;

    char str[14];

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

    if(fd1 < 0)

        perror("open file1 error");

    write(fd1,"abcdefghijklmn",14);

    lseek(fd1,0,SEEK_SET);

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

    if(fd2 < 0)

        perror("open file2 error");

    write(fd2,"ABCDEFGHIJKLMN",14);

    read(fd1,str,14);

    write(fd2,str,14);

    close(fd1);

    close(fd2);

    return 0; }

第二题,

  1. 编写代码,完成以下功能:
  2. 1.创建新文件,该文件具有用户读写权限。
  3. 2.采用dup/dup2/fcntl复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”字符串;
  4. 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. 编写程序完成以下功能:
  2. 1.递归遍历/home目录,打印出所有文件和子目录名称及节点号。
  3. 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. 编写代码实现以下功能:
  1. 1.打印字符串“hello world!”
  1. 在打印字符串“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. 创建子进程
  2. 1.在子进程中打开文件file1,写入自己的“班级_姓名_学号”,
  3. 2.父进程读取file1中的内容,并且打印显示。
  1. 在父进程中获取已经结束的子进程的状态信息,打印该信息,并且打印结束的子进 程的进程号。

#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] = "zhinengyiban_mapenghui_1915925470";

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;

}

file文件

实验四,第三题

编写程序实现以下功能:

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

写进程向有名管道文件写入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");

}

}

编程:

4.两个文件之间的拷贝

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

int main()

{

    int fd , fdcopy;

    char buf[15]={'0'};

    fd = open("./test.txt", O_RDWR|O_CREAT);

    

    fdcopy=dup(fd);

    

    write(fdcopy,"CLASS_NAME",10);

    

    lseek(fdcopy, 0, SEEK_SET);

    

    read(fd, buf, 10);

    

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

    close(fd);

    return 0;

}

5.打开一个文件,把小写字母转换到大写字母写到另一个文件中?

#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;

}

5.3个fork打印8个helloword

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

int main()

{fork();

fork();

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;

 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值