feat(lab04): ex1
This commit is contained in:
3
doc/lab04-multiprocessing/main.typ
Normal file
3
doc/lab04-multiprocessing/main.typ
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#import "/doc/metadata.typ": *
|
||||||
|
|
||||||
|
= Multiprocessing
|
||||||
13
src/04-multiprocessing/.clangd
Normal file
13
src/04-multiprocessing/.clangd
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
CompileFlags:
|
||||||
|
Add:
|
||||||
|
# Architecture and cross-compilation
|
||||||
|
- "--target=aarch64-linux-gnu"
|
||||||
|
|
||||||
|
# Setup sysroot for buildroot
|
||||||
|
- "--sysroot=/buildroot/output/host/aarch64-buildroot-linux-gnu/sysroot"
|
||||||
|
|
||||||
|
# Add specific header of linux from buildroot
|
||||||
|
- "-I/buildroot/output/build/linux-headers-5.15.148/include"
|
||||||
|
- "-I/buildroot/output/build/linux-headers-5.15.148/arch/arm64/include"
|
||||||
|
- "-I/buildroot/output/build/linux-headers-5.15.148/arch/arm64/include/generated"
|
||||||
|
- "-I/buildroot/output/build/linux-headers-5.15.148/**"
|
||||||
5
src/04-multiprocessing/Makefile
Normal file
5
src/04-multiprocessing/Makefile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
EXE=multiprocessing
|
||||||
|
SRCS=$(wildcard *.c)
|
||||||
|
|
||||||
|
# Include the standard application Makefile for the CSEL1 labs
|
||||||
|
include ../appl.mk
|
||||||
7
src/04-multiprocessing/justfile
Normal file
7
src/04-multiprocessing/justfile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
build:
|
||||||
|
make
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf build
|
||||||
|
rm -rf .obj
|
||||||
|
rm multiprocessing
|
||||||
100
src/04-multiprocessing/main.c
Normal file
100
src/04-multiprocessing/main.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
|
||||||
|
const char * MSG = "Nique ta mère !!\0";
|
||||||
|
|
||||||
|
static void catch_signal(int signal) {
|
||||||
|
|
||||||
|
switch (signal) {
|
||||||
|
case SIGHUP:
|
||||||
|
printf("SIGHUP received\n");
|
||||||
|
break;
|
||||||
|
case SIGINT:
|
||||||
|
printf("SIGINT received\n");
|
||||||
|
break;
|
||||||
|
case SIGQUIT:
|
||||||
|
printf("SIGQUIT received\n");
|
||||||
|
break;
|
||||||
|
case SIGTERM:
|
||||||
|
printf("SIGTERM received\n");
|
||||||
|
break;
|
||||||
|
case SIGABRT:
|
||||||
|
printf("SIGABRT received\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void install_catch_signal()
|
||||||
|
{
|
||||||
|
struct sigaction act = {
|
||||||
|
.sa_handler = catch_signal,
|
||||||
|
};
|
||||||
|
sigemptyset(&act.sa_mask);
|
||||||
|
sigaction(SIGHUP, &act, 0);
|
||||||
|
sigaction(SIGINT, &act, 0);
|
||||||
|
sigaction(SIGQUIT, &act, 0);
|
||||||
|
sigaction(SIGTERM, &act, 0);
|
||||||
|
sigaction(SIGABRT, &act, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
|
install_catch_signal();
|
||||||
|
|
||||||
|
/* Setup socket for inter-process communication */
|
||||||
|
int fd[2];
|
||||||
|
int err = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
|
||||||
|
if (err == -1) {
|
||||||
|
perror("socketpair fail");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fork a child process */
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
|
/* Parent processus */
|
||||||
|
printf("Parent processus: pid=%d\n", pid);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
write(fd[0], MSG, strlen(MSG));
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} else if (pid > 0) {
|
||||||
|
/* Child processus */
|
||||||
|
printf("Child processus: pid=%d\n", pid);
|
||||||
|
char buffer[strlen(MSG)];
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
read(fd[1], buffer, strlen(MSG));
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
/* error */
|
||||||
|
perror("fork fail");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
src/04-multiprocessing/multiprocessing
Executable file
BIN
src/04-multiprocessing/multiprocessing
Executable file
Binary file not shown.
Reference in New Issue
Block a user