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, ¶m);
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://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;
}
...
}