2014-09-26 65 views
2

該程序應該使用fork()創建進程,將子進程的PID存儲到單個鏈接列表中,fork失敗後一次終止一個進程,然後釋放節點鏈接列表,最後在結束程序之前打印出創建了多少個進程。過程管理數據結構和過程控制

目前它不做這些事情,我不知道該去哪裏。它會正確編譯,但是當我在Minix終端中運行它時,我不得不使用它。當我去關閉終端,我最終得到「關機:不能fork():資源暫時不可用」。所以有些事情出錯了,任何幫助將不勝感激。謝謝。

/* 
    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: %d\n", 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); 
} 

回答

0

我想到了晚上離開這個程序後在早上嘗試新鮮的地方後我會出錯。

當fork()會在while循環中失敗時,它會退出程序,這會導致問題,因爲一旦我刪除了這部分,程序就會正確執行。我相信這是因爲程序的kill部分沒有運行,所有這些進程都在運行。此外,我意識到我忘記了包含一行代碼來增加計數器以跟蹤正在創建多少個進程。

這裏是主要的工作代碼:

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); 
}