0% found this document useful (0 votes)
14 views4 pages

Unix Lab Outputs

The document contains a lab manual for UNIX operating system programming, detailing two programs: one that executes two commands concurrently using a command pipe and another that simulates the producer-consumer problem using semaphores. The first program demonstrates inter-process communication by piping the output of the 'ls -l' command from a child process to a parent process. The second program allows users to produce and consume items while managing buffer states through user input and semaphore operations.

Uploaded by

raimayur71
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)
14 views4 pages

Unix Lab Outputs

The document contains a lab manual for UNIX operating system programming, detailing two programs: one that executes two commands concurrently using a command pipe and another that simulates the producer-consumer problem using semaphores. The first program demonstrates inter-process communication by piping the output of the 'ls -l' command from a child process to a parent process. The second program allows users to produce and consume items while managing buffer states through user input and semaphore operations.

Uploaded by

raimayur71
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

UNIX OPERATING SYSTEM LAB MANUAL

Program Outputs

Program 4: Execute Two Commands Concurrently with a Command Pipe


Aim: Write a program that illustrates how to execute two commands concurrently with a command pipe.

Source Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("pipe failed");
exit(1);
}
if(!fork())
{
close(1);
dup(pfds[1]);
system("ls -l");
}
else
{
printf("parent reading from pipe\n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}

Output:
parent reading from pipe
total 48
-rwxr-xr-x 1 user user 8456 Mar 29 20:00 [Link]
-rw-r--r-- 1 user user 312 Mar 29 19:58 pipe_prog.c
drwxr-xr-x 2 user user 4096 Mar 29 18:00 folder1
-rw-r--r-- 1 user user 512 Mar 28 10:30 [Link]
[... (ls -l output is piped from child to parent and printed here) ...]

Explanation:
The program creates a pipe using pipe(). The child process (fork() returns 0) closes stdout (fd 1),
duplicates the write-end of the pipe to stdout using dup(), then runs 'ls -l' — so all output of 'ls -l' goes into
the pipe. The parent process reads from the read-end of the pipe and prints the data, effectively displaying
the child's 'ls -l' output.
Program 13: Producer-Consumer Problem Using Semaphores
Aim: Write a C program to simulate the producer and consumer problem using semaphores.

Source Code:
#include<stdio.h>
int mutex=1, full=0, empty=3, x=0;

main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\[Link]\[Link]\[Link]\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
}

int wait(int s)
{
return(--s);
}

int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces item%d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nconsumer consumes item%d",x);
x--;
mutex=signal(mutex);
}

Output:
[Link]
[Link]
[Link]

ENTER YOUR CHOICE


1
producer produces item1

ENTER YOUR CHOICE


1
producer produces item2

ENTER YOUR CHOICE


1
producer produces item3

ENTER YOUR CHOICE


1
BUFFER IS FULL

ENTER YOUR CHOICE


2
consumer consumes item3

ENTER YOUR CHOICE


2
consumer consumes item2

ENTER YOUR CHOICE


2
consumer consumes item1

ENTER YOUR CHOICE


2
BUFFER IS EMPTY

ENTER YOUR CHOICE


3
(Program exits)

Explanation:
The program simulates the classic Producer-Consumer synchronization problem using semaphores
(mutex, full, empty). mutex=1 ensures mutual exclusion. empty=3 is the buffer size; full=0 starts empty.
When the user selects Producer (1): if mutex is available and buffer is not full, the producer produces an
item, increments full, decrements empty. When the user selects Consumer (2): if mutex is available and
buffer is not empty, the consumer consumes an item, decrements full, increments empty. Choosing 3 exits
the program.

You might also like