tag:blogger.com,1999:blog-13663677820002551882024-03-12T16:10:11.132-07:00eLearningSarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-1366367782000255188.post-4465323131261591352009-06-17T19:53:00.000-07:002009-06-17T19:53:33.512-07:00Transfer Control Protocol (TCP)<strong>TRANSFER CONTROL PROTOCOL(TCP)</strong><br />
<br />
<br />
<strong>Server</strong><br />
<br />
<strong>Algorithm</strong><br />
<br />
The activity involved in a tcp server are..........<br />
1.Create a socket.<br />
2.Bind it to the operating system.<br />
3.Listen over it.<br />
4.Accept connections.<br />
5.Read/Write processes.<br />
6.Close the socket.<br />
<br />
<strong>Program</strong><br />
<br />
/*Program to demonstrate the creation and usage of tcp socket.<br />
* This module acts as the tcp server which listens to a socket and accepts connection.<br />
* The server initially reads a message from the client and then writes a message to it.<br />
* This module should be compiled using the command. 'c++ tcpserver.cpp' and execute './a.out'.*/<br />
<br />
//inculsion.<br />
#include<iostream></iostream><br />
#include<stdio.h></stdio.h><br />
#include<sys types.h=""></sys><br />
#include<netinet in.h=""></netinet><br />
#include<netdb.h></netdb.h><br />
int main()<br />
{<br />
<br />
//variables to store the socket id.<br />
int serversocket,clientsocket;<br />
<br />
//variables to store the network host addresses.<br />
sockaddr_in serveraddr,clientaddr;<br />
<br />
//variable to store address length.<br />
socklen_t len;<br />
char message[50];<br />
<br />
//creating a socket.<br />
serversocket=socket(AF_INET,SOCK_STREAM,0);<br />
<br />
//steps to include the host address<br />
bzero((char*)&serveraddr,sizeof(serveraddr));<br />
serveraddr.sin_family=AF_INET;<br />
serveraddr.sin_port=htons(5030);<br />
serveraddr.sin_addr.s_addr=INADDR_ANY;<br />
<br />
//binding the socket to the operating system.<br />
bind(serversocket,(sockaddr*)&serveraddr,sizeof(serveraddr));<br />
bzero((char*)&clientaddr,sizeof(clientaddr));<br />
len=sizeof(clientaddr);<br />
<br />
//listening over the socket.<br />
listen(serversocket,5);<br />
printf("\nWaiting for client connectivity.\n");<br />
<br />
//accepting the connection.<br />
clientsocket=accept(serversocket,(sockaddr*)&clientaddr,&len);<br />
printf("\nClient connectivity received.\n");<br />
printf("\nReading message from the client.\n");<br />
<br />
//reading activity.<br />
read(clientsocket,message,sizeof(message));<br />
printf("\nThe client has send:\t%s\n",message);<br />
printf("\nSending message to the client.\n");<br />
<br />
//writing activity.<br />
write(clientsocket,"YOUR MESSAGE RECEIVED.",sizeof("YOUR MESSAGE RECEIVED.")); <br />
close(clientsocket);<br />
close(serversocket);<br />
}<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/110.jpg">Output</a></strong><br />
<br />
<strong>Client</strong><br />
<br />
<strong>Algorithm</strong><br />
<br />
The different processes involved in a udp client process are........<br />
1.Create a datagram socket.<br />
2.Receive/Send message to the server.<br />
3.Close the socket.<br />
<br />
<strong>Program</strong><br />
/* Program to demonstrate the creation and usage of datagram sockets.<br />
* This module acts as a udp client which sends and receives messages from a udp server.<br />
* This module should be compiled into different folder using the command 'c++ <br />
udpclient.cpp -o b' and execute using './b'<br />
* For execution the 'udpserver' module should be executed first then this module.<br />
<br />
//inculsion.<br />
#include<iostream></iostream><br />
#include<stdio.h></stdio.h><br />
#include<sys types.h=""></sys><br />
#include<netinet in.h=""></netinet><br />
#include<netdb.h></netdb.h><br />
int main()<br />
{<br />
<br />
//variable to store the socket_id.<br />
int clientsocket;<br />
<br />
//variable to store the address.<br />
sockaddr_in serveraddr; <br />
<br />
//variableto store the address length.<br />
socklen_t len;<br />
<br />
//variable to store the network byte order address.<br />
hostent *server;<br />
char message[50];<br />
<br />
//socket creation.<br />
clientsocket=socket(AF_INET,SOCK_DGRAM,0);<br />
<br />
//steps involved in the server address creation.<br />
bzero((char*)&serveraddr,sizeof(serveraddr));<br />
len=sizeof(serveraddr);<br />
serveraddr.sin_family=AF_INET;<br />
serveraddr.sin_port=htons(5015);<br />
server=gethostbyname("127.0.0.1");<br />
bcopy((char*)server->h_addr,(char*)&serveraddr.sin_addr.s_addr,sizeof(server->h_addr));<br />
printf("\nPRESS ENTER TO START THE CONNECTION PROCESS.\n");<br />
fgets(message,2,stdin);<br />
printf("\nSending message for server connection\n");<br />
<br />
//sending message.<br />
sendto(clientsocket,"HI I AM CLIENT...",sizeof("HI I AM CLIENT...."),0,(sockaddr*)&serveraddr,sizeof(serveraddr));<br />
printf("\nReceiving message from server.\n");<br />
<br />
//receiving messages.<br />
recvfrom(clientsocket,message,sizeof(message),0,(sockaddr*)&serveraddr,&len);<br />
printf("\nMessage received:\t%s\n",message);<br />
close(clientsocket);<br />
}<br />
<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/28.jpg">Output</a></strong>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-15376270283902273182009-06-17T19:44:00.000-07:002009-06-17T19:44:23.116-07:00Socket Programming<b>SOCKET PROGRAMMING</b><br />
<br />
<strong>Theory </strong><br />
<div style="text-align: justify;">Sockets provide point-to-point, two-way communication between two processes. Sockets are very versatile and are a basic component of interprocess and intersystem communication. A socket is an endpoint of communication to which a name can be bound. It has a type and one or more associated processes. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Sockets exist in communication domains. A socket domain is an abstraction that provides an addressing structure and a set of protocols. Sockets connect only with sockets in the same domain. Twenty three socket domains are identified (see <sys socket.h=""></sys></sys><//sys>), of which only the UNIX and Internet domains are normally used Solaris 2.x Sockets can be used to communicate between processes on a single system, like other forms of IPC. </div><div style="text-align: justify;"></div><div style="text-align: justify;">The UNIX domain provides a socket address space on a single system. UNIX domain sockets are named with UNIX paths. Sockets can also be used to communicate between processes on different systems. The socket address space between connected systems is called the Internet domain. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Internet domain communication uses the TCP/IP internet protocol suite. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Socket types define the communication properties visible to the application. Processes communicate only between sockets of the same type. There are five types of socket. </div><div style="text-align: justify;"></div><strong>A stream socket</strong> <br />
<div style="text-align: justify;">-- provides two-way, sequenced, reliable, and unduplicated flow of data with no record boundaries. A stream operates much like a telephone conversation. The socket type is SOCK_STREAM, which, in the Internet domain, uses Transmission Control Protocol (TCP). </div><br />
<strong>A datagram socket</strong> <br />
<div style="text-align: justify;">-- supports a two-way flow of messages. A on a datagram socket may receive messages in a different order from the sequence in which the messages were sent. Record boundaries in the data are preserved. Datagram sockets operate much like passing letters back and forth in the mail. The socket type is SOCK_DGRAM, which, in the Internet domain, uses User Datagram Protocol (UDP). </div><div style="text-align: justify;"></div><strong>A sequential packet socket</strong> <br />
<div style="text-align: justify;">-- provides a two-way, sequenced, reliable, connection, for datagrams of a fixed maximum length. The socket type is SOCK_SEQPACKET. No protocol for this type has been implemented for any protocol family. </div><div style="text-align: justify;"></div><strong>A raw socket</strong> <br />
provides access to the underlying communication protocols. <br />
<br />
These sockets are usually datagram oriented, but their exact characteristics depend on the interface provided by the protocol. <br />
<br />
<strong>Socket Creation and Naming</strong> <br />
<br />
<div style="text-align: justify;">int socket(int domain, int type, int protocol) is called to create a socket in the specified domain and of the specified type. If a protocol is not specified, the system defaults to a protocol that supports the specified socket type. The socket handle (a descriptor) is returned. A remote process has no way to identify a socket until an address is bound to it. Communicating processes connect through addresses. In the UNIX domain, a connection is usually composed of one or two path names. In the Internet domain, a connection is composed of local and remote addresses and local and remote ports. In most domains, connections must be unique. </div><div style="text-align: justify;">int bind(int s, const struct sockaddr *name, int namelen) is called to bind a path or internet address to a socket. There are three different ways to call bind(), depending on the domain of the socket. </div><br />
• For UNIX domain sockets with paths containing 14, or fewer characters, you can: <br />
• #include <sys socket.h=""></sys></sys><//sys><br />
• ... <br />
• bind (sd, (struct sockaddr *) &addr, length);<br />
• If the path of a UNIX domain socket requires more characters, use: <br />
• #include <sys un.h=""></sys></sys><//sys><br />
• ... <br />
• bind (sd, (struct sockaddr_un *) &addr, length); <br />
• For Internet domain sockets, use <br />
• #include <netinet in.h=""></netinet></netinet><//netinet><br />
• ... <br />
• bind (sd, (struct sockaddr_in *) &addr, length);<br />
In the UNIX domain, binding a name creates a named socket in the file system. Use unlink() or rm () to remove the socket. <br />
<br />
<strong>Connecting Stream Sockets</strong> <br />
<br />
<div style="text-align: justify;">Connecting sockets is usually not symmetric. One process usually acts as a server and the other process is the client. The server binds its socket to a previously agreed path or address. It then blocks on the socket. For a SOCK_STREAM socket, the server calls int listen(int s, int backlog) , which specifies how many connection requests can be queued. A client initiates a connection to the server's socket by a call to int connect(int s, struct sockaddr *name, int namelen) . A UNIX domain call is like this: </div><div style="text-align: justify;">struct sockaddr_un server; </div>... <br />
connect (sd, (struct sockaddr_un *)&server, length);<br />
while an Internet domain call would be: <br />
struct sockaddr_in; <br />
... <br />
connect (sd, (struct sockaddr_in *)&server, length);<br />
<div style="text-align: justify;">If the client's socket is unbound at the time of the connect call, the system automatically selects and binds a name to the socket. For a SOCK_STREAM socket, the server calls accept(3N) to complete the connection. </div><div style="text-align: justify;">int accept(int s, struct sockaddr *addr, int *addrlen) returns a new socket descriptor which is valid only for the particular connection. A server can have multiple SOCK_STREAM connections active at one time.</div><div style="text-align: justify;"></div><strong>Stream Data Transfer and Closing</strong> <br />
<br />
<div style="text-align: justify;">Several functions to send and receive data from a SOCK_STREAM socket. These are write(), read(), int send(int s, const char *msg, int len, int flags), and int recv(int s, char *buf, int len, int flags). send() and recv() are very similar to read() and write(), but have some additional operational flags. </div><br />
The flags parameter is formed from the bitwise OR of zero or more of the following: <br />
<br />
<strong>MSG_OOB</strong> <br />
-- 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. <br />
<br />
<strong>MSG_DONTROUTE</strong> <br />
-- The SO_DONTROUTE option is turned on for the duration of the operation. It is used only by diagnostic or routing pro- grams. <br />
<br />
<strong>MSG_PEEK</strong> <br />
-- "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. <br />
<br />
<strong>A SOCK_STREAM</strong> socket is discarded by calling close(). <br />
<br />
<strong>Datagram sockets</strong> <br />
<br />
<div style="text-align: justify;">A datagram socket does not require that a connection be established. Each message carries the destination address. If a particular local address is needed, a call to bind() must precede any data transfer. Data is sent through calls to sendto() or sendmsg(). The sendto() call is like a send() call with the destination address also specified. To receive datagram socket messages, call recvfrom() or recvmsg(). While recv() requires one buffer for the arriving data, recvfrom() requires two buffers, one for the incoming message and another to receive the source address. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Datagram sockets can also use connect() to connect the socket to a specified destination socket. When this is done, send() and recv() are used to send and receive data. </div><br />
accept() and listen() are not used with datagram sockets.Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-63835720319326051772009-06-17T07:31:00.000-07:002009-06-17T08:02:35.015-07:00Shared Memory<b>SHARED MEMORY</b><br />
<br />
<strong>Theory</strong><br />
<br />
<div style="text-align: justify;">In computing, shared memory is a memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them. Depending on context, programs may run on the same physical processor or on separate ones. Using memory for communication inside a single program, for example among its multiple threads, is generally not referred to as shared memory.</div><div style="text-align: justify;">In computer software, shared memory is a method of inter-process communication (IPC), i.e. a way of exchanging data between programs running at the same time. One process will create an area in RAM which other processes can access.</div><div style="text-align: justify;">Since both processes can access the shared memory area like regular working memory, this is a very fast way of communication (as opposed to other mechanisms of IPC such as named pipes, Unix sockets or CORBA). On the other hand, it is less powerful, as for example the communicating processes must be running on the same machine (whereas other IPC methods can use a computer network).</div><div style="text-align: justify;">IPC by shared memory is mainly used on Unix systems.</div><div style="text-align: justify;">POSIX provides a standardized API for using shared memory, POSIX Shared Memory. This uses the function shm_open from sys/mman.h.</div><div style="text-align: justify;">Shared Memory is an efficeint means of passing data between programs. One program will create a memory portion which other processes (if permitted) can access. </div><div style="text-align: justify;">In the Solaris 2.x operating system, the most efficient way to implement shared memory applications is to rely on the mmap() function and on the system's native virtual memory facility. Solaris 2.x also supports System V shared memory, which is another way to let multiple processes attach a segment of physical memory to their virtual address spaces. When write access is allowed for more than one process, an outside protocol or mechanism such as a semaphore can be used to prevent inconsistencies and collisions. </div><div style="text-align: justify;">A process creates a shared memory segment using shmget()|. The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can perform various control functions on the shared memory segment using shmctl(). Once created, a shared segment can be attached to a process address space using shmat(). It can be detached using shmdt() (see shmop()). The attaching process must have the appropriate permissions for shmat(). Once attached, the process can read or write to the segment, as allowed by the permission requested in the attach operation. A shared segment can be attached multiple times by the same process. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control structures and prototypews can be found in <sys shm.h=""></sys></sys><//sys>. </div><div style="text-align: justify;">Accessing a Shared Memory Segment </div><div style="text-align: justify;">shmget() is used to obtain access to a shared memory segment. It is prottyped by: </div><div style="text-align: justify;">int shmget(key_t key, size_t size, int shmflg);</div><div style="text-align: justify;">The key argument is a access value associated with the semaphore ID. The size argument is the size in bytes of the requested shared memory. The shmflg argument specifies the initial access permissions and creation control flags. </div><div style="text-align: justify;">When the call succeeds, it returns the shared memory segment ID. This call is also used to get the ID of an existing shared segment (from a process requesting sharing of some existing memory portion). </div><div style="text-align: justify;">The following code illustrates shmget(): </div><br />
#include <sys types.h=""></sys></sys><//sys><br />
#include <sys ipc.h=""></sys></sys><//sys><br />
#include <sys shm.h=""></sys></sys><//sys><br />
... <br />
key_t key; /* key to be passed to shmget() */ <br />
int shmflg; /* shmflg to be passed to shmget() */ <br />
int shmid; /* return value from shmget() */ <br />
int size; /* size to be passed to shmget() */ <br />
... <br />
key = ... <br />
size = ...<br />
shmflg) = ... <br />
<br />
if ((shmid = shmget (key, size, shmflg)) == -1) {<br />
perror("shmget: shmget failed"); exit(1); } else {<br />
(void) fprintf(stderr, "shmget: shmget returned %d\n", shmid);<br />
exit(0);<br />
}<br />
...<br />
<br />
<strong>Controlling a Shared Memory Segment</strong> <br />
<br />
shmctl() is used to alter the permissions and other characteristics of a shared memory segment. It is prototyped as follows: <br />
int shmctl(int shmid, int cmd, struct shmid_ds *buf);<br />
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: <br />
<strong>SHM_LOCK</strong> -- Lock the specified shared memory segment in memory. The process must have the effective ID of superuser to perform this command. <br />
<strong>SHM_UNLOCK</strong> -- Unlock the shared memory segment. The process must have the effective ID of superuser to perform this command. <br />
<strong>IPC_STAT</strong>-- 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. <br />
<strong>IPC_SET</strong>-- 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. <br />
<strong>IPC_RMID</strong> -- Remove the shared memory segment. <br />
The buf is a sructure of type struct shmid_ds which is defined in <br />
<sys shm.h=""></sys></sys><//sys><br />
<strong>Program</strong><br />
<strong>Server</strong><br />
<br />
/*Program to demonstrate the use of shared memory in interprocess communication.<br />
* This program acts as a server which waits for a message from the client process.<br />
* The process is implemented using a shared memory space called 'shared_location'.<br />
* 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).<br />
* This module should be compiled and executed first.<br />
* This module is compile using 'cc shmserver.c' and executed using './a.out'*/<br />
<br />
//inculsion<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h><br />
#include<unistd.h></unistd.h></unistd.h><//unistd.h><br />
#include<string.h></string.h></string.h><//string.h><br />
#include<sys types.h=""></sys></sys><//sys><br />
#include<sys ipc.h=""></sys></sys><//sys><br />
#include<sys shm.h=""></sys></sys><//sys><br />
<br />
//structure definition for acquiring a shared location.<br />
struct shared_location<br />
{<br />
int written;<br />
char message[30];<br />
};<br />
int main()<br />
{<br />
int shmid,running;<br />
void *address;<br />
struct shared_location * ptr;<br />
<br />
//for getting a shared location<br />
shmid=shmget((key_t)1234,sizeof(struct shared_location),0666|IPC_CREAT);<br />
<br />
//optional if required for error checking.<br />
if(shmid<=0) { printf("\nERROR:\tCannot allocate shared space.\n"); exit(0); } //attaching the shared location to this process. <br />
address=shmat(shmid,(void *)0,0); //optional if required for error checking. <br />
if(address==(void*)0) { printf("\nEROOR:\tShared location cannot be attached.\n"); } //type casting so as to convert the obyained location into our format. <br />
ptr=(struct shared_location*)address; ptr->written=0;<br />
strcpy(ptr->message,"Hi I am server.");<br />
running=1;<br />
while(running)<br />
{<br />
printf("\nWaiting for the client to enter the message.\n");<br />
<br />
while(ptr->written==0)<br />
{<br />
}<br />
if(ptr->written==1)<br />
{ <br />
printf("The client has entered the message: %s\n",ptr->message);<br />
} <br />
ptr->written=0;<br />
if(strcmp(ptr->message,"end")==0)<br />
{<br />
running=0;<br />
}<br />
} <br />
<br />
//detaching the shared location.<br />
shmdt(address);<br />
<br />
//deleting the shared location.<br />
shmctl(shmid,IPC_RMID,NULL);<br />
}<br />
<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/19.jpg">Output</a></strong><br />
<br />
<strong>Client </strong><br />
<br />
<strong>Program</strong><br />
<br />
/* Program to demonstrate the use of shared memory in interprocess communication.<br />
* This module acts as a client which sends messages to the server process.<br />
* In this the message entered into the message part of the 'shared_location' structure and the 'written' field is set to 1.<br />
* The remaining process is similar to that of the server process.<br />
* This module can be compiled using 'cc shmclient.c -o b' and executed using './b'<br />
<br />
//inculsion<br />
#include<stdio.h></stdio.h><br />
#include<unistd.h></unistd.h><br />
#include<string.h></string.h><br />
#include<sys types.h=""></sys><br />
#include<sys ipc.h=""></sys><br />
#include<sys shm.h=""></sys><br />
<br />
//structure definition for the shared location.<br />
struct shared_location<br />
{<br />
int written;<br />
char message[30];<br />
};<br />
<br />
//the functions and other terms used have similar meanings to that used in the server module<br />
//but they don't bear any specific relationship.<br />
int main()<br />
{<br />
int shmid,running;<br />
void *address;<br />
struct shared_location * ptr;<br />
shmid=shmget((key_t)1234,sizeof(struct shared_location),0666|IPC_CREAT);<br />
if(shmid<=0)<br />
{<br />
printf("\nERROR:\tCannot allocate shared memory.\n");<br />
exit(0);<br />
}<br />
address=shmat(shmid,(void*)0,0);<br />
if(address==(void*)0)<br />
{<br />
printf("\nERROR:\tCannot attach the shared location\n");<br />
exit(0);<br />
}<br />
ptr=(struct shared_location*)address;<br />
running=1;<br />
while(running)<br />
{<br />
printf("\nWaiting for the server\n");<br />
while(ptr->written==1)<br />
{<br />
}<br />
printf("\nEnter your message.(type 'end' to exit)\n");<br />
scanf("%s",ptr->message);<br />
ptr->written=1;<br />
if(strcmp(ptr->message,"end")==0)<br />
{<br />
running=0;<br />
}<br />
} <br />
address=shmat(shmid,(void *)0,0);<br />
shmdt(address);<br />
}<br />
<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/27.jpg">Output</a></strong>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-42328089829016925642009-06-17T07:11:00.000-07:002009-06-17T19:57:46.577-07:00Pipes<strong>PIPES - Linux in C</strong><br />
<br />
<br />
<strong>Theory</strong><br />
<br />
<div style="text-align: justify;">A pipe is a method of connecting the standard output of one process to the standard input of another. Pipes are the eldest of the IPC tools, having been around since the earliest incarnations of the UNIX operating system. They provide a method of one-way communications (hence the term half-duplex) between processes. </div><div style="text-align: justify;"></div><div style="text-align: justify;">This feature is widely used, even on the UNIX command line (in the shell). </div><div style="text-align: justify;"></div><div style="text-align: justify;">When a process creates a pipe, the kernel sets up two file descriptors for use by the pipe. One descriptor is used to allow a path of input into the pipe (write), while the other is used to obtain data from the pipe (read). At this point, the pipe is of little practical use, as the creating process can only use the pipe to communicate with itself.</div><div style="text-align: justify;"></div><div style="text-align: justify;">While a pipe initially connects a process to itself, data traveling through the pipe moves through the kernel. Under Linux, in particular, pipes are actually represented internally with a valid inode. Of course, this inode resides within the kernel itself, and not within the bounds of any physical file system. This particular point will open up some pretty handy I/O doors for us, as we will see a bit later on. </div><div style="text-align: justify;"></div><div style="text-align: justify;">At this point, the creating process typically forks a child process. Since a child process will inherit any open file descriptors from the parent, we now have the basis for multiprocess communication (between parent and child). It is at this stage, that a critical decision must be made. In which direction do we desire data to travel? Does the child process send information to the parent, or vice-versa? The two processes mutually agree on this issue, and proceed to ``close'' the end of the pipe that they are not concerned with.</div><div style="text-align: justify;"></div><div style="text-align: justify;">To access a pipe directly, the same system calls that are used for low-level file I/O can be used (recall that pipes are actually represented internally as a valid inode). </div><div style="text-align: justify;"></div><div style="text-align: justify;">To send data to the pipe, we use the write() system call, and to retrieve data from the pipe, we use the read() system call. Remember, low-level file I/O system calls work with file descriptors! However, keep in mind that certain system calls, such as lseek(), do not work with descriptors to pipes. </div><div style="text-align: justify;"></div><div style="text-align: justify;">To create a simple pipe with C, we make use of the pipe() system call. It takes a single argument, which is an array of two integers, and if successful, the array will contain two new file descriptors to be used for the pipeline.</div><br />
SYSTEM CALL: pipe(); <br />
PROTOTYPE: int pipe( int fd[2] ); <br />
<br />
RETURNS: 0 on success <br />
-1 on error: errno = EMFILE (no free descriptors) <br />
<br />
EMFILE (system file table is full) <br />
EFAULT (fd array is not valid) <br />
<br />
NOTES: fd[0] is set up for reading, fd[1] is set up for writing<br />
<br />
<div style="text-align: justify;">The popen() function creates a pipe between the calling program and the command to be executed. The arguments topopen() are pointers to null-terminated strings. The com-mand argument consists of a shell command line. The modeargument is an I/O mode, either r for reading or w for writing. The value returned is a stream pointer such that one can write to the standard input of the command, if the I/Omode is w, by writing to the file stream and one can read from the standard output of the command, if the I/O mode is r, by reading from the file stream.</div><br />
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).<br />
<br />
<strong>SIMPLE PIPES OR LOWLEVEL PIPES</strong><br />
<br />
<strong>Program</strong><br />
<br />
/* Program to demonstrate the creation and use of simple/low level pipe.<br />
* This program creates a pipe that connects the child process with its parent process.<br />
* Here the child process writes 'REQUEST FROM CHILD PROCESS.' on to the pipe which is read by the parent process.<br />
* The function 'fork()' is a system call used to create a child process.<br />
* This module can be compiled using 'cc simplepipe.c' and executed using './a.out'<br />
<br />
//inculsion<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
#include<sys types.h=""></sys></sys><//sys></sys><//sys></ sys=""><//></sys><//sys></ sys=""><//></ sys=""><//></><//></sys><//sys></ sys=""><//></ sys=""><//></><//></ sys=""><//></><//></><//></><//></sys><//sys></ sys=""><//></ sys=""><//></><//></ sys=""><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></sys><//sys></ sys=""><//></ sys=""><//></><//></ sys=""><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></sys><//sys></ sys=""><//></ sys=""><//></><//></ sys=""><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></sys><//sys></ sys=""><//></ sys=""><//></><//></ sys=""><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></ sys=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
int main()<br />
{<br />
<br />
//array to store the pipe pointers returned by the pipe() function.<br />
int pipe_pointer[2];<br />
<br />
//variable to store the child process id.<br />
pid_t child;<br />
char message[50]; <br />
<br />
//creating a pipe<br />
pipe(pipe_pointer);<br />
<br />
//creating a child process.<br />
child=fork();<br />
if(child==0)<br />
{<br />
printf("\nThe child process is writing.\n");<br />
<br />
//closing the read pointer.<br />
close(pipe_pointer[0]);<br />
<br />
//writing on to the pipe.<br />
write(pipe_pointer[1],"REQUEST FROM CHILD PROCESS.",sizeof("REQUEST FROM CHILD PROCESS."));<br />
}<br />
else<br />
{<br />
printf("\nThe parent process is reading.\n");<br />
<br />
//closing the write pointer.<br />
close(pipe_pointer[1]);<br />
<br />
//reading from the pipe.<br />
read(pipe_pointer[0],message,sizeof(message));<br />
printf("\nThe parent has read:\n\n** %s **\n",message);<br />
}<br />
close(pipe_pointer[0]);<br />
close(pipe_pointer[1]);<br />
}<br />
<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/18.jpg">Output</a></strong><br />
<br />
<strong>FORMATTED PIPE</strong><br />
<br />
<br />
/* Program to demonstrate the creation and use of formatted pipe.<br />
* This program creates a formatted pipe which is connected to the system process denoted by the command 'CMD'.<br />
* The pipe is established for reading and the read content is displayed.<br />
* This module can be compiled using 'cc formattedpipe.c' and executed './a.out'.*/<br />
<br />
//inculsion part<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
#include<string.h></string.h></string.h><//string.h></string.h><//string.h></ string.h=""><//></string.h><//string.h></ string.h=""><//></ string.h=""><//></><//></string.h><//string.h></ string.h=""><//></ string.h=""><//></><//></ string.h=""><//></><//></><//></><//></string.h><//string.h></ string.h=""><//></ string.h=""><//></><//></ string.h=""><//></><//></><//></><//></ string.h=""><//></><//></><//></><//></><//></><//></><//></><//></string.h><//string.h></ string.h=""><//></ string.h=""><//></><//></ string.h=""><//></><//></><//></><//></ string.h=""><//></><//></><//></><//></><//></><//></><//></><//></ string.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
#define CMD "cal"<br />
int main()<br />
{<br />
FILE *ptr;<br />
char message[100];<br />
char cmd[20];<br />
strcpy(cmd,CMD);<br />
<br />
//creating a formatted pipe.<br />
ptr=popen(cmd,"r");<br />
while( fgets(message,sizeof(message),ptr)!=NULL)<br />
{<br />
printf("%s",message); <br />
}<br />
<br />
//closing the pipe.<br />
pclose(ptr);<br />
}<br />
<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/26.jpg">Output</a></strong><br />
<br />
Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-44824922849200084332009-06-16T22:55:00.000-07:002009-06-16T22:55:16.576-07:00Computer Science - Seminar Report - Free Download<strong>iSCSI(Small Computer System Interface)</strong><br />
<br />
<strong>Introduction</strong><br />
<br />
<div style="text-align: justify;"><a href="http://sarjus-elearning.blogspot.com/">iSCSI</a>(Small Computer System Interface), an Internet Protocol (IP)-based storage networking standard for linking data storage facilities, developed by the Internet Engineering Task Force (IETF). By carrying SCSI commands over IP networks, <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> is used to facilitate data transfers over intranets and to manage storage over long distances. The <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> protocol is among the key technologies expected to help bring about rapid development of the storage area network (SAN) market, by increasing the capabilities and performance of storage data transmission. Because of the ubiquity of IP networks, <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> can be used to transmit data over local area networks (LANs), wide area networks (WANs), or the Internet and can enable location-independent data storage and retrieval. </div><div style="text-align: justify;"></div><div style="text-align: justify;"><a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> builds on the two widely used protocols from the storage and networking worlds. From the storage side, <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> uses SCSI command set, the core storage commands used throughout all storage configurations. On the networking side, <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> uses IP and Ethernet, which are the basis for most corporate networks.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> is one of two main approaches to storage data transmission over IP networks; the other method, Fibre Channel over IP (FCIP), translates Fibre Channel control codes and data into IP packets for transmission between geographically distant Fibre Channel SANs. FCIP (also known as Fibre Channel tunneling or storage tunneling) can only be used in conjunction with Fibre Channel technology; in comparison, <a href="http://sarjus-elearning.blogspot.com/">iSCSI</a> can run over existing Ethernet networks. A number of vendors, including Cisco, IBM, and Nishan have introduced iSCSI-based products (such as switches and routers). </div><br />
<strong><a href="http://www.4shared.com/file/112363496/bc623ba4/iSCSI_Seminar_Report.html">Download Full Seminar Report iSCSI</a></strong><br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://www.4shared.com/file/112363496/bc623ba4/iSCSI_Seminar_Report.html" imageanchor="1" style="clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZiMtu6bf3oJ3faFZZUnhh90TXri74N5DqfOsyjd1xsWyig4AgZNzWaSZuNC7OSehGJt8n75lwWGL4iq_ZG21YlvH2pxr1W1l5-tiQ0NK_IOhJZZxzAWwDkPkUU3HBF9t5kXym7O177-c/s320/Download.jpg" tj="true" /></a></div>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-22186326861131304022009-06-15T22:33:00.001-07:002009-06-15T22:33:43.453-07:00Computer Graphics - Moving Ball/**** Author:Sarju<br />
<br />
<br />
www.sarjus-elearning.blogspot.com****/<br />
<br />
# include <graphics.h></graphics.h><br />
<br />
# include <stdlib.h></stdlib.h><br />
<br />
# include <conio.h></conio.h><br />
<br />
# include <dos.h></dos.h><br />
<br />
<br />
<br />
int x,y,maxx,maxy,i,j;<br />
<br />
<br />
<br />
int main(void)<br />
<br />
{<br />
<br />
int gdriver = DETECT, gmode;<br />
<br />
void *image;<br />
<br />
unsigned int size;<br />
<br />
char ch;<br />
<br />
void fillBox(int,int);<br />
<br />
int Row,Col;<br />
<br />
<br />
<br />
// Initialize graphics drivers and mode.<br />
<br />
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");<br />
<br />
<br />
<br />
// Draw a rectangle<br />
<br />
// rectangle(x1,y1,x2,y2)<br />
<br />
maxx=getmaxx(); // x2<br />
<br />
maxy=getmaxy(); // y2<br />
<br />
rectangle(10,10,maxx-10,maxy-10);<br />
<br />
<br />
<br />
// Puts Pixel in the Rectangle.<br />
<br />
fillBox(maxx,maxy);<br />
<br />
x=y=70;<br />
<br />
// Draw Circle and FillColor<br />
<br />
setfillstyle(1,14);<br />
<br />
circle(x,y,20);<br />
<br />
floodfill(x,y,15);<br />
<br />
<br />
<br />
// Creating the Image<br />
<br />
size = imagesize(x,y,x+20,y+20);<br />
<br />
image=malloc(size);<br />
<br />
getimage(x-20,y-20,x+20,y+20,image);<br />
<br />
x-=20;<br />
<br />
y-=20;<br />
<br />
// Will Displays till Any key is hit !!!...<br />
<br />
while(!kbhit())<br />
<br />
{<br />
<br />
Row = x; Col = y;<br />
<br />
putimage(x,y,image,XOR_PUT); // clears the Image from Screen<br />
<br />
//x=random(maxx-70);<br />
<br />
//y=random(maxy-70);<br />
<br />
//fillBox(maxx,maxy);<br />
<br />
x=x+10;<br />
<br />
/* if(x<50)<br />
<br />
x=50;<br />
<br />
if(y<50)<br />
<br />
y=50;*/<br />
<br />
putimage(x,y,image,OR_PUT); // Puts the image on screen.<br />
<br />
delay(50); // Waits for few Seconds.<br />
<br />
if(x>540)<br />
<br />
{<br />
<br />
putimage(x,y,image,XOR_PUT);<br />
<br />
Row = x; Col = y;<br />
<br />
for(i=0;i<540;i++)<br />
<br />
{<br />
<br />
x=random(maxx-10);<br />
<br />
y=random(maxy-10);<br />
<br />
if(x>10 && y>10)<br />
<br />
putpixel(x,y,14);<br />
<br />
}<br />
<br />
//putpixel(x,y,14);<br />
<br />
x= Row;<br />
<br />
y=Col;<br />
<br />
x=10;<br />
<br />
y=y+20;<br />
<br />
putimage(x,y,image,XOR_PUT);<br />
<br />
}<br />
<br />
if(y>410)<br />
<br />
{<br />
<br />
putimage(x,y,image,XOR_PUT);<br />
<br />
Row = x; Col = y;<br />
<br />
fillBox(maxx,maxy);<br />
<br />
x= Row;<br />
<br />
y=Col;<br />
<br />
x=10;<br />
<br />
y=30;<br />
<br />
putimage(x,y,image,XOR_PUT);<br />
<br />
}<br />
<br />
}<br />
<br />
free(image); //Removes the image from the Screen.<br />
<br />
closegraph(); // Closes the Graphics Mode.<br />
<br />
return(0);<br />
<br />
}<br />
<br />
<br />
<br />
void fillBox(int maxx,int maxy)<br />
<br />
{<br />
<br />
cleardevice(); // Clears the Graphics Screen<br />
<br />
maxx=getmaxx(); // x2<br />
<br />
maxy=getmaxy(); // y2<br />
<br />
rectangle(10,10,maxx-10,maxy-10);<br />
<br />
for(i=0;i<8000;i++)<br />
<br />
{<br />
<br />
x=random(maxx-10);<br />
<br />
y=random(maxy-10);<br />
<br />
if(x>10 && y>10)<br />
<br />
putpixel(x,y,14);<br />
<br />
}<br />
<br />
}Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-61869794983964840722009-06-15T22:28:00.000-07:002009-06-15T22:28:26.562-07:00Computer Graphics - Animated Car#include<graphics.h></graphics.h><br />
<br />
<br />
/*** Author : Sarju S<br />
<a href="http://www.sarjus-elearning.blogspot.com/">http://www.sarjus-elearning.blogspot.com/</a><br />
***/<br />
<br />
#include<conio.h></conio.h><br />
#include<dos.h></dos.h><br />
#include<stdlib.h></stdlib.h><br />
#include<process.h></process.h><br />
void main()<br />
{<br />
int gd=DETECT,gm;<br />
initgraph(&gd,&gm,"c:\\tc\\bgi");<br />
int c=12;<br />
setbkcolor(0);<br />
//setlinestyle(0,1,2);<br />
int t;<br />
while(1)<br />
{<br />
settextstyle(2,0,5);<br />
outtextxy(100,10,"Press L,H ,T,P");<br />
outtextxy(100,30,"Press 1 for Quit");<br />
as:<br />
setcolor(13);<br />
ellipse(380,127,20,152,130,35);<br />
//////////////////////////////rear//////////////////////////<br />
line(490,109,560,142);<br />
line(560,142,569,142);<br />
<br />
line(569,142,582,102);<br />
<br />
line(582,102,620,92);<br />
<br />
line(593,132,617,125);<br />
<br />
<br />
<br />
line(617,124,627,96);<br />
<br />
line(620,92,628,97);<br />
<br />
<br />
<br />
line(472,86,602,96);<br />
<br />
line(501,113,575,121);<br />
<br />
line(443,77,475,80);<br />
<br />
<br />
<br />
line(443,77,432,93);<br />
<br />
line(475,80,472,85);<br />
<br />
//setcolor(4);<br />
<br />
line(593,132,593,137);<br />
<br />
line(593,137,600,141);<br />
<br />
line(600,141,600,185);<br />
<br />
line(600,185,608,192);<br />
<br />
line(608,192,608,234);<br />
<br />
line(608,234,586,253);<br />
<br />
line(586,253,577,248);<br />
<br />
<br />
<br />
///////////////////////// mirror<br />
<br />
line(263,112,363,127);<br />
<br />
line(193,160,263,112);<br />
<br />
line(193,160,220,170);<br />
<br />
line(220,170,280,180);<br />
<br />
line(280,180,320,185);<br />
<br />
line(320,185,363,127);<br />
<br />
////////////////////////////////sidemirror<br />
<br />
line(340,194,460,169);<br />
<br />
line(460,169,519,152);<br />
<br />
<br />
<br />
ellipse(512,144,300,30,10,10);<br />
<br />
ellipse(467,143,28,100,50,30);<br />
<br />
line(510,128,521,138);<br />
<br />
line(435,116,440,171);<br />
<br />
<br />
<br />
// setcolor(4); <br />
<br />
////////////////////////////////////////cont//<br />
<br />
line(339,194,372,144);<br />
<br />
// line(372,140,386,128);<br />
<br />
ellipse(454,208,87,123,128,95);<br />
<br />
line(372,144,384,128);<br />
<br />
int b,x,y;<br />
<br />
////////////////////////lower<br />
<br />
line(365,298,524,264);<br />
<br />
line(365,298,330,310);<br />
<br />
line(330,310,323,310);<br />
<br />
<br />
<br />
<br />
<br />
///////////////////////////////bumper<br />
<br />
ellipse(162,221,135,190,90,40);<br />
<br />
line(96,193,140,174);<br />
<br />
line(140,174,160,168);<br />
<br />
line(160,168,192,161);<br />
<br />
<br />
<br />
//////////////////////front<br />
<br />
ellipse(75,246,95,190,18,18);<br />
<br />
line(57,251,57,286);<br />
<br />
//setcolor(4);<br />
<br />
ellipse(181,178,232,263,200,137);<br />
<br />
ellipse(195,180,256,286,200,137);<br />
<br />
<br />
<br />
<br />
<br />
ellipse(191,171,228,247,200,100);<br />
<br />
ellipse(231,198,234,275,200,80);<br />
<br />
<br />
<br />
//setcolor(9);<br />
<br />
//ellipse(195,170,256,286,200,137);<br />
<br />
//setcolor(12);<br />
<br />
<br />
<br />
ellipse(196,167,228,246,200,90);<br />
<br />
ellipse(231,184,234,276,200,80);<br />
<br />
<br />
<br />
<br />
<br />
ellipse(191,200,228,246,200,90);<br />
<br />
ellipse(228,218,234,276,200,80);<br />
<br />
<br />
<br />
ellipse(258,268,180,220,200,40);<br />
<br />
ellipse(178,296,244,355,16,10);<br />
<br />
<br />
<br />
ellipse(238,249,227,250,200,60);<br />
<br />
<br />
<br />
<br />
<br />
/////////////wheel1<br />
<br />
ellipse(302,281,320,77,26,45);<br />
<br />
ellipse(290,277,65,162,40,45);<br />
<br />
ellipse(278,288,144,212,31,45);<br />
<br />
<br />
<br />
/////////////wheel2<br />
<br />
//setcolor(5);<br />
<br />
ellipse(302+260,229,328,87,26,45);<br />
<br />
ellipse(290+280-7,277-50+2,90,162,40,45);<br />
<br />
ellipse(278+270,288-50,144,215,27,45);<br />
<br />
b=0;<br />
<br />
int v=0;<br />
<br />
<br />
<br />
/////////<br />
<br />
ellipse(302+250+v,227+b,295,90,29,41);<br />
<br />
ellipse(302+234+v,231+b,245,306,50,40);<br />
<br />
//setlinestyle(3,0,3);<br />
<br />
ellipse(302+248+v,229+b,0,360,21,30);<br />
<br />
<br />
<br />
ellipse(302+247+v,229+b,0,360,8,10);<br />
<br />
setfillstyle(6,11);<br />
<br />
//floodfill(302+248+v,230+b,13);<br />
<br />
//line(546,201,546,257);<br />
<br />
//line(554,201,554,257);<br />
<br />
//setcolor(4);<br />
<br />
<br />
<br />
line(546+v,201+b,546+v,220+b);<br />
<br />
line(551+v,201+b-2,551+v,220+b);<br />
<br />
<br />
<br />
line(546+v,238+b,546+v,257+b);<br />
<br />
line(551+v,238+b+2,551+v,257+b+2);<br />
<br />
<br />
<br />
<br />
<br />
line(530+v,225+b,541+v,225+b);<br />
<br />
line(530+v,230+b,541+v,230);<br />
<br />
<br />
<br />
line(557+v,225+b,570+v,225+b);<br />
<br />
line(557+v,230+b,570+v,230+b);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
line(563+v,206+b,552+v,222+b);<br />
<br />
line(534+v,246+b,543+v,232+b);<br />
<br />
<br />
<br />
line(566+v,210+b,556+v,223+b);<br />
<br />
line(536+v,250+b,544+v,238+b);<br />
<br />
<br />
<br />
line(536+v,207+b,546+v,222+b);<br />
<br />
line(532+v,213+b,542+v,224+b);<br />
<br />
<br />
<br />
line(556+v,235+b,566+v,247+b);<br />
<br />
line(551+v,237+b,563+v,253+b);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
////////////////////////////////////////////////////<br />
<br />
v=-260;<br />
<br />
b=56;<br />
<br />
ellipse(302+233+v,221+b,260,60,49,51);<br />
<br />
//ellipse(302+234+v,231+b,245,306,50,40);<br />
<br />
//setlinestyle(3,0,3);<br />
<br />
ellipse(302+243+v,224+b,0,360,28,35);<br />
<br />
// line(249,328,269,328);<br />
<br />
ellipse(300+245+v,223+b,0,360,10,12);<br />
<br />
<br />
<br />
ellipse(285+249+v,239+b,210,260,30,33);<br />
<br />
//floodfill(285+258+v,230+b,12);<br />
<br />
b=45;<br />
<br />
v=v-4;<br />
<br />
line(546+v,201+b,546+v,220+b+2);<br />
<br />
line(551+v,201+b,551+v,220+b+2);<br />
<br />
b=b+8;<br />
<br />
line(546+v,238+b,546+v,257+b+4);<br />
<br />
line(551+v,238+b,551+v,257+b+4);<br />
<br />
v=v-2;<br />
<br />
line(530+v-6,225+b,541+v,225+b);<br />
<br />
line(530+v-6,230+b,541+v,230+b);<br />
<br />
v=v+5;<br />
<br />
line(557+v,225+b,570+v+3,225+b);<br />
<br />
line(557+v-1,230+b,570+v+3,230+b);<br />
<br />
<br />
<br />
<br />
<br />
b=b-5;<br />
<br />
v=v-5;<br />
<br />
line(565+v+3,206+b,552+v+4,222+b-2);<br />
<br />
b=b+15;<br />
<br />
<br />
<br />
line(534+v,246+b,543+v+3,232+b-5);<br />
<br />
b=b-10;<br />
<br />
line(566+v+7,210+b-5,556+v+4,220+b);<br />
<br />
line(536+v-5,250+b,544+v-2,238+b-4);<br />
<br />
<br />
<br />
<br />
<br />
line(536+v,207+b-8,545+v,222+b-5);<br />
<br />
line(531+v,212+b-8,542+v,224+b-2);<br />
<br />
<br />
<br />
line(556+v,235+b,566+v+3,247+b+5);<br />
<br />
line(551+v,237+b,563+v+2,253+b+3);<br />
<br />
<br />
<br />
///////////////////lights<br />
<br />
ellipse(199,250,144,345,18,8);<br />
<br />
line(185,245,206,230);<br />
<br />
//setcolor(4);<br />
<br />
ellipse(223,234,340,110,8,5);<br />
<br />
line(230,237,217,252);<br />
<br />
line(206,230,220,229);<br />
<br />
//setfillstyle(1,4);<br />
<br />
<br />
<br />
//floodfill(200,240,12);<br />
<br />
<br />
<br />
/////////////////////////////////////<br />
<br />
line(90,223,152,236);<br />
<br />
line(152,236,137,254);<br />
<br />
line(90,223,90,242);<br />
<br />
<br />
<br />
//setfillstyle(10,9);<br />
<br />
//floodfill(91,230,14);<br />
<br />
ellipse(240,270,104,136,100,60);<br />
<br />
ellipse(185,237,120,160,100,60);<br />
<br />
ellipse(80,221,357,134,10,10);<br />
<br />
<br />
<br />
line(152,236,168,228);<br />
<br />
///////////////////////////////////////////////<br />
<br />
line(435,116,440,171);<br />
<br />
//////////////////////////////////////////hp<br />
<br />
//line(134,185,220,210);<br />
<br />
line(134,185,196,160);<br />
<br />
line(214,212,318,185);<br />
<br />
/////////////////////////////////////////////////light<br />
<br />
<br />
<br />
//setcolor(14);<br />
<br />
ellipse(166,247,99,330,8,8);<br />
<br />
ellipse(171,243,310,129,7,7);<br />
<br />
putpixel(174,250,13);<br />
<br />
putpixel(173,251,13);<br />
<br />
putpixel(164,239,13);<br />
<br />
putpixel(165,238,13);<br />
<br />
<br />
<br />
/////////////////////////////////////////road/////////////////////<br />
<br />
setcolor(13);<br />
<br />
line(1,430,639,300);<br />
<br />
line(1,445,639,315);<br />
<br />
<br />
<br />
line(1,210,93,194);<br />
<br />
line(1,195,194,158);<br />
<br />
<br />
<br />
//line(1,170,639,71);<br />
<br />
//line(1,170,229,135);<br />
<br />
line(520,90,639,71);<br />
<br />
line(478,86,639,56);<br />
<br />
<br />
<br />
int c=0;<br />
<br />
<br />
<br />
line(10,194+c,10,208+c);<br />
<br />
line(40,189+c,40,204+c);<br />
<br />
line(70,183+c,70,198+c);<br />
<br />
line(100,176+c,100,190+c);<br />
<br />
line(130,170+c,130,177+c);<br />
<br />
line(160,166+c,160,168+c);<br />
<br />
line(190,160+c,190,161+c);<br />
<br />
<br />
<br />
line(190+330,78+c,190+330,89+c);<br />
<br />
<br />
<br />
line(190+360,72+c,190+360,85+c);<br />
<br />
line(190+390,67+c,190+390,81+c);<br />
<br />
line(190+420,62+c,190+420,76+c);<br />
<br />
line(190+449,57+c,190+449,71+c);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
c=236;<br />
<br />
<br />
<br />
line(10,192+c,10,208+c);<br />
<br />
line(40,189+c-2,40,204+c-3);<br />
<br />
line(70,183+c-3,70,198+c-3);<br />
<br />
line(100,176+c-2,100,190+c-2);<br />
<br />
line(130,170+c-2,130,177+c+5);<br />
<br />
line(160,166+c-3,160,168+c+8);<br />
<br />
line(190,160+c-4,190,161+c+9);<br />
<br />
<br />
<br />
line(190+30,156+c-5,190+30,170+c-5);<br />
<br />
<br />
<br />
<br />
<br />
line(190+30+30,156+c-12,190+30+30,170+c-12);<br />
<br />
<br />
<br />
line(190+90,156+c-18,190+90,170+c-17);<br />
<br />
<br />
<br />
line(190+120,156+c-25,190+120,170+c-25);<br />
<br />
<br />
<br />
line(190+150,156+c-30,190+150,170+c-30);<br />
<br />
<br />
<br />
line(190+180,156+c-37,190+180,170+c-36);<br />
<br />
<br />
<br />
<br />
<br />
line(190+210,156+c-42,190+210,170+c-42);<br />
<br />
<br />
<br />
<br />
<br />
line(190+240,156+c-48,190+240,170+c-48);<br />
<br />
<br />
<br />
<br />
<br />
line(190+270,156+c-55,190+270,170+c-54);<br />
<br />
<br />
<br />
<br />
<br />
line(190+300,156+c-61,190+300,170+c-61);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
line(190+330,78+c+10,190+330,89+c+13);<br />
<br />
<br />
<br />
line(190+360,72+c+11,190+360,85+c+13);<br />
<br />
line(190+390,67+c+10,190+390,81+c+10);<br />
<br />
line(190+420,62+c+8,190+420,76+c+10);<br />
<br />
line(190+449,57+c+8,190+449,71+c+8);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
/////////////////road<br />
<br />
<br />
<br />
setcolor(12); /////////////////////////////1<br />
<br />
<br />
<br />
line(1,310,25,306);<br />
<br />
line(6,318,30,315);<br />
<br />
line(1,310,6,318);<br />
<br />
line(25,306,30,314);<br />
<br />
int k,m;<br />
<br />
k=13*45+19;<br />
<br />
m=16*(-8);<br />
<br />
//2<br />
<br />
setcolor(12);<br />
<br />
<br />
<br />
line(605,310-128,629,306-128);<br />
<br />
line(610,318-128,634,315-128);<br />
<br />
line(605,310-128,610,318-128);<br />
<br />
line(629,306-128,634,314-128);<br />
<br />
<br />
<br />
setcolor(12); //////////////////////////////////3<br />
<br />
k=45;<br />
<br />
m=-8;<br />
<br />
line(46,302,70,298);<br />
<br />
line(51,310,75,307);<br />
<br />
line(46,302,51,310);<br />
<br />
line(70,298,75,306);<br />
<br />
<br />
<br />
<br />
<br />
setfillstyle(1,0);<br />
<br />
floodfill(64,303,12);<br />
<br />
<br />
<br />
setfillstyle(1,14);<br />
<br />
floodfill(14,314,12);<br />
<br />
floodfill(617,183,12);<br />
<br />
<br />
<br />
setfillstyle(1,0);<br />
<br />
floodfill(14,314,12);<br />
<br />
floodfill(617,183,12);<br />
<br />
<br />
<br />
setfillstyle(1,14);<br />
<br />
floodfill(64,303,12);<br />
<br />
<br />
<br />
t=getch();<br />
<br />
if(t=='1')<br />
<br />
exit(0);<br />
<br />
if(t=='h')<br />
<br />
{<br />
<br />
sound(710);<br />
<br />
delay(500);<br />
<br />
nosound();<br />
<br />
//break;<br />
<br />
}<br />
<br />
if(t=='t')<br />
<br />
{<br />
<br />
while(!kbhit()) {<br />
<br />
setfillstyle(1,0);<br />
<br />
floodfill(536,213,13);<br />
<br />
floodfill(563,213,13);<br />
<br />
floodfill(561,244,13);<br />
<br />
floodfill(538,244,13);<br />
<br />
floodfill(274,295,13);<br />
<br />
floodfill(294,295,13);<br />
<br />
floodfill(274,265,13);<br />
<br />
floodfill(294,265,13);<br />
<br />
floodfill(548,250,13);<br />
<br />
floodfill(548,214,13);<br />
<br />
floodfill(533,228,13);<br />
<br />
floodfill(563,228,13);<br />
<br />
floodfill(262,281,13);<br />
<br />
floodfill(308,281,13);<br />
<br />
floodfill(284,251,13);<br />
<br />
floodfill(284,295,13);<br />
<br />
<br />
<br />
setfillstyle(1,random(12));<br />
<br />
<br />
<br />
floodfill(200,250,13);<br />
<br />
delay(10);<br />
<br />
//setfillstyle(1,11);<br />
<br />
<br />
<br />
floodfill(170,250,13);<br />
<br />
floodfill(80,230,13);<br />
<br />
<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
setfillstyle(1,0);<br />
<br />
<br />
<br />
floodfill(200,250,13);<br />
<br />
delay(10);<br />
<br />
//setfillstyle(1,11);<br />
<br />
<br />
<br />
floodfill(170,250,13);<br />
<br />
floodfill(80,230,13);<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
<br />
<br />
if(t=='l')<br />
<br />
{<br />
<br />
while(!kbhit())<br />
<br />
{<br />
<br />
<br />
<br />
delay(120);<br />
<br />
setfillstyle(6,0); //////////////////////////ty<br />
<br />
floodfill(536,213,13);<br />
<br />
floodfill(563,213,13);<br />
<br />
floodfill(561,244,13);<br />
<br />
floodfill(538,244,13);<br />
<br />
floodfill(274,295,13);<br />
<br />
floodfill(294,295,13);<br />
<br />
floodfill(274,265,13);<br />
<br />
floodfill(294,265,13);<br />
<br />
<br />
<br />
setfillstyle(1,0);<br />
<br />
floodfill(64,303,12);<br />
<br />
<br />
<br />
///////////////////////////////////road<br />
<br />
<br />
<br />
setfillstyle(9,0); /////////////////////color<br />
<br />
floodfill(81-40+5,419+7,13);<br />
<br />
floodfill(151-40,409+7,13);<br />
<br />
floodfill(211-40,397+7,13);<br />
<br />
floodfill(271-40,380+7,13);<br />
<br />
floodfill(331-40,368+7,13);<br />
<br />
floodfill(396-40,355+7,13);<br />
<br />
floodfill(450-40,345+7,13);<br />
<br />
floodfill(510-40,335+7,13);<br />
<br />
floodfill(570-40,325+7,13);<br />
<br />
floodfill(630-40,312+7,13);<br />
<br />
<br />
<br />
<br />
<br />
//////////////////////<br />
<br />
floodfill(50,197,13);<br />
<br />
floodfill(110,177,13);<br />
<br />
floodfill(166,165,13);<br />
<br />
floodfill(527,86,13);<br />
<br />
floodfill(587,71,13);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
setfillstyle(6,14); //////////////////////////ty<br />
<br />
floodfill(548,250,13);<br />
<br />
floodfill(548,214,13);<br />
<br />
floodfill(533,228,13);<br />
<br />
floodfill(563,228,13);<br />
<br />
floodfill(262,281,13);<br />
<br />
floodfill(308,281,13);<br />
<br />
floodfill(284,251,13);<br />
<br />
floodfill(284,295,13);<br />
<br />
////////////////////////////////////////road<br />
<br />
<br />
<br />
setfillstyle(9,10);///////////////////////////////////color<br />
<br />
floodfill(19,429,13);<br />
<br />
floodfill(81,419,13);<br />
<br />
floodfill(151,409,13);<br />
<br />
floodfill(211,397,13);<br />
<br />
floodfill(271,380,13);<br />
<br />
floodfill(331,368,13);<br />
<br />
floodfill(396,355,13);<br />
<br />
floodfill(450,345,13);<br />
<br />
floodfill(510,335,13);<br />
<br />
floodfill(570,325,13);<br />
<br />
floodfill(630,312,13);<br />
<br />
//////////////////////////////////////<br />
<br />
floodfill(20,197,13);<br />
<br />
floodfill(80,187,13);<br />
<br />
floodfill(133,174,13);<br />
<br />
floodfill(517,86,13);<br />
<br />
floodfill(557,81,13);<br />
<br />
floodfill(627,70,13);<br />
<br />
<br />
<br />
setfillstyle(1,14);<br />
<br />
floodfill(14,314,12);<br />
<br />
floodfill(617,183,12);<br />
<br />
<br />
<br />
///////////////////////////////////////<br />
<br />
setfillstyle(10,4);<br />
<br />
floodfill(302+248,230,13);<br />
<br />
floodfill(302+248+v,230+b,13);<br />
<br />
///light<br />
<br />
setfillstyle(6,11); ///////////<br />
<br />
<br />
<br />
floodfill(200,250,13);<br />
<br />
<br />
<br />
floodfill(170,250,13);<br />
<br />
floodfill(80,230,13);<br />
<br />
<br />
<br />
delay(120);<br />
<br />
<br />
<br />
setfillstyle(6,0);/////////////////////ty<br />
<br />
floodfill(548,250,13);<br />
<br />
floodfill(548,214,13);<br />
<br />
floodfill(533,228,13);<br />
<br />
floodfill(563,228,13);<br />
<br />
floodfill(262,281,13);<br />
<br />
floodfill(308,281,13);<br />
<br />
floodfill(284,251,13);<br />
<br />
floodfill(284,295,13);<br />
<br />
/////////////////////////////////////road<br />
<br />
setfillstyle(9,0); ///////////////color<br />
<br />
<br />
<br />
floodfill(19,429,13);<br />
<br />
floodfill(81,419,13);<br />
<br />
floodfill(151,409,13);<br />
<br />
floodfill(211,397,13);<br />
<br />
floodfill(271,380,13);<br />
<br />
floodfill(331,368,13);<br />
<br />
floodfill(396,355,13);<br />
<br />
floodfill(450,345,13);<br />
<br />
floodfill(510,335,13);<br />
<br />
floodfill(570,325,13);<br />
<br />
floodfill(630,312,13);<br />
<br />
///////////////////////////////////////////////////////<br />
<br />
floodfill(20,197,13);<br />
<br />
floodfill(80,187,13);<br />
<br />
floodfill(133,174,13);<br />
<br />
floodfill(517,86,13);<br />
<br />
floodfill(557,81,13);<br />
<br />
floodfill(627,70,13);<br />
<br />
/////////////////////////////<br />
<br />
setfillstyle(1,0);<br />
<br />
floodfill(14,314,12);<br />
<br />
floodfill(617,183,12);<br />
<br />
<br />
<br />
setfillstyle(6,10); /////////////ty<br />
<br />
<br />
<br />
floodfill(536,213,13);<br />
<br />
floodfill(563,213,13);<br />
<br />
floodfill(561,244,13);<br />
<br />
floodfill(538,244,13);<br />
<br />
floodfill(274,295,13);<br />
<br />
floodfill(294,295,13);<br />
<br />
floodfill(274,265,13);<br />
<br />
floodfill(294,265,13);<br />
<br />
////////////////////////////////////////////////road<br />
<br />
setfillstyle(9,14);/////////////////////////////////////////color<br />
<br />
floodfill(81-40+5,419+7,13);<br />
<br />
floodfill(151-40,409+7,13);<br />
<br />
floodfill(211-40,397+7,13);<br />
<br />
floodfill(271-40,380+7,13);<br />
<br />
floodfill(331-40,368+7,13);<br />
<br />
floodfill(396-40,355+7,13);<br />
<br />
floodfill(450-40,345+7,13);<br />
<br />
floodfill(510-40,335+7,13);<br />
<br />
floodfill(570-40,325+7,13);<br />
<br />
floodfill(630-40,312+7,13);<br />
<br />
/////////////////////////////////////////<br />
<br />
<br />
<br />
floodfill(50,197,13);<br />
<br />
floodfill(110,177,13);<br />
<br />
floodfill(166,165,13);<br />
<br />
floodfill(527,86,13);<br />
<br />
floodfill(587,71,13);<br />
<br />
setfillstyle(1,14);<br />
<br />
floodfill(64,303,12);<br />
<br />
<br />
<br />
setfillstyle(9,4);<br />
<br />
floodfill(302+248,230,13);<br />
<br />
floodfill(302+248+v,230+b,13);<br />
<br />
<br />
<br />
delay(20);<br />
<br />
setfillstyle(1,14);<br />
<br />
<br />
<br />
floodfill(200,250,13);<br />
<br />
<br />
<br />
floodfill(170,250,13);<br />
<br />
floodfill(80,230,13);<br />
<br />
<br />
<br />
delay(20);<br />
<br />
setfillstyle(1,0);<br />
<br />
<br />
<br />
floodfill(200,250,13);<br />
<br />
<br />
<br />
floodfill(170,250,13);<br />
<br />
floodfill(80,230,13);<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
} }<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
if(t=='p')<br />
<br />
{<br />
<br />
int n=0;<br />
<br />
while(!kbhit())<br />
<br />
{<br />
<br />
if(n<=60)<br />
<br />
n++;<br />
<br />
setcolor(0);<br />
<br />
rectangle(1+1,-10,90-1,-12+n);<br />
<br />
delay(14);<br />
<br />
<br />
<br />
setcolor(9);<br />
<br />
rectangle(1,-10,90,-10+n);<br />
<br />
if(n==60)<br />
<br />
{<br />
<br />
<br />
<br />
outtextxy(10,10,"L-LIGHTS");<br />
<br />
outtextxy(10,20,"H-HORN");<br />
<br />
//outtextxy(10,30,"T-AllOY");<br />
<br />
delay(400);<br />
<br />
}<br />
<br />
<br />
<br />
<br />
<br />
}<br />
<br />
setcolor(0);<br />
<br />
rectangle(1,-10,90,-10+n);<br />
<br />
rectangle(1,-10,90,-11+n);<br />
<br />
outtextxy(10,10,"L-LIGHTS");<br />
<br />
outtextxy(10,20,"H-HORN");<br />
<br />
//outtextxy(10,30,"T-AllOY");<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
circle(300,100,3);<br />
<br />
<br />
<br />
nosound();<br />
<br />
<br />
<br />
getch();<br />
<br />
}Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-40425629994870541322009-06-15T22:26:00.000-07:002009-06-15T22:26:05.404-07:00Computer Graphics - Piano Music#include <stdio.h></stdio.h><br />
<br />
<br />
#include <stdlib.h></stdlib.h><br />
<br />
#include <dos.h></dos.h><br />
<br />
#include <conio.h></conio.h><br />
<br />
/** PC Internal Speaker Required **/<br />
<br />
//** Author: Sarju www.sarjus-elearning.blogspot.com **/<br />
<br />
main()<br />
<br />
<br />
<br />
<br />
<br />
{<br />
<br />
float octave[7]={130.81,146.83,164.81,174.61,196.220,246.94};<br />
//You can change the music by changing above values<br />
int adn;<br />
<br />
while(!kbhit())<br />
<br />
<br />
<br />
<br />
<br />
{<br />
<br />
adn=random(7);<br />
<br />
sound(octave[adn]*10);<br />
<br />
delay(190);<br />
<br />
nosound();<br />
<br />
}}Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-79875854148657836512009-06-15T22:23:00.000-07:002009-06-15T22:23:43.077-07:00Computer Graphics - BLINCK#include<conio.h></conio.h></conio.h><//conio.h><br />
#include<graphics.h></graphics.h></graphics.h><//graphics.h><br />
#include<stdlib.h></stdlib.h></stdlib.h><//stdlib.h><br />
#include<dos.h></dos.h></dos.h><//dos.h><br />
void main()<br />
{<br />
int gdriver=DETECT,gmode;<br />
int i,x,y;<br />
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");<br />
//"c:\\tc\\bgi" give location in your system where C installed<br />
while(!kbhit())<br />
{<br />
x=random(640);<br />
y=random(480);<br />
setcolor(15);<br />
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--)<br />
{<br />
line(x+8,y-2,x+i,y);<br />
line(x+8,y+2,x+i,y);<br />
}<br />
for(i=40;i>=10;i--)<br />
{<br />
line(x-8,y-2,x-i,y);<br />
line(x-8,y+2,x-i,y);<br />
}<br />
for(i=40;i>=10;i--)<br />
{<br />
line(x-2,y+8,x,y+i);<br />
line(x+2,y+8,x,y+i);<br />
}<br />
for(i=40;i>=10;i--)<br />
{<br />
line(x-2,y-8,x,y-i);<br />
line(x+2,y-8,x,y-i);<br />
}<br />
for(i=20;i>=7;i--)<br />
{<br />
line(x+8,y-2,x+i,y-i);<br />
line(x+2,y-8,x+i,y-i);<br />
}<br />
for(i=20;i>=7;i--)<br />
{<br />
line(x+8,y+2,x+i,y+i);<br />
line(x+2,y+8,x+i,y+i);<br />
}<br />
for(i=20;i>=7;i--)<br />
{<br />
line(x-8,y+2,x-i,y+i);<br />
line(x-2,y+8,x-i,y+i);<br />
}<br />
for(i=20;i>=7;i--)<br />
{<br />
line(x-8,y-2,x-i,y-i);<br />
line(x-2,y-8,x-i,y-i);<br />
}<br />
for(i=9;i>0;i--)<br />
{<br />
circle(x,y,i);<br />
delay(10);<br />
}<br />
nosound();<br />
}<br />
cleardevice();<br />
setcolor(2);<br />
settextstyle(2,0,5);<br />
outtextxy(220,160,"Creator:SARJU");<br />
outtextxy(265,235,"www.sarjus-elearning.blogspot.com");<br />
getch();getch();<br />
}Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-65090908348148978842009-06-15T20:10:00.000-07:002009-06-15T20:28:04.431-07:00Banker's Algorithm - Linux Lab<div style="text-align: justify;"></div><div style="text-align: justify;"><b>BANKER’S ALGORITHM</b></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Theory</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The Banker's algorithm is a resource allocation & deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of pre-determined maximum possible amounts of all resources, and then makes a "safe-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.</div><div style="text-align: justify;"></div><div style="text-align: justify;">The algorithm was developed in the design process for the THE operating system and originally described (in Dutch) in EWD108[1]. The name is by analogy with the way that bankers account for liquidity constraints.</div><div style="text-align: justify;"></div><div style="text-align: justify;">The Banker's algorithm is run by the operating system whenever a process requests resources.The algorithm prevents deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur).</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Resources</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">For the Banker's algorithm to work, it needs to know three things:</div><div style="text-align: justify;"></div><div style="text-align: justify;">• How much of each resource each process could possibly request </div><div style="text-align: justify;">• How much of each resource each process is currently holding </div><div style="text-align: justify;">• How much of each resource the system has available </div><div style="text-align: justify;"></div><div style="text-align: justify;">Some of the resources that are tracked in real systems are memory, semaphores and interface access.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Example</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Assuming that the system distinguishes between four types of resources, (A, B, C and D), the following is an example of how those resources could be distributed. Note that this example shows the system at an instant before a new request for resources arrives. Also, the types and number of resources are abstracted. Real systems, for example, would deal with much larger quantities of each resource.</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">Available system resources: </div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">3 1 1 2</div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (currently allocated resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 1 2 2 1</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 0 3 3</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 1 0</div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (maximum resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 3 3 2 2</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 2 3 4</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 5 0</div><div style="text-align: justify;"></div><div style="text-align: justify;">Safe and Unsafe States</div><div style="text-align: justify;"></div><div style="text-align: justify;">A state (as in the above example) is considered safe if it is possible for all processes to finish executing (terminate). Since the system cannot know when a process will terminate, or how many resources it will have requested by then, the system assumes that all processes will eventually attempt to acquire their stated maximum resources and terminate soon afterward. This is a reasonable assumption in most cases since the system is not particularly concerned with how long each process runs (at least not from a deadlock avoidance perspective). Also, if a process terminates without acquiring its maximum resources, it only makes it easier on the system.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical set of requests by the processes that would allow each to acquire its maximum resources and then terminate (returning its resources to the system). Any state where no such set exists is an unsafe state.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Example</div><div style="text-align: justify;"></div><div style="text-align: justify;">We can show that the state given in the previous example is a safe state by showing that it is possible for each process to acquire its maximum resources and then terminate.</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. P1 acquires 2 A, 1 B and 1 D more resources, achieving its maximum </div><div style="text-align: justify;">o The system now still has 1 A, no B, 1 C and 1 D resource available </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. P1 terminates, returning 3 A, 3 B, 2 C and 2 D resources to the system </div><div style="text-align: justify;">o The system now has 4 A, 3 B, 3 C and 3 D resources available </div><div style="text-align: justify;"></div><div style="text-align: justify;">3. P2 acquires 2 B and 1 D extra resources, then terminates, returning all its resources </div><div style="text-align: justify;">o The system now has 5 A, 3 B, 6 C and 6 D resources </div><div style="text-align: justify;"></div><div style="text-align: justify;">4. P3 acquires 4 C resources and terminates </div><div style="text-align: justify;">o The system now has all resources: 6 A, 4 B, 7 C and 6 D </div><div style="text-align: justify;"></div><div style="text-align: justify;">5. Because all processes were able to terminate, this state is safe </div><div style="text-align: justify;"></div><div style="text-align: justify;">Note that these requests and acquisitions are hypothetical. The algorithm generates them to check the safety of the state, but no resources are actually given and no processes actually terminate. Also note that the order in which these requests are generated – if several can be fulfilled – doesn't matter, because all hypothetical requests let a process terminate, thereby increasing the system's free resources.</div><div style="text-align: justify;"></div><div style="text-align: justify;">For an example of an unsafe state, look at what would happen if process 2 were holding 1 more unit of resource B at the beginning.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Requests</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">When the system receives a request for resources, it runs the Banker's algorithm to determine if it is safe to grant the request. The algorithm is fairly straight forward once the distinction between safe and unsafe states is understood.</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. Can the request be granted? </div><div style="text-align: justify;"></div><div style="text-align: justify;">o If not, the request is impossible and must either be denied or put on a waiting list </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Assume that the request is granted </div><div style="text-align: justify;"></div><div style="text-align: justify;">3. Is the new state safe? </div><div style="text-align: justify;"></div><div style="text-align: justify;">o If so grant the request </div><div style="text-align: justify;"></div><div style="text-align: justify;">o If not, either deny the request or put it on a waiting list </div><div style="text-align: justify;"></div><div style="text-align: justify;">Whether the system denies an impossible or unsafe request or makes it wait is an operating system specific decision.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Continuing the previous examples, assume process 3 requests 2 units of resource C.</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. There is not enough of resource C available to grant the request </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. The request is denied </div><div style="text-align: justify;"></div><div style="text-align: justify;">On the other hand, assume process 3 requests 1 unit of resource C.</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. There are enough resources to grant the request </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Assume the request is granted </div><div style="text-align: justify;"></div><div style="text-align: justify;">o The new state of the system would be: </div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">Available system resources: A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">Free 3 1 0 2</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (currently allocated resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 1 2 2 1</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 0 3 3</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 2 0</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (maximum resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 3 3 2 2</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 2 3 4</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 5 0</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. Determine if this new state is safe </div><div style="text-align: justify;"></div><div style="text-align: justify;">1. P1 can acquire 2 A, 1 B and 1 D resources and terminate </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Then, P2 can acquire 2 B and 1 D resources and terminate </div><div style="text-align: justify;"></div><div style="text-align: justify;">3. Finally, P3 can acquire 3 C resources and terminate </div><div style="text-align: justify;"></div><div style="text-align: justify;">4. Therefore, this new state is safe </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Since the new state is safe, grant the request </div><div style="text-align: justify;"></div><div style="text-align: justify;">Finally, assume that process 2 requests 1 unit of resource B.</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. There are enough resources </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Assuming the request is granted, the new state would be: </div><div style="text-align: justify;"></div><div style="text-align: justify;">Available system resources:</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">Free 3 0 1 2</div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (currently allocated resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 1 2 2 1</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 1 3 3</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 1 0</div><div style="text-align: justify;"></div><div style="text-align: justify;">Processes (maximum resources):</div><div style="text-align: justify;"></div><div style="text-align: justify;">A B C D</div><div style="text-align: justify;"></div><div style="text-align: justify;">P1 3 3 2 2</div><div style="text-align: justify;"></div><div style="text-align: justify;">P2 1 2 3 4</div><div style="text-align: justify;"></div><div style="text-align: justify;">P3 1 1 5 0</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. Is this state safe? Assuming P1, P2, and P3 request more of resource B and C. </div><div style="text-align: justify;"></div><div style="text-align: justify;">o P1 is unable to acquire enough B resources </div><div style="text-align: justify;">o P2 is unable to acquire enough B resources </div><div style="text-align: justify;">o P3 is unable to acquire enough C resources </div><div style="text-align: justify;">o No process can acquire enough resources to terminate, so this state is not safe </div><div style="text-align: justify;"></div><div style="text-align: justify;">2. Since the state is unsafe, deny the request</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Algorithm</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">* The algorithm for the process is...........</div><div style="text-align: justify;"></div><div style="text-align: justify;">1. Start.</div><div style="text-align: justify;">2. Wait for the request.</div><div style="text-align: justify;">3. If the request is for allocation then</div><div style="text-align: justify;"></div><div style="text-align: justify;">i. Obtain the request.</div><div style="text-align: justify;">ii. Check for safe state.</div><div style="text-align: justify;">iii. If safe state detected then allocate resources.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Else put the request in the waiting stack.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Else (for deallocation)</div><div style="text-align: justify;"></div><div style="text-align: justify;">i.Deallocate the resources.</div><div style="text-align: justify;">ii. Process the request of the process in the </div><div style="text-align: justify;"></div><div style="text-align: justify;">wait stack.</div><div style="text-align: justify;"></div><div style="text-align: justify;">4. Stop.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Program</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">/* Program to demonstrate the working of the BANKERS ALGORITHM.</div><div style="text-align: justify;">* This program gives a general idea of how the operating system process allocates resources to the requesting processes.</div><div style="text-align: justify;">* Here it is assumed that request are collected in a 'req_queue' whose type is stored in 'req_type'.</div><div style="text-align: justify;">* It has been assumed that there are 3 processes which can request for 4 different types of resources say A,B,C & D.</div><div style="text-align: justify;">* The program works with certain assumed values as shown below.........</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIrY_Nzjl1vRrDYoN5VoTOlFzA-YDxY5chcke80IONeSUpwQxU6oadczgUoHKblPbxxdyAsR32Mi7z1SJ5r8RvUMhVumfdXDnHZ5l9u7lT9gAmFOlXoVPEsRb797EmUkVAgcHrR3ficFA/s1600-h/NOS.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIrY_Nzjl1vRrDYoN5VoTOlFzA-YDxY5chcke80IONeSUpwQxU6oadczgUoHKblPbxxdyAsR32Mi7z1SJ5r8RvUMhVumfdXDnHZ5l9u7lT9gAmFOlXoVPEsRb797EmUkVAgcHrR3ficFA/s400/NOS.GIF" tj="true" /></a></div><div style="text-align: justify;"></div><div class="separator" style="clear: both; text-align: center;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">* This module can be compiled using 'cc bankersalgorithm.c' and executed using './a.out' </div><div style="text-align: justify;"></div><div style="text-align: justify;">//inculsion.</div><div style="text-align: justify;">#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></div><div style="text-align: justify;"></div><div style="text-align: justify;">//macro definition</div><div style="text-align: justify;">#define cls() printf("\033[H\033[J")</div><div style="text-align: justify;">#define delay() for(h=0;h<30000;h++){for(k=0;k<30000;k++){}}</div><div style="text-align: justify;"></div><div style="text-align: justify;">//function prototype.</div><div style="text-align: justify;">void getrequest(int);</div><div style="text-align: justify;">int issafe();</div><div style="text-align: justify;">void allocate(int);</div><div style="text-align: justify;">void deallocate(int);</div><div style="text-align: justify;">void display();</div><div style="text-align: justify;"></div><div style="text-align: justify;">//global matrixes as shown above.</div><div style="text-align: justify;">int available_res[4],cur_allocated_res[3][4],max_req_res[3][4],request[4];</div><div style="text-align: justify;">int main()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int req_queue[10],wait_queue[10],req_type[10],i,j,h,k;</div><div style="text-align: justify;"></div><div style="text-align: justify;">//inserting the assumed values.</div><div style="text-align: justify;">available_res[0]=3; available_res[1]=1; available_res[2]=1; available_res[3]=2;</div><div style="text-align: justify;">cur_allocated_res[0][0]=1;cur_allocated_res[0][1]=2;cur_allocated_res[0][2]=2;cur_allocated_res[0][3]=1;</div><div style="text-align: justify;">cur_allocated_res[1][0]=1;cur_allocated_res[1][1]=0;cur_allocated_res[1][2]=3;cur_allocated_res[1][3]=3;</div><div style="text-align: justify;">cur_allocated_res[2][0]=1;cur_allocated_res[2][1]=1;cur_allocated_res[2][2]=1;cur_allocated_res[2][3]=1;</div><div style="text-align: justify;">max_req_res[0][0]=3; max_req_res[0][1]=3; max_req_res[0][2]=2; max_req_res[0][3]=2;</div><div style="text-align: justify;">max_req_res[1][0]=1; max_req_res[1][1]=2; max_req_res[1][2]=3; max_req_res[1][3]=4;</div><div style="text-align: justify;">max_req_res[2][0]=1; max_req_res[2][1]=1; max_req_res[2][2]=1; max_req_res[2][3]=0;</div><div style="text-align: justify;"></div><div style="text-align: justify;">//assuming the request.</div><div style="text-align: justify;">req_queue[0]=0;req_queue[1]=1;req_queue[2]=2;req_queue[3]=0;req_queue[4]=2;req_queue[5]=1;</div><div style="text-align: justify;">req_type[0]=0;req_type[1]=0;req_type[2]=0;req_type[3]=1;req_type[4]=1;req_type[5]=1;</div><div style="text-align: justify;">display();</div><div style="text-align: justify;">i=0;j=0;</div><div style="text-align: justify;">while(i<6)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">cls();</div><div style="text-align: justify;">printf("\n\t\t\tWaiting for the request.............\n");</div><div style="text-align: justify;">delay();</div><div style="text-align: justify;">printf("\n\tRequest obtained from the process P%d ",req_queue[i]+1);</div><div style="text-align: justify;">if(req_type[i]==0)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("for resource allocation.\n");</div><div style="text-align: justify;">printf("\nGetting request.....\n");</div><div style="text-align: justify;">getrequest(req_queue[i]);</div><div style="text-align: justify;">printf("\nChecking safe state.....\n");</div><div style="text-align: justify;">delay();</div><div style="text-align: justify;">if(issafe())</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\nSafe state detected....\nAllocating resources .......\n");</div><div style="text-align: justify;">allocate(req_queue[i]);</div><div style="text-align: justify;">}</div><div style="text-align: justify;">else</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\nUnsafe state detected....\nInserting the process into waiting queue.\n");</div><div style="text-align: justify;">j=j+1;</div><div style="text-align: justify;">wait_queue[j]=req_queue[i];</div><div style="text-align: justify;">}<br />
}</div><div style="text-align: justify;">else</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("for resource deallocation.\n"); </div><div style="text-align: justify;">deallocate(req_queue[i]);</div><div style="text-align: justify;">printf("\nDeallocation done.\n\n%d processes in the wait queue\n",j); </div><div style="text-align: justify;">if(j>0)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">while(j>0)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\nConsidering the waiting queue process P%d\n",wait_queue[j]+1);</div><div style="text-align: justify;">getrequest(wait_queue[j]);</div><div style="text-align: justify;">printf("\nChecking safe state.\n");</div><div style="text-align: justify;">delay(); </div><div style="text-align: justify;">if(issafe())</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\nSafe state detected\nResources allocated\n");</div><div style="text-align: justify;">allocate(wait_queue[j]);</div><div style="text-align: justify;">j--;</div><div style="text-align: justify;">}</div><div style="text-align: justify;">else</div><div style="text-align: justify;">{</div><div style="text-align: justify;">break; </div><div style="text-align: justify;">}</div><div style="text-align: justify;">} </div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">delay();</div><div style="text-align: justify;">i++;</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">void getrequest(int i)<br />
{</div><div style="text-align: justify;">int j;</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{ request[j]=max_req_res[i][j]-cur_allocated_res[i][j];</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">int issafe()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int j;</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">if(request[j]>available_res[j])</div><div style="text-align: justify;">{</div><div style="text-align: justify;">return 0;</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">return 1;</div><div style="text-align: justify;">}</div><div style="text-align: justify;">void allocate(int i)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int j;</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">cur_allocated_res[i][j]=cur_allocated_res[i][j]+request[j];<br />
available_res[j]=available_res[j]-request[j];</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">void deallocate(int i)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int j;</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">available_res[j]=available_res[j]+cur_allocated_res[i][j];</div><div style="text-align: justify;">cur_allocated_res[i][j]=0;</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;">void display()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int i,j;</div><div style="text-align: justify;">char a[3];</div><div style="text-align: justify;">cls();</div><div style="text-align: justify;">printf("\n\n\n\n\n\n\n\n"); </div><div style="text-align: justify;">printf("_______________________________________________________________");</div><div style="text-align: justify;">printf("\n\n\n\n\t\t\t\tBANKERS ALGORITHM.\n");</div><div style="text-align: justify;">printf("\t\t\t\t__________________\n\n\n");</div><div style="text-align: justify;">printf("\n\n\n\n\t\tPRESS ENTER TO CONTINUE..................");</div><div style="text-align: justify;">fgets(a,2,stdin);</div><div style="text-align: justify;">cls();</div><div style="text-align: justify;">printf("\n\n\t\t\t\tDETAILS.");</div><div style="text-align: justify;">printf("\n\t\t\t\t________");</div><div style="text-align: justify;">printf("\n\n\t\tNO OF PROCESSES:\tP1 P2 P3");</div><div style="text-align: justify;">printf("\n\n\t\tNO OF RESOURCES:\tA B C D");</div><div style="text-align: justify;">printf("\n\n\t\t\tThe status .............");</div><div style="text-align: justify;">printf("\nThe available resources are.\n\t\tA B C D\n\t\t");</div><div style="text-align: justify;">for(i=0;i<4;i++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("%d ",available_res[i]);</div><div style="text-align: justify;">}</div><div style="text-align: justify;">printf("\nThe currently allocated resources are...\n\t\t A B C D\n\t\t");</div><div style="text-align: justify;">for(i=0;i<3;i++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("P%d ",i+1);</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("%d ",cur_allocated_res[i][j]);</div><div style="text-align: justify;">}</div><div style="text-align: justify;">printf("\n\t\t");</div><div style="text-align: justify;">}</div><div style="text-align: justify;">printf("\nThe maximum required resources are...\n\t\t A B C D\n\t\t");</div><div style="text-align: justify;">for(i=0;i<3;i++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("P%d ",i+1);</div><div style="text-align: justify;">for(j=0;j<4;j++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("%d ",max_req_res[i][j]);<br />
}</div><div style="text-align: justify;">printf("\n\t\t");</div><div style="text-align: justify;">}</div><div style="text-align: justify;">printf("\n\n\tPRESS ENTER TO START THE PROCESS...\n");</div><div style="text-align: justify;">fgets(a,2,stdin);</div><div style="text-align: justify;">}</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong><a href="http://samplecodes.files.wordpress.com/2008/11/17.jpg">Output</a></strong></div>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-30790925200283172672009-06-15T07:56:00.000-07:002009-06-15T20:29:21.240-07:00Dining Philosopher's Problem - Linux Lab<div style="text-align: left;"></div><strong>DINING PHILOSOPHER’S PROBLEM</strong><br />
<br />
<strong>The problem</strong><br />
<br />
<br />
<div style="text-align: justify;">The dining philosophers problem is summarized as five philosophers sitting at a table doing one of two things - eating or thinking. While eating, they are not thinking, and while thinking, they are not eating. The five philosophers sit at a circular table with a large bowl of spaghetti in the center. A fork is placed in between each philosopher, and as such, each philosopher has one fork to his or her left and one fork to his or her right. As spaghetti is difficult to serve and eat with a single fork, it must be assumed that in order for a philosopher to eat, the philosopher must have two forks. In the case of the dining philosopher, the philosopher can only use the fork on his or her left or right.</div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhH_ojNGPy_MYlFYEg__ZyXuDw_WchG6lciZsRbob7-igyYmWrymMF7xA1jcwNdp6KFUP_NREbOF3Nv4sdqX8K6jRaI98BsD957k8M162UrV-k4kw54WTTFJbm7cJQn6ioB6_b-XEi2q3w/s1600-h/Dining.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhH_ojNGPy_MYlFYEg__ZyXuDw_WchG6lciZsRbob7-igyYmWrymMF7xA1jcwNdp6KFUP_NREbOF3Nv4sdqX8K6jRaI98BsD957k8M162UrV-k4kw54WTTFJbm7cJQn6ioB6_b-XEi2q3w/s400/Dining.bmp" tj="true" /></a></div><div class="separator" style="clear: both; text-align: justify;">In some cases, the dining philosophers problem is explained using rice and chopsticks as opposed to spaghetti and forks, as it is generally easier to understand that two chopsticks are required, whereas one could arguably eat spaghetti using a single fork, or using a fork and a spoon. In either case, only one instrument (fork or chopstick) can be picked up at a time, and the philosopher must have two instruments in order to eat.</div><br />
<br />
<div class="separator" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJD5k2ugoq4QxV6CZ9gpp9iZ_cVkKhX7eshiJQmeluXL1xcoGo-XXmxart0HIPfBlAH2BwIO4G89iemAtFu0wpgmnr2pA8kZbLGZtR9rJFmhuSwlbzO8qsJenOrb84Uts5MviAYxc4Tn0/s1600-h/Din.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJD5k2ugoq4QxV6CZ9gpp9iZ_cVkKhX7eshiJQmeluXL1xcoGo-XXmxart0HIPfBlAH2BwIO4G89iemAtFu0wpgmnr2pA8kZbLGZtR9rJFmhuSwlbzO8qsJenOrb84Uts5MviAYxc4Tn0/s400/Din.bmp" tj="true" /></a></div><div class="separator" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; clear: both; text-align: left;">The philosophers never speak to each other, which creates a dangerous possibility of deadlock in which every philosopher holds a left fork and waits perpetually for a right fork (or vice versa).</div><br />
<br />
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.<br />
<br />
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.<br />
<br />
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.<br />
<br />
<br />
<br />
<strong>Algorithm</strong><br />
<br />
* The algorithm for the process is..........<br />
1.Start.<br />
2.Initialise the thread variables as required.<br />
3.Create the threads representing philosophers.<br />
4.Wait until the threads finishes execution.<br />
5.Stop.<br />
<br />
The algorithm for the philosopher is as..........<br />
1.Start.<br />
2.Wait for the left fork/spoon.<br />
3.Wait for the right fork/spoon.<br />
4.Start eating.<br />
5 Release the left fork/spoon.<br />
6.Release the right fork/spoon.<br />
7.Stop.<br />
<br />
<strong>Program</strong><br />
<br />
/* Program to implement the solution to dinning philosophers' problem.<br />
* This program uses five thread functions as five philosophers.<br />
* The spoons or forks are represented with five semaphore variables.<br />
* To use shared memory just include the code after the sem_wait() in each thread.<br />
* To compile use 'cc diningphilosopher.c -lpthread'.<br />
* To run './a.out'.<br />
<br />
//inculsion <br />
<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></ stdio.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></ pthread.h=""><//></><//></><//></><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></ pthread.h=""><//></><//></><//></><//></ pthread.h=""><//></><//></><//></><//></><//></><//></><//></><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></ pthread.h=""><//></><//></><//></><//></ pthread.h=""><//></><//></><//></><//></><//></><//></><//></><//></ pthread.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
#include<semaphore.h></semaphore.h></semaphore.h><//semaphore.h></semaphore.h><//semaphore.h></ semaphore.h=""><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></ semaphore.h=""><//></><//></><//></><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></ semaphore.h=""><//></><//></><//></><//></ semaphore.h=""><//></><//></><//></><//></><//></><//></><//></><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></ semaphore.h=""><//></><//></><//></><//></ semaphore.h=""><//></><//></><//></><//></><//></><//></><//></><//></ semaphore.h=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//><br />
//macro definition<br />
#define cls() printf("\033[H\033[J")<br />
#define EATINGTIME 1<br />
<br />
//function prototype for the thread function.<br />
void * philosopher1();<br />
void * philosopher2();<br />
void * philosopher3();<br />
void * philosopher4();<br />
void * philosopher5();<br />
<br />
//global decalration of semaphore variables.<br />
sem_t sem15,sem12,sem23,sem34,sem45;<br />
<br />
//global variable to control the execution of main.<br />
int end=0;<br />
int main()<br />
{<br />
char a[2];<br />
pthread_t t1,t2,t3,t4,t5;<br />
pthread_attr_t at1;<br />
pthread_attr_init(&at1);<br />
pthread_attr_setdetachstate(&at1,PTHREAD_CREATE_DETACHED);<br />
sem_init(&sem15,0,1);<br />
sem_init(&sem12,0,1);<br />
sem_init(&sem23,0,1);<br />
sem_init(&sem34,0,1);<br />
sem_init(&sem45,0,1);<br />
cls();<br />
printf("\n\n\n\n\n\n");<br />
printf("____________________________________________________________________");<br />
printf("\n\n\n\t\t\tDINNING PHILOSOPHER PROBLEM.");<br />
printf("\n\t\t\t____________________________");<br />
printf("\n\n\t\tNO. OF PHILOSOPHERS :5");<br />
printf("\n\n\t\tNO. OF FORKS(SPOONS):5\n\n"); printf("___________________________________");<br />
printf("\n\n\n\t\tPRESS ENTER TO CONTINUE.....................");<br />
fgets(a,2,stdin);<br />
cls();<br />
pthread_create(&t1,&at1,philosopher1,NULL);<br />
pthread_create(&t2,&at1,philosopher2,NULL);<br />
pthread_create(&t3,&at1,philosopher3,NULL);<br />
pthread_create(&t4,&at1,philosopher4,NULL);<br />
pthread_create(&t5,&at1,philosopher5,NULL);<br />
while(end!=5)<br />
{<br />
}<br />
}<br />
<br />
//definition of philosopher1.<br />
void * philosopher1()<br />
{<br />
int i=0;<br />
printf("\n\t\tPHILOSOPHER-1 THINKING.\n");<br />
while(i<eatingtime)< p=""></eatingtime)<></eatingtime)<><//eatingtime)<></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> <br />
{<br />
sleep(1);<br />
<br />
//waiting to acquire the forks.<br />
sem_wait(&sem15);<br />
sem_wait(&sem12);<br />
printf("\n\t\t\t* PHILOSOPHER-1 EATING.*\n");<br />
sleep(1);<br />
<br />
//releasing the forks<br />
sem_post(&sem15);<br />
sem_post(&sem12);<br />
printf("\n\t\tPHILOSOPHER-1 THINKING.\n");<br />
i++;<br />
}<br />
end++;<br />
}<br />
<br />
//philosopher2.<br />
void * philosopher2()<br />
{<br />
int i=0;<br />
printf("\n\t\tPHILOSOPHER-2 THINKING.\n");<br />
while(i<eatingtime)< p=""></eatingtime)<></eatingtime)<><//eatingtime)<></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> <br />
{<br />
sleep(1);<br />
sem_wait(&sem12);<br />
sem_wait(&sem23);<br />
printf("\n\t\t\t* PHILOSOPHER-2 EATING.*\n");<br />
sleep(1);<br />
sem_post(&sem12);<br />
sem_post(&sem23);<br />
printf("\n\t\tPHILOSOPHER-2 THINKING.\n");<br />
i++;<br />
}<br />
end++;<br />
}<br />
<br />
//philosopher 3<br />
void * philosopher3()<br />
{<br />
int i=0;<br />
printf("\n\t\tPHILOSOPHER-3 THINKING.\n");<br />
while(i<eatingtime)< p=""></eatingtime)<></eatingtime)<><//eatingtime)<></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> <br />
{<br />
sleep(1);<br />
sem_wait(&sem23);<br />
sem_wait(&sem34);<br />
printf("\n\t\t\t* PHILOSOPHER-3 EATING.*\n");<br />
sleep(1);<br />
sem_post(&sem23);<br />
sem_post(&sem34);<br />
printf("\n\t\tPHILOSOPHER-3 THINKING.\n");<br />
i++;<br />
}<br />
end++;<br />
}<br />
<br />
//philosopher 4.<br />
void * philosopher4()<br />
{<br />
int i=0;<br />
printf("\n\t\tPHILOSOPHER-4 THINKING.\n");<br />
while(i<eatingtime)< p=""></eatingtime)<></eatingtime)<><//eatingtime)<></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> <br />
{<br />
sleep(1);<br />
sem_wait(&sem34);<br />
sem_wait(&sem45);<br />
printf("\n\t\t\t* PHILOSOPHER-4 EATING.*\n");<br />
sleep(1);<br />
sem_post(&sem34);<br />
sem_post(&sem45);<br />
printf("\n\t\tPHILOSOPHER-4 THINKING.\n");<br />
i++;<br />
}<br />
end++;<br />
}<br />
<br />
//philosopher 5.<br />
void * philosopher5()<br />
{<br />
int i=0;<br />
printf("\n\t\tPHILOSOPHER-5 THINKING.\n");<br />
while(i<eatingtime)< p=""></eatingtime)<></eatingtime)<><//eatingtime)<></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></eatingtime)<><//eatingtime)<></ eatingtime)<=""><//></ eatingtime)<=""><//></><//></ eatingtime)<=""><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></ eatingtime)<=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> <br />
{<br />
sleep(1);<br />
sem_wait(&sem45);<br />
sem_wait(&sem15);<br />
printf("\n\t\t\t* PHILOSOPHER-5 EATING.*\n");<br />
sleep(1);<br />
sem_post(&sem45);<br />
sem_post(&sem15);<br />
printf("\n\t\tPHILOSOPHER-5 THINKING.\n");<br />
i++;<br />
}<br />
end++;<br />
}<br />
<br />
<a href="http://samplecodes.files.wordpress.com/2008/11/33.jpg"><strong>output</strong></a>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-49229290654756786712009-06-15T07:42:00.000-07:002009-06-15T20:30:50.311-07:00Semaphore - Linux Lab<div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>SEMAPHORE</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Theory</strong></div><div style="text-align: justify;">Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s. Dijkstra's model was the operation of railroads: consider a stretch of railroad in which there is a single track over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait before entering the single track until the semaphore is in a state that permits travel. When the train enters the track, the semaphore changes state to prevent other trains from entering the track. A train that is leaving this section of track must again change the state of the semaphore to allow another train to enter. In the computer version, a semaphore appears to be a simple integer. A process (or a thread) waits for permission to proceed by waiting for the integer to become 0. The signal if it proceeds signals that this by performing incrementing the integer by 1. When it is finished, the process changes the semaphore's value by subtracting one from it. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Semaphore is a variable that can take only the values 0 and 1, binary semaphore. This is the most common form. Semaphore that can take many positive values are called general semaphores.</div><div style="text-align: justify;"></div><div style="text-align: justify;">The definition of P and V are surprisingly simple. Suppose we have semaphore variable sv. The two operations are defined as follows:</div><div style="text-align: justify;"></div><div style="text-align: justify;">P(sv) – If sv is greater than zero, decrement sv, if sv is zero, suspend execution of this process.</div><div style="text-align: justify;">V(sv) – If some other process has been suspend waiting for sv, make it resume execution. If no process is suspended waiting for sv, Increment sv.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Semaphores let processes query or alter status information. They are often used to monitor and control the availability of system resources such as shared memory segments. </div><div style="text-align: justify;"></div><div style="text-align: justify;">POSIX semaphores are much lighter weight than are System V semaphores. A POSIX semaphore structure defines a single semaphore, not an array of up to twenty five semaphores. The POSIX semaphore functions are: </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_open() -- Connects to, and optionally creates, a named semaphore </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_init() -- Initializes a semaphore structure (internal to the calling program, so not a named semaphore). </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_close() -- Ends the connection to an open semaphore. </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_unlink() -- Ends the connection to an open semaphore and causes the semaphore to be removed when the last process closes it. </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_destroy() -- Initializes a semaphore structure (internal to the calling program, so not a named semaphore). </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_getvalue() -- Copies the value of the semaphore into the specified integer. </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_wait(), sem_trywait() -- Blocks while the semaphore is held by other processes or returns an error if the semaphore is held by another process. </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_post() -- Increments the count of the semaphore. </div><div style="text-align: justify;"></div><div style="text-align: justify;">All POSIX semaphore functions and types are prototyped or defined in semaphore.h. To define a semaphore object, use </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_t sem_name;</div><div style="text-align: justify;"></div><div style="text-align: justify;">To initialize a semaphore, use sem_init(): </div><div style="text-align: justify;"></div><div style="text-align: justify;">int sem_init(sem_t *sem, int pshared, unsigned int value);</div><div style="text-align: justify;"></div><div style="text-align: justify;">• sem points to a semaphore object to initialize </div><div style="text-align: justify;">• pshared is a flag indicating whether or not the semaphore should be shared with fork()ed processes. LinuxThreads does not currently support shared semaphores </div><div style="text-align: justify;">• value is an initial value to set the semaphore to </div><div style="text-align: justify;"></div><div style="text-align: justify;">Example of use: </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_init(&sem_name, 0, 10);</div><div style="text-align: justify;"></div><div style="text-align: justify;">________________________________________</div><div style="text-align: justify;"></div><div style="text-align: justify;">To wait on a semaphore, use sem_wait: </div><div style="text-align: justify;"></div><div style="text-align: justify;">int sem_wait(sem_t *sem);</div><div style="text-align: justify;"></div><div style="text-align: justify;">Example of use: </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_wait(&sem_name); </div><div style="text-align: justify;"></div><div style="text-align: justify;">• If the value of the semaphore is negative, the calling process blocks; one of the blocked processes wakes up when another process calls sem_post.</div><div style="text-align: justify;"></div><div style="text-align: justify;">________________________________________</div><div style="text-align: justify;"></div><div style="text-align: justify;">To increment the value of a semaphore, use sem_post: </div><div style="text-align: justify;"></div><div style="text-align: justify;">int sem_post(sem_t *sem);</div><div style="text-align: justify;"></div><div style="text-align: justify;">Example of use: </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_post(&sem_name); </div><div style="text-align: justify;"></div><div style="text-align: justify;">• It increments the value of the semaphore and wakes up a blocked process waiting on the semaphore, if any. </div><div style="text-align: justify;"></div><div style="text-align: justify;">________________________________________</div><div style="text-align: justify;"></div><div style="text-align: justify;">To find out the value of a semaphore, use </div><div style="text-align: justify;"></div><div style="text-align: justify;">int sem_getvalue(sem_t *sem, int *valp);</div><div style="text-align: justify;"></div><div style="text-align: justify;">• gets the current value of sem and places it in the location pointed to by valp </div><div style="text-align: justify;"></div><div style="text-align: justify;">Example of use: </div><div style="text-align: justify;"></div><div style="text-align: justify;">int value; </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_getvalue(&sem_name, &value); </div><div style="text-align: justify;"></div><div style="text-align: justify;">printf("The value of the semaphors is %d\n", value);</div><div style="text-align: justify;"></div><div style="text-align: justify;">________________________________________</div><div style="text-align: justify;"></div><div style="text-align: justify;">To destroy a semaphore, use </div><div style="text-align: justify;"></div><div style="text-align: justify;">int sem_destroy(sem_t *sem);</div><div style="text-align: justify;"></div><div style="text-align: justify;">• destroys the semaphore; no threads should be waiting on the semaphore if its destruction is to succeed. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Example of use: </div><div style="text-align: justify;"></div><div style="text-align: justify;">sem_destroy(&sem_name); </div><div style="text-align: justify;">________________________________________</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Program</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">/* Program to demonstrate the usage of semaphore variable in controlling the access to specific resources.</div><div style="text-align: justify;">* This program contains two functions which display their own messages.</div><div style="text-align: justify;">* The display is suitably controlled by the use of semaphore variable.</div><div style="text-align: justify;">* It is a program in which a semaphore variable is shared between the main function and a thread function.</div><div style="text-align: justify;">* This module can be compiled using 'cc semaphore1.c -lpthread' and executed './a.out'</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">//inculsion part. </div><div style="text-align: justify;"></div><div style="text-align: justify;">#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></div><div style="text-align: justify;">#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></ pthread.h=""><//></><//></><//></><//></div><div style="text-align: justify;">#include<semaphore.h></semaphore.h></semaphore.h><//semaphore.h></semaphore.h><//semaphore.h></ semaphore.h=""><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></ semaphore.h=""><//></><//></><//></><//></div><div style="text-align: justify;"></div><div style="text-align: justify;">//function prototype for the thread function.</div><div style="text-align: justify;">void * display_function();</div><div style="text-align: justify;"></div><div style="text-align: justify;">//global semaphore variable to be shared.</div><div style="text-align: justify;">sem_t sem_var;</div><div style="text-align: justify;">int main()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">pthread_t tid;</div><div style="text-align: justify;">pthread_attr_t attr;</div><div style="text-align: justify;">int i;</div><div style="text-align: justify;"></div><div style="text-align: justify;">//initialising the semaphore variable with an initial value 0 (third arguement.)</div><div style="text-align: justify;">sem_init(&sem_var,0,0);</div><div style="text-align: justify;">pthread_attr_init(&attr);</div><div style="text-align: justify;">pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);</div><div style="text-align: justify;">pthread_create(&tid,&attr,display_function,NULL);</div><div style="text-align: justify;">for(i=0;i<5;i++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\n Displaying from the main function:\t%d\n",i+1);</div><div style="text-align: justify;"></div><div style="text-align: justify;">//waiting for the resources. </div><div style="text-align: justify;">sem_wait(&sem_var); </div><div style="text-align: justify;">}</div><div style="text-align: justify;"></div><div style="text-align: justify;">//destroying the semaphore variable.</div><div style="text-align: justify;">sem_destroy(&sem_var);</div><div style="text-align: justify;">}</div><div style="text-align: justify;">void * display_function()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int i;</div><div style="text-align: justify;">for(i=0;i<5;i++)</div><div style="text-align: justify;">{</div><div style="text-align: justify;">printf("\n***** Displaying from the thread function.:\t%d\n",i+1);</div><div style="text-align: justify;"></div><div style="text-align: justify;">//releasing resources.</div><div style="text-align: justify;">sem_post(&sem_var);</div><div style="text-align: justify;">sleep(1);</div><div style="text-align: justify;">}</div><div style="text-align: justify;">}</div><div style="text-align: justify;"></div><div style="text-align: justify;"><a href="http://samplecodes.files.wordpress.com/2008/11/15.jpg"><strong>Output</strong></a></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>SERIALISABILITY PROBLEM.</strong></div><div style="text-align: justify;">(By using Semaphore Variable)</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Program</strong></div><div style="text-align: justify;">* This program uses a variable 'data' whose value is used by two thread function.</div><div style="text-align: justify;">* But the condition is that when a function uses it no other function should use it.</div><div style="text-align: justify;">* This problem is tackled by definig two thread function and suitably making them access it by the use of semaphore variable.</div><div style="text-align: justify;">* This module can be compiled using 'cc semaphore2.c -lpthread' and executed './a.out'</div><div style="text-align: justify;"></div><div style="text-align: justify;">//inculsion part.</div><div style="text-align: justify;">#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></stdio.h><//stdio.h></ stdio.h=""><//></ stdio.h=""><//></><//></ stdio.h=""><//></><//></><//></><//></div><div style="text-align: justify;">#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></pthread.h><//pthread.h></ pthread.h=""><//></ pthread.h=""><//></><//></ pthread.h=""><//></><//></><//></><//></div><div style="text-align: justify;">#include<semaphore.h></semaphore.h></semaphore.h><//semaphore.h></semaphore.h><//semaphore.h></ semaphore.h=""><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></semaphore.h><//semaphore.h></ semaphore.h=""><//></ semaphore.h=""><//></><//></ semaphore.h=""><//></><//></><//></><//></div><div style="text-align: justify;"></div><div style="text-align: justify;">//prototype for thread functions.</div><div style="text-align: justify;">void * thread_function1();</div><div style="text-align: justify;">void * thread_function2();</div><div style="text-align: justify;"></div><div style="text-align: justify;">//global variables.</div><div style="text-align: justify;">int data=0,end=0;</div><div style="text-align: justify;"></div><div style="text-align: justify;">//global semaphore variables.</div><div style="text-align: justify;">sem_t sem_var;</div><div style="text-align: justify;">int main()</div><div style="text-align: justify;">{<br />
pthread_t t1,t2;</div><div style="text-align: justify;">pthread_attr_t attr;</div><div style="text-align: justify;">pthread_attr_init(&attr);</div><div style="text-align: justify;">pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);</div><div style="text-align: justify;">sem_init(&sem_var,0,1); </div><div style="text-align: justify;">pthread_create(&t1,&attr,thread_function1,NULL);</div><div style="text-align: justify;">pthread_create(&t2,&attr,thread_function2,NULL);</div><div style="text-align: justify;">while(end!=2)</div><div style="text-align: justify;">{<br />
}</div><div style="text-align: justify;">printf("\n\t****** The value of DATA is:\t%d ******\n",data);</div><div style="text-align: justify;">}</div><div style="text-align: justify;"></div><div style="text-align: justify;">//definition of first thread function.</div><div style="text-align: justify;">void * thread_function1()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int a;</div><div style="text-align: justify;">printf("\nEntered into thread function:1\n");</div><div style="text-align: justify;">printf("\nThread function:1 waiting to gain access\n");</div><div style="text-align: justify;">sleep(1);</div><div style="text-align: justify;">sem_wait(&sem_var);</div><div style="text-align: justify;">printf("\nAccess gained by thread function:1\n");</div><div style="text-align: justify;">printf("\nThread function:1 using the value of 'DATA' \n");</div><div style="text-align: justify;">a=data;</div><div style="text-align: justify;">a=a+1;</div><div style="text-align: justify;">data=a;</div><div style="text-align: justify;">sem_post(&sem_var);</div><div style="text-align: justify;">printf("\nResources released by thread function:1\n");</div><div style="text-align: justify;">end++;</div><div style="text-align: justify;">}</div><div style="text-align: justify;"></div><div style="text-align: justify;">//definition of second thread function.</div><div style="text-align: justify;">void * thread_function2()</div><div style="text-align: justify;">{</div><div style="text-align: justify;">int b;</div><div style="text-align: justify;">printf("\nEntered into thread function:2\n");</div><div style="text-align: justify;">printf("\nThread function:2 waiting to gain access\n");</div><div style="text-align: justify;">sleep(1);</div><div style="text-align: justify;">sem_wait(&sem_var);</div><div style="text-align: justify;">printf("\nAccess gained by thread function:2\n");</div><div style="text-align: justify;">printf("\nThread function:2 using the value of 'DATA' \n");</div><div style="text-align: justify;">b=data;</div><div style="text-align: justify;">b=b+1;</div><div style="text-align: justify;">data=b;</div><div style="text-align: justify;">sem_post(&sem_var);</div><div style="text-align: justify;">printf("\nResources released by thread function:2\n");</div><div style="text-align: justify;">end++;</div><div style="text-align: justify;">}</div><div style="text-align: justify;"><strong><a href="http://samplecodes.files.wordpress.com/2008/11/24.jpg">Output</a></strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-40153870081340014682009-06-15T07:07:00.000-07:002009-06-15T20:31:26.815-07:00Thread - Linux Lab<strong>Thread</strong><br />
<br />
<strong>Theory</strong><br />
<br />
Multiple strands of execution in a single program are called threads or thread is a sequences of control within a process.Thread proces include thread creation, termination, synchronization (joins,blocking), scheduling and process interaction.All threads within a process share the same addresss space.<br />
<br />
A thread in computer science is short for a thread of execution. Threads are a way for a program to fork (or split) itself into two or more simultaneously (or pseudo-simultaneously) running tasks. Threads and processes differ from one operating system to another, but in general, the way that a thread is created and shares its resources is different from the way a process does.<br />
<br />
Multiple threads can be executed in parallel on many computer systems. This multithreading generally occurs by time slicing, wherein a single processor switches between different threads, in which case the processing is not literally simultaneous, for the single processor is only really doing one thing at a time. This switching can happen so fast as to give the illusion of simultaneity to an end user.<br />
<br />
Threads are distinguished from traditional multi-tasking operating system processes in that processes are typically independent, carry considerable state information, have separate address spaces, and interact only through system-provided inter-process communication mechanisms. Multiple threads, on the other hand, typically share the state information of a single process, and share memory and other resources directly. Context switching between threads in the same process is typically faster than context switching between processes. Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes; in other operating systems there is not so great a difference.<br />
<br />
<strong>Thread Basics</strong><br />
<br />
• Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction. <br />
• A thread does not maintain a list of created threads, nor does it know the thread that created it. <br />
• All threads within a process share the same address space. <br />
• Threads in the same process share: <br />
<br />
o Process instructions <br />
o Most data <br />
o open files (descriptors) <br />
o signals and signal handlers <br />
o current working directory <br />
o User and group id <br />
<br />
• Each thread has a unique: <br />
<br />
o Thread ID <br />
o set of registers, stack pointer <br />
o stack for local variables, return addresses <br />
o signal mask <br />
o priority <br />
o Return value: errno <br />
<br />
• pthread functions return "0" if OK. <br />
<br />
<strong>Thread Creation</strong><br />
Function call : pthread_create<br />
int pthread_create(pthread_t*thread,pthread_attr_t*attr,void *(*start_routine) (void*),void *arg);<br />
<br />
<strong>Arguments</strong><br />
<br />
->thread - returns the thread id.(unsigned long int defined in bits/pthreadtypes.h)<br />
->attr-Set to NULL if default thread attributes are used.(else define members of the struct pthread_attr_t defined in bits/pthreadtypes.h). Attributes include:<br />
<br />
• detached state(joinable? Default: PTHREAD_CREATE_JOINABLE.Other option:PTHREAD_CREATE_DETACHED)<br />
• schedulingpolicy(realtime?PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)<br />
• scheduling parameter<br />
• inheritsched attribute(Default:PTHREAD_EXPLICIT_SCHED,inherit from parent thread:PTHREAD_INHERIT_SCHED)<br />
<br />
• scope(Kernelthreads:PTHREAD_SCOPE_SYSTEM user threads PTHREAD_SCOPE_PROCESS, pick one or the other not both)<br />
• guard size<br />
• stack address<br />
• stack size(default minimum PTHREAD_STACK_SIZE set in pthread.h)<br />
<br />
->void *(start_routine)-pointer to the function to be threaded.Function has a single argument:pointer to void.<br />
->*arg-pointer to argument of function. To pass multiple arguments,sent a pointer to the structure. <br />
<br />
<strong>Termination Of Thread</strong><br />
<br />
Function call: pthread_exit<br />
void pthread_exit(void *retval);<br />
<br />
<strong>Arguments </strong><br />
<br />
retval – return value of the thread.<br />
This routine kills the thread.The pthread _exit never returns. If the thread is not detached the thread id and return value may be examined from another thread by using pthread_join.<br />
<br />
<strong>Thread Synchronization</strong><br />
<br />
The thread libraries provide three synchronization mechanisms<br />
<br />
• mutexes – Mutual exclusion lock: Block access to variable by other threads. This enforces exclusive access by thread to a variable or set of variables.<br />
• joins–Make athread wait till others are complete(terminated).<br />
• conditions variables- data type pthread_cond_t.<br />
<br />
<strong>Benefits of Threads vs Processes</strong> <br />
<br />
If implemented correctly then threads have some advantages of (multi) processes, They take: <br />
• Less time to create a new thread than a process, because the newly created thread uses the current process address space. <br />
• Less time to terminate a thread than a process. <br />
• Less time to switch between two threads within the same process, partly because the newly created thread uses the current process address space. <br />
• Less communication overheads -- communicating between the threads of one process is simple because the threads share everything: address space, in particular. So, data produced by one thread is immediately available to all the other threads. <br />
<br />
POSIX Threads is a POSIX standard for threads. The standard defines an API for creating and manipulating threads.<br />
<br />
Libraries implementing the POSIX Threads standard are often named Pthreads. Pthreads are most commonly used on POSIX systems such as Linux and Unix,<br />
<br />
<strong>Algorithm</strong><br />
<br />
1. Start.<br />
2. Provide function prototype and definition for the function that is used as thread.<br />
3. Declare variables for storing the thread id and thread attributes.<br />
4. Initialise the thread attributes.<br />
5. Create the thread.<br />
6. Call the thread for execution.<br />
7. Wait for the thread to execute.<br />
8. Stop.<br />
<br />
<br />
<br />
Program<br />
<br />
/*Program for the creation of simple thread created in a non detached manner.<br />
* This program illustrates the manner in which a thread can be created from within the main() function.<br />
* This program creates a thread called 'add' which accepts two integer values from the user and prints the sum of it.<br />
* NOTE:->This program should be compiled using the command 'cc simplethread.c -lpthread' and executed using './a.out'.*/<br />
<br />
//inculsion part<br />
<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//><br />
#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//><br />
void* add(void*); //prototype of the thread function.<br />
int main() //main function (return type should be int in linux). <br />
{<br />
int error; //variable to check error (optional)<br />
pthread_t thread_id; //variable to store thread id.<br />
pthread_attr_t thread_attribute; //variable to store properties of thread.<br />
pthread_attr_init(&thread_attribute); //function call for setting the default properties.<br />
error=pthread_create(&thread_id,&thread_attribute,add,NULL); //function for thread creation.<br />
<br />
//error checking (optional)<br />
if(error!=0)<br />
{<br />
printf("\nError in thread creation.\n");<br />
exit(0);<br />
}<br />
error=pthread_join(thread_id,NULL); //function for thread calling.<br />
<br />
//error checking (optional)<br />
if(error!=0) <br />
{ printf("\nError in thread joining.\n");<br />
exit(0);<br />
}<br />
} <br />
//function definition for the thread function.<br />
void * add(void *args)<br />
{ int a,b;<br />
printf("\nEnter the first number.\n");<br />
scanf("%d",&a);<br />
printf("\nEnter the second number.\n");<br />
scanf("%d",&b);<br />
printf("\nThe sum is:\t%d\n",a+b);<br />
}<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/1.jpg">Output</a></strong><br />
<br />
<strong>MULTIPLE THREAD</strong><br />
<br />
<strong>Algorithm</strong><br />
<br />
<br />
1. Start.<br />
2. Provide function prototype and definition for the function that has to be used as thread.<br />
3. Declare the variables for storing the thread id and thread attributes as required depending on the no: of threads to be created.<br />
4. Initialize the thread attribute variables to their default values.<br />
5. Set detach property of the thread.<br />
6. Create the threads as required.<br />
7. Wait for the thread to execute.<br />
8. Stop.<br />
<br />
<strong>Program</strong><br />
<br />
//Inculsion part.<br />
<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//><br />
#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//><br />
<br />
//function prototype.<br />
void * add(void *);<br />
<br />
//Variable to count the executed thread.<br />
int end;<br />
<br />
//main function.<br />
int main()<br />
{<br />
pthread_t tid1,tid2;<br />
pthread_attr_t attribute;<br />
pthread_attr_init(&attribute);<br />
<br />
//function to set detach property of the thread.<br />
pthread_attr_setdetachstate(&attribute,PTHREAD_CREATE_DETACHED);<br />
pthread_create(&tid1,&attribute,add,(void *)1);<br />
pthread_create(&tid2,&attribute,add,(void*)2);<br />
<br />
//to wait for the thread to finish execution.<br />
while(end<2)<br />
{<br />
}<br />
}<br />
<br />
//function prototype for the thread function.<br />
void * add(void * args)<br />
{<br />
int a,b;<br />
printf("\nEntered into %d thread\n",args);<br />
printf("\nEnter the first number for thread %d\n",args);<br />
scanf("%d",&a);<br />
printf("\nEnter the second number for thread %d\n",args);<br />
scanf("%d",&b);<br />
printf("The sum computed by thread %d:\t%d\n",args,a+b);<br />
end++;<br />
}<br />
<strong><a href="http://samplecodes.files.wordpress.com/2008/11/2.jpg">output</a></strong><br />
<br />
<strong>DYNAMIC MULTITHREAD CREATION</strong><br />
<br />
<br />
<strong>Algorithm</strong><br />
1. Start.<br />
2. Provide the function prototype and definition for the function that has to be used as thread.<br />
3. Declare variables to store the thread id and thread attributes.<br />
4. Accept the no. of threads from the user.<br />
5. Create the threads by suitably initialising the thread variables.<br />
6. Wait until the threads execute.<br />
7. Stop.<br />
<br />
<strong>Program</strong><br />
<br />
/*Program to describe dynamic multiple thread creation and passing arguments to it. <br />
* This program illustrates the steps involved in creating multiple threads dynamically.<br />
* This program accepts the number of threads from the user and creates it at run time.<br />
* The procedure is similar to that we used to create multiple threads.<br />
<br />
NOTE:->This module can be compiled using the command 'cc dynamic multithread creation.c -lpthread' and executed using './a.out'<br />
<br />
//inculsion.<br />
#include<stdio.h></stdio.h></stdio.h><//stdio.h></stdio.h><//stdio.h></ stdio.h=""><//><br />
#include<pthread.h></pthread.h></pthread.h><//pthread.h></pthread.h><//pthread.h></ pthread.h=""><//><br />
//function prototype.<br />
void * thread_function(void *args[]);<br />
//variable to know the end of threads execution.<br />
int end;<br />
int main()<br />
{<br />
int i,count,j;<br />
int args[2];<br />
pthread_attr_t attribute;<br />
pthread_t tid;<br />
pthread_attr_init(&attribute);<br />
pthread_attr_setdetachstate(&attribute,PTHREAD_CREATE_DETACHED);<br />
<br />
//for accepting the number of threads to be created.<br />
printf("\nEnter the number of threads.\n");<br />
scanf("%d",&count);<br />
for(i=0,j=count;i<count;i++,j--)< p=""></count;i++,j--)<></count;i++,j--)<><//count;i++,j--)<></count;i++,j--)<><//count;i++,j--)<></ count;i++,j--)<=""><//> <br />
{<br />
<br />
//inserting the arguments for the thread <br />
args[0]=i+1;<br />
args[1]=j;<br />
printf("\n\t\tCreating thread %d.\n",i+1); <br />
pthread_create(&tid,&attribute,(void *)thread_function,(void *)args);<br />
printf("\n\t\tCreated thread %d\n",i+1);<br />
sleep(1);<br />
}<br />
<br />
//waiting for the thread execution.<br />
while(end!=count)<br />
{<br />
}<br />
}<br />
<br />
//function definition.<br />
void * thread_function(void *args[])<br />
{<br />
int a,b;<br />
a=(int)args[0];<br />
b=(int)args[1];<br />
printf("\n\t\tEntered into %d thread.\n",a);<br />
printf("\nFirst value obtained from main function:\t%d\n",a);<br />
printf("\nSecond value obtained from main function:\t%d\n",b);<br />
end++;<br />
}<br />
<br />
<a href="http://samplecodes.files.wordpress.com/2008/11/31.jpg"><strong>Output</strong></a>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-83773178256534650502009-06-11T00:41:00.000-07:002009-06-11T07:03:30.450-07:00How to take seminars effectively?<div style="text-align: justify;"></div><div style="text-align: justify;">Seminar presentations are an integral part of most units, and they require skills that need to be learnt and rehearsed. Being able to give a good and clear presentation to a public audience is an asset you - and your future employer - will greatly value. Oral skills, alongside writing and research skills, teamwork, and time management, are aspects of your degree course, or key skills, which will have application to your future career in whatever field that may be. Future employers always ask for these key skills in references, and they are often seen as more important than the subject of your degree.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><br />
<strong><span style="font-size: large;">Design and delivery guidelines</span></strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The following points may be useful in designing and giving a seminar presentation:</div><div style="text-align: justify;"></div><div style="text-align: justify;">• A seminar presentation is a means of <strong>communication</strong> with a <strong>specific audience</strong> (in the case of seminar presentations, with your fellow students). It is NOT a monologue, delivered to a lecturer, to prove to him/her that you’ve done your homework.</div><div style="text-align: justify;">• A seminar presentation is an <strong>oral presentation</strong>. It is NOT a written essay that is simply read out from paper. If you don’t feel confident enough to speak completely freely, this would be ideal, and then use notes/bullet points on prompt cards. Make sure that your presentation uses English as it is <strong>spoken</strong> (so no longwinded sentences of the kind that you are likely to read), but also that the language fits the occasion (so: neither too formal, nor too casual).</div><div style="text-align: justify;">• Speak in a lively and <strong>engaged</strong> way, so that you avoid monotonous delivery. If you give the impression that you are not particularly interested in your subject, no one else will be either (particularly if the session is early in the morning or late in the day). If in a large room, try to <strong>project</strong> your presentation, that is: speak loudly enough.</div><div style="text-align: justify;">• Don’t speak too quickly, but keep your pace with your audience and allow your material to ‘sink in’.</div><div style="text-align: justify;">• Make frequent <strong>eye contact</strong> with your fellow students. Address them as your audience - not just the lecturer.</div><div style="text-align: justify;">• If at all possible, stand up while giving your presentation; if you prefer to sit down, try not to look down too much, or hide behind your notes. Choose a seat where you face your audience, rather than blend into it.</div><div style="text-align: justify;">• At the beginning of your presentation, <strong>outline</strong> in a few words the <strong>aims</strong> of your presentation. When doing a joint presentation, the first speaker should explain how the different parts will work together. It is therefore absolutely essential that you co-ordinate your part of the presentation with your co-presenters in advance, so that you avoid overlaps, or an overall disjointed presentation.</div><div style="text-align: justify;">• Distribute a prepared <strong>handout</strong>. This handout should give a run down of your presentation, preferably numbered or in bullet points and it should have a title. In any case it should be structured, and easy to read/follow. A handout is NOT identical with your notes, but a condensation of your presentation, so don’t have more than one to two A4 pages. Use illustrations only if they relate to your argument or if you refer to them.</div><div style="text-align: justify;">• If you use (particularly lengthy) <strong>quotations</strong> from secondary sources, print them in full on your handout, as your audience will find it easier to follow them than if you just recite them. When you come to these quotes in your presentation, tell your audience they can find them on their handout.</div><div style="text-align: justify;">• Also list on your handout all <strong>names</strong> and <strong>specific terms</strong> you mention in your presentation, particularly those that your audience may find difficult to note down without seeing them spelt out (i.e. foreign names, words, etc.).</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong><span style="font-size: large;">Illustrating your argument</span></strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">• A presentation is usually a timed exercise, and you must therefore <strong>select</strong> your material carefully so that it fits into the time allocated. Depending on your time, choose a selected number of <strong>key points</strong>, rather than attempt to cram everything you’ve read into your presentation. If there is too much, this will be likely to result in information overload for your audience.</div><div style="text-align: justify;">• Concentrate on <strong>arguments</strong> or <strong>developments</strong>, rather than simple facts. The presentation should encourage your audience to think, and to follow this up with a discussion.</div><div style="text-align: justify;">• To facilitate a subsequent discussion, you may want to end your presentation with a number of <strong>conclusions</strong>, or even better with a set of <strong>questions</strong> that emerge from your research. This is particularly important when you are dealing, for example, with theoretical arguments or texts, which you may not agree with, or which you don’t fully understand. Don’t try to gloss over this, but use it instead as a way into the discussion with your audience. For example: </div><div style="text-align: justify;"><em> I don’t think I fully understand what X means when s/he argues... How did you interpret this...? What do you make of...?</em></div><div style="text-align: justify;"></div><div style="text-align: justify;">•This will help clarify matters both for you and your fellow students who may indeed have similar problems. </div><div style="text-align: justify;">• Aim to make your presentation more interesting by using <strong>visual aids</strong>, such as <strong>OHP</strong> transparencies. The most important thing about these is that they need to be tied in with your presentation:</div><div style="text-align: justify;">• Visual aids are used to illustrate a point that you have made (or are about to make) in your presentation. Avoid using them as a kind of visual wallpaper, for example by ending your talk by showing a transparency without commenting on it.</div><div style="text-align: justify;">• Choose transparencies to display items such as key points, graphs, grids, statistics, illustrations, and photos.</div><div style="text-align: justify;">• If you use video clips, use them economically: don’t show about ten minutes when all you are talking about can be justified by a shorter extract. If you have a limited time for your presentation, use perhaps only one to three clips.</div><div style="text-align: justify;">• Have your visual aids ready to use, and in the right order. </div><div style="text-align: justify;">• Introduce visual aids and speak to them. For example, you could say: I am now going to show you... What I want to show you here is... </div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong><span style="font-size: large;">Preparing yourself for the presentation</span></strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">• Before the presentation: make sure you are familiar with the room where you are giving your presentation, and with any audio-visual equipment you will be using. Make sure you are close to your equipment, and that you face the audience. </div><div style="text-align: justify;">• It might be a good idea to rehearse your presentation at home. If possible, ask friends/house mates to listen to your performance, and ask them for advice which parts may still be a bit unclear.</div><div style="text-align: justify;">• Time your presentation with a watch, to make sure you are within the limits you’ve been given.</div><div style="text-align: justify;">• Do your last rehearsal at the latest the evening before the presentation. Have a good night’s sleep, avoid getting anxious, and don’t think about it until the moment of the presentation arrives. And do remember, your seminar tutor is always there in case you get stuck.</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><br />
<strong>Seminars useful links</strong><br />
<a href="http://www.seminartopics.net/">http://www.seminartopics.net/</a><br />
<a href="http://www.bestneo.com/">http://www.bestneo.com/</a><br />
<a href="http://www.freshersworld.com/resources/sp/mechsem.htm">http://www.freshersworld.com/resources/sp/mechsem.htm</a><br />
<a href="http://www.seminartopics.in/">http://www.seminartopics.in/</a><br />
<a href="http://www.college-seminars.com/">http://www.college-seminars.com/</a><br />
<br />
<strong>Computer topics</strong><br />
<a href="http://www.seminartopic.org/seminars.php?catId=8&pageNumber=0">http://www.seminartopic.org/seminars.php?catId=8&pageNumber=0</a><a href="http://draft.blogger.com/"></a><br />
<a href="http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Computer%20Science">http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Computer%20Science</a><br />
<a href="http://www.seminarsonly.com/computer%20science/computer%20seminar%20topics.php">http://www.seminarsonly.com/computer%20science/computer%20seminar%20topics.php</a><br />
<a href="http://www.computerseminartopics.net/">http://www.computerseminartopics.net/</a><br />
<br />
<strong>Electronics topics</strong><br />
<a href="http://www.seminartopic.org/seminars.php?catId=9&pageNumber=0">http://www.seminartopic.org/seminars.php?catId=9&pageNumber=0</a><br />
<a href="http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Electronic%20Science">http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Electronic%20Science</a><br />
<a href="http://www.seminarsonly.com/electronics/electronics%20seminar%20topics.php">http://www.seminarsonly.com/electronics/electronics%20seminar%20topics.php</a><br />
<a href="http://www.electronicsseminartopics.com/">http://www.electronicsseminartopics.com/</a><br />
<br />
<strong>Civil topics</strong><br />
<a href="http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Civil%20Engineering">http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Civil%20Engineering</a><br />
<br />
<br />
<strong>Electrical and Electronics topics</strong><br />
<a href="http://www.seminarsonly.com/nursing/Electrical%20&%20Electronics%20Seminar%20Topics.php">http://www.seminarsonly.com/nursing/Electrical%20&%20Electronics%20Seminar%20Topics.php</a><br />
<br />
<strong>Mechanical topics</strong><br />
<a href="http://www.seminartopic.org/seminars.php?catId=15&pageNumber=0">http://www.seminartopic.org/seminars.php?catId=15&pageNumber=0</a><br />
<a href="http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Mechanical%20Engineering">http://www.seminartopics.net/Seminars.php?flag=index&pageNumber=0&category=Mechanical%20Engineering</a><br />
<a href="http://www.seminarsonly.com/mech%20&%20auto/mechanical%20seminar%20topics.php">http://www.seminarsonly.com/mech%20&%20auto/mechanical%20seminar%20topics.php</a>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-86176889538673808772009-06-09T23:38:00.000-07:002009-06-09T23:38:45.311-07:00Java Basics<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTFRiT-elwfV1eOT-zoIQx563B7CtKIhpW6oWaevWaDxA4pMWx9sZqBNUq7W5l-EdmSeDebAEPGBSxDdnew8MAxJbvr7__qHBuS2OvutTPROUws2LBHD3Z3EhqHct1djWN_RPM6XzVJ-s/s1600-h/java.jpg" imageanchor="1" style="clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" fj="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTFRiT-elwfV1eOT-zoIQx563B7CtKIhpW6oWaevWaDxA4pMWx9sZqBNUq7W5l-EdmSeDebAEPGBSxDdnew8MAxJbvr7__qHBuS2OvutTPROUws2LBHD3Z3EhqHct1djWN_RPM6XzVJ-s/s320/java.jpg" /></a><strong></strong></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><strong>Origins of the language</strong></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">► James Gosling and others at Sun, 1990 – 95</div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">► Oak language for “set-top box”</div>► small networked device with television display<br />
► support for graphics<br />
► execution of simple programs<br />
► communication between local program and remote site<br />
► no “expert programmer” to deal with crash, etc.<br />
► Internet application<br />
► simple language for writing programs<br />
<br />
<strong>Design Goals</strong><br />
► Portability<br />
► Internet-wide distribution: PC, Unix, Mac<br />
► Reliability<br />
► Avoid program crashes and error messages<br />
► Safety<br />
► Programmer may be malicious<br />
► Simplicity and familiarity<br />
► Appeal to average programmer; less complex than C++<br />
► Efficiency<br />
<br />
<strong>General Design Decisions</strong><br />
► Simplicity<br />
► Almost everything is an object<br />
► No functions, no multiple inheritance, no go to, no<br />
► Operator overloading<br />
► Portability and Network transfer<br />
► Bytecode interpreter on many platforms<br />
► Reliability and Safety<br />
► Typed source and typed bytecode language<br />
► Run-time type and bounds checks<br />
► Garbage collection<br />
<br />
<strong>Java System</strong><br />
► The Java programming language<br />
► Compiler and run-time system<br />
► Programmer compiles code<br />
► Compiled code transmitted on network<br />
► Receiver executes on interpreter (JVM)<br />
► Safety checks made before/during execution<br />
► Library, including graphics, security, etc.<br />
► Large library made it easier for projects to adopt Java<br />
► Interoperability<br />
► Provision for “native” methods<br />
<br />
<br />
<strong>Applets, Servlets and Applications</strong><br />
► An applet is designed to be embedded in a Web page, and run by a browser<br />
► Applets run in a sandbox with numerous restrictions; for example, they can’t read files and then use the network<br />
► A servlet is designed to be run by a web server<br />
► An application is a conventional program<br />
Building Standalone JAVA Programs <br />
► Prepare the file foo.java using an editor<br />
► Invoke the compiler: javac foo.java<br />
► This creates foo.class<br />
► Run the java interpreter: java foo<br />
<br />
<strong>Java .class Life Cycle</strong><br />
<br />
Programs written in Java are compiled into machine language<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYYVIvbQPHVjHowhb7fomAgkhALwqpCApW3_Nm02B4ztxzk_pSj22WhVX74JCxA3JpKvVR0PcLuPJdOzssasZBR8NFINiKhSaejukya6TG8thRFRjdNPTKitQVdc4Dh3k18r4Wu2JUwlY/s1600-h/JVM.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" fj="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYYVIvbQPHVjHowhb7fomAgkhALwqpCApW3_Nm02B4ztxzk_pSj22WhVX74JCxA3JpKvVR0PcLuPJdOzssasZBR8NFINiKhSaejukya6TG8thRFRjdNPTKitQVdc4Dh3k18r4Wu2JUwlY/s400/JVM.bmp" /></a></div><br />
<br />
<br />
► A different Java bytecode interpreter (JVM) is needed for each type of computer <br />
► This is one of the essential features of Java: the same compiled program can be run on many different types of computers. <br />
<br />
<strong>Java Virtual Machine</strong><br />
► The .class files generated by the compiler are not executable binaries<br />
► so Java combines compilation and interpretation<br />
► Instead, they contain “byte-codes” to be executed by the Java Virtual Machine<br />
► other languages have done this, e.g. UCSD Pascal<br />
► This approach provides platform independence, and greater security<br />
<br />
<strong>What is an Object?</strong><br />
► Objects are conceptually similar to real-world objects: they consist of state and related behavior <br />
► An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods <br />
► Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. <br />
► Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming. <br />
<br />
Bundling code into individual software objects provides a number of benefits, including: <br />
► Modularity <br />
► Information-hiding <br />
► Code re-use: <br />
► Plugability and debugging ease: <br />
<br />
<strong>What Is a Class?</strong> <br />
► Object is an instance of the class <br />
► A class is the blueprint from which individual objects are created. <br />
► A class consists of<br />
► a collection of fields, or variables, very much like the named fields of a struct <br />
► all the operations (called methods) that can be performed on those fields<br />
► can be instantiated<br />
► A class describes objects and operations defined on those objects<br />
<br />
<strong>The class hierarchy</strong><br />
► Classes are arranged in a hierarchy<br />
► The root, or topmost, class is Object<br />
► Every class but Object has at least one superclass<br />
► A class may have subclasses<br />
► Each class inherits all the fields and methods of its (possibly numerous) superclasses<br />
<br />
<strong>HelloWorld (standalone)</strong><br />
<br />
public class HelloWorld {<br />
public static void main(String[] args) {<br />
System.out.println("Hello World!");<br />
}<br />
}<br />
<br />
Note that String is built in<br />
println is a member function for the System.out class<br />
<br />
Comments are almost like C++<br />
<br />
► /* This kind of comment can span multiple lines */<br />
► // This kind is to the end of the line<br />
► /**<br />
* This kind of comment is a special<br />
* ‘javadoc’ style comment<br />
*/<br />
<br />
OOPs - means Object Oriented Programming<br />
<br />
► Encapsulation, Inheritance and Polymorphism are main pillars of OOPs. <br />
<br />
► Encapsulation: Encapsulation is the process of binding together the methods and data variables as a single entity. This keeps both the data and functionality code safe from the outside world. It hides the data within the class and makes it available only through the methods. <br />
<br />
► Inheritance: Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code. <br />
► Polymorphism : Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds according to the current state of the runtime system, all without bothering the user. <br />
<br />
► There are two types of polymorphism : <br />
► Compile-time polymorphism <br />
► Runtime Polymorphism <br />
<br />
What is Inheritance? <br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFx-9ob3AS6WjWEqKjEXYUcI5_zV0LaN5bLgg3rYj0GoR95b_1_4sAI917CvShC3Nchz-oRG-i-4ZmpqEXZipxD9ZbRFqx7J5GcLyYLIr4nzW2U-bNN_06B7q3xTfltW59UU6AOe_QE3Q/s1600-h/inheritance.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" fj="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFx-9ob3AS6WjWEqKjEXYUcI5_zV0LaN5bLgg3rYj0GoR95b_1_4sAI917CvShC3Nchz-oRG-i-4ZmpqEXZipxD9ZbRFqx7J5GcLyYLIr4nzW2U-bNN_06B7q3xTfltW59UU6AOe_QE3Q/s400/inheritance.bmp" /></a></div><br />
► Object-oriented programming allows classes to inherit commonly used state and behavior from other classes .<br />
<br />
<br />
<br />
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. <br />
<br />
What Is an Interface? <br />
► Methods form the object's interface with the outside world<br />
<br />
► An interface is a group of related methods with empty bodies <br />
<br />
► Implementing an interface allows a class to become more formal about the behavior it promises to provide <br />
<br />
► If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. <br />
<br />
What is a Package? <br />
A package is a namespace that organizes a set of related classes and interfaces. <br />
<br />
Access Modifiers <br />
► Access modifiers are used to specify the visibility and accessibility of a class, member variables and methods. They can also be used with the member variables and methods to specify their accessibility. <br />
<br />
► public keyword specifies that the public class, the public fields and the public methods can be accessed from anywhere.<br />
<br />
► private: This keyword provides the accessibility only within a class i.e. private fields and methods can be accessed only within the same class.<br />
<br />
► protected: This modifier makes a member of the class available to all classes in the same package and all sub classes of the class. <br />
<br />
► default : Its not a keyword. When we don't write any access modifier then default is considered. It allows the class, fields and methods accessible within the package only. <br />
<br />
Variables <br />
The Java programming language defines the following kinds of variables: <br />
<br />
► Instance Variables - instance variables because their values are unique to each instance of a class (to each object) <br />
<br />
► Class Variables (Static Variables ) - A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. <br />
<br />
► Local Variables - local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. <br />
<br />
► Parameters - <br />
► Main data types are int, double, boolean, char<br />
<br />
► Also have byte, short, long, float<br />
<br />
► boolean has values true and false<br />
<br />
► Declarations look like C, for example,<br />
► double x, y;<br />
► int count = 0;<br />
<br />
Expressions<br />
► Assignment statements mostly look like those in C; you can use =, +=, *= etc.<br />
<br />
► Arithmetic uses the familiar + - * / %<br />
<br />
► Java also has ++ and –<br />
<br />
► Java has boolean operators && || !<br />
<br />
► Java has comparisons < <= == != >= ><br />
<br />
► Java does not have pointers or pointer arithmetic<br />
<br />
Name conventions<br />
► Java is case-sensitive; maxval, maxVal, and MaxVal are three different names<br />
<br />
► Class names begin with a capital letter<br />
<br />
► All other names begin with a lowercase letter<br />
<br />
► Subsequent words are capitalized: theBigOne<br />
<br />
► Underscores are not used in names<br />
<br />
► These are very strong conventions!<br />
Creating and using an object<br />
► Person john;<br />
john = new Person ( );<br />
john.name = "John Smith";<br />
john.age = 37;<br />
► Person mary = new Person ( );<br />
mary.name = "Mary Brown";<br />
mary.age = 33;<br />
mary.birthday ( );<br />
An array is an object<br />
► Person mary = new Person ( );<br />
► int myArray[ ] = new int[5];<br />
► or:<br />
► int myArray[ ] = {1, 4, 9, 16, 25};<br />
► String languages [ ] = {"Prolog", "Java"};Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0tag:blogger.com,1999:blog-1366367782000255188.post-40726295562654648232009-06-06T21:18:00.000-07:002009-06-06T22:05:07.297-07:00Network and OS Lab Tutorials<div style="text-align: justify;"></div><div style="text-align: center;"><strong><a href="http://draft.blogger.com/"></a>LINUX - THE OS</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">Linux is a Unix-like computer operating system. Linux is one of the most prominent examples of free software and open source development; its underlying source code can be modified, used, and redistributed by anyone, freely.</div><div style="text-align: justify;"></div><div style="text-align: justify;">The Linux kernel was first released to the public on 17 September 1991, for the Intel x86 PC architecture. The kernel was augmented with system utilities and libraries from the GNU project to create a usable operating system, which later led to the alternate term GNU/Linux.Linux is now packaged for different uses in Linux distributions, which contain the kernel along with a variety of other software packages tailored to requirements.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Predominantly known for its use in servers, Linux has gained the support of corporations such as IBM, Sun Microsystems, Hewlett-Packard, and Novell, and is used as an operating system for a wide variety of computer hardware, including desktop computers, supercomputers, and embedded devices such as mobile phones and routers.</div><div style="text-align: justify;"></div><div style="text-align: justify;">The Unix operating system was conceived and implemented in the 1960s and first released in 1970. Its wide availability and portability meant that it was widely adopted, copied and modified by academic institutions and businesses, with its design being influential on authors of other systems.</div><div style="text-align: justify;"></div><div style="text-align: justify;">A 2001 study of Red Hat Linux 7.1 found that this distribution contained 30 million source lines of code. Using the Constructive Cost Model, the study estimated that this distribution required about eight thousand man-years of development time. According to the study, if all this software had been developed by conventional proprietary means, it would have cost about 1.08 billion dollars (year 2000 U.S. dollars) to develop in the United States. </div><div style="text-align: justify;"></div><div style="text-align: justify;">Most of the code (71%) was written in the C programming language, but many other languages were used, including C++, Lisp, assembly language, Perl, Fortran, Python and various shell scripting languages. Slightly over half of all lines of code were licensed under the GPL. The Linux kernel itself was 2.4 million lines of code, or 8% of the total</div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Programming on Linux</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">Most Linux distributions support dozens of programming languages. Core system software such as libraries and basic utilities are usually written in C. Enterprise software is often written in C, C++, Java, Perl, Ruby, or Python. The most common collection of utilities for building both Linux applications and operating system programs is found within the GNU toolchain, which includes the GNU Compiler Collection (GCC) and the GNU build system. Amongst others, GCC provides compilers for C, C++, Java, and Fortran. The Linux kernel itself is written to be compiled with GCC.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Most also include support for Perl, Ruby, Python and other dynamic languages. Examples of languages that are less common, but still well-supported, are C# via the Mono project, and Scheme. A number of Java Virtual Machines and development kits run on Linux, including the original Sun Microsystems JVM (HotSpot), and IBM's J2SE RE, as well as many open-source projects like Kaffe. The two main frameworks for developing graphical applications are those of GNOME and KDE. These projects are based on the GTK+ and Qt widget toolkits, respectively, which can also be used independently of the larger framework. Both support a wide variety of languages. There are a number of Integrated development environments available including Anjuta, Eclipse, KDevelop, MonoDevelop, NetBeans, and Omnis Studio while the traditional editors Vim and Emacs remain popular.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Although free and open source compilers and tools are widely used under Linux, there are also proprietary solutions available from a range of companies, including the Intel C++ Compiler, PathScale, Micro Focus COBOL, Franz Inc, and the Portland Group. </div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;"></div><div style="text-align: justify;">We use here, vi editor for all entering and editing purposes. The commands used in vi editors are….</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>To Start</strong> </div><div style="text-align: justify;"></div><div style="text-align: justify;">To use vi on a file type in vi filename. If the file named filename exist, then it wthe file will be displayed,otherwise an empty file and screen are created into which you may enter the text.</div><div style="text-align: justify;"></div><div style="text-align: justify;">vi filename –edit filename startng at line 1</div><div style="text-align: justify;"></div><div style="text-align: justify;">vi –r filename recover filename that was being edited when system crashed.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>To Exit vi</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">Usually the new or modified filename is saved when you leave vi. However it also possible to quit without saving the file.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:x<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> - quit vi, writing out modified file named in original invocation.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:wq<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> - quit vi, writing out the modified file to file named in original invocation.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:q< return> -quit(or exit) vi.</div><div style="text-align: justify;"></div><div style="text-align: justify;">: q!<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> -quit vi even though latest cvhanges have not been saved for this vi call.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Moving the cursor</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The symbol ^ shown below , before a letter means that the <ctrl></ctrl></ctrl><//ctrl></ctrl><//ctrl></ ctrl=""><//></ctrl><//ctrl></ ctrl=""><//></ ctrl=""><//></><//></ctrl><//ctrl></ ctrl=""><//></ ctrl=""><//></><//></ ctrl=""><//></><//></><//></><//></ctrl><//ctrl></ ctrl=""><//></ ctrl=""><//></><//></ ctrl=""><//></><//></><//></><//></ ctrl=""><//></><//></><//></><//></><//></><//></><//></><//></ctrl><//ctrl></ ctrl=""><//></ ctrl=""><//></><//></ ctrl=""><//></><//></><//></><//></ ctrl=""><//></><//></><//></><//></><//></><//></><//></><//></ ctrl=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>key should be held down while the letter is pressed.</div><div style="text-align: justify;"></div><div style="text-align: justify;">J or ,return>{or down arrow] - move the cursor down one line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">K[or up-arrow] - move the cursor up one line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">H or <backspace></backspace></backspace><//backspace></backspace><//backspace></ backspace=""><//></backspace><//backspace></ backspace=""><//></ backspace=""><//></><//></backspace><//backspace></ backspace=""><//></ backspace=""><//></><//></ backspace=""><//></><//></><//></><//></backspace><//backspace></ backspace=""><//></ backspace=""><//></><//></ backspace=""><//></><//></><//></><//></ backspace=""><//></><//></><//></><//></><//></><//></><//></><//></backspace><//backspace></ backspace=""><//></ backspace=""><//></><//></ backspace=""><//></><//></><//></><//></ backspace=""><//></><//></><//></><//></><//></><//></><//></><//></ backspace=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>[or left arrow] –move cursor left one character.</div><div style="text-align: justify;"></div><div style="text-align: justify;">L or <space></space></space><//space></space><//space></ space=""><//></space><//space></ space=""><//></ space=""><//></><//></space><//space></ space=""><//></ space=""><//></><//></ space=""><//></><//></><//></><//></space><//space></ space=""><//></ space=""><//></><//></ space=""><//></><//></><//></><//></ space=""><//></><//></><//></><//></><//></><//></><//></><//></space><//space></ space=""><//></ space=""><//></><//></ space=""><//></><//></><//></><//></ space=""><//></><//></><//></><//></><//></><//></><//></><//></ space=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>[or right arrow] -move cursor right one character.</div><div style="text-align: justify;"></div><div style="text-align: justify;">0(zero) -move cursor to start of the current line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">$ -move cursor to end of current line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">w –move cursor to the beginning of the next word.</div><div style="text-align: justify;"></div><div style="text-align: justify;">B -move the cursor back to beginning of the preceeding word.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:0<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>or 1G -move the cursor to first line in file.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:n<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> or nG -move the cursor to line n.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:$<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> or G -move cursor to last of the line.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Screen Manipulation</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The following commands allow the vi editor screen(or window) to move up or down several lines and to be refreshed.</div><div style="text-align: justify;"></div><div style="text-align: justify;">^f -move forward one screen.</div><div style="text-align: justify;"></div><div style="text-align: justify;">^b –move backward one screen.</div><div style="text-align: justify;"></div><div style="text-align: justify;">^d –move down(forward) one half screen.</div><div style="text-align: justify;"></div><div style="text-align: justify;">^u –move up(back) one half screen.</div><div style="text-align: justify;"></div><div style="text-align: justify;">^l – redraws the screen removing the deleted lines.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Inserting or Adding Text</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus the >Esc> key must be pressed to terminate the entry of thetext and to put the vi editor back into command mode.</div><div style="text-align: justify;"></div><div style="text-align: justify;">i - insert text before cursor, until<esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;">I - insert text at begginning of current line, until<esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;">a – append text after cursor, until <esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;">A –append text to end of the current line,until<esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;">o –open and put text in a new line below current line, until<esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;">O –open and put text in anew line above current line, until <esc></esc></esc><//esc></esc><//esc></ esc=""><//></esc><//esc></ esc=""><//></ esc=""><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></esc><//esc></ esc=""><//></ esc=""><//></><//></ esc=""><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></ esc=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//>hit.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Deleting Text</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The following commands allow you to delete text.</div><div style="text-align: justify;"></div><div style="text-align: justify;">x- delete single character under cursor.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Nx –delete N characters, starting with character under cursor.</div><div style="text-align: justify;"></div><div style="text-align: justify;">dw – delete the single word beginning with the character under cursor.</div><div style="text-align: justify;"></div><div style="text-align: justify;">dNw – delete N words beginning with the character under cursor.</div><div style="text-align: justify;"></div><div style="text-align: justify;">D – delete the remainder of line,starting with current cursor position.</div><div style="text-align: justify;"></div><div style="text-align: justify;">dd –delete entire curent line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Ndd or dNd –delete N lines beginning with the curent line.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Cutting and Pasting Text</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">The following commands allow you to copy and paste text.</div><div style="text-align: justify;"></div><div style="text-align: justify;">yy - copy(yank,cut)the curent line into buffer.</div><div style="text-align: justify;"></div><div style="text-align: justify;">Nyy or yNy –copy(yank,cut) the next N lines, including the curent line, into the buffer.</div><div style="text-align: justify;"></div><div style="text-align: justify;">p –put(paste) the line(s) in the buffer into the text after the current line.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Searching Text</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">A common occurences in text editing is to replace one word or phrase by another.To locate instances of particular sets of characters(or strings), use the following commands.</div><div style="text-align: justify;"></div><div style="text-align: justify;">/string –search forward for occurrence of string in text.</div><div style="text-align: justify;"></div><div style="text-align: justify;">?string –search back ward for occurences of string in text.</div><div style="text-align: justify;"></div><div style="text-align: justify;">n –move to next occurrence of search string.</div><div style="text-align: justify;"></div><div style="text-align: justify;">N –move the next occurence of search string in opposite direction.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>Saving and Reading Files</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;">These commands permit you to input and output files other than the named file with which you are currently working.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:r filename<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> -read file named filename and insert after current line.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:w<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> -write current contents to file named in original vi call.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:w newfile<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> -write current contents to a new file bnamed newfile.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:12,35w smallfile<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> -write the contents of the lines numbered 12 through 35 to a new file named smallfile.</div><div style="text-align: justify;"></div><div style="text-align: justify;">:w! prevfile<return></return></return><//return></return><//return></ return=""><//></return><//return></ return=""><//></ return=""><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></return><//return></ return=""><//></ return=""><//></><//></ return=""><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></ return=""><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//></><//> - write curent contents over a pre-existing file named prevfile.</div><div style="text-align: justify;"></div><div style="text-align: justify;"><strong>GCC Commands</strong></div><div style="text-align: justify;"></div><div style="text-align: justify;"><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">gcc programname.c –This compile and link the program and a.out file is created . This can be executed by ./a.out .</div></div><div style="text-align: justify;"></div><div style="text-align: justify;"><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">gcc –o outputprogramname programname.c- This compile and link the programand a.out file is created. This can be executed by outputprogramname.</div></div><div style="text-align: justify;"></div><div style="text-align: justify;"><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">gcc –o outputprogramname programname.c-lpthread – To compile with threads</div></div><div style="text-align: justify;"></div><div style="text-align: right;"><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><a href="http://www.4shared.com/file/110347238/c7855785/Network__OS_Lab_Tutorials.html"><span style="font-size: large;">CLICK HERE</span></a><span style="font-size: large;"> <strong>To Download the full Tutorial</strong></span> </div></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div class="separator" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; clear: both; text-align: center;"><a href="http://www.4shared.com/file/110347238/c7855785/Network__OS_Lab_Tutorials.html" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" fj="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkSZbNvIGjDFnrNl3sp63x8rUJos3W-I2BC0ha4fn6xaJxuVmsYdu6whPjRaayVAh83yMctb1CM8kvTyAYgwaDILbG2XqegT6xbkhsnmm0IL307PkG60r2w4GMB0Zs6s28_9FG8Y7sFoU/s200/Download.jpg" /></a></div>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com1tag:blogger.com,1999:blog-1366367782000255188.post-57673815844210381872009-06-04T23:19:00.000-07:002009-06-04T23:34:13.299-07:00Operating System<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">Operating system (commonly abbreviated to either OS or O/S) is an interface between hardware and user; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer. The operating system acts as a host for applications that are run on the machine. As a host, one of the purposes of an operating system is to handle the details of the operation of the hardware. This relieves application programs from having to manage these details and makes it easier to write applications. Almost all computers, including handheld computers, desktop computers, supercomputers, and even video game consoles, use an operating system of some type. Some of the oldest models may however use an embedded operating system, which may be contained on a compact disk or other data storage device.</div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><a href="http://www.cs.uic.edu/~i385/CourseNotes/" imageanchor="1" style="clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" fj="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfIr6tFenDzKDRXAVSSE4yrt9FDdS4TE1OFtxRKhE5rmVktuwOSXbx4LOBX9EVhTbEDJnkUSGEi5wUPi838fNovjt9y3K45GEMhyphenhyphenl77Sj1wJMa7d6HqsPz0O9fLA4hir52uTrWD6ayoh8/s200/NoteBook.jpg" /></a></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><a href="http://www.cs.uic.edu/~i385/CourseNotes/"><strong>Notes for Computer Science and Engineering Students</strong></a> <a href="http://www.cs.uic.edu/~i385/CourseNotes/"><strong>Click Here</strong></a></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div>Sarjukottapuramhttp://www.blogger.com/profile/12700110105223303925noreply@blogger.com0