0% found this document useful (0 votes)
2 views14 pages

Concurrent Sharedmemory

The document describes a C program that implements a server-client architecture using shared memory and sockets for communication. The server manages shared memory segments to allocate and deallocate resources based on client requests, while the client interacts with the server to register and unregister resources. Key functions such as ftok(), shmget(), shmat(), shmdt(), and shmctl() are utilized for managing shared memory in the application.

Uploaded by

Purvi Rajpurohit
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)
2 views14 pages

Concurrent Sharedmemory

The document describes a C program that implements a server-client architecture using shared memory and sockets for communication. The server manages shared memory segments to allocate and deallocate resources based on client requests, while the client interacts with the server to register and unregister resources. Key functions such as ftok(), shmget(), shmat(), shmdt(), and shmctl() are utilized for managing shared memory in the application.

Uploaded by

Purvi Rajpurohit
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/ 14

Function Signature Description

ftok() key_t ftok() It is used to generate a unique key.

int shmget(key_t Upon successful completion,


key,size_t size, int shmget() returns an identifier for
shmget() shmflg); the shared memory segment.

Before you can use a shared


memory segment, you have to
attach yourself to it using shmat().
void *shmat(int shmid
Here, shmid is a shared memory
,void *shmaddr ,int
ID and shmaddr specifies the
shmflg);
specific address to use but we
should set it to zero and OS will
shmat() automatically choose the address.

When you’re done with the shared


int shmdt(void memory segment, your program
*shmaddr); should detach itself from it using
shmdt() shmdt().

When you detach from shared


shmctl(int
memory, it is not destroyed. So, to
shmid,IPC_RMID,NULL);
shmctl() destroy shmctl() is used.
Server:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#define PORT 4444

int main(){

int sockfd, ret,s,*shm,*sh;

struct sockaddr_in serverAddr;

key_t key = ftok("shmfile",65);// // ftok to generate unique key

int shmid=shmget(key,1024,0666|IPC_CREAT);// // shmget returns an


identifier in shmid

shm=(int*) shmat(shmid,NULL,0);

int *ti=(int*) shmat(shmid,(void*)0,0);// // shmat to attach to shared


memory

sh=shm;

sh[0]=2;

sh[1]=2;

sh[2]=2;

sh[3]=2;

sh[4]=2;
sh[5]=2;

*ti=-1;

int newSocket;

struct sockaddr_in newAddr;

socklen_t addr_size;

char buffer[1024];

printf("Test %d",*ti);

pid_t childpid;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0){

printf("[-]Error in connection.\n");

exit(1);

printf("[+]Server Socket is created.\n");

memset(&serverAddr, '\0', sizeof(serverAddr));

serverAddr.sin_family = AF_INET;

serverAddr.sin_port = htons(PORT);

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

ret = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

if(ret < 0){

printf("[-]Error in binding.\n");

exit(1);

}
printf("[+]Bind to port %d\n", 4444);

if(listen(sockfd, 10) == 0){

printf("[+]Listening....\n");

}else{

printf("[-]Error in binding.\n");

while(1){

newSocket = accept(sockfd, (struct sockaddr*)&newAddr,


&addr_size);

if(newSocket < 0){

exit(1);

printf("Connection accepted from %s:%d\n",


inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));

if((childpid = fork()) == 0)

close(sockfd);

*ti=1;

while(1)

recv(newSocket, buffer, 1024, 0);

if(strcmp(buffer, "exit") == 0)

*ti=-1;
printf("Disconnected from %s:%d\n", inet_ntoa(newAddr.sin_addr),
ntohs(newAddr.sin_port));

exit(1);

break;

else

printf("Client: %s\n", buffer);//exitcon

bzero(buffer, sizeof(buffer));

strcpy(buffer,"1.REGISTER 2.UNREGISTER");

send(newSocket, buffer, strlen(buffer), 0);//REG UNREG

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//REG UNREG

s=atoi(buffer);

bzero(buffer, sizeof(buffer));

if(s==1)

strcpy(buffer,"CHOOSE SHED NO:1 2 3");

send(newSocket, buffer, strlen(buffer), 0);//shed

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//shed

s=atoi(buffer);

bzero(buffer, sizeof(buffer));

switch(s)

case 1:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));
recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

if(sh[0]>0)

sh[0]=sh[0]-1;

strcpy(buffer,"ALLOTTED");

else

strcpy(buffer,"NOT AVAILABLE");

if(s==2)

if(sh[1]>0)

sh[1]=sh[1]-1;

strcpy(buffer,"ALLOTTED");

else

strcpy(buffer,"NOT AVAILABLE");

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;
case 2:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

if(sh[2]>0)

sh[2]=sh[2]-1;

strcpy(buffer,"ALLOTTED");

else

strcpy(buffer,"NOT AVAILABLE");

if(s==2)

if(sh[3]>0)

sh[3]=sh[3]-1;

strcpy(buffer,"ALLOTTED");

else

strcpy(buffer,"NOT AVAILABLE");

}
}

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;

