Skip to main content

Posts

Showing posts from December, 2013

Linux & C:- Unix Pipes and Child Processes

UNIX PIPES:- The UNIX domain sockets (UNIX Pipes) are typically used when communicating between two processes running in the same UNIX machine. UNIX Pipes usually have a very good throughput. They are also more secure than TCP/IP, since Pipes can only be accessed from applications that run on the computer where the server executes. When starting a server using UNIX Pipes, you must reserve a unique listening name (inside that machine) for the server, for instance, 'soliddb'. Because UNIX Pipes handle the UNIX domain sockets as standard file system entries, there is always a corresponding file created for every listened pipe. In solidDB®'s case, the entries are created under the path /tmp Our example listening name 'soliddb' creates the directory/tmp/solunp_SOLIDDB and shared files in that directory. The /tmp/solunp_ is a constant prefix for all created objects while the latter part ('SOLIDDB' in this case) is the listening name in upper ca

Linux & C :- Socket Application, server will perform arithmetic operations

Code for Client #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> int main(int argc,int *argv[]) { int n1=atoi(argv[1]),n2=atoi(argv[2]),res=0; char operator=argv[3][0]; int sock_id=socket(AF_UNIX,SOCK_STREAM,0); if(sock_id<0) { printf("Error in getting socket\n"); return 0; } struct sockaddr_in clientstruct; clientstruct.sin_family=AF_UNIX; clientstruct.sin_addr.s_addr=inet_addr("127.0.0.1"); clientstruct.sin_port=1025; int ret=connect(sock_id,(struct sockaddr *)&clientstruct,sizeof(clientstruct)); write(sock_id,&n1,sizeof(n1)); write(sock_id,&n2,sizeof(n2)); write(sock_id,&operator,sizeof(operator)); int ret1=read(sock_id,&res,sizeof(res)); printf("FROM SERVER:%d \n Bytes=%d\n",res,ret1); close(sock_id); return 0; } Code for Server #include<stdio.h>

Linux & C :- File transfer application using Sockets

Code for Client #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> int main(int argc,int *argv[]) { char buffer[100]; int fd=open("abc.txt",O_RDONLY); read(fd,buffer,100); int sock_id=socket(AF_INET,SOCK_STREAM,0); if(sock_id<0) { printf("Error in getting socket\n"); return 0; } struct sockaddr_in clientstruct; clientstruct.sin_family=AF_INET; clientstruct.sin_addr.s_addr=inet_addr("127.0.0.1"); clientstruct.sin_port=10125; int ret=connect(sock_id,(struct sockaddr *)&clientstruct,sizeof(clientstruct)); write(sock_id,buffer,100); close(sock_id); return 0; } Code for Server #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> int main()

Linux & C :- Producer Consumer Problem with pthreads using semaphores

Subscribe us for our YouTube channel and any kind of help Producer Consumer Problem:-              The “Producer-Consumer problem” is one of the best ways to make an example of issues found in Process Communication. To make a simple analogy of the problem , let’s consider a restaurant , with a client who eats what is served for him on the table and a cook that brings food to the table. The issue is that we want to avoid two unpleasant situations. First the cook might bring food to the table when the table is full and then it might fall off. Second we want to avoid the client trying to eat the table , because there is no food on it.             In reality, the cook is a process known as a “producer” while the client is a process known as the “cosnumer”, the food is data, and the table is a buffer. The problem is also reffered to as the “bounded buffer problem” as we have a limited size buffer that must be shared by two processes (maybe part of the same program)

Linux & C :- Use of semaphores to solve race condition

Definition A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and initialization operation called 'Semaphoiinitislize'. Binary Semaphores can assume only the value 0 or the value 1 counting semaphores also called general semaphores can assume only nonnegative values. The P (or wait or sleep or down) operation on semaphores S, written as P(S) or wait (S), operates as follows: P(S):   IF   S  >  0                  THEN  S :=  S - 1                  ELSE   (wait on S) The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S), operates as follows: V(S):   IF  (one or more process are waiting on S)                 THEN (let one of these processes proceed)                 ELSE   S := S +1 Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a semaphore operations has stared, no other proces

Linux & C :- Race Condition, an example

#include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> #include<stdlib.h> sem_t sem1; int i=5,res=0; void * fun1(void* p) { sem_wait(&sem1); printf("thread 1 is running:\n"); i=0; } void * fun2(void* p) { printf("thread 2 is running:\n"); res=200/i; sem_post(&sem1); } int main() { sem_init(&sem1,0,0); int ret; void *status; pthread_t p_id,t_id; pthread_create(&p_id,NULL,fun1,NULL); pthread_create(&t_id,NULL,fun2,NULL); //printf("%d",i); pthread_join(p_id,&status); // printf("thread1:%s\n",(char*)status); pthread_join(t_id,&status); // printf("thread1:%s",(char*)status); return 0; }

Linux & C :- Child processes and Process Signals

Race Condition:- A race condition'' can be defined as ``Anomalous behavior due to unexpected critical dependence on the relative timing of events'' [FOLDOC]. Race conditions generally involve one or more processes accessing a shared resource (such a file or variable), where this multiple access has not been properly controlled. In general, processes do not execute atomically; another process may interrupt it between essentially any two instructions. If a secure program's process is not prepared for these interruptions, another process may be able to interfere with the secure program's process. Any pair of operations in a secure program must still work correctly if arbitrary amounts of another process's code is executed between them. Race condition problems can be notionally divided into two categories: Interference caused by untrusted processes. Some security taxonomies call this problem a ``sequence'' or ``non-atomic'' c

C++ & Linux :- Fork system call

The fork() Function The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it. The child has a unique PID like any other process running in the operating system. The child has a parent process ID which is same as the PID of the process that created it. Resource utilization and CPU time counters are reset to zero in child process. Set of pending signals in child is empty. Child does not inherit any timers from its parent Note that the above list is not exhaustive. There are a whole lot of points mentioned in the man page of fork(). I’d strongly recommend readers of this article to go through those points in the man page of fork() function. The Return Type Fork(

C++ Binary Search and Sequential Search

#include<iostream> using namespace std; class search_item { int a[20]; int size; public: search_item() { size=0; } void get_array() { cout<<"enter the size\n"; cin>>size; cout<<"Enter the items\n"; for(int i=0;i<size;i++) { cin>>a[i]; } } void put_array() { cout<<"ARRAY IS\n"; for(int i=0;i<size;i++) { cout<<a[i]; } } void sequential_search(int item) { int loc,flag; for(int i=0;i<size;i++) { if(item==a[i]) { loc=i; flag=1; break; } } if(flag==1) cout<<"Item found at "<<loc+1<<endl; else cout<<"Not found\n"; } void binary_search(int item) { int mid,beg=0,end=size-1; mid=(beg+end)/2; while(beg<=end&&item!=a[mid]) { if(item<a[m