Skip to main content

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 in the next section. Our illustrative programs, though, show just a single process accessing the memory, to focus on the shared memory mechanism and to avoid cluttering the sample code with synchronization logic.

 There are three flag values include while dealing with shared memory, these flags are supplied as the third parameter of the function

 int segment_id = shmget (shm_key, getpagesize (), IPC_CREAT | S_IRUSR | S_IWUSER);

IPC_CREAT—This flag indicates that a new segment should be created.This permits creating a new segment while specifying a key value.

IPC_EXCL—This flag, which is always used with IPC_CREAT, causes shmget to fail if a segment key is specified that already exists.Therefore, it arranges for the calling process to have an “exclusive” segment. If this flag is not given and the key of an existing segment is used, shmget returns the existing segment instead of creating a new one.

Mode flags—This value is made of 9 bits indicating permissions granted to owner, group, and world to control access to the segment. Execution bits are ignored. An easy way to specify permissions is to use the constants defined in <sys/stat.h> and documented in the section 2 stat man page.1 For example, S_IRUSR and S_IWUSR specify read and write permissions for the owner of the shared memory segment, and S_IROTH and S_IWOTH specify read and write permissions for others.

Program:

 

#include <stdio.h>

#include <sys/shm.h>

#include <sys/stat.h>

int main ()

{

int segment_id;

char* shared_memory;

struct shmid_ds shmbuffer;

int segment_size;

const int shared_segment_size = 0x6400;

/* Allocate a shared memory segment. */

segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

/* Attach the shared memory segment. */


shared_memory = (char*) shmat (segment_id, 0, 0);

printf ("shared memory attached at address %p\n", shared_memory);


/* Determine the segment’s size. */

shmctl (segment_id, IPC_STAT, &shmbuffer);

segment_size = shmbuffer.shm_segsz;

printf ("segment size: %d\n", segment_size);


/* Write a string to the shared memory segment. */

sprintf (shared_memory, "Hello, world.");


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Reattach the shared memory segment, at a different address. */

shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0);

printf ("shared memory reattached at address %p\n", shared_memory);


/* Print out the string from shared memory. */

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


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Deallocate the shared memory segment. */

shmctl (segment_id, IPC_RMID, 0);

return 0;

}

 

Observation

The prorgam can be compiled by

$ gcc -o sharedmem sharedmem.c

Comments

Popular posts from this blog

Installing TexLive 2019 in Ubuntu 18.04

Installation of TexLive 2019 in Linux (Ubuntu 18.04 LTS)
TeX (Tech)

Installation of TexLive 2019

Please watch the video for full installation



I used .iso file to download, the Total size is 3.3GB for Linux,

and i used the torrent file to download, it took me just 20 min to download the entire .iso file

Extract the .iso file to a folder and open a terminal

$] sudo ./install-tl
(it goes into a terminal mode, which is faster compared to the GUI Mode)

$] sudo ./install-tl -gui
after the installation, set the PATH, MANPATH and INFOPATH as suggested by LATEX

export PATH=$PATH:/usr/local/texlive/2019/bin/x86_64-linux
export MANPATH=/usr/local/texlive/2019/texmf-dist/doc/man
export INFOPATH=/usr/local/texlive/2019/texmf-dist/doc/info

put these lines in to the /home/pradeepkumar/.bashrc

$] gedit /home/pradeepkumar/.bashrc
We have installed TexLive 2019 and texstudio.

To install texstudio

$] sudo apt install texstudio
The look and feel of TexStudio looks like this image.


texlive, it install everyt…

Implementing a new system call in Kernel version 2.6.32

A system call is used by application or user programs to request service from the operating systems. Since the user programs does not have direct access to the kernel whereas the OS has the direct access. OS can access the hardware through system calls only.The following files has to be modified for implementing a system call/usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S/usr/src/linux-2.6.32.5/arch/x86/include/asm/unistd_32.h/usr/src/linux-2.6.32.5/include/linux/syscalls.h/usr/src/linux-2.6.32.5/MakefileNew set of files to be createdCreate a new directory newcall/ inside the path “/usr/src/linux-2.6.32.5/” Create new files Makefile, newcall.c and put them in the /usr/src/linux-2.6.32.5/newcall/ folder Create new user files (in any folder of Linux) to test the system call
testnewcall.c, testnewcall.h (created in /home/pradeepkumar) syscall_table_32.S Find the file /usr/src/linux-2.6.32.5/arch/x86/kernel/syscall_table_32.S and add the following line at the end
"…

Electrical Machine Design (equations)

FactorsDC Machine Transformers Induction Machines Synchronous MachinesOutput EquationPa=CoD2Ln, where Pa=P/h for generators, Pa=P for motorsFor Single Phase
Q=2.22 f Bm Ai Kw Aw d10-3
For Three Phase
Q=3.33 f Bm Ai Kw Aw d 10-3Q=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * hQ=CoD2 L ns
KVA Input Q=
HP * 0.746 / Cos f * h
For Turbo alternators
Q=1.11Bavac KwsVa2 L 10-3/nsOutput CoefficientCo=Bav ac* 10-3where Bav-magnetic loading and ac - electric loadingDNACo=11 Kws Bav ac 10-3Co=11 Kws Bav ac 10-3 Choice of Magnetic LoadingFlux Density in Teeth Frequency of Flux Reversals Size of machineDNAMagnetizing current, Flux Density, Iron lossIron loss, Stability, Voltage Rating, Parallel Operation, Transient ShortCircuit current Choice of Electric LoadingTemperature rise,
speed of machine, Voltage, Armature reaction, CommutationDNAOverload Capacity, Copper losses, Temperature rise, Leakage ReactanceCopper loss, Synchronous reactance, Temperature rise, Stray Load losses,
Voltage rating Flux …