0% found this document useful (0 votes)
22 views

Labsheet 7

The document outlines a lab sheet focused on Inter Process Communication and concurrent programming using pipes, shared memory, and synchronization techniques. It includes programming tasks for creating unnamed and named pipes, two-way communication, shared memory exchange, and implementing Peterson's and Dekker's algorithms. Each task specifies the required programming structure, relevant header files, and hints for implementation.

Uploaded by

aachalladdha03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Labsheet 7

The document outlines a lab sheet focused on Inter Process Communication and concurrent programming using pipes, shared memory, and synchronization techniques. It includes programming tasks for creating unnamed and named pipes, two-way communication, shared memory exchange, and implementing Peterson's and Dekker's algorithms. Each task specifies the required programming structure, relevant header files, and hints for implementation.

Uploaded by

aachalladdha03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab-sheet-7

objective: To learn Inter Process Communication and concurrent programming using

pipe

shared memory and

synchronization problems

Part A

************** Unnamed pipe **********************

Q0. Write a programme in which child and parent process communicates via unnamed pipe.

child process reads from keyboard and write into pipe, parent process reads from pipe , change the
char to upper case then write on terminal(display).

header files : stdio.h,stdlib.h, unistd.h and ctype.h

// relevant header files

int main()

char ch;

int p[2]; // pipe descriptors

pipe(p);// create pipe and return pipe descriptor in p[]

if(fork())

{//parent

close(p[1]); // close pipe end not used by parent

while(read(p[0],&ch,1)>0) // read a char from pipe

{c=toupper(ch);write(1,&ch,1); if (ch=='q')exit(0);} // write in stdout

else

{ //child

close(p[0]);// close pipe end not used by child


while( read(0,&ch,1)>0 ){ // read from stdin

write(p[1],&ch,1);// write a char in pipe

if (ch=='q' ) {close(p[1]);exit(1);}

return 0;

************** named pipe **********************

---------PROBLEMS--------------------

Q1(a) Write a programme (server), lab-7-1a.c, which creates a named pipe /tmp/xxxx,

(where xxxx is last four digits of your id) open it for writing,

reads a sentence from keyboard using fgets() and write into FIFO using write()- infinite loop

terminating condition of loop - If the input from keyboard of pipe is "quit"

when programme terminates unlink the FIFO. (run this prg before other pairing prg in one terminal)

(b) write another programme (client), lab-7-1b.c, which opens named pipe /tmp/xxxx, for reading,

reads from FIFO using read() and write onto screen- infinite loop

(run this prg in second terminal)

Q2. Write program for two way communication using named pipe.

Hint: two named pipes , open one for read ad other for write

file names lab-7-2a.c and lab-7-2b.c

open two separate terminals(sessions) , and run one programme in each terminal.

enter data from one of the window, other will show output in case single pipe.

enter data from one of the window, other will show output write data in second terminal, read in
terminal one in case two pipes.

Q3. Write two programmes which exchange string using shared memory in given format

"hello my name is ------. My process id is ----"

( use the key for shared memory- last four digits of your loginID)
The first programme creates and attach shared memory. It reads data from keyboard and write in
shared memory.

( file name lab-7-3a.c )

The other programme(s) get, attached shared memory,

the other programme first reads from shared memory and displays the message

then writes its data in shared memory and exit.

( file name lab-7-3b.c )

Q4. Implement Peterson's algorithm

(using single data structure based shared variable )

Hint:

struct ptrsn {

int flag[2];// indicate choice to enter in CS of process

int turn ; //indicate turn of a process

};

struct ptrsn *shd;

( file name lab-7-4a.c and lab-7-4b.c )

Q5. Implement Dekker's algorithm

(using three different character sized shared variables for sychronization)

HINT Q1:

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<fcntl.h>

#include<string.h>

#include <sys/stat.h>

#include <sys/types.h>

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

{ int n;

char fifop1[]="/tmp/xxxx1"; // where xxxx is your login id, n is fifo number, in case you are creating
multiple fifos for this sheet
char buf[256]; // buffer to store string

int a= mkfifo(fifop1,0666); //only one process creates

int fp1=open(fifop1,O_WRONLY); // open for write (only one process)

while(1) // infinite loop

printf("\nEnter:"); //prompt user

fgets(buf,40,stdin) ;// read from stdin, not more than 40 chars

n=write(fp1,buf,strlen(buf)); //write to pipe, n returns number of byte written in pipe

printf("wrote in pipe: %d bytes, %s ",n,buf);

if(!strncmp(buf,"quit",4))break;

close(fp1);

return 0;

// for other pairing process FILL up proper statements

int fp2=open(fifop1,O_RDONLY); // open for read (rest process)

while(1)

printf("\nWaiting to read");

.........

printf("\n recv<--: n %d bytes, ",n);

fputs(buf,stdout); // write in stdout

if(!strncmp(buf,"quit",4))break;

close(fp1);
return 0;

HINT Q2:

one of the process creates both pipes or one each by both

while(1)

open pipe1 for read

read from pipe1

close pipe1

display

if(!strncmp(buf,"quit",4))break;

read from keyboard

open pipe2 for write

write to pipe2

close pipe2

if(!strncmp(buf,"quit",4))break;

close pipe1

close pipe2

return 0;

HINT Q3:

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

#include <stdlib.h>

#include<time.h>
// format of data to be exchanged

struct myd{

char msg[50];//to store message string

int a; //to store pid

};

int sid,p;

char *sm;

struct myd *shd;// mydata of structure type data7

key_t k=xxxxn;// use last four digits of your ID, n 1 tt 9 as per number of shared memory you creates
for this sheet

// you may give key from command line and convert to integer (k= atoi(argv[1])

sid = shmget(k, sizeof(struct myd), IPC_CREAT | 0666);//creates shared area associated with key k
of type structure myd

sm = shmat(sid, NULL, 0);// attaches shared area sid to your programm with id sm;

shd= (struct myd *)sm; // point shared memory sm to mydata

// read data from keyboard and write in shared memory

p=getpid(); //get pid

// write pid and message "start" in shared tata

while(1)

printf(" process(pid)%u message: %s\n",shd->a,shd->msg);

if(!strncmp(shd->msg,"quit",4))break;//unless other quit

// read data from keyboard and write in shared memory

printf("Enter your message:");// prompt user to enter message

fgets(shd->msg,45,stdin);// Enter your message from keyboard in shared memory

shd->a =p;//save pid of your process in shared memory


if(!strncmp(shd->msg,"quit",4))break;//unless "quit"

// wait till other process not changed value of shd->a

while(p ==shd->a){sleep(1);}// wait until other process not changed data

sleep(1);// wait for one second

shmctl(shmid,IPC_RMID,0);//delete shared memory

HINT Q4:

similar to Q3 for creating shared memory

shd->turn=1 ;// process 1 turn

shd->flag[0] =0;// P1 outside of CS/ NOT wants to enter

shd->flag[1] =0;// P2 outside of CS/ NOT want to enter

// this is for P1

for(i=0;i<5;i++) // repeat five times

sleep(1);

shd->flag[0] =1;// P1 wants to enter in CS

shd->turn=2;// lets say it is others turn FIRST YOU

printf(" \nP1 checks:Variables: turn %d flag[0] %d flag[1] %d\n",shd->turn,shd->flag[0],shd-


>flag[1]);

fflush(stdout);

while( shd->turn==2 && shd->flag[1])

{printf("\nprocess P1 waiting as turn: %d flag[0]: %d flag[1]: %d\n",shd->turn,shd->flag[0],shd-


>flag[1]);fflush(stdout);sleep(1);};// P1 blocks

printf("\n--->>process P1 inside CS as turn: %d flag[0]: %d flag[1]: %d\n",shd->turn,shd-


>flag[0],shd->flag[1]);

fflush(stdout);

sleep(1);

printf("\n++ process P1 comes out");fflush(stdout);

shd->flag[0] =0;// P1 does NOT want to enter/ outside the CS


}

(use two tabs in print statments of P2)

(After compilation ,run both prg in background. $./lab-7-4a&./lab-7-4b& )

HINT Q5:

// use 5digit key number where is first four digits of your ID and last digit is 3-4

// example if your id is 1234 then key1 = 12343, key2 =12344 and key3 =12345

key_t key1,key2,key3; //keys to create three char size shared memory

int shmid1,shmid2,shmid3; //id of these shared memories

char *shm1,*shm2,*shm3; // pointer of shared memories

char *turn,*want1,*want2;// cast the sharedmemories to your variables (since the data is char
assigned instead cast)

//

turn=shm1;// point shared memory to the variable turn

want1=shm2;// point shared memory to the variable want1 of process 1

want2=shm3;// point shared memory to the variable want of process 2

//initialize variables

*turn=1; // set turn to P1 (in first prg)

*want1=1;// P1 does not want to enter in CS ; *want2=1;// P2 does not want to enter in CS

( *turn= 2;// set turn of P2 ,in second prg)

//check if P1 can enter in critical section

*want1=0;//p1 want to enter if want1=1 , P1 is outside or not want to enter in CS

printf("\n\t\tP1 WANT TO enter %s in CR\n",argv[0]);// prg name instead of P1 or P2

while(*want2 == 0)// is P2 want or inside CR

if(*turn == 2)//if P2's turn

{ *want1=1;//P1 givesup chance

while(*turn == 2){printf("\n\t\tP1 WAITING to enter %s in CR\n",argv[0]);usleep(5000000);}


*want1=0;

// inside CS

printf("\n\t\tP1 ENTERED %s in CS\n",argv[0]);

sleep(rand()%20);//sleep(2);

//release

*turn= 2; //others process turn

*want1=1;// P1 outside or not interested to enter

printf("\n\t\tP1 OUTSIDE %s from CS\n",argv[0]);usleep(5000000);

// fine tune these statments for both the prg

( To use rand() functions-

use headerfile #include<time.h>

srand( time(NULL) );//randomize random function

You might also like