OS Lab Week-6
OS Lab Week-6
Lab Task – 6
Linux offers several Inter-Process Communication (IPC) mechanisms,
including pipes, message queues, shared memory, semaphores, and sockets.
Below are example programs for some common IPC mechanisms in Linux.
1. Unnamed Pipe
Aim: To Write a program and to demonstrate named and unnamed pipes for IPC.
int main() {
int pipefds[2];
char write_msg[100];
char read_msg[100];
// Create pipe
if (pipe(pipefds) == -1) {
perror("pipe");
return 1;
}
if (pid == 0) {
// Child process: Reads from the pipe
close(pipefds[1]); // Close unused write end
read(pipefds[0], read_msg, sizeof(read_msg)); // Read from pipe
printf("Child received: %s\n", read_msg); // Print received message
close(pipefds[0]); // Close read end
} else {
// Parent process: Gets input from the user and writes to the pipe
close(pipefds[0]); // Close unused read end
printf("Enter a message: ");
return 0;
}
OUTPUT:
1.Named Pipe
Aim: To Write a program and to demonstrate named and named pipes for IPC.
Description: Named pipes (also called FIFOs) allow unrelated processes to
communicate. First, create the FIFO file using mkfifo command:
bash
mkfifo /tmp/myfifo
Then, two separate programs can communicate through the FIFO.
Code:
Writer.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char write_msg[100];
return 0;
}
Reader.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char read_msg[100];
return 0;
}
Output:
Writer.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
int main() {
// Generate a unique key for shared memory
key_t key = ftok("shmfile", 65);
return 0;
}
Reader.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main() {
// Generate the same unique key
key_t key = ftok("shmfile", 65);
return 0;
}
Output:
Code:
Sender.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
int main() {
key_t key;
int msgid;
return 0;
}
Receiver.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
} message;
int main() {
key_t key;
int msgid;
return 0;
}
Output:
Code:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h> // For wait()
#include <unistd.h>
int main() {
key_t key = ftok("semfile", 65); // Generate a unique key
int semid = semget(key, 1, 0666 | IPC_CREAT); // Create a semaphore set with
one semaphore
if (fork() == 0) {
// Child process
semaphore_wait(semid); // Enter critical section
return 0;
}
Output:
int main() {
// Register signal handler for SIGINT
signal(SIGINT, handle_sigint);
return 0;
}
Output:
Code:
Socket:
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
bzero(&servaddr, sizeof(servaddr));
len = sizeof(cli);
} else {
printf("Server accepted the client...\n");
}
Client:
#include <arpa/inet.h> // inet_addr()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // bzero()
#include <sys/socket.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string: ");
n = 0;
break;
}
}
}
int main() {
int sockfd;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
Output: