Implementation of System Calls

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

IMPLEMENTATION OF SYSTEM CALLS

1.exec, exit,wait-command

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

#include<stdlib.h>

void main(int argc, char *argv [])

int pid;

/* fork another process */

pid = fork();

if (pid < 0) { /* error occurred */

fprintf (stderr , "Fork Failed") ;

exit(1) ;

else if (pid == 0) { /* child process */

execlp("/bin/ls" , "ls" ,NULL) ;

else { /* parent process */

/* parent will wait for the child to complete */

wait (NULL) ;

printf ("Child Complete") ;

exit (0) ;
}

2)stat command

#include<stdio.h>

#include<sys/stat.h>

main()

int fd;

char pathl[10];

struct stat*nfile;

nfile=(struct stat*)malloc(sizeof(struct stat));

printf("\n Enter the file name:\t");

scanf("%s",pathl);

stat(pathl,nfile);

printf("Program inodeno:%d\n",nfile->st_ino);

printf("Program Size :%d\n",nfile->st_blksize);

printf("Access Time :%s\n",ctime(&nfile->st_atime));

printf("Modified Time :%s\n",ctime(&nfile->st_mtime));

printf("Protection :%d\n",nfile->st_mode);

printf("User id :%d\n",nfile->st_uid);

printf("Group id :%d\n",nfile->st_gid);

printf("Device driverno:%d\n",nfile->st_dev);

printf("No. of links :%d\n",nfile->st_nlink);

return 0;
}

3)opendir ,closedir ,readdir command

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
struct dirent *dptr;
DIR *dname;
if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n");
exit(-1);
}
if((dname = opendir(argv[1])) == NULL)
{
perror(argv[1]);
exit(-1);
}
while(dptr=readdir(dname))
printf("%s\n", dptr->d_name);
closedir(dname);
}

4.Grep command ((globally search a regular expression and print))

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1024
void usage()
{
printf("usage:\t. /a.out filename word\n ");
}
int main(int argc, char *argv[])
{
FILE *fp;
char fline[max];
char *newline;
int count=0;
int occurrences=0;
if(argc!=3)
{
usage();
exit(1);
}
if(!(fp=fopen(argv[1],"r")))
{
printf("grep: couldnot open file : %s\n",argv[1]);
exit(1);
}
while(fgets(fline,max,fp)!=NULL)
{
count++;
if(newline=strchr(fline, '\n'))
*newline='\0';
if(strstr(fline,argv[2])!=NULL)
{
printf("%s %d %s\n", argv[1],count, fline);
occurrences++;
}
}
}

[exam41@telnet PROGRAMS]$ cc EX3b.c


[exam41@telnet PROGRAMS]$ ./a.out EX3b.c if
EX3b.c: 16 if(argc!=3)
EX3b.c: 21 if(!(fp=fopen(argv[1],"r")))
EX3b.c: 29 if(newline=strchr(fline, '\n'))
EX3b.c: 31 if(strstr(fline,argv[2])!=NULL)

5.SIMULATION OF CAT
UNIX COMMANDS

#include<stdio.h>
#include<stdlib.h>
void printfile(FILE *f)
{
char str[80];
while(fgets(str,80,f))
printf("%s",str);
}
int main(int argc,char *argv[])
{
char str[80];
FILE *f;
if(argc<2)
printfile(stdin);
else
{
int i;
for(i=1;i<argc;i++)
{
if(!(f=fopen(argv[i],"rt")))
perror(argv[i]);
else
printfile(f);
fclose(f);
}
}
}

ccetstudent@bcse17:~$ gcc ccat.c


ccetstudent@bcse17:~$ ./a.out
welcome
welcome
hi
hi

You might also like