Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EmbeddedLinuxProject
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christoph-Anton Schwierz
EmbeddedLinuxProject
Commits
ff19954e
Commit
ff19954e
authored
5 months ago
by
Chris
Browse files
Options
Downloads
Patches
Plain Diff
added scalability to main.c
need to test and refactor
parent
f2f3582b
No related branches found
No related tags found
1 merge request
!2
Adding scalability
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.c
+118
-0
118 additions, 0 deletions
main.c
with
118 additions
and
0 deletions
main.c
+
118
−
0
View file @
ff19954e
...
...
@@ -6,6 +6,123 @@
#include
<string.h>
#include
<errno.h>
#define MAX_PROCESSES 10
#define PIPE_DIR "/tmp/"
#define PROCESS_MONITORING_DIR "/Processes/process_monitoring.sh" //bitte überprüfen ob das die richtige DIR ist
typedef
struct
{
char
name
[
256
];
char
pipe_path
[
256
];
int
fd
;
}
ProcessPipe
;
ProcessPipe
processes
[
MAX_PROCESSES
];
//bis auf 10 prozesse skalierbar
int
process_count
=
0
;
void
scan_processes
()
{
FILE
*
file
=
fopen
(
PROCESS_MONITORING_DIR
,
"r"
);
//file öffnen
if
(
!
file
)
{
perror
(
"Could not open process file"
);
exit
(
EXIT_FAILURE
);
//break; oder return 1; ?
}
char
line
[
512
];
while
(
fgets
(
line
,
sizeof
(
line
),
file
)
&&
process_count
<
MAX_PROCESSES
)
{
if
(
strncmp
(
line
,
"/usr/local/bin/"
,
15
)
==
0
)
{
//zeilenweise nach prozessen suchen
char
*
newline
=
strchr
(
line
,
'\n'
);
if
(
newline
)
{
*
newline
=
'\0'
;
}
//prozess in dateistruktur schreiben
snprintf
(
processes
[
process_count
].
name
,
sizeof
(
processes
[
process_count
].
name
),
"%s"
,
strrchr
(
line
,
'/'
)
+
1
);
snprintf
(
processes
[
process_count
].
pipe_path
,
sizeof
(
processes
[
process_count
].
pipe_path
),
"%s%s"
,
PIPE_DIR
,
processes
[
process_count
].
name
);
// mkfifo() nur ausführen, wenn die Pipes noch nicht existieren,
// bzw. Fehler ignorieren, falls schon existiert:
if
(
mkfifo
(
processes
[
process_count
].
pipe_path
,
0666
)
<
0
&&
errno
!=
EEXIST
)
{
//mkfifo machen
perror
(
"mkfifo failed"
);
exit
(
EXIT_FAILURE
);
}
process_count
++
;
}
}
fclose
(
file
);
}
void
open_pipes
()
{
//pipe blockierend öffnen
for
(
int
i
=
0
;
i
<
process_count
;
i
++
)
{
processes
[
i
].
fd
=
open
(
processes
[
i
].
pipe_path
,
O_RDONLY
);
if
(
processes
[
i
].
fd
==
-
1
)
{
perror
(
"Error opening pipe"
);
exit
(
EXIT_FAILURE
);
}
}
}
void
read_pipes
()
{
while
(
1
)
{
//alle pipes in dauerschlife nacheinander lesen
for
(
int
i
=
0
;
i
<
process_count
;
i
++
)
{
char
buffer
[
128
]
=
{
0
};
// read() kann blockieren, wenn noch keine Daten da sind,
// da wir blockierendes I/O verwenden.
// Wenn die Schreibseite offen bleibt, kommen periodisch neue Daten.
ssize_t
bytes_read
=
read
(
processes
[
i
].
fd
,
buffer
,
sizeof
(
buffer
));
if
(
bytes_read
>
0
)
{
// Gültige Daten -> ausgeben
printf
(
"%s: %s
\n
"
,
processes
[
i
].
name
,
buffer
);
}
else
if
(
bytes_read
==
0
)
{
// EOF -> Schreibseite hat geschlossen
// -> ggf. Pipe erneut öffnen
printf
(
"%s closed, reopening...
\n
"
,
processes
[
i
].
name
);
close
(
processes
[
i
].
fd
);
processes
[
i
].
fd
=
open
(
processes
[
i
].
pipe_path
,
O_RDONLY
);
if
(
processes
[
i
].
fd
==
-
1
)
{
perror
(
"Reopen pipe"
);
exit
(
EXIT_FAILURE
);
}
}
else
{
// bytesReadOne < 0 => Fehler
if
(
errno
==
EINTR
)
{
// Signal unterbrochen, einfach weiter
continue
;
}
perror
(
"read error"
);
exit
(
EXIT_FAILURE
);
}
}
// Kurze Pause, damit CPU-Last nicht durch
// Dauerschleife hochgetrieben wird
usleep
(
200000
);
//20ms
}
// Sollte man jemals aus der while(1)-Schleife ausbrechen:
close_pipes
();
}
void
close_pipes
()
{
//braucht man das?
for
(
int
i
=
0
;
i
<
process_count
;
i
++
)
{
close
(
processes
[
i
].
fd
);
}
return
;
}
int
main
()
{
scan_processes
();
open_pipes
();
read_pipes
();
return
0
;
}
//alter code:
/*
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
static const char *PIPE_ONE = "/tmp/pipeOne";
static const char *PIPE_TWO = "/tmp/pipeTwo";
...
...
@@ -102,3 +219,4 @@ int main(void)
return 0;
}
*/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment