Pipe classique:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd[2];
char buf[20];
pipe(fd); // fd[0] = lecture, fd[1] = écriture
if(fork() == 0) { // enfant
close(fd[0]);
write(fd[1], "Bonjour", 7);
close(fd[1]);
} else { // parent
close(fd[1]);
read(fd[0], buf, sizeof(buf));
printf("Parent a reçu: %s\n", buf);
close(fd[0]);
}
return 0;
}
Socket Unix
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#define SOCKET_PATH "/tmp/mysocket"
int main() {
int sockfd;
struct sockaddr_un addr;
char buf[100];
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SOCKET_PATH);
unlink(SOCKET_PATH);
if(fork() == 0) { // enfant = serveur
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
listen(sockfd, 1);
int client = accept(sockfd, NULL, NULL);
read(client, buf, sizeof(buf));
printf("Serveur a reçu: %s\n", buf);
close(client);
close(sockfd);
} else { // parent = client
sleep(1); // attendre que le serveur écoute
int client = socket(AF_UNIX, SOCK_STREAM, 0);
connect(client, (struct sockaddr*)&addr, sizeof(addr));
write(client, "Hello Unix Socket", 17);
close(client);
}
return 0;
}
Shared Memory + Sémaphore (SysV)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>
#include <unistd.h>
int main() {
key_t key = 1234;
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char* data = shmat(shmid, NULL, 0);
if(fork() == 0) { // enfant
strcpy(data, "Message SHM");
} else { // parent
sleep(1); // attendre que l'enfant écrive
printf("Parent a reçu: %s\n", data);
shmdt(data);
shmctl(shmid, IPC_RMID, NULL);
}
return 0;
}
Signals (SIGUSR1 / SIGUSR2)
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handler(int sig) {
printf("Signal reçu: %d\n", sig);
}
int main() {
signal(SIGUSR1, handler);
if(fork() == 0) { // enfant
sleep(1);
kill(getppid(), SIGUSR1);
} else { // parent
pause(); // attendre le signal
}
return 0;
}
TCP Socket (localhost)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
if(fork() == 0) { // serveur
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(12345), htonl(INADDR_ANY)};
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
listen(sockfd, 1);
int client = accept(sockfd, NULL, NULL);
char buf[100];
read(client, buf, sizeof(buf));
printf("Serveur a reçu: %s\n", buf);
close(client);
close(sockfd);
} else { // client
sleep(1);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(12345), inet_addr("[Link]")};
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
write(sockfd, "Hello TCP", 9);
close(sockfd);
}
return 0;
}
UDP Socket (localhost)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
if(fork() == 0) { // serveur
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {AF_INET, htons(12346), htonl(INADDR_ANY)};
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
char buf[100];
recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
printf("Serveur UDP a reçu: %s\n", buf);
close(sockfd);
} else { // client
sleep(1);
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr = {AF_INET, htons(12346), inet_addr("[Link]")};
sendto(sockfd, "Hello UDP", 9, 0, (struct sockaddr*)&addr, sizeof(addr));
close(sockfd);
}
return 0;
}