UNIX实验项目四:UNIX系统核心技术研究与实践

来源:互联网 发布:软件数据彻底清除 编辑:程序博客网 时间:2024/05/21 17:42

       

 

学号

03093245

姓名

 

课堂号

09078051-0

实验日期

2006/4/24

实验名称

实验项目四:UNIX系统核心技术研究与实践

实验用时

8

一、实验目的和要求

1)、掌握教材中有关文件管理与应用的内容。

2)、掌握进程的概念,深入理解进程的含义。熟悉进程管理中系统调用的使用。掌握使用管道通信和消息队列通信的基本编程技术。

 

二、实验内容及操作说明

1)、编写一段c语言程序使其完成:对用户输入的文件内容进行拷贝。

用户输入“exe afile bfile”,则将文件afile的内容拷贝到文件bfile中。

第一步:在vi中编写程序代码:

 

#include "string.h"

mail(argc,argv)

int argc;

char *argv[ ];

{

int fd1, fd2, n;

char buf[512],ch='/n'

if (argc<=2)

  {

  printf("you forget the enter a filename/n");

  exit(1);

  }

fd1=open(argv[1],0);

fd2=creat(argv[2],0644);

while((n=read(fd1,buf,512))>0)

  write(fd2,buf,n);

close(fd1);

close(fd2);

}

daiyancp.c: new file: 21 lines, 306 characters

~$

如图1所示:

图1

 

第二步:编译执行程序如下:

$ gcc daiycp.c -o daiycp.exe

$

$ ls -l daiycp.*

-rw-r--r--  1 daiyanxg  people   282 Jun 12 02:51 daiycp.c

-rwxr-xr-x  1 daiyanxg  people  6687 Jun 12 09:49 daiycp.exe

$

$ daiycp.exe

/bin/sh: daiycp.exe: not found

$ ./daiycp.exe

you forget enter a finename

$ ls

*           daiycp.c    file1       myshell     test2

copy.c      daiycp.exe  file2       score.bsh   test3

daicp.c     dycp.c      mycp.c      shell1      youname

daiyancp.c  dyfile.c    myscore.bsh show.bsh    youyname

$ cat show.bsh

 

#! /bin/sh

if [ $# -eq 0 ]

then

  echo "Usage:$0 directory"

else

for myDir in $*

do

if test -d $myDir

then

  echo "In $myDir"

  cd $myDir

  echo 'ls *.txt *.sh color'

else

  echo "Error:Direct $myDir cant't found!"

fi

done

fi

exit 0;

 

$ ./daiycp.exe show.bsh test.bsh    //test.bsh是新文件,用于测试。

$ cat test.bsh

 

#! /bin/sh

if [ $# -eq 0 ]

then

  echo "Usage:$0 directory"

else

for myDir in $*

do

if test -d $myDir

then

  echo "In $myDir"

  cd $myDir

  echo 'ls *.txt *.sh color'

else

  echo "Error:Direct $myDir cant't found!"

fi

done

fi

exit 0;

 

$                                        //结果显示程序执行正确。

编译执行过程如图2、图3所示

 

至此,改部分正确完成。

 

2)、编写一段c语言程序使其完成:创建一个新文件,输入一段数据,然后随机移动指针,接着插入一段数据。完成后查看该文件的大小和内容。

第一步:在vi中编写程序:daiyanfile.c

#include<stdio.h>

#include<unistd.h>

char buf1[]="I am daiyan";

char buf2[]="123456789";

main()

{

int fid;

if((fid=creat("TEST",0644))<0)

{

printf("creat file error/n");

exit(0);

}

else

{

if(write(fid,buf1,5)!=5)

{

printf("buf1 write error/n");

exit(1);

}

if(lseek(fid,30,SEEK_SET)==-1)

{

printf("lseek errpr/n");

exit(2);

}

if(write(fid,buf2,5)!=5)

{

printf("buf2 write error/n");

exit(3);

}

close(fid);

}

exit(0);

}

 

$

程序如图4所示:

 

       第二步:编译程序daiyanfile.cdaiyanfile.exe

$ gcc daiyanfile.c

$ ls -l daiyanfile.*

-rw-r--r--  1 daiyanxg  people  420 Jun 12 10:35 daiyanfile.c

$ gcc daiyanfile.c -o daiyanfile.exe

$ ls -l daiyanfile.*

-rw-r--r--  1 daiyanxg  people   420 Jun 12 10:35 daiyanfile.c

-rwxr-xr-x  1 daiyanxg  people  6777 Jun 12 10:37 daiyanfile.exe

 

编译过程如图5所示:

第三步:执行daiyanfile.exe

$ ls -l daiyanfile.c

-rw-r--r--  1 daiyanxg  people  420 Jun 12 10:35 daiyanfile.c

$ daiyanfile.exe

/bin/sh: daiyanfile.exe: not found

$ ./daiyanfile.exe

$ ls -l daiyanfile.c

-rw-r--r--  1 daiyanxg  people  420 Jun 12 10:35 daiyanfile.c

$ ls - -l TEST

ls: -: No such file or directory

ls: -l: No such file or directory

TEST

$ ls -l TEST

-rw-r--r--  1 daiyanxg  people  35 Jun 12 10:44 TEST

$ od -c TEST

0000000    I       a   m      /0  /0  /0  /0  /0  /0  /0  /0  /0  /0  /0

0000020   /0  /0  /0  /0  /0  /0  /0  /0  /0  /0  /0  /0  /0  /0   1   2

0000040    3   4   5

0000043

$

执行结果如图6所示:

  至此,该部分成功完成。

 

3)、编写一段c语言程序使其完成:父进程完成子进程,子进程运行中显示当前系统的记录时钟;父进程运行时完成将数字1100循环显示到标准输出流上;同时注意程序运行时控制其执行顺序为子进程先运行,父进程再运行。

第一步:在vi中书写程序father2.c,代码如下:

#include<stdio.h>

#include<time.h>

#include<sys/types.h>

void show_systime(void);

main()

{

pid_t pid;

int i;

pid=fork();

if(pid<0)

{

perror("filed ehwn creating new process/n");

exit(1);

}

else

if(pid==0)

{show_systime();

}

else

{wait(NULL);

for(i=1;i<=100;i++)

{printf("%d",i);

printf("/n");}

}

}

void show_systime(void)

{

time_t t;

if(time(&t)==((time_t)-1))

{printf("Error when getting time!/n");

exit(1);}

else{

char *tt;

tt=ctime(&t);

printf("now is %s/n",tt);}

}

 

上述代码如图7所示:

第二步:编译执行程序,过程如下:

$ gcc father2.c -o father2.exe

$ ls -l father2.*

-rw-r--r--  1 daiyanxg  people   477 Jun 12 20:25 father2.c

-rwxr-xr-x  1 daiyanxg  people  6781 Jun 12 20:25 father2.exe

$ ./father2.exe

now is Mon Jun 12 20:30:43 2006

 

1

2

3

4

5

6

7

8

9

10

┅ ┅ //省略118-89的输出数字以减少篇幅

90

91

92

93

94

95

96

97

98

99

100

$

执行过程成功完成,如下面三幅截图(图8、9、10)所示:

该程序成功完成。

 

4)、编写一段C语言程序,使其完成父子进程通过无名管道传递如下三条消息:

管道文件的测试程序开始

管道文件测试正在进行

管道通信测试结束

接收进程收到信息后将它们送到标准输出文件上。

第一步:vi中书写程序pipe.c,代码如下:

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<string.h>

main()

{

int i, aa[2];

char message[3][BUFSIZ]={"start the test of pipe file","I am daiyan","Hello!"};

if(pipe(aa)==-1)

{

perror("Pipe");

exit(2);

}

switch(fork())

{

case -1:

 perror("fork");

exit(3);

case 0:

 close(aa[1]);

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

{if(read(aa[0],message[i],BUFSIZ)!=-1)

{printf("message received by child :[%s]/n",message[i]);

fflush(stdout);

}

else

{perror("read failed");

exit(4);

}

}

break;

default:

close(aa[0]);

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

{

if(write(aa[i],message[i],BUFSIZ)!=-1)

{

printf("Message sent by parent :[%s]/n",message[i]);

fflush(stdout);

}

else

{perror("write failed!");

exit(5);

}

}

 

}

exit(0);

}

上述程序如下面两幅截图(图11、12)所示:

 

第二步:编译pipe.c pipe.exe,并执行pipe.exe

$ gcc pipe.c

$ gcc pipe.c -o pipe.exe

$ ls -l pipe.*

-rw-r--r--  1 daiyanxg  people    695 Jun 12 22:00 pipe.c

-rwxr-xr-x  1 daiyanxg  people  10838 Jun 12 22:01 pipe.exe

$ ./pipe.exe

write failed!: Bad file descriptor

message received by child :[start the test of pipe file]

$ message received by child :[I am daiyan]

message received by child :[Hello!]

 

$

 

编译过程及程序执行结果如下图(图13)所示:

该程序成功完成。至此,该实验全部程序成功完成。

三、回答问题

    各个程序执行结果详见第二步,均有文字拷贝和效果截图。

四、实验结论及问题

本实验通过编写C语言程序练习文件的读,写系统调用,改变文件指针位置;进程创建、同步,以及无名管道通信等一系列操作。通过此次实验,掌握了creat,read,write,lseek,wait,fork,close,pipe等命令的使用,理解了unix系统在文件管理,进程通信等方面的原理及实现方法。

 

 

 

 
原创粉丝点击