NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


// This is the first program / process that will use the shared memory

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main()
{
//Step1: Define a variable or structure to be shared
struct shdata{
char c;
int n;
};

int shmid; //shared memory id

//Step2: Every shared memory is identified by a unique key.
key_t key=5678;

struct shdata *shm; // a pointer to the shared memory

//Step3: Shared memory is obtained by using shmget system call which returns an integer
shmid= shmget (key,sizeof(struct shdata),IPC_CREAT | 0666);
//set initial permissions 0666 for the shared memory

if (shmid<0)
{ cout<<"Error .. Cannot get memoryn";
// cannot create or obtain a shared memory
exit(0);
}
//Step 4: Attatch the process and get a pointer to shared memory to access the shared data
shm=(struct shdata*) shmat(shmid, NULL,0);
//casting shmat to this type (struct shdata*)

if(shm<= (struct shdata*)(0))
{ cout<<"Error .. Cannot attachn"; // cannot attach process to memory
exit(1);
}
//Now you can place data in shared memory
shm ->c='a';
shm->n= 10;
return 0;}

// The second program/process can also use the same shared memory as:

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
int main()
{
//Data to be shared
struct shdata{
char c;
int n;
};

int shmid; //shared memory id
key_t key=5678; //shared memory id
struct shdata *shm; // a pointer to the shared memory

//obtain an existind shared memory
shmid= shmget(key,sizeof(struct shdata),0);

if (shmid<0)
{ cout<<"Error .. Can not get memoryn";
// cannot obtain a shared memory
exit(0);
}

//attatch another process with the existed shared memory
shm=(struct shdata*) shmat(shmid, 0,0);

if(shm<= (struct shdata*)(0))
{ cout<<"Error .. Can not attachn";// cannot attach process to memory
exit(1);
}

//Now the second process can access the shared data

cout<<"The values stored by the first program are:n";
cout<<"C = "<<shm ->c<<"n n="<<shm->n<<"n ";

return 0;
}


 To destroy the shared memory:

shmctl (shmid, IPC_RMID, NULL);

----


//Ali Mahdi 20110966
//Yousif Saeed 20113191
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <sys/wait.h>
using namespace std;
int PipeOne[2];
int PipeTow[2];
int size=10;

//this function stores n randomly generated integers on array
vector<int> StoreRandom(vector<int> arr)
{
//generate 10 random integers based in system time and stored in array
srand ( time(NULL) );
for (int i =0;i<size; i++)
{
int x = rand() % 10 + 1;
arr.push_back(x);
}
cout <<"Success! , random integers based in system time stored in the array "<<endl;
return arr;
}//End of StoreRandom Function


// this function sort an array
// its take array of integers
// its return sorted array of integers
vector<int> sort ( vector<int> arr)
{
//sort Array
std::sort(arr.begin(), arr.end());
cout <<"Success! , random integers sorted and seved in to the array"<<endl;
return arr;

}//End of sort Function


// this function print an array
// its take array of integers and print it
void print(vector<int> arr)
{
//print sorted array
for (int i=0; i < arr.size(); i++)
cout << arr[i] << ' ';
cout << endl;
}//End of print Function

// This is Second Child Process
// its read chars from pipe and store it in to array than print it
void SecondChildProcess()
{
vector<int> arr;
cout<<"Second Child Process ID : "<<getpid()<<endl;
char CharArray2[10];
close(PipeTow[1]);
read(PipeTow[0],CharArray2,size);
for(int i = 0 ; i < size ; i++)
arr.push_back(static_cast<int>(CharArray2[i]));
cout<<"Array = ";
print(arr);
}//End of SecondChildProcess Function


// This is Child Process
// its read chars from pipe and store it in to array than store the array
// next its create pipe and pass array on it and call Second child Process using fork
void ChildProcess()
{
vector<int> arr;
char CharArray2[10];
//Create pipe and read chars from it and store it in to array
close(PipeOne[1]);
read(PipeOne[0],CharArray2,size);
for(int i = 0 ; i < size ; i++)
arr.push_back(static_cast<int>(CharArray2[i]));
arr=sort(arr);//sort the array
//Create pipe and pass array on it
pipe(PipeTow);
char CharArray[size];
for(int i=0;i<size;i++)
CharArray[i] = static_cast<char>(arr[i]);
write(PipeTow[1],CharArray,size);
//Create Fork
int Stutes=fork();
if(Stutes == 0)
{
SecondChildProcess();
}
else{
wait(NULL);
}
}//End of ChildProcess Function


// This is Parent Process
// its print the Process id than create array and store random integer in to it
// next its create pipe and pass array on it and call child Process using fork
void ParentProcess()
{
cout<<"Parent Process ID : "<<getpid()<<endl; // print ParentProcess ID
vector<int> arr;
arr=StoreRandom(arr);// call function StoreRandom to store random integer in to array
//Create pipe and pass array on it
pipe(PipeOne);
char CharArray[size];
for(int i=0;i<size;i++)
CharArray[i] = static_cast<char>(arr[i]);
write(PipeOne[1],CharArray,size);
//Create Fork
int Stutes = fork();
if(Stutes == 0)
{
ChildProcess();
}
else{
wait(NULL);
}
}//End of ParentProcess Function


//this is main function
int main ()
{
ParentProcess();
return 0;
}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.