0% found this document useful (0 votes)
60 views11 pages

Experiment 7: Avi Ajmera N203 MBA Tech CS (A) 70471118003

The document contains 11 programs demonstrating system calls in C including read, write, fork, execl, dup, and redirection using open, close and dup. It also contains a program demonstrating pipes using pipe, read and write. The programs perform input/output to files and standard channels, create child processes, and execute other programs to demonstrate the basic functionality of various Unix system calls.

Uploaded by

Avi Ajmera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views11 pages

Experiment 7: Avi Ajmera N203 MBA Tech CS (A) 70471118003

The document contains 11 programs demonstrating system calls in C including read, write, fork, execl, dup, and redirection using open, close and dup. It also contains a program demonstrating pipes using pipe, read and write. The programs perform input/output to files and standard channels, create child processes, and execute other programs to demonstrate the basic functionality of various Unix system calls.

Uploaded by

Avi Ajmera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment 7

Avi Ajmera
N203
MBA Tech CS(A)
70471118003

System call implementation (READ, WRITE):

Program no. 1: Write a program to read through channel 0.


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int fd1,fd2,j;
char b[50];
read(0,b,50);
printf("The string entered through channel 0 is: %s",b);
return 0;
}

Program no. 2: Write a program for write through channel 1.


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
char b[] ="DATA WRITTEN THROUGH CHANNEL 1:";
write(1,b,35);
return 0;
}

Program no. 3: Write a program for read through channel 0 and write through channel 1.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
char a[50];
printf("ENTER STRING:");
read(0,a,35);
write(1,a,35);
return 0;
}

System call implementation (FORK EXECL DUP):


Program no. 4: Write a program to implementing fork system call
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
fork();
fork();
fork();
printf("hello hello\n");
return 0;
}

Program no. 5: Write a program for implementing execl system call.


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
printf("Hello before execl\n");
execl("/bin/date","date",NULL);
printf("Hello after execl\n");
return 0;
}
Program no. 6: Write a program to implement fork and execl system call.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int i;
printf("Hello before execl\n");
i=fork();
if(i==0)
{
printf("In child process\n");
execl("/bin/date","date",NULL);
printf("Hello after fork and execl\n");
}
return 0;
}
Program no. 7: Write a program to implement dup system call.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{
int fd1,fd2;
char b[]="Output on screen using fd= 5\n";
fd1=dup(1);
close(1);
write(fd1,b,30);
return 0;
}
REDIRECTIONS:

Program no. 8: Write a program for input redirection.


#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd, len;
char str[1024];
fd=open("new.txt",O_RDWR);
printf("Enter the text you want to insert:\n");
fgets(str,1024,stdin);
len=strlen(str);
write(fd,str,len);
}
Program no. 9: Write a program for output redirection.
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd1;
printf("before output redirection\n");
fd1=("new.txt",O_WRONLY|O_CREAT);
close(1);
dup(fd1);
printf("The output of file \n");
return 0;
}
Program no. 10: Write a program for input re-redirection.
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd1,fd2,j;
char input[100];
printf("before output redirection\n");
fd1=open("new.txt",O_RDONLY);
fd2=dup(0);
close(0);
dup(fd1);
scanf("%s",input);
printf("The input taken from file is %s\n",input);
close(0);
dup(fd2);
printf("Enter value of j:\n");
scanf("%d",&j);
printf("The entered value of j from keyboard is %d\n",j);
return 0;
}
Program no. 11: Write a program for output re-redirection.
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd1,fd2,j;
char input[100];
printf("before output redirection\n");
fd1=open("new.txt",O_WRONLY|O_CREAT);
fd2=dup(0);
close(1);
dup(fd1);
printf("The output of file\n");
close(1);
dup(fd2);
return 0;
}
Pipes:

Program no. 12: Write a program for implementing pipe.


#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#define MSGSIZE 16
char* msg1 = "hello, world #1";
char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";

int main()
{
char inbuf[MSGSIZE];
int p[2], i;

if (pipe(p) < 0)
exit(1);
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);

for (i = 0; i < 3; i++) {


/* read pipe */
read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
return 0;
}

You might also like