小编use*_*893的帖子

计算二维数组的页面错误数

我正在努力学习考试......我找到了这个例子但却无法理解他们是如何得到答案的.有人能解释一下吗?

题:

考虑二维数组A:int A [] [] = new int [100] [100]; 其中A [0] [0]位于页面大小为200的分页存储器系统中的位置200处.操作矩阵的小进程驻留在页面0(位置0到199)中.因此,每次取指令都来自第0页.对于两个页面帧,下面的数组初始化循环产生了多少页面错误,使用LRU替换并假设第一个页面帧包含进程而另一个最初是空的?

A:

for (int j=0;j<100;j++)
   for (int i=0; i<100; i++)
     A[i][j] = 0;
Run Code Online (Sandbox Code Playgroud)

B:

for(int i=0; i<100; i++)
  for (int j=0; j<100; j++)
      A[i][j] = 0;
Run Code Online (Sandbox Code Playgroud)

给出的正确答案是:a:100 x 50 = 5000 b:50

我有点理解第一部分.共有50页.(10000/200 = 50)并且每次j发生变化时,都会发生页面错误...总共有100页错误...但为什么会乘以50?为什么第二个50?

谢谢!!

arrays paging operating-system page-fault

12
推荐指数
1
解决办法
1万
查看次数

使用waitpid在后台运行进程?

如果在命令末尾找到"&",我试图模仿后台运行进程的bash功能.我有以下功能......我不认为它正在做我想做的事情

int execute(char* args[],int background,int *cstatus){
    pid_t   child;
    pid_t   ch;                         /*Pid of child returned by wait*/
    if ((child = fork()) == 0){                 /*Child Process*/
        execvp(args[0],args);       
        fprintf(stderr, "RSI: %s: command not found\n",args[0]); /*If execvp failes*/
        exit(1);

    }else{          /*Parent process*/
        if (child== (pid_t)(-1)) {
            fprintf(stderr,"Fork failed\n"); exit(1);
        }else{
            if (background==0){             /*If not running in background..wait for process to finish*/
                ch = wait(cstatus);
            }else{
                printf("%ld Started\n",(long)getpid());
        /*  printf("Parent: Child %ld exited with status = %ld\n", (long) ch, (long)cstatus);
    */  }}
    }
return 0; …
Run Code Online (Sandbox Code Playgroud)

c process waitpid

7
推荐指数
1
解决办法
2万
查看次数

使用strtok将字符串分成C中的数组

我正在制作一个小程序,用户键入一个命令,如"print 1 2"或"open file1",以便处理用户想要做的事情,我试图在每个空间分解用户输入strtok.我的问题是,对于以下代码:

void tokenize(char string[100],char tokenized[10][MAX_CHARS]){
    char delims[] = " ";        /*Delimitere is a space so tokenize when a space occurs*/
    char *result = NULL;
    int count = 0;
    result=strtok(string,delims);   /*Tokenize the string*/
    while(result!=NULL && count<10){
        tokenized[count++][MAX_CHARS] = result;  /* This is where I get the error */
        result= strtok(NULL,delims);
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

stringapi.c: In function ‘tokenize’:
stringapi.c:33:33: warning: assignment makes integer from pointer without a cast [enabled by default]
Run Code Online (Sandbox Code Playgroud)

我一直试图解决这个问题一段时间没有运气.
我试过,tokenized[count++] = result;但这给了我以下错误: …

c string strtok

1
推荐指数
1
解决办法
3655
查看次数

标签 统计

c ×2

arrays ×1

operating-system ×1

page-fault ×1

paging ×1

process ×1

string ×1

strtok ×1

waitpid ×1