Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.3 kB
8
Indexable
TW1 Pipes

#include <sys/wait.h> /* wait */
#include <stdio.h>
#include <stdlib.h> /* exit functions */
#include <unistd.h> /* read, write, pipe, _exit */
#include <string.h>
#define ReadEnd 0
#define WriteEnd 1
void report_and_exit(const char* msg) {
perror(msg);
exit(-1); /** failure **/
}
int main() {
int pipeFDs[2]; /* two file descriptors */
char buf; /* 1-byte buffer */
const char* msg = "Nature's first green is gold\n"; /* bytes to
write */
if (pipe(pipeFDs) < 0) report_and_exit("pipeFD");
pid_t cpid = fork(); /* fork a child
process */
if (cpid < 0) report_and_exit("fork"); /* check for
failure */
if (0 == cpid) { /*** child ***/ /* child process
*/
close(pipeFDs[WriteEnd]); /* child reads,
doesn't write */
while (read(pipeFDs[ReadEnd], &buf, 1) > 0) /* read until
end of byte stream */
write(STDOUT_FILENO, &buf, sizeof(buf)); /* echo to the
standard output */
close(pipeFDs[ReadEnd]); /* close the
ReadEnd: all done */
_exit(0); /* exit and
notify parent at once */
}
else { /*** parent ***/
close(pipeFDs[ReadEnd]); /* parent
writes, doesn't read */
write(pipeFDs[WriteEnd], msg, strlen(msg)); /* write the
bytes to the pipe */
close(pipeFDs[WriteEnd]); /* done writing:
generate eof */
wait(NULL); /* wait for
child to exit */
exit(0); /* exit normally
*/
}
return 0;
}

TW1 Message q

// C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10
// structure for message queue
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
// ftok to generate unique key
key = ftok("progfile", 65);
// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;
printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);
// msgsnd to send message
msgsnd(msgid, &message, sizeof(message), 0);
// display the message
printf("Data send is : %s \n", message.mesg_text);
return 0;
}
// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
// ftok to generate unique key
key = ftok("progfile", 65);
// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
// msgrcv to receive message
msgrcv(msgid, &message, sizeof(message), 1, 0);
// display the message
printf("Data Received is : %s \n",
message.mesg_text);
// to destroy the message queue
msgctl(msgid, IPC_RMID, NULL);
return 0;
}



Tw2

Server  


#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sys/types.h>
#include <unistd.h>
#define SERV_TCP_PORT 5035
int main(int argc,char**argv)
{
int sockfd,newsockfd,clength;
struct sockaddr_in serv_addr,cli_addr;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nListening...");
printf("\n");
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
printf("\nAccepted");
printf("\n");
read(newsockfd,buffer,4096);
printf("\nClient message:%s",buffer);
write(newsockfd,buffer,4096);
printf("\n");
close(sockfd);
return 0;
}

Client

#include<stdio.h>
#include<sys/types.h>
#include <unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<netdb.h>
#define SERV_TCP_PORT 5035
int main(int argc,char*argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[4096];
char buf[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
// inet_pton(AF_INET,"127.0.0.1", &serv_addr.sin_addr);
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nReady for sending...");
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nEnter the message to send\n");
printf("\nClient: ");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
read(sockfd,&buf,4096);
printf("Server echo Rxd :%s",buf);
printf("\n");
close(sockfd);
return 0;
}


term work 3


3. Implement the Distance vector routing algorithm.
#include <iostream>
#include <stdio.h>
using namespace std;
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int distance_matrix[20][20];
int n,i,j,k,count=0,src,dest;
cout<<"\nEnter the number of nodes : ";
cin>>n;
cout<<"\nEnter the cost/distance matrix :\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cin>>distance_matrix[i][j];
distance_matrix[i][i]=0;
rt[i].dist[j]=distance_matrix[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>distance_matrix[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
} while(count!=0);
for(i=0;i<n;i++)
{
cout<<"\nRouting table for router"<<i+1<<":\nDest\tNextHop\tDist\n";
for(j=0;j<n;j++)
printf("%d\t%d\t%d\n",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
cout<<"\nEnter source and destination : ";
cin>>src>>dest;
src--; dest--;
printf("Shortest path : \n Via router : %d\n Shortest distance : %d
\n",rt[src].from[dest]+1,rt[src].dist[dest]);
return 0;
}