linux实验程序.docx
《linux实验程序.docx》由会员分享,可在线阅读,更多相关《linux实验程序.docx(46页珍藏版)》请在冰豆网上搜索。
linux实验程序
实验一文件创建
#include
#include
#include
#include
#include
voidcreate_file(char*filename)
{
/*创建的文件具有可读可写的属性*/
if(creat(filename,0666)<0)
{
printf("createfile%sfailure!
\n",filename);
exit(EXIT_FAILURE);
}
else
{
printf("createfile%ssuccess!
\n",filename);
}
}
intmain(intargc,char*argv[])
{
/*判断入参有没有传入文件名*/
if(argc<2)
{
printf("youhaven'tinputthefilename,pleasetryagain!
\n");
exit(EXIT_FAILURE);
}
create_file(argv[1]);
exit(EXIT_SUCCESS);
}
实验二文件拷贝
#include
#include
#include
#include
#defineBUFFER_SIZE1024
intmain(intargc,char**argv)
{
FILE*from_fd;
FILE*to_fd;
longfile_len=0;
charbuffer[BUFFER_SIZE];
char*ptr;
/*判断入参*/
if(argc!
=3)
{
printf("Usage:
%sfromfiletofile\n",argv[0]);
exit
(1);
}
/*打开源文件*/
if((from_fd=fopen(argv[1],"rb"))==NULL)
{
printf("Open%sError\n",argv[1]);
exit
(1);
}
/*创建目的文件*/
if((to_fd=fopen(argv[2],"wb"))==NULL)
{
printf("Open%sError\n",argv[2]);
exit
(1);
}
/*测得文件大小*/
fseek(from_fd,0L,SEEK_END);
file_len=ftell(from_fd);
fseek(from_fd,0L,SEEK_SET);
printf("fromfilesizeis=%d\n",file_len);
/*进行文件拷贝*/
while(!
feof(from_fd))
{
fread(buffer,BUFFER_SIZE,1,from_fd);
if(BUFFER_SIZE>=file_len)
{
fwrite(buffer,file_len,1,to_fd);
}
else
{
fwrite(buffer,BUFFER_SIZE,1,to_fd);
file_len=file_len-BUFFER_SIZE;
}
bzero(buffer,BUFFER_SIZE);
}
fclose(from_fd);
fclose(to_fd);
exit(0);
}
实验三获取本地时间
#include
#include
intmain(void)
{
structtm*ptr;
time_tlt;
/*获取日历时间*/
lt=time(NULL);
/*转化为本地时间*/
ptr=localtime(<);
/*以本地时间的字符串方式打印*/
printf("%s\n",asctime(ptr));
/*以本地时间的字符串方式打印*/
printf("%s\n",ctime(<));
return0;
}
实验四fork创建子线程
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
/*创建子进程*/
if((child=fork())==-1)
{
printf("ForkError:
%s\n",strerror(errno));
exit
(1);
}
else
if(child==0)//子进程
{
printf("Iamthechild:
%d\n",getpid());
exit(0);
}
else//父进程
{
printf("Iamthefather:
%d\n",getpid());
return0;
}
}
实验五vfork创建子线程
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
/*创建子进程*/
if((child=vfork())==-1)
{
printf("ForkError:
%s\n",strerror(errno));
exit
(1);
}
else
if(child==0)//子进程
{
sleep
(1);//子进程睡眠一秒
printf("Iamthechild:
%d\n",getpid());
exit(0);
}
else//父进程
{
printf("Iamthefather:
%d\n",getpid());
exit(0);
}
}
实验六exec函数族
#include
#include
#include
intmain(intargc,char*argv[])
{
/*判断入参有没有传入文件名*/
if(argc<2)
{
perror("youhaven,tinputthefilename,pleasetryagain!
\n");
exit(EXIT_FAILURE);
}
/*调用execl函数,用可执行程序file_creat替换本进程*/
if(execl("./file_creat","file_creat",argv[1],NULL)<0)
perror("execlerror!
");
}
实验七进程等待
#include
#include
#include
#include
#include
#include
#include
intmain(void)
{
pid_tchild;
/*创建子进程*/
if((child=fork())==-1)
{
printf("ForkError:
%s\n",strerror(errno));
exit
(1);
}
else
if(child==0)//子进程
{
printf("thechildprocessisrun\n");
sleep
(1);//子进程睡眠一秒,但并没有去运行父进程
printf("Iamthechild:
%d\n",getpid());
exit(0);
}
else//父进程
{
wait(NULL);//等到子进程退出,父进程才会运行
printf("thefatherprocessisrun\n");
printf("Iamthefather:
%d\n",getpid());
return0;
}
}
实验八无名管道
#include
#include
#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));
/*创建管道*/
if(pipe(pipe_fd)<0)
{
printf("pipecreateerror\n");
return-1;
}
/*创建子进程*/
if((pid=fork())==0)//子进程执行序列
{
printf("\n");
close(pipe_fd[1]);//子进程先关闭了管道的写端
sleep
(2);/*让父进程先运行,这样父进程先写子进程才有内容读*/
if((r_num=read(pipe_fd[0],buf_r,100))>0)
{
printf("%dnumbersreadfromthepipeis%s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
elseif(pid>0)//父进程执行序列
{
close(pipe_fd[0]);//父进程先关闭了管道的读端
if(write(pipe_fd[1],"Hello",5)!
=-1)
printf("parentwrite1Hello!
\n");
if(write(pipe_fd[1],"Pipe",5)!
=-1)
printf("parentwrite2Pipe!
\n");
close(pipe_fd[1]);
waitpid(pid,NULL,0);/*等待子进程结束*/
exit(0);
}
return0;
}
实验九有名管道
FIFO_READ
#include
#include
#include
#include
#include
#include
#include
#defineFIFO"/tmp/myfifo"
intmain(intargc,char**argv)
{
charbuf_r[100];
intfd;
intnread;
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);
}
//后面三句话是不会被运行到的,但不会影响程序运行的效果当程序在上面的死循环中执行时收到信号后会马上结束运行而没有执行后面的三句话。
这些会在后面的信号处理中讲到,现在不理解没有关系,这个问题留给大家学习了信号处理之后来解决。
close(fd);//关闭管道
pause();/*暂停,等待信号*/
unlink(FIFO);//删除文件
}
FIFO_write
#include
#include
#include
#include
#include
#include
#include
#defineFIFO_SERVER"/tmp/myfifo"
intmain(intargc,char**argv)
{
intfd;
charw_buf[100];
intnwrite;
/*创建有名管道*/
if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!
=EEXIST))
printf("cannotcreatefifoserver\n");
/*打开管道*/
fd=open(FIFO_SERVER,O_RDWR|O_NONBLOCK,0);
if(fd==-1)
{
perror("open");
exit
(1);
}
/*入参检测*/
if(argc==1)
{
printf("Pleasesendsomething\n");
exit(-1);
}
strcpy(w_buf,argv[1]);
/*向管道写入数据*/
if((nwrite=write(fd,w_buf,100))==-1)
{
if(errno==EAGAIN)
printf("TheFIFOhasnotbeenreadyet.Pleasetrylater\n");
}
else
printf("write%stotheFIFO\n",w_buf);
close(fd);//关闭管道
return0;
}
实验十信号处理
#include
#include
#include
/*自定义信号处理函数*/
voidmy_func(intsign_no)
{
if(sign_no==SIGBUS)
printf("IhavegetSIGBUS\n");
}
intmain()
{
printf("WaitingforsignalSIGBUS\n");
/*注册信号处理函数*/
signal(SIGBUS,my_func);
pause();//将进程挂起直到捕捉到信号为止
exit(0);
}
实验十一共享内存
shml
#include
#include
#include
#include
#include
#include
#include
#include"shm_com.h"
intmain(void)
{
intrunning=1;
void*shared_memory=(void*)0;
structshared_use_st*shared_stuff;
intshmid;
/*创建共享内存*/
shmid=shmget((key_t)1234,sizeof(structshared_use_st),0666|IPC_CREAT);
if(shmid==-1)
{
fprintf(stderr,"shmgetfailed\n");
exit(EXIT_FAILURE);
}
/*映射共享内存*/
shared_memory=shmat(shmid,(void*)0,0);
if(shared_memory==(void*)-1)
{
fprintf(stderr,"shmatfailed\n");
exit(EXIT_FAILURE);
}
printf("Memoryattachedat%X\n",(int)shared_memory);
/*让结构体指针指向这块共享内存*/
shared_stuff=(structshared_use_st*)shared_memory;
/*控制读写顺序*/
shared_stuff->written_by_you=0;
/*循环的从共享内存中读数据,直到读到“end”为止*/
while(running)
{
if(shared_stuff->written_by_you)
{
printf("Youwrote:
%s",shared_stuff->some_text);
sleep
(1);//读进程睡一秒,同时会导致写进程睡一秒,这样做到读了之后再写
shared_stuff->written_by_you=0;
if(strncmp(shared_stuff->some_text,"end",3)==0)
{
running=0;//结束循环
}
}
}
/*删除共享内存*/
if(shmdt(shared_memory)==-1)
{
fprintf(stderr,"shmdtfailed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Shm2
#include
#include
#include
#include
#include
#include
#include
#include"shm_com.h"
intmain(void)
{
intrunning=1;
void*shared_memory=(void*)0;
structshared_use_st*shared_stuff;
charbuffer[BUFSIZ];
intshmid;
/*创建共享内存*/
shmid=shmget((key_t)1234,sizeof(structshared_use_st),0666|IPC_CREAT);
if(shmid==-1)
{
fprintf(stderr,"shmgetfailed\n");
exit(EXIT_FAILURE);
}
/*映射共享内存*/
shared_memory=shmat(shmid,(void*)0,0);
if(shared_memory==(void*)-1)
{
fprintf(stderr,"shmatfailed\n");
exit(EXIT_FAILURE);
}
printf("Memoryattachedat%X\n",(int)shared_memory);
/*让结构体指针指向这块共享内存*/
shared_stuff=(structshared_use_st*)shared_memory;
/*循环的向共享内存中写数据,直到写入的为“end”为止*/
while(running)
{
while(shared_stuff->written_by_you==1)
{
sleep
(1);//等到读进程读完之后再写
printf("waitingforclient...\n");
}
printf("Enersometext:
");
fgets(buffer,BUFSIZ,stdin);
strncpy(shared_stuff->some_text,buffer,TEXT_SZ);
shared_stuff->written_by_you=1;
if(strncmp(buffer,"end",3)==0)
{
running=0;//结束循环
}
}
/*删除共享内存*/
if(shmdt(shared_memory)==-1)
{
fprintf(stderr,"shmdtfailed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Shm_com
#defineTEXT_SZ2048
structshared_use_st
{
intwritten_by_you;
charsome_text[TEXT_SZ];
};
实验十二消息队列
Msg1
#include
#include
#include
#include
#include
#include
#include
#include
structmy_msg_st
{
longintmy_msg_type;
charsome_text[BUFSIZ];
};
intmain(void)
{
intrunning=1;
intmsgid;
structmy_msg_stsome_data;
longintmsg_to_