Skip to main content

Posts

Showing posts from April, 2012

Inter process communication using Shared Memory

Explanation:One of the simplest interprocess communication methods is using shared memory. Shared memory allows two or more processes to access the same memory as if they all called malloc and were returned pointers to the same actual memory. When one process changes the memory, all the other processes see the modification.Shared memory is the fastest form of interprocess communication because all processes share the same piece of memory. Access to this shared memory is as fast as accessing a process’s nonshared memory, and it does not require a system call or entry to the kernel. It also avoids copying data unnecessarily. Because the kernel does not synchronize accesses to shared memory, you must provide your own synchronization. For example, a process should not read from the memory until after data is written there, and two processes must not write to the same memory location at the same time.A common strategy to avoid these race conditions is to use semaphores, which are discussed…

Semaphore implementation using POSIX Threads

Aim: To illustrate the semaphores in Linux using C for handling two threads.Requirements:GCC Compiler with Linux OSExplanation This program has two threads, one for reading the input and another for converting the text to upper case letter. However, the thread for converting the text will wait for the semaphore to be released before it starts the operation.Program://illustration of semaphores.#include <stdio.h>
#include <unistd.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <pthread.h>#include <semaphore.h>
#define BUFFER 1024
sem_t sem;char buffer[BUFFER];
void *read_thread(void *arg){while(strncmp("stop", buffer,4)!=0){printf("Enter text");fgets(buffer,BUFFER, stdin);sem_post(&sem);}pthread_exit("read Thread exit successful");}
void *convert_thread(){int i;sem_wait(&sem);while(strncmp("stop",buffer,4)!=0){printf("Converted text");for(i=0;i<strlen(buffer);i++)

Multithreading Example using POSIX Threads

Requirements : GCC Compiler and any Linux OS with pthread LibraryExplanation:There are two threads in the following program which are implemented using function pointers, in which one thread is helpful for getting a input from the keyboard and other thread is helpful to convert it to upper case letters. But here both the threads does not depends on each other.That is, the second thread automatically converts the string to upper case letters itself, does not bothering about the first thread(the string entered through the keyboard). Two seconds delay is given to enter the string via the key board.Program:#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <pthread.h>#define BUFFER_SIZE 1024char buffer[BUFFER_SIZE];void *read_thread(void *arg){while(strncmp("stop", buffer,4) !=0){printf("enter text:");fgets(buffer,BUFFER_SIZE, stdin);sleep(2);}pthread_exit("Read thread exit succ…

Send email attachments to Dropbox

Have you ever wonder to send email that get stored in Dropbox. Here is a way to do it. http://sendtodropbox.com/. Create a dropbox account and connect the account with this website http://sendtodropbox.com/Once you authorize dropbox to this website, you will be getting a email id something@sendtodropbox.com and just you can send email to this Mail ID. Automatically, the contents are stored in the "Attachments" folder inside dropbox. This will be a very nice application, if in case if your office does not have internet and you want to send information to someone else, you can send email and that gets stored in Dropbox.Also if you are a teacher or professor, you can accept assignments, project reports to be sent to a email address and that gets stored in dropbox folder. Also there is a provision for a teacher to group the contents based on the students Registration number, categorize based on date of email or users email address.Also if a small company want to hire employees, …