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

Unix Operating System Lab Lab Practical Exercise-1

The document contains source code for C programs that demonstrate various UNIX system calls and functions including fork(), wait(), exec(), pipe(), and file copying. The programs provide examples of how to create child processes, execute other programs, and communicate between processes using pipes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Unix Operating System Lab Lab Practical Exercise-1

The document contains source code for C programs that demonstrate various UNIX system calls and functions including fork(), wait(), exec(), pipe(), and file copying. The programs provide examples of how to create child processes, execute other programs, and communicate between processes using pipes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIX OPERATING SYSTEM LAB

LAB PRACTICAL
EXERCISE-1
2) Write a C program that makes a copy of a file using
standard I/O, and system calls.
AIM: C program that makes a copy of a file using standard I/O,
and system calls.
//SOURCE CODE
#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);
exit(0);
}
O/P:
3) Write a C program to emulate the UNIX ls –l command.
AIM: C program to emulate the UNIX ls –l command.
//SOURCE CODE
#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
}
else { //parent
wait (NULL); //wait for child
printf("\nchild complete\n");
exit (0);
}
}
4) Write a C program that illustrates how to execute two
commands concurrently with a command pipe.
AIM : C program that illustrates how to execute two
commands concurrently with a command pipe.
//SOURCE CODE1
#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 ("sort sample.txt| uniq");
}
else{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}
O/P:
//SOURCE CODE2
#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 | more");
}
else{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}
O/P:
//SOURCE CODE3
#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 ("cat sample.txt | sort");
}
else{
printf("parent reading from pipe \n");
while(read(pfds[0],buf,80))
printf("%s \n" ,buf);
}
}
O/P:
6) Multiprogramming-Memory management-
Implementation of fork (), wait (), exec() and exit (), System
calls.
AIM: C program for Implementation of fork () and exit ().
//SOURCE CODE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void main(){
pid_t pid = 0;
pid = fork();
if (pid == 0){
printf("I am the child.\n");
}
if(pid > 0){
printf("I am the parent, the child is %d.\n", pid);
}
if(pid < 0){
perror("In fork():");
}
exit(0);
}
O/P:
AIM: C program for Implementation of wait().
//SOURCE CODE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void main() {
pid_t pid = 0;
int status;
pid = fork();
if (pid == 0) {
printf("I am the child.\n");
sleep(10);
printf("I am the child, 10 seconds later.\n");
}
if (pid > 0) {
printf("I am the parent, the child is %d.\n", pid);
pid = wait(&status);
printf("End of process %d: ", pid);
if (WIFEXITED(status)) {
printf("The process ended with exit(%d).\n",
WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
printf("The process ended with kill -%d.\n",
WTERMSIG(status));
}
}
if (pid < 0) {
perror("In fork():");
}
exit(0);
}
O/P:
AIM: C program for Implementation of exec().
//SOURCE CODE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void main() {
pid_t pid = 0;
int status;
pid = fork();
if (pid == 0) {
printf("I am the child.\n");
execl("/bin/ls", "ls", "-l", (char *) 0);
perror("In exec(): ");
}
if (pid > 0) {
printf("I am the parent, and the child is %d.\n", pid);
pid = wait(&status);
printf("End of process %d: ", pid);
if (WIFEXITED(status)) {
printf("The process ended with exit(%d).\n",
WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
printf("The process ended with kill -%d.\n",
WTERMSIG(status));
}
}
if (pid < 0) {
perror("In fork():");
}
exit(0);
}
O/P:

You might also like