第11周测试-多进程测试

推荐在OpenEuler中实现

  1. 编写程序 rxx(xx为你学号后两位), rxx -o 生成并打印一个奇数随机数,rxx -e 生成并打印一个偶数随机数。提交代码和运行结果截图。
  2. 编写一个多进程程序,父进程通过调用exec和rxx 打印奇数随机数,同时打印自己PID,子进程通过调用exec和rxx 打印奇数随机数,同时打印PPID和PID,提交代码和运行结果截图。

1.

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
    char *o = "-o";
    char *e = "-e";
    int a;
	srand((unsigned int)time(NULL));
    if(strcmp(argv[1], o)==0)
		printf("偶数随机数为:%d\n",rand()*2);
    else if(strcmp(argv[1], e)==0)
        printf("奇数随机数为:%d\n",rand()*2+1);
    else
        printf("none!");
	return 0;
}


image

2.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<time.h>

int main()
{
	pid_t pid=-1;
	pid_t ppid=-1;
	int status=-1;
	pid=fork();
	if(pid>0)
	{
		srand((unsigned int)time(NULL));
		printf("偶数随机数为:%d\n",rand()*2);
		printf("pid = %d\n",getpid());
		printf("ppid = %d\n",getppid());
	}
	else if(pid==0)
	{
		srand((unsigned int)time(NULL));
		printf("奇数随机数为:%d\n",rand()*2+1);
		printf("pid = %d\n",getpid());
	}
	else
	{
		perror("fork");
		return -1;
	}
	return 0;
}

image

posted @   油菜园12号  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示