#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h>…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <signal.h> #include <unistd.h> #include <sys/stat.h> #include <time.h>
// Define a structure for process nodes typedef struct ProcessNode { unsigned long pid; unsigned long ppid; long elapsed_time; int isDefunct; int defunct_children_count; struct ProcessNode *next; } ProcessNode;
// Function prototypes ProcessNode *create_process(unsigned long pid, unsigned long ppid, long elapsed_time, int isDefunct); void insert_process(ProcessNode **head, ProcessNode *new_process); void read_processes(ProcessNode **head); int process_exists(ProcessNode *head, unsigned long pid); ProcessNode *get_process(ProcessNode *head, unsigned long pid); void terminate_parents(ProcessNode *head, unsigned long root_pid, int option1, int option2, unsigned long exclude_pid);
// Main function int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, “Usage: deftreeminus [root_process] [OPTION1] [OPTION2] [-processid]\n”); return 1; }
codeProcessNode *head = NULL; read_processes(&head); unsigned long root_pid = strtoul(argv[1], NULL, 10); if (!process_exists(head, root_pid)) { fprintf(stderr, “Invalid root process.\n”); return 1; } unsigned long exclude_pid = 0; int option1 = 0; int option2 = 0; if (argc > 3) { if (argv[argc - 1][0] == ‘-’) { exclude_pid = strtoul(argv[argc - 1] + 1, NULL, 10); if (!process_exists(head, exclude_pid)) { fprintf(stderr, “Invalid exclude process ID.\n”); return 1; } } if (strcmp(argv[2], “-t”) == 0) { option1 = 1; } else if (strcmp(argv[2], “-b”) == 0) { option1 = 2; } else { fprintf(stderr, “Invalid OPTION1 value.\n”); return 1; } option2 = atoi(argv[3]); if (option2 < 1) { fprintf(stderr, “Invalid OPTION2 value.\n”); return 1; } } terminate_parents(head, root_pid, option1, option2, exclude_pid); return 0;}
// Function to create a process node ProcessNode *create_process(unsigned long pid, unsigned long ppid, long elapsed_time, int isDefunct) { ProcessNode *process = malloc(sizeof(ProcessNode)); process->pid = pid; process->ppid = ppid; process->elapsed_time = elapsed_time; process->isDefunct = isDefunct; process->defunct_children_count = 0; process->next = NULL; return process; }
- Assistant