编写一个困的小程序,该程序从命令行获取循环计数

我的任务需要帮助。问题是“编写一个困的小程序,该程序从命令行获取循环计数: 困了 其中n是程序应运行的秒数。通过循环n次sleep(1)来实现此计时-这将使程序在退出前进入n秒睡眠状态。在每个循环中,打印出进程ID和循环计数,以便可以识别该特定进程。

yuzisheng 回答:编写一个困的小程序,该程序从命令行获取循环计数

循环从命令行(argc)的每个参数执行一次,而要求是解析(第一个)命令行参数(argv [1])的迭代次数

您还希望按照要求将睡眠时间更改为1秒。

int count = atoi(argv[1]) ;
for (counter = 0; counter<count ; counter++) {
   sleep (1) ;
   ...
}
,

更改:

  1. 检查argc是否恰好为2(困和n)。
  2. 计数器需要循环到argv[1]
  3. 在每个循环迭代中睡眠1秒。
    if (argc == 2) {
    for (counter = 0; counter<atoi(argv[1]); counter++) {

        sleep(1); // sleep function which is taking the number from cmd and performing afterwards.
        printf("Awake!\n");

        printf("\n count[%d]",counter); // printing loop counting,and argument passed in cmd

        printf("Process ID: %d\n",p_id); //printing the process ids
    }
本文链接:https://www.f2er.com/3166233.html

大家都在问