Lab - II (CS) 11 September 2025


This lab contains 4 problems on fork(), execlp(), pipe() and popen() system calls.


You may use the Internet and any standard websites
for help but must acknowledge the sources when you
submit your answers.

Submission Procedure

  1. Each question must contain code in separate files labelled as lab02-01.c, lab02-02.c, etc.
  2. Zip the files into a single zip file named <roll_number>.zip.
  3. Email the zip file to
    chakravarthybhagvati@uohyd.ac.in
    or if there is a problem with emailing zip files, share it on your Google Drive with me!

Question 1

Write a program that uses fork() to create a child process. Declare auto, static, extern variables and use your program to show clearly which variables are copied and which are shared between the parent and the child.

Explain clearly in a comment at the top of your .c file why and how your program shows what has been asked. If you feel that you would like to explain in a separate file, create one with your favourite software and label it as lab02-01-desc with the suitable extension. Add it in your zip file.

You may use the code stub below to help!

In [ ]:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    /* declare some variables here */
    
    if ((pid = fork()) != 0) {
        
        /* Modify the variables here and print them */
        
    } else {
        
        /* Modify the variables and print them */
        
    }
    exit(0);
}

Question 2

Write a program that catches the signals SIGINT and SIGQUIT to print a message, "You cannot kill me with <signal number>"

How will you kill this process?

You may use the code stub below

In [ ]:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

int main(void)
    /* Set up the signal handler */
        
        while(1)            /* Puts program in infinite wait */
            pause();
    exit(0);
}

Question 3

Create processes P1, P2 and P3 as shown below. Set up a pipe between P2 and P3. P2 reads the file /etc/hosts and writes into the pipe. P3 reads the pipe and outputs the contents from the pipe on the screen one line at a time. Each line is prefixed by the string, Child: . P1 terminates when P3 terminates (remember the waitpid() system call).

Can you make P2 read from the terminal instead of reading from /etc/hosts file? The process terminates when the user types Ctrl-d at the terminal. P1 terminates when P2 terminates.

No description has been provided for this image

You may use the code stub below.

In [ ]:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

#define READ 0
#define WRITE 1

int main(void) 
{
    /* Declare necessary variables and create a pipe */
    
    /* Create child processes appropriately */
    
    /* Use a waitpid() system call in the right place */
    /* Otherwise, code just hangs!                    */
    
    exit(0);
}

Question 4

In this program, use the popen() system function to use the same process structure as above with P2 executing the cat command and P3 running the nl command. P2 reads a line typed by the user at the terminal and sends it to P3 which uses nl command to output whatever the user typed. The program ends when the user types Ctrl-d at the terminal.

Use the code stub below if you are stuck for inspiration!

In [ ]:
%%file lab02-04.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>

int main(void)
{
    FILE *wfp, *rfp;
    char line[81];
    
    /* Set up the two pipes using popens */
    
    /* Send information through the pipes */
    
    /* Close the pipes and exit */
    
    exit(0);
}

T H E     E N D