0% found this document useful (0 votes)
8 views

Program 5

The document describes a program that implements mutual exclusion for file sharing using a token-based algorithm in C. It includes code for a server that listens on a specified port, handles critical sections, and communicates with other processes via UDP sockets. The program allows processes to enter a critical section, log their activity to a shared file, and terminate gracefully upon receiving specific messages.

Uploaded by

Hardik Jain
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)
8 views

Program 5

The document describes a program that implements mutual exclusion for file sharing using a token-based algorithm in C. It includes code for a server that listens on a specified port, handles critical sections, and communicates with other processes via UDP sockets. The program allows processes to enter a critical section, log their activity to a shared file, and terminate gracefully upon receiving specific messages.

Uploaded by

Hardik Jain
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/ 4

Program - 5

Aim: Program to implement Mutual Exclusion to share files using token based
algorithm.

Code:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#define MAXLINE 1024

typedef struct sockaddr SA;


typedef struct sockaddr_in SA_IN;

void verify(int num, char * quitMessage){


if(num < 0)
{
perror(quitMessage);
exit(EXIT_FAILURE);
}
}

int connect_to_port(int port_num){


int sock_id; int opt = 1;
SA_IN server_addr;

verify(sock_id = socket(AF_INET, SOCK_DGRAM, 0), "Unable to create socket");


setsockopt(sock_id, SOL_SOCKET, SO_REUSEADDR,
(const void *)&opt, sizeof(int));
memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port_num);

verify(bind(sock_id, (const SA * ) & server_addr,


sizeof(server_addr)), "Unable to bind to port");
return sock_id;
}

void critical_section(int self_port){


printf("Critical Section Entered\n");
sleep(5);
FILE * fp = fopen("common_token_ring.txt","a");
fprintf(fp,"Process %d was here\n", self_port);
fclose(fp);
}

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

int self_port = atoi(argv[1]);


int next_port = atoi(argv[2]);
int initiate = atoi(argv[3]);

char buffer[MAXLINE];
printf("Initialising the server at port %d.\n", self_port);
int sockfd = connect_to_port(self_port);

struct sockaddr_in next_client_addr, prev_client_addr;


int len, n;
char response[MAXLINE];
memset(&next_client_addr,0,sizeof(next_client_addr));

next_client_addr.sin_family=AF_INET;
next_client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
next_client_addr.sin_port = htons(next_port);

if(initiate)
{
critical_section(self_port);

strcpy(response,"ACK");
printf("Printing %s to port %d.\n", response, next_port);
int check = sendto(sockfd, (const char * ) response, strlen(response),
MSG_CONFIRM,(const SA *)&next_client_addr,
sizeof(next_client_addr));

memset(&prev_client_addr, 0 , sizeof(prev_client_addr));
n = recvfrom(sockfd, (char*)buffer,MAXLINE, MSG_WAITALL,
(SA * ) &prev_client_addr, &len);
buffer[n]='\0';

if(!strcmp(buffer,"ACK"))
{
strcpy(response,"TERM");
sendto(sockfd, (const char * ) response, strlen(response),
MSG_CONFIRM, (const SA *) & next_client_addr,
sizeof(next_client_addr));
printf("Exiting\n");
}
else
{
printf("Invalid Message Recieved\n");
}
exit(0);
}
else
{
while(1)
{
memset(&prev_client_addr, 0, sizeof(prev_client_addr));
printf("Ready to Listen at port %d\n", self_port);
n = recvfrom(sockfd, (char*)buffer, MAXLINE,
MSG_WAITALL, ( SA *)&prev_client_addr, &len);
buffer[n]='\0';

if(!strcmp(buffer,"ACK"))
{
critical_section(self_port);
printf("Printing %s to port %d.\n", buffer, next_port);
sendto(sockfd, (const char * ) buffer, strlen(buffer),
MSG_CONFIRM, (const SA *) & next_client_addr,
sizeof(next_client_addr));
}
else if(!strcmp(buffer,"TERM"))
{
printf("Printing %s to port %d.\n", buffer , next_port);
sendto(sockfd, (const char * ) buffer, strlen(buffer),
MSG_CONFIRM, (const SA *) & next_client_addr,
sizeof(next_client_addr));
printf("Exiting\n");
exit(0);
}
else{
printf("Invalid Message Recieved\n");
}
}
}
}

Output:

Process 1: -

Process 2: -
Process 3: -

Shared File: -

You might also like