You are on page 1of 7

OS IN LAB 06

#include <iostream>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/wait.h>

using namespace std;

int main(int argc, char *arg[])

key_t key;

int num;

num = shmget (key,1024,0666|IPC_CREAT|IPC_EXCL);

cout<<"NUM IS: "<<num<<endl;

pid_t pid,pid2;

pid=fork(); //1st fork

if(pid==0) //first child

char* data = (char*) shmat(num, NULL, 0);

cout<<"I’m in a child process"<<endl;

int q=10;

cout<<"Enter Data"<<endl;

for(int i=0; i<=q; i++)

char d;

cin>>d;
data[i]=d;

d=NULL;

int f;

f=shmdt(data);

cout<<"Chlid Memory Detached: "<<f<<endl;

else if (pid>0) //first parent

wait (NULL);

char* data = (char*) shmat(num, NULL, 0);

cout<<"I’m in the parent process"<<endl;

for(int i=0; i<=10; i++)

int a;

int l=65;

int v=97;

a=data[i];

for(int j=48 ; j<=57; j++)

if(a==j)

a=32;

data[i]=a;

for(int k=65 ; k<=90; k++)


{

int d=a;

if(a==k)

a=v;

data[i]=a;

v++;

a=d;

for(int p=97 ; p<=122; p++)

int d=a;

if(a==p)

a=l;

data[i]=a;

l++;

a=d;

}//first loop

pid2=fork(); //2nd fork


if(pid2==0) //for 2nd fork

cout<<"I am in child Process 2"<<endl;

cout<<"Final data output by child 2 is: "<<data<<endl;

else if(pid2>0) //for 2nd fork

wait(NULL);

cout<<"I am in parent Process 2"<<endl;

int u;

u=shmctl(num, IPC_RMID, NULL);

cout<<"Memory Deleted: "<<u<<endl;

int o;

o=shmdt(data);

cout<<"Parent Memory Detached: "<<o<<endl;

else //for first fork

cout<<"Error"<<endl;
}

return 0;

POST LAB

#include <iostream>
#include <fstream>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/wait.h>

using namespace std;

int main(int argc, char *arg[])

ofstream of;

key_t key;

int num;

num = shmget (key,1024,0666);

cout<<"NUM IS: "<<num<<endl;

pid_t pid,pid2;

pid=fork(); //1st fork

if(pid==0) //first child

char* data = (char*) shmat(num, NULL, 0);

cout<<"Child process"<<endl;

cout<<"Enter student roll NUmbers: "<<endl;

for(int i=0; i<=5; i++)

char d;

cin>>d;

data[i]=d;

d=NULL;
}

else if(pid>0)

wait(NULL);

char* data = (char*) shmat(num, NULL, 0);

cout<<"Parent process"<<endl;

of.open("student.txt");

of<<"Students data in shared memory is: "<<endl;

of<<data<<endl;

of.close();

int u;

u=shmctl(num, IPC_RMID, NULL);

cout<<"MD FULL: "<<u<<endl;

else

cout<<"Error"<<endl;

return 0;

You might also like