教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

Red hat Linux 实验简易教程 - 1.1 - 图文(9)

来源:网络收集 时间:2026-04-27
导读: } } close(fd1); close(fd2); exit(EXIT_SUCCESS); } int main(void) { char fnamestr1[20], fnamestr2[20]; printf(\ printf(\ scanf(\ printf(\ scanf(\ file_copy(fnamestr1,fnamestr2); return 0; } 2.编写一个

} }

close(fd1); close(fd2);

exit(EXIT_SUCCESS); }

int main(void) {

char fnamestr1[20], fnamestr2[20]; printf(\ printf(\ scanf(\

printf(\ scanf(\

file_copy(fnamestr1,fnamestr2); return 0; }

2.编写一个程序,它利用fork() 创建一个子进程;父进程打开一个文件,父进程向管道中写信息,子进程从管道中读信息。打印每个进程的两个ID号。最后父进程执行waitpid()。另外如果没有waitpid调用,会出现什么情况? 示例如下所示: #include #include #include #include #include #include

int main(int argc, char *argv) { int fd[2]; int len; pid_t pid; pid_t current_process; pid_t parent_process; char filename[10]; char childbuf[10]; //create a pipe if (pipe(fd) < 0) {

perror(\ exit(1); }

//create the child process if ((pid = fork()) < 0) { perror(\ exit(1);

- 35 -

}

if (pid == 0) { close(fd[1]);

len = read(fd[0], childbuf, 100); childbuf[len] = '\\0';

printf(\ current_process=getpid(); parent_process=getppid();

printf(\ printf(\ printf(\ printf(\

if (execlp(\ perror(\ exit(1); } } else {

close(fd[0]);

printf(\ current_process=getpid(); parent_process=getppid();

printf(\ printf(\ printf(\ scanf(\ write(fd[1], filename, strlen(filename)); //wait the pid process quit waitpid(pid, NULL, 0); return 0; } }

管道就是为了暂时存储东西的,int pipe(int filedes[2]); filedes[0]是表示管道的读端,filedes[1]是表示管道的写端。若成功,则返回0,失败,返回-1 。 执行结果如下所示:

- 36 -

可是如果把倒数第四行waitpid(pid, NULL, 0);注释起来的话。结果又会怎么样呢? 运行结果如下所示:

我可以从结果不难看出,当前进程在子进程没有退出之前就提前退出了。

3.编写一个程序,尽可能多地输出有关当前进程的信息:PID、PPID、打开文件、当前目录、nice值等。你如何确定文件是否打开?如果多个文件描述符表示同一个文件,请简要说明。(同样地,如何确定?) 示例如下所示: #include #include #include #include #include #include

int main(int argc, char *argv) { int fd[2];

int len,n,fd1,fd2; pid_t pid; pid_t current_process; pid_t parent_process; char filename[10]; char childbuf[10]; if (pipe(fd) < 0) {

perror(\ exit(1); }

//create the child process if ((pid = fork()) < 0) { perror(\ exit(1);

- 37 -

}

Red hat Linux 实验简易教程 - 1.1 - 图文(9).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/520866.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)