Labsheet 7
Labsheet 7
pipe
synchronization problems
Part A
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).
int main()
char ch;
if(fork())
{//parent
else
{ //child
if (ch=='q' ) {close(p[1]);exit(1);}
return 0;
---------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
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
Q2. Write program for two way communication using named pipe.
Hint: two named pipes , open one for read ad other for write
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
( 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.
the other programme first reads from shared memory and displays the message
Hint:
struct ptrsn {
};
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 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
if(!strncmp(buf,"quit",4))break;
close(fp1);
return 0;
while(1)
printf("\nWaiting to read");
.........
if(!strncmp(buf,"quit",4))break;
close(fp1);
return 0;
HINT Q2:
while(1)
close pipe1
display
if(!strncmp(buf,"quit",4))break;
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{
};
int sid,p;
char *sm;
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;
while(1)
HINT Q4:
// this is for P1
sleep(1);
fflush(stdout);
fflush(stdout);
sleep(1);
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
char *turn,*want1,*want2;// cast the sharedmemories to your variables (since the data is char
assigned instead cast)
//
//initialize variables
*want1=1;// P1 does not want to enter in CS ; *want2=1;// P2 does not want to enter in CS
// inside CS
sleep(rand()%20);//sleep(2);
//release