TRANSFER CONTROL PROTOCOL(TCP)
Server
Algorithm
The activity involved in a tcp server are..........
1.Create a socket.
2.Bind it to the operating system.
3.Listen over it.
4.Accept connections.
5.Read/Write processes.
6.Close the socket.
Program
/*Program to demonstrate the creation and usage of tcp socket.
* This module acts as the tcp server which listens to a socket and accepts connection.
* The server initially reads a message from the client and then writes a message to it.
* This module should be compiled using the command. 'c++ tcpserver.cpp' and execute './a.out'.*/
//inculsion.
#include
#include
#include
#include
#include
int main()
{
//variables to store the socket id.
int serversocket,clientsocket;
//variables to store the network host addresses.
sockaddr_in serveraddr,clientaddr;
//variable to store address length.
socklen_t len;
char message[50];
//creating a socket.
serversocket=socket(AF_INET,SOCK_STREAM,0);
//steps to include the host address
bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5030);
serveraddr.sin_addr.s_addr=INADDR_ANY;
//binding the socket to the operating system.
bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));
bzero((char*)&clientaddr,sizeof(clientaddr));
len=sizeof(clientaddr);
//listening over the socket.
listen(serversocket,5);
printf("\nWaiting for client connectivity.\n");
//accepting the connection.
clientsocket=accept(serversocket,(sockaddr*)&clientaddr,&len);
printf("\nClient connectivity received.\n");
printf("\nReading message from the client.\n");
//reading activity.
read(clientsocket,message,sizeof(message));
printf("\nThe client has send:\t%s\n",message);
printf("\nSending message to the client.\n");
//writing activity.
write(clientsocket,"YOUR MESSAGE RECEIVED.",sizeof("YOUR MESSAGE RECEIVED."));
close(clientsocket);
close(serversocket);
}
Output
Client
Algorithm
The different processes involved in a udp client process are........
1.Create a datagram socket.
2.Receive/Send message to the server.
3.Close the socket.
Program
/* Program to demonstrate the creation and usage of datagram sockets.
* This module acts as a udp client which sends and receives messages from a udp server.
* This module should be compiled into different folder using the command 'c++
udpclient.cpp -o b' and execute using './b'
* For execution the 'udpserver' module should be executed first then this module.
//inculsion.
#include
#include
#include
#include
#include
int main()
{
//variable to store the socket_id.
int clientsocket;
//variable to store the address.
sockaddr_in serveraddr;
//variableto store the address length.
socklen_t len;
//variable to store the network byte order address.
hostent *server;
char message[50];
//socket creation.
clientsocket=socket(AF_INET,SOCK_DGRAM,0);
//steps involved in the server address creation.
bzero((char*)&serveraddr,sizeof(serveraddr));
len=sizeof(serveraddr);
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5015);
server=gethostbyname("127.0.0.1");
bcopy((char*)server->h_addr,(char*)&serveraddr.sin_addr.s_addr,sizeof(server->h_addr));
printf("\nPRESS ENTER TO START THE CONNECTION PROCESS.\n");
fgets(message,2,stdin);
printf("\nSending message for server connection\n");
//sending message.
sendto(clientsocket,"HI I AM CLIENT...",sizeof("HI I AM CLIENT...."),0,(sockaddr*)&serveraddr,sizeof(serveraddr));
printf("\nReceiving message from server.\n");
//receiving messages.
recvfrom(clientsocket,message,sizeof(message),0,(sockaddr*)&serveraddr,&len);
printf("\nMessage received:\t%s\n",message);
close(clientsocket);
}
Output
C/C++
Linux
Followers
SOCKET PROGRAMMING
Theory
A datagram socket
provides access to the underlying communication protocols.
These sockets are usually datagram oriented, but their exact characteristics depend on the interface provided by the protocol.
Socket Creation and Naming
• For UNIX domain sockets with paths containing 14, or fewer characters, you can:
• #include
• ...
• bind (sd, (struct sockaddr *) &addr, length);
• If the path of a UNIX domain socket requires more characters, use:
• #include
• ...
• bind (sd, (struct sockaddr_un *) &addr, length);
• For Internet domain sockets, use
• #include
• ...
• bind (sd, (struct sockaddr_in *) &addr, length);
In the UNIX domain, binding a name creates a named socket in the file system. Use unlink() or rm () to remove the socket.
Connecting Stream Sockets
connect (sd, (struct sockaddr_un *)&server, length);
while an Internet domain call would be:
struct sockaddr_in;
...
connect (sd, (struct sockaddr_in *)&server, length);
The flags parameter is formed from the bitwise OR of zero or more of the following:
MSG_OOB
-- Send "out-of-band" data on sockets that support this notion. The underlying protocol must also support "out-of-band" data. Only SOCK_STREAM sockets created in the AF_INET address family support out-of-band data.
MSG_DONTROUTE
-- The SO_DONTROUTE option is turned on for the duration of the operation. It is used only by diagnostic or routing pro- grams.
MSG_PEEK
-- "Peek" at the data present on the socket; the data is returned, but not consumed, so that a subsequent receive operation will see the same data.
A SOCK_STREAM socket is discarded by calling close().
Datagram sockets
accept() and listen() are not used with datagram sockets.
SHARED MEMORY
Theory
#include
#include
#include
...
key_t key; /* key to be passed to shmget() */
int shmflg; /* shmflg to be passed to shmget() */
int shmid; /* return value from shmget() */
int size; /* size to be passed to shmget() */
...
key = ...
size = ...
shmflg) = ...
if ((shmid = shmget (key, size, shmflg)) == -1) {
perror("shmget: shmget failed"); exit(1); } else {
(void) fprintf(stderr, "shmget: shmget returned %d\n", shmid);
exit(0);
}
...
Controlling a Shared Memory Segment
shmctl() is used to alter the permissions and other characteristics of a shared memory segment. It is prototyped as follows:
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
The process must have an effective shmid of owner, creator or superuser to perform this command. The cmd argument is one of following control commands:
SHM_LOCK -- Lock the specified shared memory segment in memory. The process must have the effective ID of superuser to perform this command.
SHM_UNLOCK -- Unlock the shared memory segment. The process must have the effective ID of superuser to perform this command.
IPC_STAT-- Return the status information contained in the control structure and place it in the buffer pointed to by buf. The process must have read permission on the segment to perform this command.
IPC_SET-- Set the effective user and group identification and access permissions. The process must have an effective ID of owner, creator or superuser to perform this command.
IPC_RMID -- Remove the shared memory segment.
The buf is a sructure of type struct shmid_ds which is defined in
Program
Server
/*Program to demonstrate the use of shared memory in interprocess communication.
* This program acts as a server which waits for a message from the client process.
* The process is implemented using a shared memory space called 'shared_location'.
* The 'message' part of this space stores the message while the 'written' part is used to indicate whether the server has read the message or the client has written the message.(0->read,1->written).
* This module should be compiled and executed first.
* This module is compile using 'cc shmserver.c' and executed using './a.out'*/
//inculsion
#include
#include
#include
#include
#include
#include
//structure definition for acquiring a shared location.
struct shared_location
{
int written;
char message[30];
};
int main()
{
int shmid,running;
void *address;
struct shared_location * ptr;
//for getting a shared location
shmid=shmget((key_t)1234,sizeof(struct shared_location),0666|IPC_CREAT);
//optional if required for error checking.
if(shmid<=0) { printf("\nERROR:\tCannot allocate shared space.\n"); exit(0); } //attaching the shared location to this process.
address=shmat(shmid,(void *)0,0); //optional if required for error checking.
if(address==(void*)0) { printf("\nEROOR:\tShared location cannot be attached.\n"); } //type casting so as to convert the obyained location into our format.
ptr=(struct shared_location*)address; ptr->written=0;
strcpy(ptr->message,"Hi I am server.");
running=1;
while(running)
{
printf("\nWaiting for the client to enter the message.\n");
while(ptr->written==0)
{
}
if(ptr->written==1)
{
printf("The client has entered the message: %s\n",ptr->message);
}
ptr->written=0;
if(strcmp(ptr->message,"end")==0)
{
running=0;
}
}
//detaching the shared location.
shmdt(address);
//deleting the shared location.
shmctl(shmid,IPC_RMID,NULL);
}
Output
Client
Program
/* Program to demonstrate the use of shared memory in interprocess communication.
* This module acts as a client which sends messages to the server process.
* In this the message entered into the message part of the 'shared_location' structure and the 'written' field is set to 1.
* The remaining process is similar to that of the server process.
* This module can be compiled using 'cc shmclient.c -o b' and executed using './b'
//inculsion
#include
#include
#include
#include
#include
#include
//structure definition for the shared location.
struct shared_location
{
int written;
char message[30];
};
//the functions and other terms used have similar meanings to that used in the server module
//but they don't bear any specific relationship.
int main()
{
int shmid,running;
void *address;
struct shared_location * ptr;
shmid=shmget((key_t)1234,sizeof(struct shared_location),0666|IPC_CREAT);
if(shmid<=0)
{
printf("\nERROR:\tCannot allocate shared memory.\n");
exit(0);
}
address=shmat(shmid,(void*)0,0);
if(address==(void*)0)
{
printf("\nERROR:\tCannot attach the shared location\n");
exit(0);
}
ptr=(struct shared_location*)address;
running=1;
while(running)
{
printf("\nWaiting for the server\n");
while(ptr->written==1)
{
}
printf("\nEnter your message.(type 'end' to exit)\n");
scanf("%s",ptr->message);
ptr->written=1;
if(strcmp(ptr->message,"end")==0)
{
running=0;
}
}
address=shmat(shmid,(void *)0,0);
shmdt(address);
}
Output
PIPES - Linux in C
Theory
SYSTEM CALL: pipe();
PROTOTYPE: int pipe( int fd[2] );
RETURNS: 0 on success
-1 on error: errno = EMFILE (no free descriptors)
EMFILE (system file table is full)
EFAULT (fd array is not valid)
NOTES: fd[0] is set up for reading, fd[1] is set up for writing
The pclose() function closes a stream opened by popen() by closing the pipe. It waits for the associated process to terminate and returns the termination status of the process running the command language interpreter. This is the value returned by waitpid(2).
SIMPLE PIPES OR LOWLEVEL PIPES
Program
/* Program to demonstrate the creation and use of simple/low level pipe.
* This program creates a pipe that connects the child process with its parent process.
* Here the child process writes 'REQUEST FROM CHILD PROCESS.' on to the pipe which is read by the parent process.
* The function 'fork()' is a system call used to create a child process.
* This module can be compiled using 'cc simplepipe.c' and executed using './a.out'
//inculsion
#include
#include
int main()
{
//array to store the pipe pointers returned by the pipe() function.
int pipe_pointer[2];
//variable to store the child process id.
pid_t child;
char message[50];
//creating a pipe
pipe(pipe_pointer);
//creating a child process.
child=fork();
if(child==0)
{
printf("\nThe child process is writing.\n");
//closing the read pointer.
close(pipe_pointer[0]);
//writing on to the pipe.
write(pipe_pointer[1],"REQUEST FROM CHILD PROCESS.",sizeof("REQUEST FROM CHILD PROCESS."));
}
else
{
printf("\nThe parent process is reading.\n");
//closing the write pointer.
close(pipe_pointer[1]);
//reading from the pipe.
read(pipe_pointer[0],message,sizeof(message));
printf("\nThe parent has read:\n\n** %s **\n",message);
}
close(pipe_pointer[0]);
close(pipe_pointer[1]);
}
Output
FORMATTED PIPE
/* Program to demonstrate the creation and use of formatted pipe.
* This program creates a formatted pipe which is connected to the system process denoted by the command 'CMD'.
* The pipe is established for reading and the read content is displayed.
* This module can be compiled using 'cc formattedpipe.c' and executed './a.out'.*/
//inculsion part
#include
#include
#define CMD "cal"
int main()
{
FILE *ptr;
char message[100];
char cmd[20];
strcpy(cmd,CMD);
//creating a formatted pipe.
ptr=popen(cmd,"r");
while( fgets(message,sizeof(message),ptr)!=NULL)
{
printf("%s",message);
}
//closing the pipe.
pclose(ptr);
}
Output
iSCSI(Small Computer System Interface)
Introduction
Download Full Seminar Report iSCSI
/**** Author:Sarju
www.sarjus-elearning.blogspot.com****/
# include
# include
# include
# include
int x,y,maxx,maxy,i,j;
int main(void)
{
int gdriver = DETECT, gmode;
void *image;
unsigned int size;
char ch;
void fillBox(int,int);
int Row,Col;
// Initialize graphics drivers and mode.
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
// Draw a rectangle
// rectangle(x1,y1,x2,y2)
maxx=getmaxx(); // x2
maxy=getmaxy(); // y2
rectangle(10,10,maxx-10,maxy-10);
// Puts Pixel in the Rectangle.
fillBox(maxx,maxy);
x=y=70;
// Draw Circle and FillColor
setfillstyle(1,14);
circle(x,y,20);
floodfill(x,y,15);
// Creating the Image
size = imagesize(x,y,x+20,y+20);
image=malloc(size);
getimage(x-20,y-20,x+20,y+20,image);
x-=20;
y-=20;
// Will Displays till Any key is hit !!!...
while(!kbhit())
{
Row = x; Col = y;
putimage(x,y,image,XOR_PUT); // clears the Image from Screen
//x=random(maxx-70);
//y=random(maxy-70);
//fillBox(maxx,maxy);
x=x+10;
/* if(x<50)
x=50;
if(y<50)
y=50;*/
putimage(x,y,image,OR_PUT); // Puts the image on screen.
delay(50); // Waits for few Seconds.
if(x>540)
{
putimage(x,y,image,XOR_PUT);
Row = x; Col = y;
for(i=0;i<540;i++)
{
x=random(maxx-10);
y=random(maxy-10);
if(x>10 && y>10)
putpixel(x,y,14);
}
//putpixel(x,y,14);
x= Row;
y=Col;
x=10;
y=y+20;
putimage(x,y,image,XOR_PUT);
}
if(y>410)
{
putimage(x,y,image,XOR_PUT);
Row = x; Col = y;
fillBox(maxx,maxy);
x= Row;
y=Col;
x=10;
y=30;
putimage(x,y,image,XOR_PUT);
}
}
free(image); //Removes the image from the Screen.
closegraph(); // Closes the Graphics Mode.
return(0);
}
void fillBox(int maxx,int maxy)
{
cleardevice(); // Clears the Graphics Screen
maxx=getmaxx(); // x2
maxy=getmaxy(); // y2
rectangle(10,10,maxx-10,maxy-10);
for(i=0;i<8000;i++)
{
x=random(maxx-10);
y=random(maxy-10);
if(x>10 && y>10)
putpixel(x,y,14);
}
}
#include
/*** Author : Sarju S
http://www.sarjus-elearning.blogspot.com/
***/
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int c=12;
setbkcolor(0);
//setlinestyle(0,1,2);
int t;
while(1)
{
settextstyle(2,0,5);
outtextxy(100,10,"Press L,H ,T,P");
outtextxy(100,30,"Press 1 for Quit");
as:
setcolor(13);
ellipse(380,127,20,152,130,35);
//////////////////////////////rear//////////////////////////
line(490,109,560,142);
line(560,142,569,142);
line(569,142,582,102);
line(582,102,620,92);
line(593,132,617,125);
line(617,124,627,96);
line(620,92,628,97);
line(472,86,602,96);
line(501,113,575,121);
line(443,77,475,80);
line(443,77,432,93);
line(475,80,472,85);
//setcolor(4);
line(593,132,593,137);
line(593,137,600,141);
line(600,141,600,185);
line(600,185,608,192);
line(608,192,608,234);
line(608,234,586,253);
line(586,253,577,248);
///////////////////////// mirror
line(263,112,363,127);
line(193,160,263,112);
line(193,160,220,170);
line(220,170,280,180);
line(280,180,320,185);
line(320,185,363,127);
////////////////////////////////sidemirror
line(340,194,460,169);
line(460,169,519,152);
ellipse(512,144,300,30,10,10);
ellipse(467,143,28,100,50,30);
line(510,128,521,138);
line(435,116,440,171);
// setcolor(4);
////////////////////////////////////////cont//
line(339,194,372,144);
// line(372,140,386,128);
ellipse(454,208,87,123,128,95);
line(372,144,384,128);
int b,x,y;
////////////////////////lower
line(365,298,524,264);
line(365,298,330,310);
line(330,310,323,310);
///////////////////////////////bumper
ellipse(162,221,135,190,90,40);
line(96,193,140,174);
line(140,174,160,168);
line(160,168,192,161);
//////////////////////front
ellipse(75,246,95,190,18,18);
line(57,251,57,286);
//setcolor(4);
ellipse(181,178,232,263,200,137);
ellipse(195,180,256,286,200,137);
ellipse(191,171,228,247,200,100);
ellipse(231,198,234,275,200,80);
//setcolor(9);
//ellipse(195,170,256,286,200,137);
//setcolor(12);
ellipse(196,167,228,246,200,90);
ellipse(231,184,234,276,200,80);
ellipse(191,200,228,246,200,90);
ellipse(228,218,234,276,200,80);
ellipse(258,268,180,220,200,40);
ellipse(178,296,244,355,16,10);
ellipse(238,249,227,250,200,60);
/////////////wheel1
ellipse(302,281,320,77,26,45);
ellipse(290,277,65,162,40,45);
ellipse(278,288,144,212,31,45);
/////////////wheel2
//setcolor(5);
ellipse(302+260,229,328,87,26,45);
ellipse(290+280-7,277-50+2,90,162,40,45);
ellipse(278+270,288-50,144,215,27,45);
b=0;
int v=0;
/////////
ellipse(302+250+v,227+b,295,90,29,41);
ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+248+v,229+b,0,360,21,30);
ellipse(302+247+v,229+b,0,360,8,10);
setfillstyle(6,11);
//floodfill(302+248+v,230+b,13);
//line(546,201,546,257);
//line(554,201,554,257);
//setcolor(4);
line(546+v,201+b,546+v,220+b);
line(551+v,201+b-2,551+v,220+b);
line(546+v,238+b,546+v,257+b);
line(551+v,238+b+2,551+v,257+b+2);
line(530+v,225+b,541+v,225+b);
line(530+v,230+b,541+v,230);
line(557+v,225+b,570+v,225+b);
line(557+v,230+b,570+v,230+b);
line(563+v,206+b,552+v,222+b);
line(534+v,246+b,543+v,232+b);
line(566+v,210+b,556+v,223+b);
line(536+v,250+b,544+v,238+b);
line(536+v,207+b,546+v,222+b);
line(532+v,213+b,542+v,224+b);
line(556+v,235+b,566+v,247+b);
line(551+v,237+b,563+v,253+b);
////////////////////////////////////////////////////
v=-260;
b=56;
ellipse(302+233+v,221+b,260,60,49,51);
//ellipse(302+234+v,231+b,245,306,50,40);
//setlinestyle(3,0,3);
ellipse(302+243+v,224+b,0,360,28,35);
// line(249,328,269,328);
ellipse(300+245+v,223+b,0,360,10,12);
ellipse(285+249+v,239+b,210,260,30,33);
//floodfill(285+258+v,230+b,12);
b=45;
v=v-4;
line(546+v,201+b,546+v,220+b+2);
line(551+v,201+b,551+v,220+b+2);
b=b+8;
line(546+v,238+b,546+v,257+b+4);
line(551+v,238+b,551+v,257+b+4);
v=v-2;
line(530+v-6,225+b,541+v,225+b);
line(530+v-6,230+b,541+v,230+b);
v=v+5;
line(557+v,225+b,570+v+3,225+b);
line(557+v-1,230+b,570+v+3,230+b);
b=b-5;
v=v-5;
line(565+v+3,206+b,552+v+4,222+b-2);
b=b+15;
line(534+v,246+b,543+v+3,232+b-5);
b=b-10;
line(566+v+7,210+b-5,556+v+4,220+b);
line(536+v-5,250+b,544+v-2,238+b-4);
line(536+v,207+b-8,545+v,222+b-5);
line(531+v,212+b-8,542+v,224+b-2);
line(556+v,235+b,566+v+3,247+b+5);
line(551+v,237+b,563+v+2,253+b+3);
///////////////////lights
ellipse(199,250,144,345,18,8);
line(185,245,206,230);
//setcolor(4);
ellipse(223,234,340,110,8,5);
line(230,237,217,252);
line(206,230,220,229);
//setfillstyle(1,4);
//floodfill(200,240,12);
/////////////////////////////////////
line(90,223,152,236);
line(152,236,137,254);
line(90,223,90,242);
//setfillstyle(10,9);
//floodfill(91,230,14);
ellipse(240,270,104,136,100,60);
ellipse(185,237,120,160,100,60);
ellipse(80,221,357,134,10,10);
line(152,236,168,228);
///////////////////////////////////////////////
line(435,116,440,171);
//////////////////////////////////////////hp
//line(134,185,220,210);
line(134,185,196,160);
line(214,212,318,185);
/////////////////////////////////////////////////light
//setcolor(14);
ellipse(166,247,99,330,8,8);
ellipse(171,243,310,129,7,7);
putpixel(174,250,13);
putpixel(173,251,13);
putpixel(164,239,13);
putpixel(165,238,13);
/////////////////////////////////////////road/////////////////////
setcolor(13);
line(1,430,639,300);
line(1,445,639,315);
line(1,210,93,194);
line(1,195,194,158);
//line(1,170,639,71);
//line(1,170,229,135);
line(520,90,639,71);
line(478,86,639,56);
int c=0;
line(10,194+c,10,208+c);
line(40,189+c,40,204+c);
line(70,183+c,70,198+c);
line(100,176+c,100,190+c);
line(130,170+c,130,177+c);
line(160,166+c,160,168+c);
line(190,160+c,190,161+c);
line(190+330,78+c,190+330,89+c);
line(190+360,72+c,190+360,85+c);
line(190+390,67+c,190+390,81+c);
line(190+420,62+c,190+420,76+c);
line(190+449,57+c,190+449,71+c);
c=236;
line(10,192+c,10,208+c);
line(40,189+c-2,40,204+c-3);
line(70,183+c-3,70,198+c-3);
line(100,176+c-2,100,190+c-2);
line(130,170+c-2,130,177+c+5);
line(160,166+c-3,160,168+c+8);
line(190,160+c-4,190,161+c+9);
line(190+30,156+c-5,190+30,170+c-5);
line(190+30+30,156+c-12,190+30+30,170+c-12);
line(190+90,156+c-18,190+90,170+c-17);
line(190+120,156+c-25,190+120,170+c-25);
line(190+150,156+c-30,190+150,170+c-30);
line(190+180,156+c-37,190+180,170+c-36);
line(190+210,156+c-42,190+210,170+c-42);
line(190+240,156+c-48,190+240,170+c-48);
line(190+270,156+c-55,190+270,170+c-54);
line(190+300,156+c-61,190+300,170+c-61);
line(190+330,78+c+10,190+330,89+c+13);
line(190+360,72+c+11,190+360,85+c+13);
line(190+390,67+c+10,190+390,81+c+10);
line(190+420,62+c+8,190+420,76+c+10);
line(190+449,57+c+8,190+449,71+c+8);
/////////////////road
setcolor(12); /////////////////////////////1
line(1,310,25,306);
line(6,318,30,315);
line(1,310,6,318);
line(25,306,30,314);
int k,m;
k=13*45+19;
m=16*(-8);
//2
setcolor(12);
line(605,310-128,629,306-128);
line(610,318-128,634,315-128);
line(605,310-128,610,318-128);
line(629,306-128,634,314-128);
setcolor(12); //////////////////////////////////3
k=45;
m=-8;
line(46,302,70,298);
line(51,310,75,307);
line(46,302,51,310);
line(70,298,75,306);
setfillstyle(1,0);
floodfill(64,303,12);
setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);
setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);
setfillstyle(1,14);
floodfill(64,303,12);
t=getch();
if(t=='1')
exit(0);
if(t=='h')
{
sound(710);
delay(500);
nosound();
//break;
}
if(t=='t')
{
while(!kbhit()) {
setfillstyle(1,0);
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
setfillstyle(1,random(12));
floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);
floodfill(170,250,13);
floodfill(80,230,13);
}
setfillstyle(1,0);
floodfill(200,250,13);
delay(10);
//setfillstyle(1,11);
floodfill(170,250,13);
floodfill(80,230,13);
}
if(t=='l')
{
while(!kbhit())
{
delay(120);
setfillstyle(6,0); //////////////////////////ty
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
setfillstyle(1,0);
floodfill(64,303,12);
///////////////////////////////////road
setfillstyle(9,0); /////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);
//////////////////////
floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);
setfillstyle(6,14); //////////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
////////////////////////////////////////road
setfillstyle(9,10);///////////////////////////////////color
floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
//////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);
setfillstyle(1,14);
floodfill(14,314,12);
floodfill(617,183,12);
///////////////////////////////////////
setfillstyle(10,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);
///light
setfillstyle(6,11); ///////////
floodfill(200,250,13);
floodfill(170,250,13);
floodfill(80,230,13);
delay(120);
setfillstyle(6,0);/////////////////////ty
floodfill(548,250,13);
floodfill(548,214,13);
floodfill(533,228,13);
floodfill(563,228,13);
floodfill(262,281,13);
floodfill(308,281,13);
floodfill(284,251,13);
floodfill(284,295,13);
/////////////////////////////////////road
setfillstyle(9,0); ///////////////color
floodfill(19,429,13);
floodfill(81,419,13);
floodfill(151,409,13);
floodfill(211,397,13);
floodfill(271,380,13);
floodfill(331,368,13);
floodfill(396,355,13);
floodfill(450,345,13);
floodfill(510,335,13);
floodfill(570,325,13);
floodfill(630,312,13);
///////////////////////////////////////////////////////
floodfill(20,197,13);
floodfill(80,187,13);
floodfill(133,174,13);
floodfill(517,86,13);
floodfill(557,81,13);
floodfill(627,70,13);
/////////////////////////////
setfillstyle(1,0);
floodfill(14,314,12);
floodfill(617,183,12);
setfillstyle(6,10); /////////////ty
floodfill(536,213,13);
floodfill(563,213,13);
floodfill(561,244,13);
floodfill(538,244,13);
floodfill(274,295,13);
floodfill(294,295,13);
floodfill(274,265,13);
floodfill(294,265,13);
////////////////////////////////////////////////road
setfillstyle(9,14);/////////////////////////////////////////color
floodfill(81-40+5,419+7,13);
floodfill(151-40,409+7,13);
floodfill(211-40,397+7,13);
floodfill(271-40,380+7,13);
floodfill(331-40,368+7,13);
floodfill(396-40,355+7,13);
floodfill(450-40,345+7,13);
floodfill(510-40,335+7,13);
floodfill(570-40,325+7,13);
floodfill(630-40,312+7,13);
/////////////////////////////////////////
floodfill(50,197,13);
floodfill(110,177,13);
floodfill(166,165,13);
floodfill(527,86,13);
floodfill(587,71,13);
setfillstyle(1,14);
floodfill(64,303,12);
setfillstyle(9,4);
floodfill(302+248,230,13);
floodfill(302+248+v,230+b,13);
delay(20);
setfillstyle(1,14);
floodfill(200,250,13);
floodfill(170,250,13);
floodfill(80,230,13);
delay(20);
setfillstyle(1,0);
floodfill(200,250,13);
floodfill(170,250,13);
floodfill(80,230,13);
} }
if(t=='p')
{
int n=0;
while(!kbhit())
{
if(n<=60)
n++;
setcolor(0);
rectangle(1+1,-10,90-1,-12+n);
delay(14);
setcolor(9);
rectangle(1,-10,90,-10+n);
if(n==60)
{
outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");
delay(400);
}
}
setcolor(0);
rectangle(1,-10,90,-10+n);
rectangle(1,-10,90,-11+n);
outtextxy(10,10,"L-LIGHTS");
outtextxy(10,20,"H-HORN");
//outtextxy(10,30,"T-AllOY");
}
}
circle(300,100,3);
nosound();
getch();
}
#include
#include
#include
#include
/** PC Internal Speaker Required **/
//** Author: Sarju www.sarjus-elearning.blogspot.com **/
main()
{
float octave[7]={130.81,146.83,164.81,174.61,196.220,246.94};
//You can change the music by changing above values
int adn;
while(!kbhit())
{
adn=random(7);
sound(octave[adn]*10);
delay(190);
nosound();
}}
#include
#include
#include
#include
void main()
{
int gdriver=DETECT,gmode;
int i,x,y;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
//"c:\\tc\\bgi" give location in your system where C installed
while(!kbhit())
{
x=random(640);
y=random(480);
setcolor(15);
for(i=1;i<10;i++) { circle(x,y,i); delay(10); } setfillstyle(1,15); line(x+8,y-2,x+40,y); line(x+8,y+2,x+40,y); floodfill(x+11,y,15); line(x-8,y-2,x-40,y); line(x-8,y+2,x-40,y); floodfill(x-11,y,15); line(x-2,y+8,x,y+40); line(x+2,y+8,x,y+40); floodfill(x,y+11,15); line(x-2,y-8,x,y-40); line(x+2,y-8,x,y-40); floodfill(x,y-11,15); line(x+8,y-2,x+20,y-20); line(x+2,y-8,x+20,y-20); floodfill(x+15,y-15,15); line(x+8,y+2,x+20,y+20); line(x+2,y+8,x+20,y+20); floodfill(x+15,y+15,15); line(x-8,y+2,x-20,y+20); line(x-2,y+8,x-20,y+20); floodfill(x-15,y+15,15); line(x-8,y-2,x-20,y-20); line(x-2,y-8,x-20,y-20); floodfill(x-15,y-15,15); sound(4000); setcolor(0); for(i=40;i>=10;i--)
{
line(x+8,y-2,x+i,y);
line(x+8,y+2,x+i,y);
}
for(i=40;i>=10;i--)
{
line(x-8,y-2,x-i,y);
line(x-8,y+2,x-i,y);
}
for(i=40;i>=10;i--)
{
line(x-2,y+8,x,y+i);
line(x+2,y+8,x,y+i);
}
for(i=40;i>=10;i--)
{
line(x-2,y-8,x,y-i);
line(x+2,y-8,x,y-i);
}
for(i=20;i>=7;i--)
{
line(x+8,y-2,x+i,y-i);
line(x+2,y-8,x+i,y-i);
}
for(i=20;i>=7;i--)
{
line(x+8,y+2,x+i,y+i);
line(x+2,y+8,x+i,y+i);
}
for(i=20;i>=7;i--)
{
line(x-8,y+2,x-i,y+i);
line(x-2,y+8,x-i,y+i);
}
for(i=20;i>=7;i--)
{
line(x-8,y-2,x-i,y-i);
line(x-2,y-8,x-i,y-i);
}
for(i=9;i>0;i--)
{
circle(x,y,i);
delay(10);
}
nosound();
}
cleardevice();
setcolor(2);
settextstyle(2,0,5);
outtextxy(220,160,"Creator:SARJU");
outtextxy(265,235,"www.sarjus-elearning.blogspot.com");
getch();getch();
}
}
{
available_res[j]=available_res[j]-request[j];
}
The problem
Originally used as a means of illustrating the problem of deadlock, this system reaches deadlock when there is a 'cycle of ungranted requests'. In this case philosopher P1 waits for the fork grabbed by philosopher P2 who is waiting for the fork of philosopher P3 and so forth, making a circular chain.
Starvation (and the pun was intended in the original problem description) might also occur independently of deadlock if a philosopher is unable to acquire both forks due to a timing issue. For example there might be a rule that the philosophers put down a fork after waiting five minutes for the other fork to become available and wait a further five minutes before making their next attempt. This scheme eliminates the possibility of deadlock (the system can always advance to a different state) but still suffers from the problem of livelock. If all five philosophers appear in the dining room at exactly the same time and each picks up their left fork at the same time the philosophers will wait five minutes until they all put their forks down and then wait a further five minutes before they all pick them up again.
The lack of available forks is an analogy to the locking of shared resources in real computer programming, a situation known as concurrency. Locking a resource is a common technique to ensure the resource is accessed by only one program or chunk of code at a time. When the resource the program is interested in is already locked by another one, the program waits until it is unlocked. When several programs are involved in locking resources, deadlock might happen, depending on the circumstances. For example, one program needs two files to process. When two such programs lock one file each, both programs wait for the other one to unlock the other file, which will never happen.
Algorithm
* The algorithm for the process is..........
1.Start.
2.Initialise the thread variables as required.
3.Create the threads representing philosophers.
4.Wait until the threads finishes execution.
5.Stop.
The algorithm for the philosopher is as..........
1.Start.
2.Wait for the left fork/spoon.
3.Wait for the right fork/spoon.
4.Start eating.
5 Release the left fork/spoon.
6.Release the right fork/spoon.
7.Stop.
Program
/* Program to implement the solution to dinning philosophers' problem.
* This program uses five thread functions as five philosophers.
* The spoons or forks are represented with five semaphore variables.
* To use shared memory just include the code after the sem_wait() in each thread.
* To compile use 'cc diningphilosopher.c -lpthread'.
* To run './a.out'.
//inculsion
#include
#include
#include
//macro definition
#define cls() printf("\033[H\033[J")
#define EATINGTIME 1
//function prototype for the thread function.
void * philosopher1();
void * philosopher2();
void * philosopher3();
void * philosopher4();
void * philosopher5();
//global decalration of semaphore variables.
sem_t sem15,sem12,sem23,sem34,sem45;
//global variable to control the execution of main.
int end=0;
int main()
{
char a[2];
pthread_t t1,t2,t3,t4,t5;
pthread_attr_t at1;
pthread_attr_init(&at1);
pthread_attr_setdetachstate(&at1,PTHREAD_CREATE_DETACHED);
sem_init(&sem15,0,1);
sem_init(&sem12,0,1);
sem_init(&sem23,0,1);
sem_init(&sem34,0,1);
sem_init(&sem45,0,1);
cls();
printf("\n\n\n\n\n\n");
printf("____________________________________________________________________");
printf("\n\n\n\t\t\tDINNING PHILOSOPHER PROBLEM.");
printf("\n\t\t\t____________________________");
printf("\n\n\t\tNO. OF PHILOSOPHERS :5");
printf("\n\n\t\tNO. OF FORKS(SPOONS):5\n\n"); printf("___________________________________");
printf("\n\n\n\t\tPRESS ENTER TO CONTINUE.....................");
fgets(a,2,stdin);
cls();
pthread_create(&t1,&at1,philosopher1,NULL);
pthread_create(&t2,&at1,philosopher2,NULL);
pthread_create(&t3,&at1,philosopher3,NULL);
pthread_create(&t4,&at1,philosopher4,NULL);
pthread_create(&t5,&at1,philosopher5,NULL);
while(end!=5)
{
}
}
//definition of philosopher1.
void * philosopher1()
{
int i=0;
printf("\n\t\tPHILOSOPHER-1 THINKING.\n");
while(i
{
sleep(1);
//waiting to acquire the forks.
sem_wait(&sem15);
sem_wait(&sem12);
printf("\n\t\t\t* PHILOSOPHER-1 EATING.*\n");
sleep(1);
//releasing the forks
sem_post(&sem15);
sem_post(&sem12);
printf("\n\t\tPHILOSOPHER-1 THINKING.\n");
i++;
}
end++;
}
//philosopher2.
void * philosopher2()
{
int i=0;
printf("\n\t\tPHILOSOPHER-2 THINKING.\n");
while(i
{
sleep(1);
sem_wait(&sem12);
sem_wait(&sem23);
printf("\n\t\t\t* PHILOSOPHER-2 EATING.*\n");
sleep(1);
sem_post(&sem12);
sem_post(&sem23);
printf("\n\t\tPHILOSOPHER-2 THINKING.\n");
i++;
}
end++;
}
//philosopher 3
void * philosopher3()
{
int i=0;
printf("\n\t\tPHILOSOPHER-3 THINKING.\n");
while(i
{
sleep(1);
sem_wait(&sem23);
sem_wait(&sem34);
printf("\n\t\t\t* PHILOSOPHER-3 EATING.*\n");
sleep(1);
sem_post(&sem23);
sem_post(&sem34);
printf("\n\t\tPHILOSOPHER-3 THINKING.\n");
i++;
}
end++;
}
//philosopher 4.
void * philosopher4()
{
int i=0;
printf("\n\t\tPHILOSOPHER-4 THINKING.\n");
while(i
{
sleep(1);
sem_wait(&sem34);
sem_wait(&sem45);
printf("\n\t\t\t* PHILOSOPHER-4 EATING.*\n");
sleep(1);
sem_post(&sem34);
sem_post(&sem45);
printf("\n\t\tPHILOSOPHER-4 THINKING.\n");
i++;
}
end++;
}
//philosopher 5.
void * philosopher5()
{
int i=0;
printf("\n\t\tPHILOSOPHER-5 THINKING.\n");
while(i
{
sleep(1);
sem_wait(&sem45);
sem_wait(&sem15);
printf("\n\t\t\t* PHILOSOPHER-5 EATING.*\n");
sleep(1);
sem_post(&sem45);
sem_post(&sem15);
printf("\n\t\tPHILOSOPHER-5 THINKING.\n");
i++;
}
end++;
}
output