SCHED_RR SCHED_FIFO-linux进程管理

https://www.cnblogs.com/arnoldlu/p/9025981.html

https://www.cnblogs.com/arnoldlu/p/9014365.html

https://www.cnblogs.com/arnoldlu/p/6241297.html#fork_test_code

https://www.cnblogs.com/arnoldlu/p/10868354.html

#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<pthread.h>

void * thread_partent()
{
	printf("%s(%d): thread ID:%lu\n",__func__,__LINE__,pthread_self());
	while(1)
	{
		printf("partent thread\n");
		sleep(2);
	}

	pthread_exit(NULL);
}

void * thread_partent_hello()
{
	int i=0;
	printf("%s(%d): thread ID:%lu\n",__func__,__LINE__,pthread_self());
	while(1)
	{
		printf("partent hello world thread begain\n");
		for(i=0; i<1000000000; i++);	
		printf("partent hello world thread end\n");
		sleep(2);
	}

	pthread_exit(NULL);
}

void * thread_child()
{
	int i=0;
	printf("%s(%d): thread ID:%lu\n",__func__,__LINE__,pthread_self());
	while(1)
	{
		printf("child thread bggain\n");
		for(i=0; i<100000000; i++);	
		printf("child thread end\n");		
		sleep(1);
	}

	pthread_exit(NULL);
}

void test_little(void)
{
	int i;
	printf("%s begain\n", __func__);
	for(i = 0; i < 30000000; i++);
	printf("%s begain\n", __func__);
}

void test_medium(void)
{
	int i;
	printf("%s begain\n", __func__);
	for(i = 0; i < 60000000; i++);
	printf("%s begain\n", __func__);
}

void test_high(void)
{
	int i;
	printf("%s begain\n", __func__);
	for(i = 0; i < 90000000; i++);
	printf("%s begain\n", __func__);
}

void test_hi(void)
{
	int i;
	printf("%s begain\n", __func__);
	for(i = 0; i < 120000000; i++);
	printf("%s begain\n", __func__);
}

int main(void)
{
	int  result;
    int i, pid;
    cpu_set_t mask;
    struct sched_param param;
	
	/*设置CPU亲和性,绑定在cpu0上*/
	CPU_ZERO(&mask);
	CPU_SET(0, &mask);
	if(sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1)
	{
		exit(EXIT_FAILURE);
	}
	
	/*设置进程调度类、优先级*/
	param.sched_priority = 50;
	sched_setscheduler(0, SCHED_FIFO, &param);
	
	for(i = 0; i<2; i++) 
	{
		result = fork();
		if(result>0)
			printf("i=%d parent parent=%d current=%d child=%d\n", i, getppid(), getpid(), result);
		else
			printf("i=%d child parent=%d current=%d\n", i, getppid(), getpid());
		
		if(i==0)
		{
			test_little();
			sleep(1);
		} else {
			test_medium();
			sleep(1);
		}
	}
	
	pid = wait(NULL);
	test_high();
	printf("pid=%d wait=%d\n", getpid(), pid);
	sleep(1);
	pid = wait(NULL);
	test_hi();
	printf("pid=%d wait=%d\n", getpid(), pid);
	
	return 0;
}
#Makeile
main:main.o
	gcc main.o -o main -lpthread

main.o:main.c
	gcc -c -Wall main.c -o main.o 

.PHONY:clean
clean:
	rm -rf *.o main

参考:

https://www.cnblogs.com/arnoldlu/p/9025981.html

http://blog.chinaunix.net/uid-24774106-id-3379478.html

https://stackoom.com/question/3jHrK/%E5%AE%8C%E5%85%A8%E5%8F%AF%E6%8A%A2%E5%8D%A0%E7%9A%84Linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84SCHED-FIFO%E7%AD%96%E7%95%A5%E6%98%AF%E5%90%A6%E4%BD%BF%E7%94%A8kthread%E4%BC%98%E5%85%88%E7%BA%A7

https://bbs.csdn.net/topics/390889542

https://blog.csdn.net/flyingleo1981/article/details/9624103

https://www.jianshu.com/p/8f967f7a3635

https://www.man7.org/linux/man-pages/man7/sched.7.html

https://cloud.tencent.com/developer/article/1516875

https://blog.csdn.net/gatieme/article/details/51569932

https://www.cnblogs.com/elnino/p/4344083.html

https://blog.csdn.net/Nonpc123/article/details/90483264

在阅读源码的过程中,发现找不到fork函数的定义。后来在linux/init/main.c中找到了这样一条语句

static inline _syscall0(int,fork)

原来这里就是fork函数的定义。

系统在linux/include/unistd.h有一个宏定义。

#define _syscall0(type name)

 

 

 

 

 

 

 

 

do_fork
_do_fork  

copy_process   

sched_fork{
...
    if (dl_prio(p->prio)) {
        put_cpu();
        return -EAGAIN;
    } else if (rt_prio(p->prio)) {
        p->sched_class = &rt_sched_class;
    } else {
        p->sched_class = &fair_sched_class;
    }
...
}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值