-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA1.c
47 lines (36 loc) · 1.35 KB
/
A1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int * getChildren(int parent, int * number) {
int child = 0; // the Pid(s) for the children in the file
char parentNum[20] = ""; // the int that gets passed in
char filePath[50] = ""; // the string for the file path
int arraySize = 0; // an array to hold the children Pid(s)
// Below we are setting the file path to the provided parent Pid
sprintf(filePath, "/proc/%d/task/%d/children", parent, parent);
FILE *in_file = fopen(filePath, "r"); // read only
// test for files not existing.
if (in_file == NULL) {
printf("Error! Could not open file\n");
exit(-1); // must include stdlib.h
}
// goes through the file and gets the numbers while counting the amount of ints in the file
while (fscanf(in_file, "%d", &child ) == 1)
{
number[arraySize] = child; // sets the child pid to an array
printf("\nNumbers: %d\n", number[arraySize]);
arraySize++; // increments the array size
}
fclose(in_file); // closes the file
Data childern;
children.result = number;
children.length = arraySize;
return &children;
}
int main(int argc, char * argv[]){
// int * example = getChildren(1);
int userInput = 1;
int number[100];
getChildren(userInput, &number[0]);
printf("numbers: %d", number[0]);
}