实验 进程管理与控制

实验 进程管理与控制

实验内容:
1、用read、write、open等系统调用编写实现如下功能的程序(要求进行必要的出错检查):
(1) 创建一个文件myfile.txt,文件内容从键盘输入;
(2) 将myfile.txt的内容显示在屏幕上,并将myfile.txt的内容复制到一个新的文件newfile.txt中。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main()
{
int fd = 0,fd1=0;
char filename[20];char des[20];
printf(“请输入要创建的源文件名字\n”);
scanf("%s",filename);
fd = open(filename, O_RDWR | O_EXCL | O_CREAT | O_TRUNC, S_IRWXG);
if (fd == -1){
perror(“file open error.\n”);
exit(-1);
}
int len = 0;
char buf[100] = { 0 };
printf(“输入文件内容\n”);
scanf("%[^\n]", buf);
len = strlen(buf);
write(fd, buf, len);
close(fd);
fd = open(filename, O_RDONLY);
if (fd == -1){
perror(“file open error.\n”);
exit(-1);
}
printf(“输出刚创建源文件的内容\n”);
off_t f_size = 0;
f_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
while (lseek(fd, 0, SEEK_CUR) != f_size)
{
read(fd, buf, 100);
printf("%s\n", buf);
}
buf[100] = 0 ;
printf(“请输入目标文件名:\n”);
scanf("%s",des);
fd1= open(filename, O_RDWR | O_EXCL | O_CREAT | O_TRUNC, S_IRWXG);
if (fd == -1){
perror(“file open error.\n”);
exit(-1);
}
f_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
while (lseek(fd, 0, SEEK_CUR) != f_size)
{
read(fd, buf, 100);
write(fd1,buf,100);
}
printf(“复制%s文件到%s文件成功!\n”,filename,des);
close(fd);
close(fd1);
return 0;
}

2、下面程序中,主程序调用了3个fork(),最后输出一个字符串,请运行至少5次这个程序,记下运行结果,观察每次结果是否相同,并分析为什么会这样(将原因写在实验报告中)。
#include <stdio.h>
Void main()
{
fork();
fork();
fork();
printf(“test\n”);
}

在调用fork()函数创建子进程后,父子进程的执行顺序由操作系统来决定,相互之间没有任何时序上的关系,所以在运行得到的结果中,我们看到每次运行的结果都有可能与上次的运行结果不同。
3、设计多进程并发执行程序,父进程从终端读入命令,子进程执行命令,执行完毕后,父进程继续等待从终端读入命令。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX_PARA_NUMS 10
#define MAX_CHAR_EACH_PARA 1000
int split(char* input, char output[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA])
{
int counter = 0;
int len = strlen(input);
int bTemp = 0;
int i, j = 0;
for(i = 0; i < len; i++)
{
if(input[i] == ’ ')
{
if(j != 0 && counter > 0)
{
output[counter - 1][j] = ‘\0’;
}
bTemp = 0;
}
else
{
if(bTemp == 0)
{
j = 0;
counter++;
bTemp = 1;
}
output[counter - 1][j] = input[i];
j++;
}
}
return counter;
}
void main()
{
int counter = 0;
char params[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA];
char buf[4096];
pid_t pid;
printf(“请输入命令:\n”);
while (fgets(buf, 4096, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == ‘\n’)
{
buf[strlen(buf) - 1] = ‘\0’;
}
pid = fork();
if (pid < 0)
{
printf(“fork error”);
}
else if (pid == 0)
{
counter = split(buf, params);
if(counter != 0)
{
printf("---------\n");
}
switch(counter)
{
case 0: break;
case 1:
{
execlp(params[0], params[0], (char*)0);
}
break;
case 2:
{
execlp(params[0], params[0], params[1], (char*)0);
}
break;
case 3:
{
execlp(params[0], params[0], params[1], params[2],
(char*)0);
}
break;
case 4:
{
execlp(params[0], params[0], params[1], params[2],
params[3], (char*)0);
}
break;
case 5:
{
execlp(params[0], params[0], params[1], params[2],
params[3], params[4], (char*)0);
}
break;
case 6:
{
execlp(params[0], params[0], params[1], params[2],
params[3], params[4], params[5], (char*)0);
}
break;
default:
{
printf(“非法输入!\n”);
}
break;
}
}
else
{
wait(NULL);
printf(“请输入命令:\n”);
}
}
}

4、编写程序创建进程树如图1和图2所示,在每个进程中显示当前进程识别码和父进程识别码。

图1:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
pid_t p1,p2,p3;
p1=fork();
if(p10)
{
p2=fork();
if(p2
0)
{
p3=fork();
if(p3==0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
}
else if(p3>0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
wait(NULL);
}
else {perror(“fork error”);exit(1);}
}
else if(p2>0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
wait(NULL);
}
else {perror(“fork error”);exit(1);}
}
else if(p1>0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
wait(NULL);
}
else {perror(“fork error”);exit(1);}
}

