Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

SOFTWARE ENGINEERING DEPARTMENT (SED)

Lab #10

SUBMITTED TO:

Ma’am Rabia Arshad

SUBMITTED BY:

Muhammad Yasir Hassan

ROLL NUMBER:

19-SE-31

SECTION:

Alpha (α)

DATE:

08-12-2021
Lab Task
Task 1:
Write a program of creation of a two-way pipe between two processes (Child process
sends a question to Parent process “What is the title of this lab?” through one pipe,
parent process reads these questions and send a reply “Inter Process Communication”
through second pipe which the child process reads.)
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
int main()
{
int pipefd[2],pipefd2[2], pid, n, rc, rc2,nr, status;
char buf[1024];
rc = pipe (pipefd);
rc2 = pipe(pipefd2);
if (rc < 0 || rc2<0)
{
perror("\n Pipe Error.... \n");
}
pid = fork ();
if (pid < 0)
{
perror("\n Fork Error.... \n");
}
if (pid == 0)
{ /* Child’s Code */
char *question = "\n What is the Title of This Lab? \n";
close(pipefd[0]);
close(pipefd2[1]);
write(pipefd[1], question, strlen(question));
close(pipefd[1]);
nr = read(pipefd2[0],buf,1024);
write(1,buf,nr);
close(pipefd2[0]);
}
else /* Parent’s Code */
{
close(pipefd[1]);
nr = read(pipefd[0], buf, 1024);
rc = write(1, buf, nr);
close(pipefd[0]);
close(pipefd2[0]);
char *answer = "\n Inter Process Communication (IPC) \n";
write(pipefd2[1], answer, strlen(answer));
wait(&status);
}
return 0;
}
Task 2:
Write a program in which parent process creates two child processes, one child process
writes and executes a command and another child process reads and executes another
command. This program is the equivalent of running the shell command:
/bin/ls -al / | /usr/bin/tr a-z A-Z
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
int fd[2];
int pid;
if (pipe(fd) < 0)
{
perror ("\n Pipe Error.... \n");
exit (1);
}
pid = fork ();
if (pid == 0)
{
dup2 (fd[0],0);
close (fd[1]);
execl ("bin/tr", "tr","a-z","A-Z",NULL);
}
else
pid = fork ();
if (pid == 0)
{
dup2 (fd[1],1);
close (fd[0]);
execl ("/bin/ls", "ls","-al", NULL);
}
else
{
close (fd[0]);
close (fd[1]);
wait (0);
wait (0);
}
return 0;
}

You might also like