Embedded Linux IPC Unit4 Notes
Embedded Linux IPC Unit4 Notes
IPC allows multiple processes to communicate and share data with each other. Synchronization ensures that
shared resources are accessed safely by multiple processes or threads without conflicts.
Common problems handled by synchronization include race conditions, deadlocks, and starvation.
2. Pipes
Pipes allow unidirectional communication between related processes, typically between parent and child.
Example:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd[2];
pipe(fd);
if (fork() == 0) {
char msg[100];
close(fd[1]);
} else {
close(fd[0]);
return 0;
}
Embedded Linux System Programming - Unit 4 (IPC & Synchronization)
3. Signals
Signals are software interrupts used to notify a process that an event has occurred.
Example:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() {
signal(SIGINT, handler);
while (1) {
printf("Working...\n");
sleep(1);
4. Timers
Example:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
printf("Timer expired!\n");
int main() {
signal(SIGALRM, timer_handler);
alarm(5);
pause();
return 0;
5. Shared Memory
Shared memory allows two or more processes to access the same memory space, making it the fastest IPC.
Example:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
shmdt(str);
return 0;
6. Message Queues
Embedded Linux System Programming - Unit 4 (IPC & Synchronization)
Message queues enable message passing between processes, where each message is stored in the kernel.
Example:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
struct msg_buffer {
long msg_type;
char msg_text[100];
};
int main() {
msg.msg_type = 1;
printf("Message sent.\n");
return 0;
7. Semaphores
Semaphores are used to control access to shared resources and prevent race conditions.
Example:
#include <stdio.h>
Embedded Linux System Programming - Unit 4 (IPC & Synchronization)
#include <sys/sem.h>
#include <unistd.h>
union semun {
int val;
};
int main() {
union semun u;
u.val = 1;
sleep(2);
semctl(semid, 0, IPC_RMID);
return 0;