操作系统实验 进程控制Word格式.docx

上传人:b****5 文档编号:16773889 上传时间:2022-11-26 格式:DOCX 页数:12 大小:80.90KB
下载 相关 举报
操作系统实验 进程控制Word格式.docx_第1页
第1页 / 共12页
操作系统实验 进程控制Word格式.docx_第2页
第2页 / 共12页
操作系统实验 进程控制Word格式.docx_第3页
第3页 / 共12页
操作系统实验 进程控制Word格式.docx_第4页
第4页 / 共12页
操作系统实验 进程控制Word格式.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

操作系统实验 进程控制Word格式.docx

《操作系统实验 进程控制Word格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验 进程控制Word格式.docx(12页珍藏版)》请在冰豆网上搜索。

操作系统实验 进程控制Word格式.docx

实验要求

A、实验进程的需要相应的截图

B、实验相应的程序

实验环境

Cygwin环境

 

1、理解系统调用fork(),exec系列函数,exit(),wait()的功能和实现过程。

(1)进程的创建

(2)进程等待

(3)进程的终止(4)System函数运用

(5)进程组(6)时间片的分配

(7)多线程库

(1)在cygwin环境下建立新文件;

($via.c)

(2)在cygwin环境中查找刚建立的文件;

($lsa.c)

(3)在C盘中找到文件、打开将进程复制在其中并保存,然后在cygwin环境中进行调试;

($cc-oaa.c)

(4)在cygwin环境下执行进程。

($./a)

实验2.1:

#include<

sys/types.h>

stdio.h>

unistd.h>

intmain()

{

pid_tpid;

char*message;

intn;

printf(“Forkprogramstarting\n”);

pid=fork()

switch(pid){

case–1:

printf(“Forkerror!

\n”);

exit

(1);

case0:

message=“Childprocessisprinting.”;

n=5;

for(;

n>

0;

n--)

{puts(message);

sleep

(1);

}

break;

default:

message=“Parentprocessidprinting.”;

n=3;

for(;

exit(0);

实验结果:

实验2.2:

sy/types.h>

if(pid=vfork())<

0){printf(“Forkerror!

elseif(pid==0)

{

printf(“ChildprocessPID:

%d.\n”,getpid());

setenv(“PS1”,”CHILD\\$”,1);

printf(“Process%4d:

callingexec.\n”,getpid());

if(execl(“/bin/sh”,”bin/sh”,”arg2”,NULL)<

0){

execleerror!

\n”,getpid());

exit(0);

}

printf(“Process%4d:

Youshouldneverseethisbecausethechildisalreadygone.\n”,gepid());

Thechildprocessisexiting.\n”,getpid());

else

printf(“ParentprocessPID:

%4d.\n”,getpid());

Theparenthasforkprocess%d.\n”,pid);

TheChildhadcalledexecorhasexited.\n”,getpid());

return0;

实验2.3:

进程等待

sys/wait.h>

intn,exit_code;

exit_code=37;

break;

message=“Parentprocessidprinting.”;

n=3;

exit_code=0;

if(pid)

intstat_val;

pid_tchild_pid;

child_pid=wait(&

stat_val);

printf(“Childhasfinished:

PID=%d.\n”,child_pid);

if(WIFEXITED(stat_val))

printf(“Childexitedwithcode%d.\n”,WEXITSTATUS(stat_val));

printf(“Childterminatedabnormally.\n”);

实验2.4:

进程的终止

voidh_exit(intstatus);

staticvoidforerror(void);

staticvoidwaiterror(void);

intmain(void)

intstatus;

if(pid=fork())<

0)atexit(forkerror);

elseif(pid==0)abort();

if(wait(&

status)!

=pid)atexit(waiterror);

h_exit(status);

voidh_exit(intstatus)

if(WIFEXITED(status))

printf(“Normaltermination,exitstatus=%d.\n”,WEXITSTATUS(status));

elseif(WIFSIGNALED(status))

printf(“Abnormaltermination,exitstatus=%d.\n”,WEXITSTATUS(status));

voidforkerror(void)

printf(“Forkerror!

voidwaiterror(void)

printf(“Waiterror!

实验2.5:

进程组

一个进程除了进程ID外,还有一个进程组ID。

进程组是一个或多个进程的集合,同一个进程组中进程都有一个统一的进程组ID。

用户可以使用system函数来在自己的程序中调用系统提供的各种命令。

stdlib.h>

printf(“runningpswithsystem.\n”);

system(“ps–ax”);

printf(“Done.\n”);

实验2.6:

多线程库

pthread.h>

intmyglobal;

pthread_mutex_tmytmutex=PTHREAD_MUTEX_INITIALIZER;

//定义静态互斥

void*thread_function(void*arg)

inti,j;

for(i=0;

i<

20;

i++)

pthread_mutex_lock(&

mymutex);

//加锁

j=myglobal;

j++;

printf(“.”);

fflush(stdout);

myglobal=j;

pthread_mutex_unlock(&

//解锁

ReturnNULL;

pthread_tmythread;

inti;

if(pthread_create(&

mythread,NULL,

thread_function,NULL))

printf(“errorcreatingthread.”);

abort();

for(i=0;

myglobal++;

if(pthread_join(mythread,NULL))

printf(“errorjoiningthread.”);

printf(“\nmyglobalequals%d\n”,myglobal);

Exit(0);

这次实验,主要是实验在cygwin环境之下进程的相关运行过程。

这让我对cygwin环境有了一些了解,但是还不是很深入,我还必须加强学习,进一步理解这类知识。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 机械仪表

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1