资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
操作系统课程设计实验报告册班级: 131112 学号: 13111xxx 姓名: xxxxx 教师: 第 2 页 共 14 页目 录实验说明重要提示实验3 Linux的进程和线程 要求:理解进程/线程的概念掌握创建和终止进程/线程的方法掌握与进程/线程控制相关的系统函数实验编号3题目Linux的进程和线程实验目的理解进程/线程的概念掌握创建和终止进程/线程的方法掌握与进程/线程控制相关的系统函数实验内容创建和终止进程/线程使用进程/线程控制相关的系统函数报告内容要求(1) 实现方法和思路(2) 测试及结果报 告 正 文 getpid():获得当前进程ID getppid():获得当前进程的父进程的ID getuid():获得用户ID getgid():获得组ID源代码:#include #include #include int main()pid_t myPid;pid_t myParentPid;gid_t myGid;uid_t myUid; myPid = getpid();myParentPid = getppid();myGid = getgid();myUid = getuid();printf(my process id is %dn, myPid);printf(my parent is process id is %dn, myParentPid);printf(my group id is %dn, myGid);printf(my user id is %dn, myUid);return 0;运行结果;API函数用途fork创建一个新的子进程wait将进程挂起直到子进程退出signal注册一个新的信号句柄pause将进程挂起直到捕获到信号kill向某个指定的进程发出信号exit正常中止当前进程Wait函数pid = wait( &status );If( WIFEXITED(status) ) printf(“Child exited normally with status %dn”, WEXITSTATUS(status);else if( WIFSIGNALED(status) ) printf(“Child exited by signal with status %dn”,WTERMSIG(status);源代码:#include #include #include int main()pid_t ret;int status , i;int role = -1;ret = fork();if(ret 0)printf(Parent: This the parent process (pid %d)n, getpid();for(i=0;i6;i+)printf(Parent: At count %dn, i);sleep(3); ret = wait(&status);/防止僵尸进程的产生role=0; elseif(ret =0)printf(Child: This the child process (pid %d)n, getpid();for(i=0;i6;i+)printf(Chile: At count %dn,i);sleep(1); role = 1; elseprintf(Parent: Error trying to fork() (%d)n, errno);printf(%s: Exiting.n, (role =0)?Parent:Child);return 0;运行结果:signal函数信号说明SIGHUP挂起SIGINT键盘中断SIGKILLKill信号SIGUSR1用户自定义信号SIGUSR2用户自定义信号SIGPIPE终止管道SIGTERM终止信号源代码:#include #include #include #include void catch_ctlc( int sig_num)printf(Caught Control-Cn);fflush(stdout);/清除标准输出的缓存区int main()signal( SIGINT, catch_ctlc);printf(Go ahead, make my day.n);pause();return 0;运行结果: pause函数 pause函数会把进程挂起,直到接收到信号。在接收到以后,调用进程从pause中返回,继续进行。如果进程捕获的信号已经注册了信号句柄,那么pause函数会在信号句柄被调用并返回之后才返回。 pause原型: 头文件 int pause( void );pid说明0信号发送到由pid指定的进程0信号发送到与调用进程同组的所有进程-1信号发送到所有进程(init进程除外)0信号发送到由pid的绝对值指定的进程组中的所有进程源代码:#include #include #include #include #include #include void usr1_handler( int sig_num)printf(Parent (%d) got the SIGUSR1n, getpid() );int main()pid_t ret;int status;int role = -1;ret = fork();if( ret 0)printf(Parent: This is the parent process (pid %d)n,getpid() );signal( SIGUSR1, usr1_handler);role = 0;pause();printf(Parent: Awaiting child exitn);ret = wait( &status); else if(ret = 0)printf(Child: This is the child process (pid %d)n, getpid();role = 1;sleep(1);printf(Child: Sending SIGUSR1 to pid %dn, getppid();kill(getppid(), SIGUSR1);sleep(2);elseprintf(Parent: Error trying to fork() (%d)n, errno);printf(%s: Exitingn, ( ( role = 0) ? Parent : Child);return 0;运行结果: exit函数 终止调用进程。传入exit的参数会返回给父进程,为wait或waitpid调用提供所需要的状态信息。 exit原型: void exit( int status); 进程调用exit时还会向父进程发出SIGCHLD信号,释放当前进程占用的资源。 这个函数调用十分重要,因为他会向Shell坏境表明状态时成功还是失败。 线程函数 头文件 创建线程原型 int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); 第一个参数为指向线程标示符的指针。 第二个参数用来设置线程属性。 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数。 若成功则返回0,若失败则返回出错编号。 线程函数 终止线程原型 int pthread_exit(void *retval);源代码:#include #include #include #include #include void *myThread(void *arg)printf(Thread rann);pthread_exit(arg);int main()int ret;pthread_t mythread;ret = pthread_create(&mythread,NU
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号