当前位置: 首页> 房产> 建筑 > 公司设立代办_广州荔湾发布公众号_青岛网站建设微动力_公司建网站流程

公司设立代办_广州荔湾发布公众号_青岛网站建设微动力_公司建网站流程

时间:2025/7/12 7:03:18来源:https://blog.csdn.net/weixin_42108533/article/details/146717537 浏览次数:0次
公司设立代办_广州荔湾发布公众号_青岛网站建设微动力_公司建网站流程

​​
在这里插入图片描述

一、软件需求

在同时运行多个CPU密集型进程时,需采集以下统计信息:

  • 当前运行在逻辑CPU上的进程ID
  • 每个进程的运行进度百分比

实验程序设计要求:

1. 命令行参数

参数说明示例值
n并发进程数量3
total总运行时长(毫秒)50
resol统计间隔(毫秒)1

2. 进程行为规范

  • 每个进程消耗total毫秒CPU时间后结束
  • resol毫秒记录:
    1. 进程ID(0~n-1)
    2. 累计运行时间(ms)
    3. 进度百分比((i+1)*100/nrecordnrecord=total/resol

二、代码实现

#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>#define NLOOP_FOR_ESTIMETION 1000000000UL
#define NSEC_PER_SEC    1000000000UL
#define NSEC_PER_MSEC   1000000UL/* 获取纳秒差 */
static inline long diff_nsec(struct timespec before, struct timespec after)
{return ((after.tv_sec * NSEC_PER_SEC + after.tv_nsec) - (before.tv_sec * NSEC_PER_SEC + before.tv_nsec));
}/* 每微秒消耗的空循环的次数 */
static unsigned long loops_per_msec(void)
{struct  timespec before, after;clock_gettime(CLOCK_MONOTONIC, &before);unsigned long i;for(i = 0; i < NLOOP_FOR_ESTIMETION; i++);clock_gettime(CLOCK_MONOTONIC, &after);return ((NLOOP_FOR_ESTIMETION * NSEC_PER_MSEC) / diff_nsec(before, after));
}static inline void load(unsigned long nloops)
{unsigned long i;for(i = 0; i < nloops; i++ );
}static void chidld_fn(int nproc, struct timespec *buf, int nrecord, unsigned long nloop_per_resol, struct timespec start)
{int i;struct timespec ts;for(i = 0; i < nrecord; i++){load(nloop_per_resol);clock_gettime(CLOCK_MONOTONIC, &ts);buf[i] = ts;}printf("nproc\tms\t step\n");printf("---------------------\n");for(i = 0; i < nrecord; i++){printf("%d\t%ld\t%d\n", nproc, diff_nsec(start, buf[i]) / NSEC_PER_MSEC,((i + 1) * 100) / nrecord );}printf("---------------------\n");exit(EXIT_SUCCESS);
}int main(int argc, char *argv[])
{int ret = EXIT_FAILURE;if( 4 > argc){fprintf(stderr, "usage: %s <nproc> <total[ms]> <resolution[ms]>\n", argv[0]);exit(EXIT_FAILURE);}int nproc = atoi(argv[1]); //同时运行的进程的数量int total = atoi(argv[2]); //程序运行的总时长,ms为单位 int resol = atoi(argv[3]); //采集统计信息的间隔if(1 > nproc) //对进程数量参数过滤非法值{fprintf(stderr, "<nproc> (%d) should be >= 1\n", nproc);exit(EXIT_FAILURE);}if(1 > total) //对程序时长参数过滤非法值{fprintf(stderr, "<total> (%d) should be >= 1\n", total);exit(EXIT_FAILURE);}if(1 > resol) //对采集统计信息参数过滤非法值{fprintf(stderr, "<resol> (%d) should be >= 1\n", resol);exit(EXIT_FAILURE);}if(total % resol){fprintf(stderr, "<total> (%d) should be mutliple of <resol> (%d) \n",total, resol);exit(EXIT_FAILURE);}int nrecord = total / resol;struct timespec* logbug = malloc(nrecord * sizeof(struct timespec)); //堆中开辟内存if(!logbug){err(EXIT_FAILURE, "malloc(nrecord) failed");}puts("estimeting workload which just take one millsecond");unsigned long nloops_per_resol = loops_per_msec() * resol;puts("end estimeting");fflush(stdout); // fflush logpid_t* pids = malloc(nrecord * sizeof(pid_t));if(!pids){warn("malloc (pids) failed");goto free_logbuf;}int i, ncreated;struct timespec start;clock_gettime(CLOCK_MONOTONIC, &start);for(i = 0, ncreated = 0; i < nproc; i++, ncreated++){pids[i] = fork();if( 0 > pids[i]){goto wait_children;}else if( 0 == pids[i]){//子进程执行空间chidld_fn(i, logbug, nrecord, nloops_per_resol, start);}}ret = EXIT_SUCCESS;wait_children:if(EXIT_FAILURE == ret) //杀死所有已创建的子进程.{for(i = 0; i < ncreated; i++){if(0 > kill(pids[i], SIGINT)){warn("kill(%d) failed", pids[i]);}}}for(i = 0; i < ncreated; i++) //等待子进程结束{if(0 > wait(NULL)){warn("wait() failed");}}free_pids:free(pids);free_logbuf:free(logbug);exit(ret);
}

三、编译配置(Makefile)

TARGET := app
SRC := sched.c
CPPFLAGS := -pthreadall: $(TARGET)@echo "make successful"$(TARGET): $(SRC)@echo $(SRC)gcc $(CPPFLAGS) $^ -I. -o $@clean:rm $(TARGET)

四、实验结果

nproc   ms      step
---------------------
2       0       2
2       1       4
2       2       6
...
2       172     100
---------------------
nproc   ms      step
---------------------
0       10      2
0       11      4
...
0       176     100
---------------------
nproc   ms      step
---------------------
1       6       2
1       7       4
...
1       203     100
关键字:公司设立代办_广州荔湾发布公众号_青岛网站建设微动力_公司建网站流程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: