Linux实验指导书(4)

2019-02-15 14:43

______________________________________________________________________________________________________________________________________________________________ 步骤6:父进程中的printf有向屏幕输出吗?为什么?

______________________________________________________________________________________________________________________________________________________________ 步骤7:利用父子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其中父进程接受用户对文件stu.info的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作。

3、实验结论

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

4、程序清单

//程序清单 4-1 #include #include #include \int global = 5;

int main(void) {

pid_t pid;

char *string = \int local = 10;

printf(\if((pid = fork())<0)

err_exit(\if(pid == 0){

16

string = \ printf(\ \

getpid(),string,pid,global,local);

global++;

}

else{ string = \ printf(\ \

getpid(),string,pid,global,local);

local++;

}

printf(\ exit(EXIT_SUCCESS);

}

//清单4-2 管道程序 # define STD_INPUT 0 // 定义标准输入设备描述符 # define STD_OUTPUT 1 // 定义标准输出设备描述符

int fd[2]; main() {

static char process1[]=”father”,process2[]=”child”; pipe(fd); // 定义管道 pipeline(process1,process2); // 调用自定义函数pipeline() exit(1); // 程序结束 }

pipeline(char* process1,char* process2) { int i; if ((i=fork())==-1) // 创建进程,失败退出 { perror(“process fork error!”); exit(1); } if (i) { close(fd[0]); // 关闭管道输入描述符 close(STD_OUTPUT); // 关闭标准输出描述符1 dup(fd[1]); // 指定标准输出描述符1为管道写指针 close(fd[1]); // 关闭原始管道写指针 execl(process1, process1, 0); // 用程序father覆盖当前程序 printf(“ father failed.\\n”); // execl()执行失败 } else {

17

close(fd[1]); // 关闭管道输出描述符 close(STD_INPUT); // 关闭标准输入描述符0 dup(fd[0]); // 指定标准输入描述符0为管道读指针 close(fd[0]); // 关闭原始管道读指针 execl(process2,process2,0); // 用程序child覆盖当前程序 printf(“child failed.\\n”); // execl()执行失败 } exit(2); // 程序结束 }

清单 4-3 father.c main() {

static char string[] = “Parent is using pipe write.” ; int len; len = sizeof(string) ; write(l, string, len) ; /* 将string中的内容写入管道中 */ printf(“parent, parent, parent \\n \\n \\n” ) ; exit(0) ; }

清单4-4 child.c main() {

char output[30] ; read (0, output, 30) ; /* 从管道中读数据并存入output中 */ printf(“%s \\n child, child. \\n” , output) ; return(0) ; }

18

实验五 Linux线程的使用

1、实验目的

学习和掌握Linux线程的创建以及同步方法。 2、实验内容和步骤

步骤1:输入程序5-1,编译并运行写出执行结果。

_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤 2:仔细研读代码,描述pthread_create函数中各个参数的意义,并给出线程的入口函数的调用方法,描述两线程间参数传递的方式。

___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤3:打开注释掉的全局变量aa及操作aa的相关代码,回答这跟进程有什么不同。 ____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤4:输入程序5-2,编译并运行,写出运行结果。

______________________________________________________________________________________________________________________________________________________________ 步骤5:仔细研读代码,描述利用信号量实现线程间同步调用方法。

19

___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤6:学习并使用线程间的同步方法,重新改写实验3步骤6要求的程序。要求启用两个线程,其中主线程接受用户对文件stu.info的操作命令然后发给子线程,子线程完成对文件的实际操作(注意线程间的同步)。

3、实验结论

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

4、程序清单

//程序清单 5-1 #include #include #include #include

void *thread_function(void *arg);

int main() {

res = pthread_create(&a_thread,NULL,thread_function,(void *)&share_int); sleep(5); int res;

pthread_t a_thread; void *thread_result; //int aa=100;

int share_int = 10;

20


Linux实验指导书(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016-2017学年仁爱版八年级英语初二上册Unit3单元测试卷及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: