1
0

feat(lab04): finish exercice 1

This commit is contained in:
2026-05-09 15:18:37 +02:00
parent 70a07f11bb
commit 390334d795

View File

@@ -1,19 +1,15 @@
#include <stdio.h> // #define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <syslog.h>
#include <string.h> #include <string.h>
#define __USE_GNU // not the define from the professor !!!!!!!!!!!!!!!!!!!!!
#include <sched.h> #include <sched.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <signal.h>
const int NBR_MSG = 5; const int NBR_MSG = 5;
const char * MSG[] = { const char * MSG[] = {
@@ -32,7 +28,7 @@ static void catch_signal(int signal) {
break; break;
case SIGINT: case SIGINT:
printf("SIGINT received\n"); printf("SIGINT received\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS); // to avoid to be blocked and kill it with ctrl+c
break; break;
case SIGQUIT: case SIGQUIT:
printf("SIGQUIT received\n"); printf("SIGQUIT received\n");
@@ -71,61 +67,52 @@ int main(int argc, char* argv[]) {
int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
if (err == -1) { if (err == -1) {
perror("socketpair fail"); perror("socketpair fail");
exit(1); exit(EXIT_FAILURE);
} }
/* Prepare cpu set for process affinity */
cpu_set_t set;
CPU_ZERO(&set);
int child_cpu = 0;
int parent_cpu = 1;
/* Fork a child process */ /* Fork a child process */
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) {
cpu_set_t set; if (pid == 0) { /* Parent processus */
CPU_ZERO(&set); pid_t parent_pid = getpid();
CPU_SET(0, &set); printf("Parent processus: pid=%d\n", parent_pid);
int ret = sched_setaffinity(pid, sizeof(set), &set);
/* Setup CPU for process */
CPU_SET(child_cpu, &set);
int ret = sched_setaffinity(parent_pid, sizeof(set), &set);
if (ret == -1) { if (ret == -1) {
perror("sched_setaffinity"); perror("sched_setaffinity");
exit(1); exit(EXIT_FAILURE);
} }
/* Parent processus */ /* Read messages from child */
printf("Parent processus: pid=%d\n", pid);
pid_t child_pid = getpid();
char buffer[100]; char buffer[100];
for (int i = 0; i < NBR_MSG; i++) {
for (int i = 0; i < 7; i++) {
read(fd[1], buffer, strlen(MSG[i])); read(fd[1], buffer, strlen(MSG[i]));
printf("%s\n", buffer); printf("Message %d: %s\n", i, buffer);
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
} }
int status = 0; } else if (pid > 0) { /* Child processus */
int waited_pid = waitpid(pid, &status, 0); pid_t child_pid = getpid();
printf("Child processus: pid=%d\n", child_pid);
if (waited_pid == -1) { /* Setup CPU affinity for processus */
perror("waitpid"); CPU_SET(parent_cpu, &set);
} else { int ret = sched_setaffinity(child_pid, sizeof(set), &set);
if (WIFEXITED(status)) {
printf("Child exited with status %d\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("Child killed by signal %d\n",
WTERMSIG(status));
}
}
} else if (pid > 0) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(1, &set);
int ret = sched_setaffinity(pid, sizeof(set), &set);
if (ret == -1) { if (ret == -1) {
perror("sched_setaffinity"); perror("sched_setaffinity");
exit(1); exit(EXIT_FAILURE);
} }
/* Child processus */
printf("Child processus: pid=%d\n", pid);
/* Write messages for the parent processus */
for (int i = 0; i < NBR_MSG; i++) { for (int i = 0; i < NBR_MSG; i++) {
if(i==2) sleep(2);
write(fd[0], MSG[i], strlen(MSG[i])); write(fd[0], MSG[i], strlen(MSG[i]));
} }
@@ -144,5 +131,5 @@ int main(int argc, char* argv[]) {
kill(getpid(), SIGABRT); kill(getpid(), SIGABRT);
kill(getpid(), SIGINT); kill(getpid(), SIGINT);
return 0; return EXIT_SUCCESS;
} }