file_creat(argv[i]);
}
//printf("%d,%s\n",*argv[0],argv[0]);
//printf("%d,%s\n",*argv[1],argv[1]);
//printf("%d\n",*argv[2]);
exit(EXIT_SUCCESS);
}
实验结果:
创建”filecreat”文件
项目五Linux系统下文件综合设计
一实验目的
1理解文件系统中文件的打开和关闭实现过程
2理解文件系统中文件读和写的实现过程
3利用文件系统调用函数实现文件的基本操作过程
4理解缓冲区在文件系统中的作用
二实验内容
1理解open函数和close函数的调用过程
2理解read函数和write函数的调用过程
3利用上述函数实现文件的复制过程,并利用缓冲区来减少系统调用次数
实验程序
#include
#include
#include
#include
#include
#include
#defineBUFFER_SIZE1024
intmain(intargc,char*argv[])
{
intfrom_fd,to_fd;
intbytes_read,bytes_write;
charbuffer[BUFFER_SIZE];
char*ptr;
if(argc!
=3)
{
printf("file_copyerror,pleaseinput2filename\n");
exit
(1);
}
/*打开源文件*/
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
printf("openfile1error\n");
exit
(1);
}
/*创建目的文件*/
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
printf("creatfile2error\nn");
exit
(1);
}
/*以下代码是一个经典的靠背文件的代码*/
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!
=EINTR))break;
elseif(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
if((bytes_write==-1)&&(errno!
=EINTR))break;
elseif(bytes_write==bytes_read)break;
elseif(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
项目六fork创建子进程
一实验目的
学会使用linux系统调用fork函数
二实验要求
编写一个应用程序,在程序中创建一个子进程,分别在父进程和子进程中打印进程ID
实验程序
#include
#include
#include
intmain()
{
pid_tpid;
intcount=0,i=0;
printf("i=%d\n",i);
pid=fork();
count++;
printf("count=%d\n",count);
if(pid<0)
printf("error\n");
elseif(pid==0)
printf("I'mthechildfork,ID=%d\n",getpid());
else
printf("I'mtheparentfork,ID=%d\n",getpid());
}
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
intcount=0;
child=vfork();
count++;
if(child==0)//子进程
{
sleep
(1);//子进程睡眠一秒
printf("Iamthechild:
%d\n",getpid());
printf("%d\n",count);
exit(0);
}
else//父进程
{
printf("Iamthefather:
%d\n",getpid());
printf("%d\n",count);
exit(0);
}
}
项目七vfock创建子进程
一实验目的
学会实验linux的系统调用函数vfock,理解fock和vfock函数的区别
二实验要求
实验vfock创建一个子进程,分别在父进程和子进程中打印进程id,观察父进程和子进程的运行程序
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
if((child=vfork())==-1)
{
printf("vforkerror\n");
exit
(1);
}
else
if(child==0)//子进程
{
sleep(3);//子进程睡眠一秒
printf("Iamthechild:
%d\n",getpid());
exit(0);
}
else//父进程
{
printf("Iamthefather:
%d\n",getpid());
exit(0);
}
}
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
intcount=0;
child=vfork();
count++;
if(child==0)//子进程
{
sleep
(1);//子进程睡眠一秒
printf("Iamthechild:
%d\n",getpid());
printf("%d\n",count);
exit(0);
}
else//父进程
{
printf("Iamthefather:
%d\n",getpid());
printf("%d\n",count);
exit(0);
}
}
项目八进程等待
一、实验目的
学会使用linux系统调试wait函数
二、实验要求
编写一个应用程序,在程序中创建一个子进程,父进程需等待子进程运行结束后才能执行
实验程序:
#include
#include
#include
#include
#include
intmain()
{
pid_tpc,pr;
pc=fork();//pc是fock的返回值,返回值是0代表子进程,若非0代表父进程
if(pc==0)
{
printf("Iamchildprocesswithpid:
%d\n",getpid());
sleep(10);
}
else
{
pr=wait(NULL);//等待子进程结束
printf("Icaptureachildprocesswithpid:
%d\n",pr);
}
exit(0);
}
项目九有名管道和无名管道
一、实验目的
(1)理解进程通信机制中有名管道和无名管道的实现原理
(2)掌握管道的建立、读、写等函数的使用
(3)掌握有名管道和无名管道实现进程之间的通信
二、实验内容
编写程序:
(1) 编写管道通信程序:
要求主进程创建子进程后,在子进程中调用exec函数执行一个新程
序前,通过管道给即将执行的程序传递命令行参数,包括:
exit,子进程退出;getpid,获取进程号等。
(2)编写有名管道程序:
实现写进程获得从键盘输入的数据,读进程获得从写进程端读的数据
并显示在屏幕中。
实验程序:
#include
#include
#include
#include
intmain()
{
intpipe_fd[2];
pid_tpid;
charbuf_r[100];
char*p_wbuf;
intr_num;
memset(buf_r,0,sizeof(buf_r));//buf_r数组清零
/*创建管道*/
if(pipe(pipe_fd)<0)
{
printf("pipecreaterror\n");
return-1;
}
/*创建子进程*/
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);//子进程不需要写管道,可以关掉
sleep(8);//睡眠两秒,让父进程去写管道
if(r_num=read(pipe_fd[0],buf_r,100)>0)//从管道中读100个字节到buf_r中
{
printf("Childprocessreadspipesuccessfully!
\n");
}
close(pipe_fd[0]);
exit(0);
}
elseif(pid>0)
{close(pipe_fd[0]);//父进程不需要读管道,可以关掉
if(write(pipe_fd[1],"hello",5)>0)//向管道中写hello
printf("Thisisfirstwritingoftheparentpipe!
\n");
if(write(pipe_fd[1],"pipe",4)>0)//向管道中写pipe
printf("Thisissecondwritingoftheparentpipe!
\n");
close(pipe_fd[1]);
sleep(8);//睡眠3s,让子进程读管道
waitpid(pid,NULL,0);
exit(0);
}
}
#include
#include
#include
#include
intmain()
{
intpipe_fd[2];
pid_tpid;
charbuf_r[100];
char*p_wbuf;
intr_num;
memset(buf_r,0,sizeof(buf_r));//buf_r数组清零
/*创建管道*/
if(pipe(pipe_fd)<0)
{
printf("pipecreaterror\n");
return-1;
}
/*创建子进程*/
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);//子进程不需要写管道,可以关掉
sleep(8);//睡眠两秒,让父进程去写管道
if(r_num=read(pipe_fd[0],buf_r,100)>0)//从管道中读100个字节到buf_r中
{
printf("Childprocessreadspipesuccessfully!
\n");
printf("%s\n",buf_r);
}
close(pipe_fd[0]);
exit(0);
}
elseif(pid>0)
{close(pipe_fd[0]);//父进程不需要读管道,可以关掉
if(write(pipe_fd[1],"hello",6)>0)//向管道中写hello
printf("Thisisfirstwritingoftheparentpipe!
\n");
if(write(pipe_fd[1],"pipe",4)>0)//向管道中写pipe
printf("Thisissecondwritingoftheparentpipe!
\n");
close(pipe_fd[1]);
sleep(8);//睡眠3s,让子进程读管道
waitpid(pid,NULL,0);
exit(0);
}
}
实验总结 :
通过本次实验理解了进程通信机制中有名管道和无名管道的实现原理,有名管道和无名管道实现通信的区别,以及文件描述符重定向和流重定向;掌握了管道的建立、读、写等函数的使用,掌握了有名管道和无名管道实现进程之间的通信。
项目十进程之间的通信
一、实验目的
为了理解和掌握UNIX和Linux进程通信系统调用的功能,这里给出了进程通信实现机制中使用的系统调用命令的格式和如何利用系统调用命令进行进程通信编程,以便通过学习,提高学生对进程通信系统调用的编程能力。
二、实验要求
编写一个有名管道程序。
一个(客户)进程从键盘循环读一系列字符,将这些字符和发送者的pid发给服务器进程,让其统计输入的是字符还是数字,分别为多少个,完成后再向客户进程发回服务的结果,由客户进程输出。
实验程序:
Shell1
#include
#include
#include
#include
#include
#include
#include
#defineFIFO"/tmp/myfifo"
intmain()
{
charbuf_r[100];
intfd;
intnread;
if(mkfifo(FIFO,O_CREAT|O_EXCL)<0&&(errno!
=EEXIST))
printf("cannotcreatfifoserver\n");
printf("Preparingforreadingbytes...\n");
memset(buf_r,0,sizeof(buf_r));
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==1)
{
perror("open");
exit
(1);
}
while
(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd,buf_r,100)==-1));
{
if(errno==EAGAIN)
printf("nodatayet\n");
}
printf("read%sfromFIFO\n",buf_r);
sleep
(1);
}
pause();
}
Shell2
#include
#include
#include
#include
#include
#include
#include
#defineFIFO_SERVER"/tmp/myfifo"
intmain(intargc,char*argv[])
{
intfd;
charw_buf[100];
intnwrite;
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{