Вы находитесь на странице: 1из 13

Distributed

Operating System
Lab

Submitted To:
Prof. SK Dhurandher

Submitted By:
Gaurav Jain
733/IT/14
Index
S. NO. Topic Date Signature

1. WAP to implement FIFO

2. WAP to implement Message Passing

3. WAP to implement Unidirectional and Bidirectional Pipes

4. WAP to implement Socket

5. WAP to implement Shared Memory

6. WAP to implement RPC

7. WAP to implement Semaphore


1. FIFO
A. Reader

// C program to implement one side of FIFO


// This side reads first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd1;

// FIFO file path


char * myfifo = "/tmp/myfifo";

// Creating the named file(FIFO)


// mkfifo(<pathname>,<permission>)
mkfifo(myfifo, 0666);

char str1[80], str2[80];


while (1)
{
// First open in read only and read
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);

// Print the read string and close


printf("User1: %s\n", str1);
close(fd1);

// Now open in write mode and write


// string taken from user.
fd1 = open(myfifo,O_WRONLY);
fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
}
return 0;
}
B. Writer

// C program to implement one side of FIFO


// This side writes first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
int fd;

// FIFO file path


char * myfifo = "/tmp/myfifo";

// Creating the named file(FIFO)


// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666);

char arr1[80], arr2[80];


while (1)
{
// Open FIFO for write only
fd = open(myfifo, O_WRONLY);

// Take an input arr2ing from user.


// 80 is maximum length
fgets(arr2, 80, stdin);

// Write the input arr2ing on FIFO


// and close it
write(fd, arr2, strlen(arr2)+1);
close(fd);

// Open FIFO for Read only


fd = open(myfifo, O_RDONLY);

// Read from FIFO


read(fd, arr1, sizeof(arr1));
// Print the read message
printf("User2: %s\n", arr1);
close(fd);
}
return 0;
}
Output:
Writer Reader
2. Message Passing
A. Message Send

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128

void die(char *s)


{
perror(s);
exit(1);
}

struct msgbuffer
{
long mtype;
char mtext[MAXSIZE];
};

main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
msgbuffer sbuf;
size_t buflen;

key = 1234;

if ((msqid = msgget(key, msgflg )) < 0)


die("msgget");

sbuf.mtype = 1;

printf("Enter a message to add to message queue : ");


scanf("%[^\n]",sbuf.mtext);
getchar();

buflen = strlen(sbuf.mtext) + 1 ;

if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)


{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}

else
printf("Message Sent\n");
exit(0);
}
B. Message Receive

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128

void die(char *s)


{
perror(s);
exit(1);
}

struct msgbuffer
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
key_t key;
msgbuffer rcvbuffer;

key = 1234;

if ((msqid = msgget(key, 0666)) < 0)


die("msgget()");

if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)


die("msgrcv");

printf("%s\n", rcvbuffer.mtext);
exit(0);
}
Output:
Message send Message receive
3. Unidirectional and Bidirectional pipes
Unidirectional pipe

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MSG_LEN 64

int main(){

int result;
int fd[2];
char *message="Linux World!!!\n";
char recvd_msg[MSG_LEN];

result = pipe (fd); //Creating a pipe fd[0] is for reading and fd[1] is for writing

if (result < 0) {
perror("pipe ");
exit(1);
}

//writing the message into the pipe

result=write(fd[1],message,strlen(message));
if (result < 0) {
perror("write");
exit(2);
}

//Reading the message from the pipe

result=read (fd[0],recvd_msg,MSG_LEN);

if (result < 0) {
perror("read");
exit(3);
}

printf("%s\n",recvd_msg);
return 0;
}

Output:
Bidirectional pipes

#include<stdio.h>
#include<fcntl.h>
main(){
int pfd1[2],pfd2[2],p,x1,x2;
char msg1[]="From parent: Yo Man\n";
char msg2[]="From child: How are you?\n"; char buf[20];
x1=strlen(msg1);
x2=strlen(msg2);
pipe(pfd1);
pipe(pfd2);
p=fork();
if(p==0){//from parent to child
close(pfd1[1]);
read(pfd1[0],buf,x1); printf("\nchild recieved:%s",buf);
}
else{
close(pfd1[0]);
write(pfd1[1],msg1,x1);
}
if(p==0){//from child to parent
close(pfd2[0]);
write(pfd2[1],msg2,x2);
}
else{
close(pfd2[1]);
read(pfd2[0],buf,x2); printf("\nparent recieved:%s",buf);
}
return 0;
}
Output:

4. Socket Programming
Socket server
// Server side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server Gaurav";

// Creating socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080


if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

// Forcefully attaching socket to the port 8080


if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}
Socket Client

// Client side C/C++ program to demonstrate Socket programming


#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080

int main(int argc, char const *argv[])


{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client Gaurav";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form


if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)


{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
Output:
Server Client
5. Shared Memory
First User
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define SHMSZ 27

main()
{
char c;
int shmid;
key_t key;
char *shm, *s;

key = 5678;

printf("Requesting a shared memory segment with key : %d ....\n\n", key);

if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {


perror("shmget");
printf("Was unable to create a shared memory segment. Exiting...\n");
exit(1);
}

printf("Shared memory segment was successfully generated with Shared Memory Id : %d\n\n",
(int)shmid);

printf("Attaching the shared memory segment to the process address space...\n\n");


if ((shm = (char *) shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
printf("Attached successfully.\n\n");

printf("Enter a string to be put into the shared memory :");

char ip[26]; scanf("%s", ip);

s = shm;

for(int i=0; ip[i]!='\0'; i++) *s++ = ip[i];


*s = '\0';

printf("Waiting for client to read the data...\n\n");


while (*shm != '*')
sleep(1);
printf("Data has been read successfully by the client.\n\n");

exit(0);
}
Second User

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define SHMSZ 27

main()
{
int shmid;
key_t key;
char *shm, *s;

key = 5678;

printf("Requesting access to the shared memory segment with key : %d ....\n\n", key);
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}

printf("Shared memory segment was successfully accessed with Shared Memory Id : %d\n\n",
(int)shmid);

printf("Attaching the shared memory segment to the process address space...\n\n");

if ((shm = (char *) shmat(shmid, NULL, 0)) == (char *) -1) {


perror("shmat");
exit(1);
}
printf("Attached successfully.\n\n");

printf("Reading data...\n\n");

for (s = shm; *s != '\0'; s++)


putchar(*s);
putchar('\n');

*shm = '*';
exit(0);
}

Output:
First User

Second User

Вам также может понравиться