查看程序的进程和线程实验报告篇一:程序实验2:11-多线程编程---实验报告程序实验二:11-多线程编程实验专业班级实验日期 5.21 姓名学号实验一(p284:11-thread.c)1、软件功能描述创建3个线程,让3个线程重用同一个执行函数,每个线程都有5次循环,可以看成5个小任务,每次循环之间会有随即等待时间(1-10s)意义在于模拟每个任务到达的时间是随机的没有任何的特定规律。
2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#define T_NUMBER 3#define P_NUMBER 5#define TIME 10.0void *thrd_func(void *arg ){(本文来自: 小草范文网:查看程序的进程和线程实验报告) int thrd_num=(int)arg;int delay_time =0;int count =0;printf("Thread %d is staraing\n",thrd_num);for(count=0;count {delay_time =(int)(rand()*TIME/(RAND_MAX))+1;sleep(delay_time);printf("\tTH%d:job%d delay =%d\n",thrd_num,count,delay_time);}printf("%d finished\n",thrd_num);pthread_exit(NULL);}int main(){pthread_t thread[T_NUMBER];int no=0,res;void * thrd_ret;srand(time(NULL));for(no=0;no {res=pthread_create(&thread[no],NULL, thrd_func,(void*)no);if(res!=0){printf("Creay th %d faild\n",no);exit(res);}}printf("success\nwaiting\n");for(no=0;no {res=pthread_join(thread[no],&thrd_ret);if(!res){printf("t %d joined\n",no);}else{printf("T %djoined faild\n",no);}}return 0;}4.编译、运行方法及结果(抓屏)5.结果分析由运行结果可以看出,创建线程、释放资源按照顺序,而每个线程的运行和结束是独立与并行的。
实验二(p287: 11-thread_mutex.c)1、软件功能描述在试验1的基础上通过互斥锁,使原本独立,无序的多个线程能够按顺序进行2、程序流程设计3.部分程序代码注释(关键函数或代码)#include#include#include#define THREAD_NUMBER3/*线程数*/#define REPEAT_NUMBER3 /*每个线程的小任务数*/#define DELAY_TIME_LEVELS 10.0 /*小任务间的最大时间间隔*/pthread_mutex_t mutex;void *thrd_func(void *arg) //线程函数例程{int thrd_num = (int)arg;int delay_time = 0, count = 0;int res;//互斥锁上锁res = pthread_mutex_lock(&mutex);if(res){篇二:操作系统实验报告_进程和线程计算机科学与软件学院操作系统上机实验报告学生姓名:学号:班级:班实验日期: XX.4.17实验名称:进程和线程实验目的:理解unix/Linux下进程和线程的创建、并发执行过程。
实验内容:1.进程的创建2.多线程应用实验步骤及分析:(此部分为关键内容:要求整理实验主要步骤,总结编写实验过程中遇到哪些问题,如何解决的,若未解决也应总结,回答思考题的答案)一、进程的创建下面这个C程序展示了UNIX系统中父进程创建子进程及各自分开活动的情况。
1、实验指导fork( )创建一个新进程。
系统调用格式:pid=fork( )参数定义:int fork( )fork( )返回值意义如下:0:在子进程中,pid变量保存的fork( )返回值为0,表示当前进程是子进程。
>0:在父进程中,pid变量保存的fork( )返回值为子进程的id值(进程唯一标识符)。
-1:创建失败。
如果fork( )调用成功,它向父进程返回子进程的PID,并向子进程返回0,即fork( )被调用了一次,但返回了两次。
此时OS在内存中建立一个新进程,所建的新进程是调用fork( )父进程(parent process)的副本,称为子进程(child process)。
子进程继承了父进程的许多特性,并具有与父进程完全相同的用户级上下文。
父进程与子进程并发执行。
2、参考程序代码/*process.c*/#include#includemain(int argc,char *argv[]){int pid;/* fork another process */pid = fork();if (pid fprintf(stderr, "Fork Failed");exit(-1);}else if (pid == 0) {/* child process */execlp( "/bin/ls", "ls",NULL);}else {/* parent process *//* parent will wait for the child to complete */ wait(NULL);printf( "Child Complete" );exit(0);}}3、编译和运行$gcc process.c –o processs4、运行$./process 程序运行截图5、思考(1)系统是怎样创建进程的?(2)扩展程序,在父进程中输出1到5,在子进程中输出6-10,要求父子进程并发输出;记录实验结果,并给出简单分析。
6.实验中遇到的问题和解决方法二、多线程应用编写unix/Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。
下面是一个最简单的多线程程序 example1.c。
1.实验指导下面的示例中,要使用到两个函数,pthread_create和pthread_join,并声明了一个pthread_t型的变量。
函数pthread_create用来创建一个线程,它的原型为:extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,void *(*__start_routine) (void *), void *__arg));第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,第三个参数是线程运行函数的起始地址,最后一个参数是运行函数的参数。
这里,我们的函数thread 不需要参数,所以最后一个参数设为空指针。
第二个参数我们也设为空指针,这样将生成默认属性的线程。
当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。
前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法。
创建线程成功后,新创建的线程则运行参数三和参数四确定的函数,原来的线程则继续运行下一行代码。
函数pthread_join用来等待一个线程的结束。
函数原型为:extern int pthread_join __P ((pthread_t __th, void **__thread_return));第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。
这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。
一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。
它的函数原型为:extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));唯一的参数是函数的返回代码,只要pthread_join中的第二个参数thread_return不是NULL,这个值将被传递给thread_return。
2、参考程序代码/* thread.c*/#include#includevoid thread(void){int i;for(i=0;i printf("This is a pthread.\n");}int main(int argc,char *argv[]){pthread_t id;int i,ret;ret=pthread_create(&id,NULL,(void *)thread,NULL);if(ret!=0){printf ("Create pthread error!\n");exit (1);}for(i=0;i printf("This is the main process.\n");pthread_join(id,NULL);return (0);}3、编译和运行编译此程序:gcc example1.c -lpthread -o example1-lpthread:使用线程库运行example1,得到如下结果:This is the main process.This is a pthread.This is the main process.This is the main process.This is a pthread.This is a pthread.再次运行,可能得到如下结果:This is a pthread.This is the main process.This is a pthread.This is the main process.This is a pthread.This is the main process.程序运行截图4、思考(1)程序运行后,进程thread中有几个线程存在?(2)为什么前后两次运行结果不一样?答(1)(2)5.实验中遇到的问题和解决方法运行结果并没有出现预期效果篇三:操作系统实验报告操作系统教程实验报告班级:软104学号:109074267姓名:王二康实验一 WINDOWS进程初识1、实验目的(1)学会使用VC编写基本的Win32 Consol Application (控制(2)台应用程序)。