《操作系统》第三次作业

第三次作业是学习进程管理,笔者竟然没有学习明白,课下还需要不断翻书回看才是。第三次作业一共两个实验

实验目的

进一步学习Linux中与进程和线程控制相关的系统调用,加深对进程、线程等概念的理解;学习Linux对线程的独特的实现方式。

第一个实验

程序selective_wait.c

#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
main()
{
	int pid1, pid2, pid3, pid4;

	pid1 = fork();
	if (pid1 == 0) {
		printf("first child\n");
		sleep(10);
		printf("first child terminates\n");
		exit(0);
	}
	pid2 = fork();
	if (pid2 == 0) {
		printf("second child\n");
		sleep(20);
		printf("second child terminates\n");
		exit(0);
	}
	pid3 = fork();
	if (pid3 == 0) {
		printf("third child\n");
		sleep(30);
		printf("third child terminates\n");
		exit(0);
	}
	pid4 = fork();
	if (pid4 == 0) {
		printf("fourth child\n");
		sleep(40);
		printf("fourth child terminates\n");
		exit(0);
	}

	waitpid(pid2, NULL, 0);
	printf("parent: second child terminated\n");
}

在这里插入图片描述

第二个实验

在这里插入图片描述

实验代码

#include<unistd.h>
#include<stdlib.h>
#include <stdio.h>
#include <sched.h>

char stack[1024];		// used by the cloned process as stack space
int  sharedVariable;	// the shared variable

int clone_process(void *s)
{
	while (1) {
		sleep(2);
		sharedVariable ++;
		printf(“cloned process: %d \n”, sharedVariable);
	}
}

main()
{
	int rc;
	rc = clone(clone_process, &stack[1024], CLONE_VM, 0);
	if (rc < 0) {
		printf(“main process: clone() failed \n”);
		exit(1);
	}
	while (1) {
		sleep(3);
		sharedVariable--;
		printf(“main process: %d \n”, sharedVariable);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值