Skip to content
Snippets Groups Projects
Commit 4077757c authored by iliya's avatar iliya
Browse files

feat: finished ex3 serie2

parent 5c21e142
No related branches found
No related tags found
No related merge requests found
*.o
mytop
CC := clang
CFLAGS := -g -pedantic -Wall -Wextra -std=c11
LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm -lpthread
TARGET := mytop
all: $(TARGET)
$(TARGET): mytop.o
@printf "=================== Building executable ===================\n"
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
%.o: %.c
@printf "=================== Building sources ===================\n"
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
@printf "=================== Cleaning up... ===================\n"
rm -f *.o $(TARGET)
.PHONY: rebuild
rebuild: clean all
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
int status;
pid_t pid = -1;
char *prog = "top";
pid = fork();
if (pid == 0) {
fprintf(stdout, "I'm the child (%d)\n", getpid());
int exit_code = system(prog);
if (exit_code == -1) {
perror("system");
}
exit(exit_code);
} else if (pid > 0) {
pid = wait(&status);
if (pid == -1) {
perror("wait");
}
if (WIFEXITED(status)) {
fprintf(stdout, "%s completed with exit code %d\n", prog,
WEXITSTATUS(status));
}
} else {
perror("fork");
}
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment