0% found this document useful (0 votes)
15 views5 pages

Embedded Linux IPC Unit4 Notes

The document covers essential concepts of Inter Process Communication (IPC) and synchronization in embedded Linux systems. It includes examples of various IPC mechanisms such as pipes, signals, timers, shared memory, message queues, and semaphores, highlighting their usage and importance in ensuring safe and efficient process communication. Key issues addressed include race conditions, deadlocks, and starvation.

Uploaded by

sec22cs100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Embedded Linux IPC Unit4 Notes

The document covers essential concepts of Inter Process Communication (IPC) and synchronization in embedded Linux systems. It includes examples of various IPC mechanisms such as pipes, signals, timers, shared memory, message queues, and semaphores, highlighting their usage and importance in ensuring safe and efficient process communication. Key issues addressed include race conditions, deadlocks, and starvation.

Uploaded by

sec22cs100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Embedded Linux System Programming - Unit 4 (IPC & Synchronization)

1. Essential Inter Process Communication (IPC) and Synchronization

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]);

read(fd[0], msg, sizeof(msg));

printf("Child read: %s\n", msg);

} else {

close(fd[0]);

char *text = "Hello from parent";

write(fd[1], text, strlen(text) + 1);

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>

void handler(int sig) {

printf("Caught signal %d\n", sig);

int main() {

signal(SIGINT, handler);

while (1) {

printf("Working...\n");

sleep(1);

4. Timers

Timers execute code after a delay or at intervals.

Example:

#include <stdio.h>

#include <unistd.h>

#include <signal.h>

void timer_handler(int sig) {


Embedded Linux System Programming - Unit 4 (IPC & Synchronization)

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() {

int shmid = shmget(1234, 1024, IPC_CREAT | 0666);

char *str = (char*) shmat(shmid, NULL, 0);

strcpy(str, "Shared memory communication");

printf("Written: %s\n", str);

shmdt(str);

shmctl(shmid, IPC_RMID, NULL);

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() {

key_t key = ftok("progfile", 65);

int msgid = msgget(key, 0666 | IPC_CREAT);

struct msg_buffer msg;

msg.msg_type = 1;

strcpy(msg.msg_text, "Hello, message queue!");

msgsnd(msgid, &msg, sizeof(msg), 0);

printf("Message sent.\n");

msgctl(msgid, IPC_RMID, NULL);

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() {

int semid = semget(1234, 1, IPC_CREAT | 0666);

union semun u;

u.val = 1;

semctl(semid, 0, SETVAL, u);

struct sembuf p = {0, -1, 0};

struct sembuf v = {0, 1, 0};

semop(semid, &p, 1);

printf("In critical section\n");

sleep(2);

semop(semid, &v, 1);

semctl(semid, 0, IPC_RMID);

return 0;

You might also like