图2:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
pid_t p1,p2,p3,p4;
p1=fork();
if(p10)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
p2=fork();
if(p2
0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
}
else if(p2>0){wait(NULL);}
else {perror(“fork error”);exit(1);}
}
else if(p1>0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());

p3=fork();
if(p30)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
p4=fork();
if(p4
0)
{
printf(“parent process:%d my process:%d\n”,getppid(),getpid());
}
else if(p4>0){wait(NULL);}
else {perror(“fork error”);exit(1);}
}
else if(p3>0){wait(NULL);}
else {perror(“fork error”);exit(1);}
wait(NULL);
}
else {perror(“fork error”);exit(1);}
}

5、请分别用单进程和多进程来实现10000个(0, 1)之间的随机数的最大值。要求:
(1) 父进程随机生成10000个(0, 1)之间的随机数;
(2) 创建4个子进程,分别求2500个(0, 1)之间随机数的最大值;
(3) 父进程接受4个子进程的计算结果求出10000个(0, 1)之间随机数的最大值并打印结果;
(4) 分别统计输出单进程与多进程计算的时间。
单进程:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define MAXK 20
float randf()
{
return (float)(rand() % 100001) * 0.00001f;
}
void main()
{
int i,j;
clock_t start, finish;
double duration;
float num=0,max=0;
start = clock();
for(j=0;j<MAXK;j++)
{
for (i=0; i<10000; i++)
{
num=randf();
if(num>max&&num<1)
max=num;
printf(“随机值%d:%.5f\n”,i+1, num);
}
}
finish = clock();
printf(“最大值%.5f\n”, max);
duration = (double)(finish - start) / CLOCKS_PER_SEC/MAXK;
printf( “运行时间:%f秒\n”, duration );
}

多进程:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <math.h>
#include <time.h>
#define MAXK 10000
float randf()
{
return (float)(rand() % 100001) * 0.00001f;
}
void main()
{
pid_t pid;
float max,max1,max2,max3,max4;
int i,j=1,m,n;
clock_t start, finish;
double duration;
float num1=0,num2=0,num3=0,num4=0;
start = clock();
printf(“开始时间:%f\n”, start);
for (i = 0; i < 4; i++)
{
if ((pid = vfork()) == 0)break;
j++;
}
if (pid == -1)
{
perror(“fork error”);
exit(2);
}
else if(pid == 0)
{
if(j1)
{
for(m=0;m<MAXK;m++)
{
for (n=0; n<2500; n++)
{
num1=randf();
if(num1>max1&&num1<1)
max1=num1;
}
}
printf(“进程1最大值:%f\n”, max1);
}
else if(j
2)
{
for(m=0;m<MAXK;m++)
{
for (n=0; n<2500; n++)
{
num2=randf();
if(num2>max2&&num2<1)
max2=num2;
}
}
printf(“进2最大值:%f\n”, max2);
}
else if(j3)
{
for(m=0;m<MAXK;m++)
{
for (n=0; n<2500; n++)
{
num3=randf();
if(num3>max3&&num3<1)
max3=num3;
}
}
printf(“进程3最大值:%f\n”, max3);
}
else if(j
4)
{
for(m=0;m<MAXK;m++)
{
for (n=0; n<2500; n++)
{
num4=randf();
if(num4>max4&&num4<1)
max4=num4;
}
}
printf(“进程4最大值:%f\n”, max4);
}
exit(1);
}
else if(pid > 0)
{
waitpid(0,NULL,0);
max=(max1+max2+max3+max4)/4;
finish = clock();
printf(“结束时间:%f\n”, finish);
printf(“最大值:%f\n”, max);
duration = (double)(finish - start) / CLOCKS_PER_SEC/MAXK;
printf( “运行时间:%ef秒\n”, duration );
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值