您的位置:首页 > 其它

第十二周预习报告

2015-11-29 17:28 211 查看
exec1

#include<stdio.h>
#include<unistd.h>

intmain()
{
char*arglist[3];

arglist[0]="ls";
arglist[1]="-l";
arglist[2]=0;//NULL
printf("***Abouttoexecls-l\n");
execvp("ls",arglist);
printf("***lsisdone.bye");

return0;
}

exec2与exec1的区别就在于,execvp函数调用的语句变成了

execvp(arglist[0],arglist);

编译运行结果与exec1.c完全相同,说明arglist数组的第一项为要运行的程序的名称。

exec3.c中给出了一系列的exec函数调用方法,其不同用法如下图所示:



4.pipe文件夹-consumer.c文件

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#include<sys/types.h>
#include<sys/stat.h>

#defineFIFO_NAME"/tmp/myfifo"
#defineBUFFER_SIZEPIPE_BUF
intmain()
{
intpipe_fd;
intres;

intopen_mode=O_RDONLY;
charbuffer[BUFFER_SIZE+1];
intbytes=0;

memset(buffer,0,sizeof(buffer));

printf("Process%dopeiningFIFOO_RDONLY\n",getpid());
pipe_fd=open(FIFO_NAME,open_mode);
printf("Process%dresult%d\n",getpid(),pipe_fd);

if(pipe_fd!=-1){
do{
res=read(pipe_fd,buffer,BUFFER_SIZE);
bytes+=res;
}while(res>0);
close(pipe_fd);
}else{
exit(EXIT_FAILURE);
}

printf("Process%dfinished,%dbytesread\n",getpid(),bytes);
exit(EXIT_SUCCESS);
}

【1.PIPE_BUF的值是多少?】

4096字节

【2.memset函数用法?】

原型:memset(void*s,intch,size_tn);将s中前n个字节用ch替换并返回s

【3.open函数用法?】

open(constchar*pathname,intflags);第一个参数是欲打开的文件路径字符串,第二个参数是打开方式

【4.FIFONAME是什么?】

这里需要补充一下fifo的含义,它是一种文件类型,可以通过查看文件stat结构中的stmode成员的值来判断文件是否是FIFO文件。fifo是用来在进程中使用文件来传输数据的,也具有管道特性,可以在数据读出的时候清除数据。

fifo

生产者和消费者问题



testpid

#include<stdio.h>
#include<unistd.h>

#include<sys/types.h>

intmain()
{
printf("mypid:%d\n",getpid());
printf("myparent'spid:%d\n",getppid());
return0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: