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

Unit 5 Linux

The document provides an overview of Inter-process communication (IPC) mechanisms in Linux, including pipes, semaphores, message queues, shared memory, and sockets. Each IPC method is described with its purpose, system calls, and examples of usage. Additionally, it covers IPC status commands for managing IPC resources.

Uploaded by

Anuj Yadav
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)
23 views5 pages

Unit 5 Linux

The document provides an overview of Inter-process communication (IPC) mechanisms in Linux, including pipes, semaphores, message queues, shared memory, and sockets. Each IPC method is described with its purpose, system calls, and examples of usage. Additionally, it covers IPC status commands for managing IPC resources.

Uploaded by

Anuj Yadav
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

LINUX UNIT 5

Inter-process communication (IPC) is essential for processes to exchange data and


synchronize their actions. Below is an overview of various IPC mechanisms in Linux:

Pipes and Named Pipes (FIFOs)

1. Pipe:
a. Purpose: Allows communication between parent and child processes.
b. Usage: Unidirectional; data flows from the write end to the read end.
c. System Call:

c
int pipe(int pipefd[2]);

2. Process Pipes:
a. Parent and Child Processes: The parent creates a pipe and then forks. Both
the parent and child have access to the pipe.
b. Example:

c
int pipefd[2];
pipe(pipefd);
if (fork() == 0) {
close(pipefd[1]); // Close unused write end
read(pipefd[0], buffer, sizeof(buffer));
close(pipefd[0]);
} else {
close(pipefd[0]); // Close unused read end
write(pipefd[1], "message", strlen("message"));
close(pipefd[1]);
}

3. Named Pipes (FIFOs):


a. Purpose: Similar to pipes but can be used between unrelated processes.
b. System Calls:

c
mkfifo("/tmp/myfifo", 0666);

Semaphores

1. Purpose: Used to control access to a common resource by multiple processes to


avoid critical section problems.
2. System Calls:
a. semget: Creates a new semaphore set or accesses an existing set.

c
int semget(key_t key, int nsems, int semflg);

b. semop: Performs operations on semaphore sets.

c
int semop(int semid, struct sembuf *sops, unsigned nsops);

c. semctl: Performs control operations on semaphore sets.

c
int semctl(int semid, int semnum, int cmd, ...);

Message Queues

1. Purpose: Allows messages to be sent between processes.


2. System Calls:
a. msgget: Creates a new message queue or accesses an existing queue.

c
int msgget(key_t key, int msgflg);

b. msgsnd: Sends a message to a queue.


c
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

c. msgrcv: Receives a message from a queue.

c
int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int
msgflg);

d. msgctl: Performs control operations on message queues.

c
int msgctl(int msqid, int cmd, struct msqid_ds *buf);

Shared Memory

1. Purpose: Allows multiple processes to access a common segment of memory.


2. System Calls:
a. shmget: Allocates a shared memory segment.

c
int shmget(key_t key, size_t size, int shmflg);

b. shmat: Attaches a shared memory segment.

c
void *shmat(int shmid, const void *shmaddr, int shmflg);

c. shmdt: Detaches a shared memory segment.

c
int shmdt(const void *shmaddr);

d. shmctl: Performs control operations on shared memory segments.

c
int shmctl(int shmid, int cmd, struct shmid_ds *buf);

IPC Status Commands

• ipcs: Displays information about active IPC resources.

sh
ipcs

• ipcrm: Removes an IPC resource.

sh
ipcrm -m shmid

Introduction to Sockets

1. Socket: An endpoint for communication between two machines.


2. Socket Connections:
a. Attributes: Specifies the protocol, address, and port.
b. System Calls:
i. socket: Creates a new socket.

c
int socket(int domain, int type, int protocol);

ii. connect: Initiates a connection on a socket.

c
int connect(int sockfd, const struct sockaddr *addr, socklen_t
addrlen);

iii. bind: Binds a socket to an address.

c
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
iv. listen: Listens for connections on a socket.

c
int listen(int sockfd, int backlog);

v. accept: Accepts a connection on a socket.

c
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

3. Socket Communications: Once connected, sockets can send and receive data
using send, recv, write, and read.

You might also like