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

Unix Exp

The document discusses three C programming exercises: 1) Copying a file using standard I/O and system calls. 2) Emulating the UNIX ls -l command. 3) Executing two commands concurrently with a pipe like ls -l | sort.

Uploaded by

volelej831
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unix Exp

The document discusses three C programming exercises: 1) Copying a file using standard I/O and system calls. 2) Emulating the UNIX ls -l command. 3) Executing two commands concurrently with a pipe like ls -l | sort.

Uploaded by

volelej831
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Expr.

NO:
Date: Experiment-3 Page No:

a) Write a C program that makes a copy of a file using standard I/O, and system call
Program:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int f1, f2; char
buff[50]; long int
n;

if(((f1 = open(argv[1], O_RDONLY)) == -1 || ((f2=open(argv[2], O_CREAT |


O_WRONLY | O_TRUNC, 0700))== 1)))
{
perror("problem in file");
exit(1);
}

while((n=read(f1, buff, 50))>0)

if(write(f2, buff, n)!=n)


{
perror("problem in writing");
exit(3);
}

if(n==-1)
{
perror("problem in reading");
exit(2);
}

close(f2);

Output:

ADITYA GROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM Reg.NO: 22A91A05C4


Expr. NO:
Date: Experiment-3 Page No:

b) Write a C program to emulate the UNIX ls –l command.


Program:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process if
( pid< 0 )
{ //fail
printf(“\nFork failed\n”); exit
(-1);
}
else if ( pid == 0 )
{ //child
execlp( “/bin/ls”, “ls”, “-l”, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(“\nchild complete\n”); exit
(0);
}
}
Output:

ADITYA GROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM Reg.NO: 22A91A05C4


Expr. NO:
Date: Experiment-3 Page No:

ADITYA GROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM Reg.NO: 22A91A05C4


Expr. NO:
Date: Experiment-3 Page No:

c) Write a C program that illustrates how to execute two commands concurrently with a
command pipe. Ex: - ls –l | sort.

Program:
#include<stdio.h>
#include<fontl.h>
#include<sys/stat.h>

Output:

ADITYA GROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM Reg.NO: 22A91A05C4


Expr. NO:
Date: Experiment-3 Page No:

ADITYA GROUP OF EDUCATIONAL INSTITUTIONS, SURAMPALEM Reg.NO: 22A91A05C4

You might also like