case 3:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

if(sh[4]>0)

sh[4]=sh[4]-1;

strcpy(buffer,"ALLOTTED");

else

strcpy(buffer,"NOT AVAILABLE");

if(s==2)

if(sh[5]>0)

sh[5]=sh[5]-1;

strcpy(buffer,"ALLOTTED");

else
{

strcpy(buffer,"NOT AVAILABLE");

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;

if(s==2)

strcpy(buffer,"CHOOSE SHED NO:1 2 3");

send(newSocket, buffer, strlen(buffer), 0);//shed

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//shed

s=atoi(buffer);

switch(s)

case 1:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

sh[0]=sh[0]+1;

if(s==2)

{
sh[1]=sh[1]+1;

strcpy(buffer,"UNREGISTERED");

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;

case 2:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

sh[2]=sh[2]+1;

if(s==2)

sh[3]=sh[3]+1;

strcpy(buffer,"UNREGISTERED");

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;

case 3:strcpy(buffer,"1.TWO WHEELER 2.FOUR WHEELER");

send(newSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(newSocket, buffer, 1024, 0);//MODE

s=atoi(buffer);

if(s==1)

{
sh[4]=sh[4]+1;

if(s==2)

sh[5]=sh[5]+1;

strcpy(buffer,"UNREGISTERED");

send(newSocket, buffer, strlen(buffer), 0);//ALLOTMENT

break;

close(newSocket);

return 0;

Client:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>
#include <netinet/in.h>

#include <arpa/inet.h>

#define PORT 4444

int main(){

int clientSocket, ret,f;

struct sockaddr_in serverAddr;

char buffer[1024];

key_t key = ftok("shmfile",65);

int shmid=shmget(key,1024,0666|IPC_CREAT);

int *ti=(int*)shmat(shmid,(void*)0,0);

printf("test %d\n",*ti);

clientSocket = socket(AF_INET, SOCK_STREAM, 0);

if(clientSocket < 0){

printf("[-]Error in connection.\n");

exit(1);

printf("[+]Client Socket is created.\n");

memset(&serverAddr, '\0', sizeof(serverAddr));

serverAddr.sin_family = AF_INET;

serverAddr.sin_port = htons(PORT);

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

ret = connect(clientSocket, (struct sockaddr*)&serverAddr,


sizeof(serverAddr));

if(ret < 0){


printf("[-]Error in connection.\n");

exit(1);

printf("[+]Connected to Server.\n");

while(*ti==1)

sleep(1);

printf("sleep finished\n");

while(1)

printf("exit or continue(type userid): \t");

scanf("%s", &buffer);

send(clientSocket, buffer, strlen(buffer), 0);

if(strncmp(buffer, "exit",4) == 0){

close(clientSocket);

printf("[-]Disconnected from server.\n");

exit(1);

recv(clientSocket, buffer, 1024, 0);//REG

printf("%s\n", buffer);

scanf("%d",&f);

snprintf(buffer,1024,"%d",f);

send(clientSocket, buffer, strlen(buffer), 0);//REG

bzero(buffer, sizeof(buffer));

recv(clientSocket, buffer, 1024, 0);//shedno


printf("%s\n", buffer);

scanf("%d",&f);

snprintf(buffer,1024,"%d",f);

send(clientSocket, buffer, strlen(buffer), 0);//shed

bzero(buffer, sizeof(buffer));

recv(clientSocket, buffer, 1024, 0);//MODE

printf("%s\n", buffer);

scanf("%d",&f);

snprintf(buffer,1024,"%d",f);

send(clientSocket, buffer, strlen(buffer), 0);//MODE

bzero(buffer, sizeof(buffer));

recv(clientSocket, buffer, 1024, 0);//ALLOT

printf("%s\n", buffer);

return 0;

You might also like