c-过程管理数据结构和过程控制



这个程序应该使用fork()创建进程,将子进程的PID存储到一个单链表中,在fork失败后,一次杀死一个进程,然后释放链表中的节点,最后打印出创建了多少个进程,再结束程序。

目前它不做这些事情,我不确定该怎么做。它会正确编译,但当我在Minix终端上运行它时,我必须使用它。当我关闭终端时,我最终会得到"shutdown:无法分叉():资源暂时不可用"。因此,出现了问题,如有任何帮助,我们将不胜感激。非常感谢。

/*
   Problem: Write a complete C-program to determine the number of
   simultaneous processes Minix can support for a single user. Be
   aware that a users' login shell is itself a process. Once this has
   been determined, any processes created will have to be terminated.
   Created processes will have to be kept track of in a singly linked list
   with node structure dynamically allocated at runtime.
   Solution: Create processes until fork() fails. Each child process will
   call pause(). The signal will be delivered by the parent process using
   kill() system call. When fork() fails, terminate the children processes
   one at a time using childs' PID and SIGKILL signal. You will have to
   keep track of children process PIDs in a singly linked list.
   Data-structure used: A singly linked list
   Accessing functions for the data structure: malloc() and free() to
   dynamically handle the node storage
   Errors handled: None.
   Limitations: None.
*/
#define _POSIX_SOURCE
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct lnode {
   int pid;
   struct lnode *next;
};
    /* Dynamically allocates node storage at runtime using
       malloc().
    */
struct lnode*
getnode(void)
{
   return malloc(sizeof(struct lnode));
}
/* Frees nodes dynamically allocated by getnode before
   the program is terminated.
*/
void
freenode(struct lnode *tmp)
{
   free(tmp);
}
/* Display the output of how many simultaneous processes Minix
   can support for a single user.
*/
void
printtotal(int count)
{
   fprintf(stdout, "For this user: %dn", count);
}
int
main(int argc, char *argv[])
{
   struct lnode *curr;
   struct lnode *tmp;
   int count = 1;
   int pidholder = 1;
   tmp = NULL;
   while(pidholder > 0) {
      curr = getnode();
      pidholder = fork();
      if(pidholder < 0)
         exit(-1);
      else if(pidholder == 0)
         pause();
      else if(pidholder > 0) {
         curr->pid = pidholder;
         curr->next = tmp;
         tmp = curr;
      }
   }
   curr = tmp;
   while(curr) {
      pidholder = curr->pid;
      kill(pidholder, SIGKILL);
      tmp = curr;
      curr = curr->next;
      freenode(tmp);
   }
   printtotal(count);
   exit(0);
}

我把程序单独放了一晚,第二天早上又重新尝试了一遍,才发现哪里出了问题。

当fork()在while循环中失败时,它将退出程序,这导致了问题,因为一旦我删除了这一部分,程序就会正确执行。我相信这是因为程序的kill部分没有运行,剩下的所有进程都在运行。此外,我意识到我忘记了包含一行代码来增加计数器,以跟踪正在创建的进程数量。

这是main的工作代码:

int
main(int argc, char *argv[])
{
   struct lnode *curr;
   struct lnode *tmp;
   int count = 1;
   int pidholder = 0;
   tmp = NULL;
   while(pidholder >= 0) {
      curr = getnode();
      pidholder = fork();
      if(pidholder == 0)
         pause();
      else if(pidholder > 0) {
         curr->pid = pidholder;
         curr->next = tmp;
         tmp = curr;
         count = count + 1;
      }
   }
   curr = tmp;
   while(curr) {
      pidholder = curr->pid;
      kill(pidholder, SIGKILL);
      tmp = curr;
      curr = curr->next;
      freenode(tmp);
   }
   printtotal(count);
   exit(0);
}

相关内容

  • 没有找到相关文章

最新更新