______________________________________________________________________________________________________________________________________________________________ 步骤6:父进程中的printf有向屏幕输出吗?为什么?
______________________________________________________________________________________________________________________________________________________________ 步骤7:利用父子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其中父进程接受用户对文件stu.info的操作命令然后通过管道发给子进程,子进程完成对文件的实际操作。
3、实验结论
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
4、程序清单
//程序清单 4-1 #include
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
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