[go: up one dir, main page]

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

Exercise OS

The document contains several examples of C programs demonstrating the implementation of system calls such as opendir, readdir, closedir, fork, getpid, open, and read. Each example includes the source code, a brief description of its functionality, and sample output. The programs illustrate directory handling, process creation, file opening, and reading from files.

Uploaded by

N Md Shakeel
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)
13 views4 pages

Exercise OS

The document contains several examples of C programs demonstrating the implementation of system calls such as opendir, readdir, closedir, fork, getpid, open, and read. Each example includes the source code, a brief description of its functionality, and sample output. The programs illustrate directory handling, process creation, file opening, and reading from files.

Uploaded by

N Md Shakeel
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
You are on page 1/ 4

EX.NO: 2a IMPLEMENTING opendir, readdir, closedir, etc..

SYSTEM CALLS

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
struct dirent *dptr;
int main()
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER THE DIRECTORY NAME:”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist…\n”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);}

OUTPUT:
[kalai@localhost ~]$ vi sys1.c
[kalai@localhost ~]$ cc sys1.c
[kalai@localhost ~]$ ./a.out

ENTER THE DIRECTORY NAME:d1


.
..
command
EX.NO: 2b IMPLEMENTING Fork, getpid, Exit, Etc…. SYSTEM CALLS

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION… \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n THE PARENT PROCESS ID IS: %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n THE CHILD PROCESS ID IS: %d\n”, pid2);
}
}

OUTPUT:
[kalai@localhost ~]$ cc sys2.c
[kalai@localhost ~]$ ./a.out

THE PARENT PROCESS ID IS: 8062

THE CHILD PROCESS ID IS: 8063


EX.NO: 2c IMPLEMENTING open,read SYSTEM CALLS

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd;
if((fd=open("prime.c"))==-1)
{
perror("\n Cannot open the file…");
exit(0);
}
else
printf("\n FILE OPENED SUCCESSSFULLY\n");
return 0;
}
OUTPUT:
[kalai@localhost ~]$ cc opdir.c
[kalai@localhost ~]$ ./a.out
FILE OPENED SUCCESSSFULLY
EX.NO: 2d READ SYSTEM CALL

#include<stdio.h>
#include<stdlib.h>
main()
{
char b[100];
int fd,xr;
if((fd=open("test.c",0))==-1)
{
printf("\n Cannot open file…\n");
exit(1);
}
do
{
xr=read(fd,b,100);
b[xr]='\0';
printf("%s",b);
}
while(xr==100);
close(fd);}

OUTPUT:
[kalai@localhost ~]$ cc sysread1.c
[kalai@localhost ~]$ ./a.out
#include <stdio.h>
main()
{
printf(" \nHello World\n");
}

You might also like